From: Brendan Hansen Date: Mon, 25 May 2020 21:47:38 +0000 (-0500) Subject: Starting on the semantic pass phase X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=75af429627a313e620d9490ff62470314115876b;p=onyx.git Starting on the semantic pass phase --- diff --git a/docs/plan b/docs/plan index 666e18db..0d11788d 100644 --- a/docs/plan +++ b/docs/plan @@ -9,15 +9,20 @@ WHAT: WHY: ONYX was made to help me learn about compiler design. +END GOAL: + ONYX will be used to make a simple-ish game for the browser that leverages + WASM and WebGL for a performant experience. Language design will reflect the + needs of the game programming. + FEATURES: - Strong type system - - Smart package loading - - Structs and enums - functions (no anonymous functions) + - Structs and enums - Control structures if, for, switch - pointers - inferred typing + - Smart package loading - defer ? polymorphic functions @@ -28,13 +33,13 @@ EXAMPLE CODE: use "core"; // Looks for "core.onyx" in the current directory -Foo :: struct { x: i32, y: i32 }; +Foo :: struct { x i32, y i32 }; -export add :: (a: i32, b: i32) -> i32 { - return a + b; +export add :: proc (a i32, b i32) -> i32 { + return a + b; }; -foo :: (a: i32) -> Foo { +foo :: proc (a i32) -> Foo { return Foo { x = a, y = 0 }; } @@ -42,16 +47,15 @@ MVP CODE: /* Comments need to be parsed */ -export proc add :: (a i32, b i32) -> i32 { +export add :: proc (a i32, b i32) -> i32 { return a + b; } -export proc max :: (a i32, b i32) -> i32 { +export max :: proc (a i32, b i32) -> i32 { /* Curly braces are required */ - if a > b { return a; } else { return b; } -} \ No newline at end of file +} diff --git a/docs/thoughts b/docs/thoughts new file mode 100644 index 00000000..ddf255fb --- /dev/null +++ b/docs/thoughts @@ -0,0 +1,24 @@ +Type checking at parse time: + Why couldn't this work? + * Every variable is of known type or the type must be known by immediate assignment + * This requires that functions are declared in a particular order like C + * This also requires immediate evaluation of external symbols (C #include style) + - Don't like this at all + - Want a proper module system + + /* foo.onyx */ + foo :: proc (a i32) -> f32 { + return a as f32; + } + + + /* main.onyx */ + use "foo"; + + export main :: proc () -> void { + a := 2.0f + foo(5); + } + + foo(5) would have a left node of SYMBOL:foo + This will be resolved in a later stage between the parsing and semantic pass + diff --git a/onyx b/onyx index 6ec79ea9..9015a7d8 100755 Binary files a/onyx and b/onyx differ diff --git a/onyxparser.c b/onyxparser.c index 82949e79..493527c5 100644 --- a/onyxparser.c +++ b/onyxparser.c @@ -207,6 +207,13 @@ static OnyxAstNode* parse_factor(OnyxParser* parser) { case TOKEN_TYPE_SYMBOL: { OnyxToken* sym_token = expect(parser, TOKEN_TYPE_SYMBOL); OnyxAstNode* sym_node = lookup_identifier(parser, sym_token); + if (sym_node == NULL) { + onyx_token_null_toggle(*sym_token); + onyx_message_add(parser->msgs, + ONYX_MESSAGE_TYPE_UNKNOWN_SYMBOL, + sym_token->pos, sym_token->token); + onyx_token_null_toggle(*sym_token); + } // TODO: Handle calling a function return sym_node; diff --git a/onyxutils.c b/onyxutils.c index 02d191f5..5fdddcde 100644 --- a/onyxutils.c +++ b/onyxutils.c @@ -2,26 +2,4 @@ void onyx_ast_print(OnyxAstNode* node) { if (node == NULL) return; - - bh_printf("%s <%d> ", onyx_ast_node_kind_string(node->kind), node->flags); - if (node->token) - bh_printf("[%b] ", node->token->token, (int) node->token->length); - - if ((i64) node->left > 10) { // HACK: but okay - bh_printf("("); - onyx_ast_print(node->left); - bh_printf(") "); - } - if ((i64) node->right > 10) { // HACK: but okay - bh_printf("("); - onyx_ast_print(node->right); - bh_printf(")"); - } - - if (node->next) { - bh_printf("{"); - onyx_ast_print(node->next); - bh_printf("}"); - } - } diff --git a/progs/minimal.onyx b/progs/minimal.onyx index 5b82b699..ee2e6516 100644 --- a/progs/minimal.onyx +++ b/progs/minimal.onyx @@ -8,13 +8,13 @@ export add :: proc (a i32, b i32) -> i32 { return a + b; } -c :: proc () -> void --- - export mul :: proc (a i32, b i32) -> i32 { + /* Typechecked */ c: const i32 = a - b; /* Don't love this syntax, but it's easy to parse so whatever Inferred type, but constant */ + /* a and b are both i32, so i32 + i32 is i32 so d is i32 */ d: const = a + b; return c * d;