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.c55
1 files changed, 49 insertions, 6 deletions
diff --git a/boot/tree.c b/boot/tree.c
index 7dd6447..dcda4da 100644
--- a/boot/tree.c
+++ b/boot/tree.c
@@ -549,6 +549,7 @@ enum Statement_Kind
     STATEMENT_DECLARATION,
     // NOTE: a block could be an expression in the future.
     STATEMENT_BLOCK,
+    STATEMENT_CONDITIONAL,
 };
 
 struct Statement_Value_Expression
@@ -570,11 +571,25 @@ struct Statement_Value_Block
     struct Block_Node inner; // the block of statements.
 };
 
+#define STATEMENT_VALUE_CONDITIONAL_MAX 8
+
+struct Statement_Value_Conditional
+{
+    struct Statement_Conditional_Branch
+    {
+        // if nil, the condition is always true.
+        struct Expression* when;
+        struct Block_Node then;
+    } conditions[STATEMENT_VALUE_CONDITIONAL_MAX];
+    uint condition_count;
+};
+
 union Statement_Value
 {
     struct Statement_Value_Expression expression;
     struct Statement_Value_Declaration declaration;
     struct Statement_Value_Block block;
+    struct Statement_Value_Conditional conditional;
 };
 
 struct Statement
@@ -592,6 +607,21 @@ struct Statement
 
 REGION(struct Statement, statement)
 
+void statement_print(const struct Statement* statement);
+
+void
+block_node_print(const struct Block_Node* block)
+{
+    printf("(block \n");
+    FOR_EACH(struct Statement*, statement, block->statements)
+    {
+        printf("\t");
+        statement_print(statement);
+        printf("\n");
+    }
+    printf(")");
+}
+
 struct Statement*
 statement_new(
     enum Statement_Kind kind, union Statement_Value value, struct Span span, struct Cursor location)
@@ -639,15 +669,28 @@ statement_print(const struct Statement* statement)
         break;
     }
     case STATEMENT_BLOCK:
-        printf("(block \n");
-        FOR_EACH(struct Statement*, sub_statement, statement->value.block.inner.statements)
-        {
-            printf("\t");
-            statement_print(sub_statement);
-            printf("\n");
+        block_node_print(&statement->value.block.inner);
+        break;
+    case STATEMENT_CONDITIONAL: {
+        printf("(conditional");
+        for (uint i = 0; i < statement->value.conditional.condition_count; ++i) {
+            const struct Statement_Conditional_Branch* branch =
+                &statement->value.conditional.conditions[i];
+
+            printf(" ");
+            if (branch->when) {
+                printf("(when ");
+                expression_print(branch->when);
+                printf(") ");
+            } else {
+                printf("(always) ");
+            }
+
+            block_node_print(&branch->then);
         }
         printf(")");
         break;
+    }
     default:
         failure("unexpected statement kind passed to `statement_print`");
         break;