From 44d26d6939c29fdc676efa07b77402acbaaad4d3 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 7 Jun 2022 15:27:18 +0200 Subject: [PATCH] Can call the compiler from command line with argument --- ccodegen.cpp | 7 ++++++- main.cpp | 26 +++++++++++++++----------- new_parse.cpp | 10 +++++++--- new_types.kl | 2 +- program.c | 5 +++++ typecheck.cpp | 8 ++++++-- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index f1f21ec..b6eb10c 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -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 #include @@ -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; diff --git a/main.cpp b/main.cpp index 18dc627..3c4441c 100644 --- a/main.cpp +++ b/main.cpp @@ -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"); - assert(f); - fprintf(f, "%.*s", (int)result.len, result.str); - fclose(f); - system("clang.exe program.c -g -o program.exe"); -#endif + 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((const char *)call.str); + } __debugbreak(); } diff --git a/new_parse.cpp b/new_parse.cpp index 4be9c4c..1d8775f 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -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; diff --git a/new_types.kl b/new_types.kl index eed0ae7..e766be1 100644 --- a/new_types.kl +++ b/new_types.kl @@ -44,7 +44,7 @@ binary_test :: (thing: S32 = 442) bit_xor :: 8 ^ 7 character :: 'รณ - +// ' boolean_equals :: true == false boolean_var: Bool = boolean_equals diff --git a/program.c b/program.c index e84448b..86ac567 100644 --- a/program.c +++ b/program.c @@ -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; diff --git a/typecheck.cpp b/typecheck.cpp index 21899c0..439cdbe 100644 --- a/typecheck.cpp +++ b/typecheck.cpp @@ -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(); }