diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2023-09-05 18:30:01 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2023-10-31 12:30:09 +0200 |
| commit | fed864bf018e7d97fda24089ebe08fa0da1c3b97 (patch) | |
| tree | 709dddea28995c58931f28a69525258129aa2fef | |
| parent | a06ed46bffa94e4613052e0ddf0cb70ab32651ab (diff) | |
| download | ek-fed864bf018e7d97fda24089ebe08fa0da1c3b97.tar.gz ek-fed864bf018e7d97fda24089ebe08fa0da1c3b97.zip | |
more or less happy with syntax
| -rw-r--r-- | TODO | 1 | ||||
| -rw-r--r-- | src/lexer.l | 1 | ||||
| -rw-r--r-- | src/parser.y | 58 | ||||
| -rw-r--r-- | tests/arr.ek | 2 | ||||
| -rw-r--r-- | tests/blocks.ek | 4 | ||||
| -rw-r--r-- | tests/callbacks.ek | 4 | ||||
| -rw-r--r-- | tests/calls.ek | 4 | ||||
| -rw-r--r-- | tests/enums.ek | 4 | ||||
| -rw-r--r-- | tests/resolve.ek | 10 | ||||
| -rw-r--r-- | tests/structs.ek | 22 | ||||
| -rw-r--r-- | tests/unions.ek | 20 | ||||
| -rw-r--r-- | tests/variadic.ek | 2 | ||||
| -rw-r--r-- | tests/vars.ek | 32 |
13 files changed, 79 insertions, 85 deletions
@@ -14,6 +14,7 @@ to be used to interface with C + Build new type system (current one build on top of AST fucking sucks) + Build the consteval stuff ++ Use built in location tracking with @$ in bison? OPTIMIZATIONS: + Use hashmaps or something similar for lookups for types and calls diff --git a/src/lexer.l b/src/lexer.l index ac8ed67..3be3ded 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -172,6 +172,7 @@ STRING \"(\\.|[^"\\])*\" "for" {return FOR;} "while" {return WHILE;} "do" {return DO;} +"let" {return LET;} "mut" {return MUT;} "const" {return CONST;} "return" {return RETURN;} diff --git a/src/parser.y b/src/parser.y index 986c47e..369ff75 100644 --- a/src/parser.y +++ b/src/parser.y @@ -104,6 +104,7 @@ %token MUT "mut" %token RETURN "return" %token CONST "const" +%token LET "let" %token EXTERN "extern" %token ENUM "enum" %token DEFINE "define" @@ -302,7 +303,7 @@ unop : "-" expr { $$ = gen_unop(AST_NEG, $2); } | "!" expr { $$ = gen_unop(AST_LNOT, $2); } | "&" expr { $$ = gen_unop(AST_REF, $2); } - | "'" expr { $$ = gen_unop(AST_DEREF, $2); } + | "*" expr { $$ = gen_unop(AST_DEREF, $2); } | "~" expr { $$ = gen_unop(AST_NOT, $2); } arr_init @@ -319,8 +320,6 @@ arr arg : "&" var_decl { $$ = gen_unop(AST_REF, $2); } - | construct - | var_decl | expr | arr @@ -395,6 +394,7 @@ expr | expr "as" type { $$ = gen_cast($1, $3); } | id "::" type { $$ = gen_fetch($1, $3); } | "as" type { $$ = gen_as($2); } + | construct | lambda | switch | assign @@ -568,11 +568,12 @@ func_sign | "(" decls ")" { $$ = gen_type(AST_TYPE_SIGN, NULL, $2, NULL); } | "(" decls "=>" ")" { $$ = gen_type(AST_TYPE_SIGN, NULL, $2, NULL); } | "(" "=>" type ")" { $$ = gen_type(AST_TYPE_SIGN, NULL, NULL, $3); } + | "(" "=>" ")" { $$ = gen_type(AST_TYPE_SIGN, NULL, NULL, NULL); } | "(" ")" { $$ = gen_type(AST_TYPE_SIGN, NULL, NULL, NULL); } type : id { $$ = gen_type(AST_TYPE_ID, $1, NULL, NULL); } - | "::" func_sign { + | "^" func_sign { /* still not entirely sold on this signature, but it's not terrible I * guess */ $$ = gen_type(AST_TYPE_POINTER, NULL, NULL, NULL); @@ -581,13 +582,13 @@ type | apply "[" types "]" { $$ = gen_type(AST_TYPE_GENERIC, $1, $3, NULL); } - | "'" type { + | "*" type { $$ = gen_type(AST_TYPE_POINTER, NULL, NULL, NULL); $$->_type.next = $2; } - | "'" "[" const_expr "]" type { - $$ = gen_type(AST_TYPE_ARR, NULL, $3, NULL); - $$->_type.next = $5; + | "[" const_expr "]" type { + $$ = gen_type(AST_TYPE_ARR, NULL, $2, NULL); + $$->_type.next = $4; } | "typeof" expr { $$ = gen_type(AST_TYPE_TYPEOF, NULL, $2, NULL); @@ -595,24 +596,22 @@ type | id "::" type { $$ = gen_type(AST_TYPE_MEMBER, $1, $3, NULL); } + | "const" type { + $$ = $2; + } + | "mut" type { + $$ = $2; ast_set_flags($$, AST_FLAG_MUTABLE); + } var_decl - : id "mut" type { - $$ = gen_var($1, $3, NULL); - ast_set_flags($$, AST_FLAG_MUTABLE); - } - | id "const" type { $$ = gen_var($1, $3, NULL); } - | id type { $$ = gen_var($1, $2, NULL); } + : type id { $$ = gen_var($2, $1, NULL); } var_init - : var_decl "=" arg { $$ = $1; $1->_var.init = $3; } - | id "mut" "=" arg { - $$ = gen_var($1, NULL, $4); - ast_set_flags($$, AST_FLAG_UNTYPED | AST_FLAG_MUTABLE); - } - | id "const" "=" arg { - $$ = gen_var($1, NULL, $4); - ast_set_flags($$, AST_FLAG_UNTYPED); + : var_decl "=" arg { $$ = $1; $$->_var.init = $3; } + | "const" id "=" arg { $$ = gen_var($2, NULL, $4); } + | "mut" id "=" arg { + $$ = gen_var($2, NULL, $4); + ast_set_flags($$, AST_FLAG_MUTABLE); } proc @@ -650,7 +649,7 @@ union } generic - : id type { $$ = gen_alias($1, $2); } + : type id { $$ = gen_alias($1, $2); } generics : generic "," generics { $$ = $1; $$->next = $3; } @@ -725,17 +724,12 @@ top_if } top_var_decl - : id "mut" type { - $$ = gen_var($1, $3, NULL); - ast_set_flags($$, AST_FLAG_MUTABLE); - } - | id "const" type { $$ = gen_var($1, $3, NULL); } - | id type { $$ = gen_var($1, $2, NULL); } + : type id { $$ = gen_var($2, $1, NULL); } top_var_init - : top_var_decl "=" const_expr { $$ = $1; $1->_var.init = $3; } - | id "mut" "=" const_expr { $$ = $1; $1->_var.init = $4; } - | id "const" "=" const_expr { $$ = $1; $1->_var.init = $4; } + : top_var_decl "=" const_expr { $$ = $1; $$->_var.init = $3; } + | "const" id "=" const_expr { $$ = gen_var($2, NULL, $4); } + | "mut" id "=" const_expr { $$ = gen_var($2, NULL, $4); } top_var : top_var_decl { $$ = $1; } diff --git a/tests/arr.ek b/tests/arr.ek index 8596fd0..c206615 100644 --- a/tests/arr.ek +++ b/tests/arr.ek @@ -1,5 +1,5 @@ main() { - a '[20]'[20]'type![u32, u32] = [20, => 20 ... 200 = 240, [200, 200]]; + [20][20]*type![u32, u32] a = [20, => 20 ... 200 = 240, [200, 200]]; a[20] = 200; } diff --git a/tests/blocks.ek b/tests/blocks.ek index e91d33c..bc210a2 100644 --- a/tests/blocks.ek +++ b/tests/blocks.ek @@ -1,10 +1,10 @@ main() { - a mut = switch 1 { + mut a = switch 1 { case 2: 2 case 1: 1 default: 20; 20 }; - c mut = ({2 + 2; 4 + 4}); + mut c = ({2 + 2; 4 + 4}); } diff --git a/tests/callbacks.ek b/tests/callbacks.ek index 9b9d649..6e753e0 100644 --- a/tests/callbacks.ek +++ b/tests/callbacks.ek @@ -1,4 +1,4 @@ -do_stuff(proc @(u32)) +do_stuff(^(u32) proc) { proc(); } @@ -13,5 +13,5 @@ other_proc() main() { - do_stuff(other_proc as @(u32)); + do_stuff(other_proc as ^(u32)); } diff --git a/tests/calls.ek b/tests/calls.ek index 4978649..0a4c20d 100644 --- a/tests/calls.ek +++ b/tests/calls.ek @@ -2,13 +2,13 @@ typedef some_type { add(some_type, some_type => some_type); } -add(a u32, b u32 => u32) +add(u32 a, u32 b => u32) { return a + b; } /* ah fuck, analyze_proc gobbles up the typeof */ -some_proc(a some_type, b typeof a => typeof a) +some_proc(some_type a, typeof a b => typeof a) { return add(a, b); } diff --git a/tests/enums.ek b/tests/enums.ek index f52f5d6..234bbd1 100644 --- a/tests/enums.ek +++ b/tests/enums.ek @@ -9,6 +9,6 @@ enum B: u8 { main() { - a const = a::A; - b const = b::B; + const a = a::A; + const b = b::B; } diff --git a/tests/resolve.ek b/tests/resolve.ek index 5ac7d71..f417fde 100644 --- a/tests/resolve.ek +++ b/tests/resolve.ek @@ -1,13 +1,13 @@ typedef A {} struct generic (T1 A, T2 A) { - a T1; - b T2; + T1 a; + T2 b; } struct other_generic(T1 A) { - a T1; - b T1; + T1 a; + T2 b; } /* wow this works pretty good */ @@ -30,6 +30,6 @@ some_func(generic){5;} main(){ // TODO: not fully qualified types in bodies should cause an error - a [20]generic![i64, generic]; + [20]generic![i64, generic] a; some_func(a); } diff --git a/tests/structs.ek b/tests/structs.ek index 20d9c48..1444d24 100644 --- a/tests/structs.ek +++ b/tests/structs.ek @@ -1,26 +1,26 @@ typedef any {} struct basic_struct { - a i64; - b i64; - c i64; + i64 a; + i64 b; + i64 c; } -struct complex_struct (A any, B any, C any) { - a A; - b B; - c C; +struct complex_struct (any A, any B, any C) { + A a; + B b; + C c; } main() { /* infer types */ - simple_named const = {.a = 1, .b = 2, .c = 3} as basic_struct; - complex_named const = {.a = 1, .b = 2, .c = 3} as complex_struct; + const simple_named = {.a = 1, .b = 2, .c = 3} as basic_struct; + const complex_named = {.a = 1, .b = 2, .c = 3} as complex_struct; /* force types */ - force_named complex_struct![u32, u32, u32] = {.a = 1, .b = 2, .c = 3}; - force_inferred_named mut = {.a = 1, .b = 2, .c = 3} as + complex_struct![u32, u32, u32] force_named = {.a = 1, .b = 2, .c = 3}; + mut force_inferred_named = {.a = 1, .b = 2, .c = 3} as complex_struct![u32, u32, u32]; /* types should be inferred */ diff --git a/tests/unions.ek b/tests/unions.ek index 6c3edc5..b493d5a 100644 --- a/tests/unions.ek +++ b/tests/unions.ek @@ -1,23 +1,21 @@ typedef any {} union basic_union { - a u32; - b i64; - c f32; + u32 a; + i64 b; + f32 c; } -union complex_union(A any, B any, C any) { - a A; - b B; - c C; +union complex_union(any A, any B, any C) { + A a; + B b; + C c; } main() { - simple_named const = {.b = 1} as basic_union; - simple_ordinal const = {1 as u32} as basic_union; + const simple_named = {.b = 1} as basic_union; // TODO: unions should be fully actualized - complex_named const = {.b = 1} as complex_union(u32, i64, f32); - complex_ordinal const = {1 as u32} as complex_union(u32, i64, f32); + const complex_named = {.b = 1} as complex_union![u32, i64, f32]; } diff --git a/tests/variadic.ek b/tests/variadic.ek index 264e2f0..ae3c2c2 100644 --- a/tests/variadic.ek +++ b/tests/variadic.ek @@ -4,4 +4,4 @@ define macro(a, b, ... c) } } -proc(a u32, b u32, ... c) {} +proc(u32 a, u32 b, ... c) {} diff --git a/tests/vars.ek b/tests/vars.ek index 8780e3f..ad56213 100644 --- a/tests/vars.ek +++ b/tests/vars.ek @@ -1,33 +1,33 @@ typedef any {} struct some_struct { - a i64; + i64 a; } -struct some_template (A any) { - a A; +struct some_template (any A) { + A a; } main() { - a mut i64 = 20; + i64 a = 20; a = 200; - b mut some_struct = {20}; - b = {200}; + some_struct b = {.a = 20}; + b = {.a = 200}; - c mut = {20} as some_struct; - c = {200}; + mut c = {.a = 20} as some_struct; + c = {.a = 200}; - d mut some_template = {20}; - d = {200}; + mut some_template d = {.a = 20}; + d = {.a = 200}; - e mut some_template (i64) = {20}; - e = {200}; + mut some_template![i64] e = {.a = 20}; + e = {.a = 200}; - f mut = {20} as some_template; - f = {200}; + mut f = {.a = 20} as some_template; + f = {.a = 200}; - g mut = {20} as some_template(i64); - g = {200}; + mut g = {.a = 20} as some_template(i64); + g = {.a = 200}; } |
