about summary refs log tree commit diff
path: root/boot/tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'boot/tree.c')
-rw-r--r--boot/tree.c50
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;