From cf1237f44960a50cb92a5a343eab6f73fcb53568 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 22:51:32 +0200 Subject: [PATCH] Fix big casting bug in CALL, euler.kl is working now too --- ccodegen.cpp | 4 ++++ euler.kl | 27 +++++++++++---------------- main.cpp | 1 + parsing.cpp | 26 +++++++++++--------------- typechecking.cpp | 7 ++++--- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index b1f40b3..e4b8f01 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -368,6 +368,10 @@ gen_ast(Ast *ast){ gen("// const Bool %s = ", node->name.str); gen_value(node->value); }break; + case TYPE_LAMBDA:{ + gen("// "); + gen_lambda(node->name, node->lambda); + } break; invalid_default_case; } diff --git a/euler.kl b/euler.kl index 931429f..05240e4 100644 --- a/euler.kl +++ b/euler.kl @@ -3,7 +3,7 @@ // @todo: Add blocks of stmts that you can simply define inside a function etc. entry :: () - printf("\n") + print_str("\n") euler1() euler3() @@ -18,7 +18,9 @@ euler1 :: () result += i else if i % 5 == 0 result += i - printf("Euler1: %lld\n", result) + print_str("Euler1: ") + print_int(result) + print_str("\n") //----------------------------------------------------------------------------- @@ -61,23 +63,16 @@ euler3 :: () if n > 2 results[results_len++] = n - printf("Euler3: ") + print_str("Euler3: ") is_correct: S64 = 1 for i := 0, i < results_len, i++ is_correct = is_correct * results[i] - printf("%lld ", results[i]) + print_int(is_correct) - printf(":: %lld", is_correct) + print_str(":: ") + print_int(is_correct) -//----------------------------------------------------------------------------- -// Euler 4 -//----------------------------------------------------------------------------- - - - -print_int :: (i: S64) - printf("%lld ", i) - -#foreign sqrt :: (v: F64): F64 -#foreign printf :: (str: String, ...) \ No newline at end of file +sqrt :: (v: F64): F64 #foreign +print_int :: (i: S64) #foreign +print_str :: (i: String) #foreign \ No newline at end of file diff --git a/main.cpp b/main.cpp index b970ee4..7c07d47 100644 --- a/main.cpp +++ b/main.cpp @@ -212,6 +212,7 @@ int main(int argument_count, char **arguments){ files.add("new_types.kl"_s); files.add("enums.kl"_s); files.add("globals.kl"_s); + files.add("euler.kl"_s); String result = compile_files(files); printf("%s", result.str); __debugbreak(); diff --git a/parsing.cpp b/parsing.cpp index a54af73..77d2191 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -183,14 +183,14 @@ parse_optional_type(){ } function Ast_Scope * -parse_stmt_scope(){ - Ast_Scope *scope = 0; +parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){ + Ast_Scope *scope = scope_defined_outside; if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context Token *token_block = token_get(); Scratch scratch; - scope = begin_stmt_scope(scratch, token_block); + if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block); do{ Token *token = token_get(); if(token_match_keyword(keyword_return)){ @@ -204,6 +204,7 @@ parse_stmt_scope(){ } else if(token_match_keyword(keyword_for)){ + Ast_Scope *for_scope = begin_stmt_scope(scratch, token_get()); Ast_Expr *init = 0; Ast_Expr *cond = 0; Ast_Expr *iter = 0; @@ -224,8 +225,9 @@ parse_stmt_scope(){ } } - Ast_Scope *for_block = parse_stmt_scope(); - scope->stmts.add(ast_for(token, init, cond, iter, for_block)); + parse_stmt_scope(for_scope); + finalize_stmt_scope(for_scope); + scope->stmts.add(ast_for(token, init, cond, iter, for_scope)); } else if(token_match_keyword(keyword_if)){ @@ -281,7 +283,7 @@ parse_stmt_scope(){ } while(token_match(SAME_SCOPE)); token_expect(CLOSE_SCOPE); - finalize_stmt_scope(scope); + if(!scope_defined_outside) finalize_stmt_scope(scope); } return scope; } @@ -318,10 +320,11 @@ parse_lambda(Token *token){ } } token_expect(TK_CloseParen); - - Ast_Expr *ret = parse_optional_type(); + Ast_Expr *ret = parse_optional_type(); + Token *foreign = token_match(TK_FOREIGN); Ast_Scope *scope = token_is(OPEN_SCOPE) ? parse_stmt_scope() : 0; Ast_Lambda *result = ast_lambda(token, params, ret, scope); + if(foreign) set_flag(result->flags, AST_FOREIGN); return result; } @@ -508,7 +511,6 @@ function Ast_Decl * parse_struct(Token *pos){ Scratch scratch; - token_match(OPEN_SCOPE); Ast_Scope *scope = begin_decl_scope(scratch, token_get()); do{ @@ -565,11 +567,6 @@ parse_decl(B32 is_global){ } } - Ast_Flag flags = 0; - if(token_match(TK_FOREIGN)){ - flags = set_flag(flags, AST_FOREIGN); - } - Token *tname = token_get(); if(token_match(TK_Identifier, TK_DoubleColon)){ // @note parse struct binding @@ -610,7 +607,6 @@ parse_decl(B32 is_global){ if(result){ result->name = tname->intern_val; - result->flags = set_flag(result->flags, flags); } return result; diff --git a/typechecking.cpp b/typechecking.cpp index 4544a59..ac656d4 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -779,8 +779,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){ S64 was_name_indexed = false; S64 default_iter = 0; - Ast_Decl *decl = (Ast_Decl *)name.type->ast; - Ast_Lambda *lambda = decl->lambda; + Ast_Lambda *lambda = (Ast_Lambda *)name.type->ast; for(S64 i = 0; i < lambda->args.len; i++){ Ast_Decl *lambda_arg = lambda->args[i]; assert(lambda_arg->type); @@ -829,7 +828,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){ else unset_flag(it->flags, AST_ITEM_INCLUDED); } - return operand_rvalue(name.type); + return operand_rvalue(name.type->func.ret); BREAK(); } @@ -877,6 +876,8 @@ resolve_decl(Ast_Decl *ast){ node->value = op.value; if(op.value.type == type_type){ node->kind = AST_TYPE; + } else if(is_lambda(op.value.type)){ + node->kind = AST_LAMBDA; } BREAK(); }