diff options
| author | Mel <mel@rnrd.eu> | 2025-05-31 03:29:51 +0200 |
|---|---|---|
| committer | Mel <mel@rnrd.eu> | 2025-05-31 03:29:51 +0200 |
| commit | a422f9aead499a526179ba2df2aff1aa44fe48d6 (patch) | |
| tree | f597bed2231e3ee05ce1d4f6c9901ed5b6a3882b /boot/tree.c | |
| parent | 41e0e31b8586c1f93b5e65cd62ef910227a6677d (diff) | |
| download | catskill-a422f9aead499a526179ba2df2aff1aa44fe48d6.tar.zst catskill-a422f9aead499a526179ba2df2aff1aa44fe48d6.zip | |
Keyword-less variable declaration parsing
Signed-off-by: Mel <mel@rnrd.eu>
Diffstat (limited to 'boot/tree.c')
| -rw-r--r-- | boot/tree.c | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/boot/tree.c b/boot/tree.c index 593103a..1d88743 100644 --- a/boot/tree.c +++ b/boot/tree.c @@ -323,6 +323,19 @@ binary_operation_to_string(enum Binary_Operation operation) } } +// nodes are parts of the syntax tree that are reused often +// and in different places. + +// a type node represents a type in the syntax tree. +// currently, we only support types that are simple names. +struct Type_Node +{ + // note: we could also just include the token here i think? + struct String name; + struct Span span; + struct Cursor location; +}; + enum Expression_Kind { EXPRESSION_NONE, @@ -524,11 +537,27 @@ enum Statement_Kind { STATEMENT_NONE, STATEMENT_EXPRESSION, + STATEMENT_DECLARATION, +}; + +struct Statement_Value_Expression +{ + struct Expression* inner; +}; + +struct Statement_Value_Declaration +{ + struct String_Array names; // the names of the variables being declared. + struct Expression* initializer; // the expression to initialize the variable with. + + bool has_type; // whether the declaration has a type. + struct Type_Node type; // the type of the variable, if any. }; union Statement_Value { - struct Expression* expression; + struct Statement_Value_Expression expression; + struct Statement_Value_Declaration declaration; }; struct Statement @@ -571,10 +600,27 @@ statement_print(const struct Statement* statement) printf("none"); break; case STATEMENT_EXPRESSION: { - const struct Expression* expression = statement->value.expression; + const struct Expression* expression = statement->value.expression.inner; expression_print(expression); break; } + case STATEMENT_DECLARATION: { + printf("(declaration "); + STRING_ARRAY_FOR_EACH(i, name, statement->value.declaration.names) + { + printf("%s ", name.data); + } + if (statement->value.declaration.has_type) { + printf("(type %s) ", statement->value.declaration.type.name.data); + } + if (statement->value.declaration.initializer) { + printf("(initializer "); + expression_print(statement->value.declaration.initializer); + printf(")"); + } + printf(")"); + break; + } default: failure("unexpected statement kind passed to `statement_print`"); break; |
