From 802dce749e417ba106810c0735afcf1bb7eff135 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 29 May 2022 23:42:53 +0200 Subject: [PATCH] Change syntax of compound exprs --- ccodegen.cpp | 2 +- globals.kl | 8 +++--- main.cpp | 18 ++++++------ new_parse.cpp | 56 ++++++++++++++++++++---------------- order1.kl | 17 +++++++++++ structs.kl => order2.kl | 2 +- order_independent_globals.kl | 37 ------------------------ 7 files changed, 65 insertions(+), 75 deletions(-) create mode 100644 order1.kl rename structs.kl => order2.kl (93%) delete mode 100644 order_independent_globals.kl diff --git a/ccodegen.cpp b/ccodegen.cpp index 293a594..9d10b8f 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -285,7 +285,7 @@ gen_ast(Ast *ast){ else if(sym->type == type_type){ if(sym->type_val->kind == TYPE_STRUCT){ Ast_Struct *agg = const_get_struct(sym->type_val->sym->ast); - if(agg){ + if(node->value->kind == AST_STRUCT){ gen("struct %s{", node->name.str); global_indent++; For(agg->members){ diff --git a/globals.kl b/globals.kl index e36d3bb..c9f10c4 100644 --- a/globals.kl +++ b/globals.kl @@ -22,14 +22,14 @@ bool_null: bool = null //----------------------------------------------------------------------------- // Compound expressions //----------------------------------------------------------------------------- -array1: [4]int = {1,2,3,4} -array2: [32]int = {1,2,3,4} -array3: [32]int = { +array1: [4]int = [4]int(1,2,3,4) +array2 := [32]int(1,2,3,4) +array3 := [32]int( [0] = null, [1] = 1, [2] = 2, [31] = 31, -} +) array_item := array1[0] array_item_imp: int = array2[2] diff --git a/main.cpp b/main.cpp index 4200680..53ded93 100644 --- a/main.cpp +++ b/main.cpp @@ -26,13 +26,15 @@ /// @todo /// [x] - Typespecs should probably be expressions so stuff like would be possible :: *[32]int -/// [ ] - Lexer: Need to insert scope endings when hitting End of file -/// [ ] - Add single line lambda expressions/ -/// [ ] - Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1} +/// [x] - Initial order independence algorithm +/// [ ] - Cleanup the mess with constant bindings /// [ ] - Structs /// [ ] - Enums /// [ ] - For loop /// [ ] - Switch +/// [ ] - Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1} +/// [ ] - Lexer: Need to insert scope endings when hitting End of file +/// [ ] - Add single line lambda expressions/ int main(){ @@ -47,12 +49,12 @@ int main(){ test_intern_table(); lex_test(); - // String result = compile_file("globals.kl"_s); - String result = compile_file("structs.kl"_s); - // String result = compile_file("order_independent_globals.kl"_s); + String result = {}; + // result = compile_file("order1.kl"_s); + // result = compile_file("lambdas.kl"_s); + // result = compile_file("order2.kl"_s); + result = compile_file("globals.kl"_s); printf("%s", result.str); - // compile_file("lambdas.kl"_s); - // compile_file("globals.kl"_s); __debugbreak(); } diff --git a/new_parse.cpp b/new_parse.cpp index 94856c3..c4bf5bc 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -136,12 +136,12 @@ Compound literals function Ast_Expr *parse_expr(S64 rbp = 0); function Ast_Compound * -parse_expr_compound(){ +parse_expr_compound(Ast_Expr *left){ Scratch scratch; Token *pos = token_get(); Array exprs = {scratch}; - while(!token_is(TK_CloseBrace)){ + while(!token_is(TK_CloseParen)){ Token *token = token_get(); Ast_Expr *index = 0; Ast_Atom *name = 0; @@ -164,9 +164,9 @@ parse_expr_compound(){ break; } } - token_expect(TK_CloseBrace); + token_expect(TK_CloseParen); - Ast_Compound *result = ast_expr_compound(pos, 0, exprs); + Ast_Compound *result = ast_expr_compound(pos, left, exprs); return result; } @@ -287,12 +287,14 @@ null_denotation(Token *token){ case TK_Integer : return ast_int(token, token->int_val, AST_EXPR); case TK_Pointer : return ast_expr_unary(token, TK_Pointer, parse_expr()); case TK_Dereference: return ast_expr_unary(token, TK_Dereference, parse_expr()); + case TK_OpenBracket: { Ast_Array *result = ast_array(token, parse_expr()); token_expect(TK_CloseBracket); - result->base = parse_expr(); + result->base = parse_expr(1); return result; }break; + case TK_Keyword: { if(token->intern_val == keyword_cast){ token_expect(TK_OpenParen); @@ -307,7 +309,7 @@ null_denotation(Token *token){ return 0; } }break; - case TK_OpenBrace: return parse_expr_compound(); + case TK_OpenParen: { if (token_is(TK_CloseParen)) return parse_lambda(token); else if(token_is(TK_Identifier) && token_is(TK_Colon, 1)) return parse_lambda(token); @@ -330,14 +332,6 @@ left_binding_power(Token_Kind kind){ } } -function S64 -postfix_binding_power(Token_Kind kind){ - switch(kind){ - case TK_Increment: case TK_Decrement: case TK_OpenBracket: return 1; - default: return 0; - } -} - function Ast_Expr * left_denotation(Token *op, Ast_Expr *left){ enum{ Left_Associative, Right_Associative }; @@ -349,6 +343,14 @@ left_denotation(Token *op, Ast_Expr *left){ } } +function S64 +postfix_binding_power(Token_Kind kind){ + switch(kind){ + case TK_Decrement: case TK_Increment: case TK_OpenBracket: case TK_OpenParen: return 1; + default: return 0; + } +} + function Ast_Expr * parse_expr(S64 rbp){ Token *token = token_next(); @@ -356,17 +358,23 @@ parse_expr(S64 rbp){ for(;;){ token = token_get(); - if((rbp == 0) && (token->kind == TK_Increment || token->kind == TK_Decrement || token->kind == TK_OpenBracket)){ + if(postfix_binding_power(token->kind) > rbp){ token_next(); - if(token->kind == TK_OpenBracket){ - Ast_Expr *index = parse_expr(); - left = ast_expr_index(token, left, index); - token_expect(TK_CloseBracket); - } - else{ - if(token->kind == TK_Increment) token->kind = TK_PostIncrement; - else if(token->kind == TK_Decrement) token->kind = TK_PostDecrement; - left = ast_expr_unary(token, token->kind, left); + // @note: parse postfix + switch(token->kind){ + case TK_OpenBracket:{ + Ast_Expr *index = parse_expr(); + left = ast_expr_index(token, left, index); + token_expect(TK_CloseBracket); + }break; + case TK_OpenParen:{ + left = parse_expr_compound(left); + }break; + default:{ + if(token->kind == TK_Increment) token->kind = TK_PostIncrement; + else if(token->kind == TK_Decrement) token->kind = TK_PostDecrement; + left = ast_expr_unary(token, token->kind, left); + } } } diff --git a/order1.kl b/order1.kl new file mode 100644 index 0000000..b224a73 --- /dev/null +++ b/order1.kl @@ -0,0 +1,17 @@ + +other_func :: () + a_val := recursive_lambda + +recursive_lambda :: (thing: int) + in_val := recursive_lambda + some_value := thing + const_in_lambda + +const_in_lambda :: 10 + +not_const := val + 10 +val := 10 + +DEPENDENCE :: CONSTANT_VAL +CONSTANT_VAL :: 10 + + diff --git a/structs.kl b/order2.kl similarity index 93% rename from structs.kl rename to order2.kl index 2245b67..6a4ce73 100644 --- a/structs.kl +++ b/order2.kl @@ -1,3 +1,4 @@ +Str16 :: String16 arena_pointer: *Arena = null thing: Arena @@ -21,4 +22,3 @@ pointer := &with_type deref := *pointer -Str16 :: String16 \ No newline at end of file diff --git a/order_independent_globals.kl b/order_independent_globals.kl deleted file mode 100644 index 0f4fa99..0000000 --- a/order_independent_globals.kl +++ /dev/null @@ -1,37 +0,0 @@ - -/* -@todo: !!! -Maybe instead of compound exprs like this: - [4]Thing{1,2,3,4} - -Do it like this - [4]Thing(1,2,3,4) - [4]Thing([0]=1, [3]=3) - Thing(a=1, b=2) - -unifying the call expression and compound expression -seems more unified and better for coherence -but more abigous and probably harder to implement - -value :: call((thing: int): int thing += 1; return thing, ) - - -*/ - - -other_func :: () - a_val := recursive_lambda - -recursive_lambda :: (thing: int) - in_val := recursive_lambda - some_value := thing + const_in_lambda - -const_in_lambda :: 10 - -not_const := val + 10 -val := 10 - -DEPENDENCE :: CONSTANT_VAL -CONSTANT_VAL :: 10 - -