Can call the compiler from command line with argument

This commit is contained in:
Krzosa Karol
2022-06-07 15:27:18 +02:00
parent 1a67fe3402
commit 44d26d6939
6 changed files with 40 additions and 18 deletions

View File

@@ -96,6 +96,7 @@ gen_expr(Ast_Expr *ast){
CASE(INDEX, Index){
gen("(");
gen_expr(node->expr);
gen("[");
gen_expr(node->index);
gen("]");
@@ -426,7 +427,6 @@ compile_string(String filecontent, String filename = "default_name"_s){
pctx->resolving_package = result;
F64 parse_end = os_time();
gen(R"==(
#include <stdint.h>
#include <stdio.h>
@@ -448,6 +448,11 @@ typedef float F32;
typedef double F64;
typedef S32 Bool;
typedef struct Slice{
S64 len;
void *data;
}Slice;
typedef struct String{
U8 *str;
S64 len;

View File

@@ -77,10 +77,9 @@ Expr:
@todo
[ ] - Passing down program to compile through command line
[ ] - Arrays with size passed
[ ] - Switch
[ ] - Fix printf somehow.
[ ] - Values inited to 0 by default
[ ] - Comma notation when declaring variables thing1, thing2: S32
[ ] - Array of inferred size
@@ -115,6 +114,7 @@ Expr:
[x] - We are parsing wrong here: (t.str=(&string_to_lex.str)[i]);
[x] - Test new operators, add constant eval for them
[x] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression
[x] - Passing down program to compile through command line
[x] - More basic types
[x] - Implementing required operations int128
[x] - Fix casting
@@ -161,7 +161,7 @@ Expr:
#include "typecheck.cpp"
#include "ccodegen.cpp"
int main(){
int main(int argument_count, char **arguments){
// test_big_int();
test_os_memory();
@@ -191,15 +191,19 @@ int main(){
printf("%s", result.str);
#endif
if(argument_count > 1){
Scratch scratch;
String name = string_fmt(scratch, "%s.kl", arguments[1]);
String c_filename = string_fmt(scratch, "%s.c", arguments[1]);
String call = string_fmt(scratch, "clang.exe %s.c -g -o %s.exe && %s.exe", arguments[1], arguments[1], arguments[1]);
#if 1
result = compile_file("program.kl"_s);
FILE *f = fopen("program.c", "w");
result = compile_file(name);
FILE *f = fopen((const char *)c_filename.str, "w");
assert(f);
fprintf(f, "%.*s", (int)result.len, result.str);
fclose(f);
system("clang.exe program.c -g -o program.exe");
#endif
system((const char *)call.str);
}
__debugbreak();
}

View File

@@ -5,12 +5,12 @@ parsing_error(Token *token, const char *str, ...){
STRING_FMT(scratch, str, string);
// @Note(Krzosa): Print nice error message
printf("\nError: %s", string.str);
printf("\nError :: %s", string.str);
if(token){
if(token->kind == TK_Error){
printf("Token Error: %.*s", (int)token->error_val.len, token->error_val.str);
}
printf(" %s:%d\n", token->file.str, (S32)token->line + 1);
printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1);
// @Note(Krzosa): Print error line
{
@@ -409,7 +409,11 @@ parse_expr(S64 min_bp){
case TK_Decrement : left = ast_expr_unary(token, TK_Decrement, parse_expr(prefix_bp.right)); break;
case TK_OpenBracket: {
Ast_Array *result = ast_array(token, parse_expr(0));
Ast_Expr *expr = 0;
if(!token_is(TK_CloseBracket))
expr = parse_expr(0);
Ast_Array *result = ast_array(token, expr);
token_expect(TK_CloseBracket);
result->base = parse_expr(prefix_bp.right);
left = result;

View File

@@ -44,7 +44,7 @@ binary_test :: (thing: S32 = 442)
bit_xor :: 8 ^ 7
character :: 'ó
// '
boolean_equals :: true == false
boolean_var: Bool = boolean_equals

View File

@@ -19,6 +19,11 @@ typedef float F32;
typedef double F64;
typedef S32 Bool;
typedef struct Slice{
S64 len;
void *data;
}Slice;
typedef struct String{
U8 *str;
S64 len;

View File

@@ -570,8 +570,12 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
CASE(INDEX, Index){
Operand left = resolve_expr(node->expr);
Operand index = resolve_expr(node->index);
if(left.type->kind != TYPE_ARRAY && left.type->kind != TYPE_POINTER) parsing_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %s instead", docname(left.type));
if(!is_int(index.type)) type_error(node->pos, untyped_int, index.type,"Trying to index the array with invalid type, expected int");
if(!is_int(index.type)){
type_error(node->pos, untyped_int, index.type,"Trying to index the array with invalid type, expected int");
}
if(!is_array(left.type) && !is_pointer(left.type)){
parsing_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %s instead", docname(left.type));
}
return operand_lvalue(left.type->arr.base);
BREAK();
}