From 0e398c84b6231f392f0bc8fac46e3cf17e29b9ea Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 26 May 2022 20:21:24 +0200 Subject: [PATCH] Cleanup, There is no decl anymore, Ast_Named --- cgenerate.cpp | 15 +++++---- lambdas.kl | 14 ++++++-- main.cpp | 3 +- new_ast.cpp | 57 ++++++++++++++++---------------- new_parse.cpp | 63 +++++++++++++++++++----------------- new_resolve.cpp | 22 ++++++------- order_independent_globals.kl | 4 +++ 7 files changed, 98 insertions(+), 80 deletions(-) create mode 100644 order_independent_globals.kl diff --git a/cgenerate.cpp b/cgenerate.cpp index d31a137..63fde68 100644 --- a/cgenerate.cpp +++ b/cgenerate.cpp @@ -197,12 +197,12 @@ gen_ast(Ast *ast){ Ast_End(); } - Ast_Begin(AST_VAR, Ast_Decl){ + Ast_Begin(AST_VAR, Ast_Var){ Sym *sym = resolved_get(node); gen_simple_decl(sym->type, node->name); - if(node->var.expr){ + if(node->expr){ gen(" = "); - gen_expr(node->var.expr); + gen_expr(node->expr); } gen(";"); Ast_End(); @@ -241,12 +241,12 @@ gen_ast(Ast *ast){ Ast_End(); } - Ast_Begin(AST_CONST, Ast_Decl){ + Ast_Begin(AST_CONST, Ast_Const){ Sym *sym = resolved_get(node); if(sym->type->kind == TYPE_Lambda){ - if(node->var.expr->kind == AST_LAMBDA){ - Ast_Lambda *lambda = (Ast_Lambda *)node->var.expr; + if(node->expr->kind == AST_LAMBDA){ + Ast_Lambda *lambda = (Ast_Lambda *)node->expr; gen("static "); gen_simple_decl(lambda->ret->resolved_type, node->name); gen("("); @@ -265,7 +265,7 @@ gen_ast(Ast *ast){ else{ gen_simple_decl(sym->type, node->name); gen(" = "); - gen_expr(node->var.expr); + gen_expr(node->expr); gen(";"); } } @@ -316,6 +316,7 @@ function String compile_file(String filename){ Scratch scratch; String filecontent = os_read_file(scratch, filename); + assert(filecontent.len); String result = compile_string(filecontent, filename); return result; } diff --git a/lambdas.kl b/lambdas.kl index d1ad82e..80008a1 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -1,11 +1,19 @@ +/* + +@todo!!! +I think it might be better to try doing order indepent decls +as well as the tree transformations in the backend code +plus some cleanups before adding more complications to the code + +*/ if_stmt :: (cond: int): int CONSTANT :: 10 - - if i := cond, cond + CONSTANT + thing := 10 + if i := thing + cond, cond + CONSTANT return i + CONSTANT else if cond - CONSTANT - return cond - CONSTANT + return i - CONSTANT else return CONSTANT diff --git a/main.cpp b/main.cpp index 27e5b6b..9c0c779 100644 --- a/main.cpp +++ b/main.cpp @@ -19,9 +19,10 @@ int main(){ test_intern_table(); lex_test(); - String result = compile_file("lambdas.kl"_s); + String result = compile_file("order_independent_globals.kl"_s); printf("%s", result.str); + compile_file("lambdas.kl"_s); compile_file("globals.kl"_s); __debugbreak(); } diff --git a/new_ast.cpp b/new_ast.cpp index dd25d3d..e832860 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -113,6 +113,7 @@ struct Ast{ bool is_stmt: 1; bool is_expr: 1; bool is_decl: 1; + bool is_named: 1; }; struct Ast_Resolved_Type; @@ -209,19 +210,24 @@ struct Ast_Typespec:Ast{ }; }; -struct Ast_Decl:Ast{ +struct Ast_Named:Ast{ Intern_String name; - union{ - struct{ - Ast_Typespec *typespec; - Ast_Expr *expr; - }var; - }; +}; + +struct Ast_Var: Ast_Named{ + Intern_String name; + Ast_Typespec *typespec; + Ast_Expr *expr; +}; + +struct Ast_Const: Ast_Named{ + Intern_String name; + Ast_Expr *expr; }; struct Ast_Package:Ast{ Intern_String name; - Array decls; + Array decls; }; function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name); @@ -398,31 +404,26 @@ ast_typespec_lambda(Token *pos, Ast_Lambda *lambda){ //----------------------------------------------------------------------------- // Declarations //----------------------------------------------------------------------------- -function Ast_Decl * -ast_decl_func(Token *pos, Intern_String name){ - AST_NEW(Decl, AST_LAMBDA, pos); + +function Ast_Var * +ast_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){ + AST_NEW(Var, AST_VAR, pos); + result->expr = expr; + result->typespec = typespec; + result->name = name; + return result; +} + +function Ast_Const * +ast_const(Token *pos, Intern_String name, Ast_Expr *expr){ + AST_NEW(Const, AST_CONST, pos); + result->expr = expr; result->name = name; return result; } -function Ast_Decl * -ast_decl_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){ - AST_NEW(Decl, AST_VAR, pos); - result->var.expr = expr; - result->var.typespec = typespec; - result->name = name; - return result; -} - -function Ast_Decl * -ast_decl_const(Token *pos, Intern_String name, Ast_Expr *expr){ - Ast_Decl *result = ast_decl_var(pos, 0, name, expr); - result->kind = AST_CONST; - return result; -} - function Ast_Package * -ast_package(Token *pos, String name, Array decls){ +ast_package(Token *pos, String name, Array decls){ AST_NEW(Package, AST_PACKAGE, pos); result->decls = decls.tight_copy(pctx->perm); result->name = intern_string(&pctx->interns, name); diff --git a/new_parse.cpp b/new_parse.cpp index 7140cdf..fbbf1a4 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -177,7 +177,22 @@ parse_optional_type(){ return result; } -function Ast_Decl *parse_decl(B32); +function Ast_Init * +parse_init_stmt(Ast_Expr *expr){ + Token *token = token_get(); + if(token_match(TK_Colon)){ + if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]"); + if(token_match(TK_Assign)){ + Ast_Expr *value = parse_expr(); + Ast_Init *result = ast_init(token, TK_Comma, (Ast_Atom *)expr, value); + return result; + } + else not_implemented; + } + return 0; +} + +function Ast_Named *parse_named(B32); function Ast_Block * parse_block(){ Ast_Block *block = 0; @@ -195,24 +210,11 @@ parse_block(){ } else if(token_match_keyword(keyword_if)){ Array if_nodes = {scratch}; - Ast_Expr *expr = parse_expr(); - Ast_Init *init_val = 0; - - { - Token *token = token_get(); - if(token_match(TK_Colon)){ - if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]"); - if(token_match(TK_Assign)){ - Ast_Expr *value = parse_expr(); - assert(expr->kind == AST_IDENT); - init_val = ast_init(token, TK_Comma, (Ast_Atom *)expr, value); - } - else not_implemented; - - if(token_match(TK_Comma)){ - expr = parse_expr(); - } - } + Ast_Expr *expr = parse_expr(); + Ast_Init *init_val = parse_init_stmt(expr); + if(init_val){ + if(token_match(TK_Comma)) expr = parse_expr(); + else expr = 0; } Ast_Block *block = parse_block(); @@ -240,7 +242,7 @@ parse_block(){ } else{ - Ast_Decl *result = parse_decl(false); + Ast_Named *result = parse_named(false); if(result) stmts.add(result); else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str); } @@ -416,16 +418,16 @@ parse_assign_expr(){ return result; } -function Ast_Decl * -parse_decl(B32 is_global){ - Ast_Decl *result = 0; +function Ast_Named * +parse_named(B32 is_global){ + Ast_Named *result = 0; if(is_global) token_match(SAME_SCOPE); if(token_is(TK_Identifier)){ if(is_global && pctx->indent != 0) parsing_error(token_get(), "Top level declarations shouldn't be indented"); Token *name = token_next(); if(token_match(TK_DoubleColon)){ // Constant Ast_Expr *expr = parse_expr(); - result = ast_decl_const(name, name->intern_val, expr); + result = ast_const(name, name->intern_val, expr); } else if(token_match(TK_Colon)){ Ast_Typespec *typespec = 0; @@ -434,7 +436,7 @@ parse_decl(B32 is_global){ if(token_match(TK_Assign)) expr = parse_expr(); if(!expr && !typespec) parsing_error(name, "invalid declaration, no type or value"); - result = ast_decl_var(name, typespec, name->intern_val, expr); + result = ast_var(name, typespec, name->intern_val, expr); } else{ Token *token = token_get(); @@ -455,16 +457,19 @@ parse_file(){ Scratch scratch; // - // @note: pop the first token which which always should be an indentation token, - // it updates the indent info on the parser, making sure that indentation on + // @note: pop the first token with token_next() / token_expect() + // which always should be an indentation token, + // it updates the indent info on the parser, + // making sure that indentation on // the first line is properly updated // Token *token = token_get(); - Arraydecls = {scratch}; + Arraydecls = {scratch}; while(!token_is(TK_End)){ token_expect(SAME_SCOPE); - Ast_Decl *decl = parse_decl(true); + Ast_Named *decl = parse_named(true); if(!decl) break; + decls.add(decl); } Ast_Package *result = ast_package(token, token->file, decls); diff --git a/new_resolve.cpp b/new_resolve.cpp index c676d77..9b6b71f 100644 --- a/new_resolve.cpp +++ b/new_resolve.cpp @@ -28,7 +28,7 @@ struct Operand{ }; }; -global Ast_Decl empty_decl = {}; +global Ast_Named empty_decl = {}; function void sym_insert(Sym *sym){ @@ -202,12 +202,12 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){ Ast_End(); } - Ast_Begin(AST_VAR, Ast_Decl){ + Ast_Begin(AST_VAR, Ast_Var){ eval_decl(node); Ast_End(); } - Ast_Begin(AST_CONST, Ast_Decl){ + Ast_Begin(AST_CONST, Ast_Const){ eval_decl(node); Ast_End(); } @@ -226,8 +226,8 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){ Ast_Begin(AST_IF, Ast_If){ For(node->ifs){ - if(it[0]->expr) eval_expr(it[0]->expr); if(it[0]->init) eval_stmt(it[0]->init, ret); + if(it[0]->expr) eval_expr(it[0]->expr); S64 scope_index = scope_push(); For_It(it[0]->block->stmts, jt){ eval_stmt(jt[0], ret); @@ -454,9 +454,9 @@ eval_decl(Ast *ast){ Ast_End(); } - Ast_Begin(AST_VAR, Ast_Decl){ - Ast_Resolved_Type *type = eval_typespec(node->var.typespec); - Operand expr = node->var.expr ? eval_expr(node->var.expr, type) : Operand{}; + Ast_Begin(AST_VAR, Ast_Var){ + Ast_Resolved_Type *type = eval_typespec(node->typespec); + Operand expr = node->expr ? eval_expr(node->expr, type) : Operand{}; Ast_Resolved_Type *resolved_type = resolve_type_pair(node->pos, type, expr.type); Sym *sym = sym_new(SYM_Var, node->name, resolved_type, node); @@ -464,13 +464,11 @@ eval_decl(Ast *ast){ Ast_End(); } - Ast_Begin(AST_CONST, Ast_Decl){ - Ast_Resolved_Type *type = eval_typespec(node->var.typespec); - if(type && type->kind == TYPE_Pointer) parsing_error(node->pos, "Const cant be a pointer"); - Operand expr = eval_expr(node->var.expr); + Ast_Begin(AST_CONST, Ast_Const){ + Operand expr = eval_expr(node->expr); if(!expr.type) parsing_error(node->pos, "Constant value without expression"); if(!expr.is_const) parsing_error(node->pos, "Value of constant variable is not a constant expression"); - Ast_Resolved_Type *resolved_type = resolve_type_pair(node->pos, type, expr.type); + Ast_Resolved_Type *resolved_type = expr.type; Sym *sym = sym_new(SYM_Const, node->name, resolved_type, node); if(resolved_type == type_int) sym->int_val = expr.int_val; diff --git a/order_independent_globals.kl b/order_independent_globals.kl new file mode 100644 index 0000000..0bb3385 --- /dev/null +++ b/order_independent_globals.kl @@ -0,0 +1,4 @@ + +DEPENDENCE :: CONSTANT_VAL +CONSTANT_VAL :: 10 +