]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
simplified AST datatype
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 30 Mar 2019 02:52:27 +0000 (22:52 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 30 Mar 2019 02:52:27 +0000 (22:52 -0400)
inc/sclpl.h
src/ast.c
src/parser.c

index 82b7d3e166b70178bf2acae758de77bcced650d4..b3ea94999555848882f04a28df3a540e8977b1ef 100644 (file)
@@ -104,44 +104,24 @@ typedef struct AST {
     ASTType nodetype;
     Type* datatype;
     union {
+        struct AST* nodes[3];
         /* Definition Node */
         struct {
             char* name;
             int flags;
             struct AST* value;
-            struct AST* type; // TODO: This should go away in favor of ->datatype
         } var;
-        /* Lambda Node */
-        struct {
-            struct AST* args;
-            struct AST* body;
-        } func;
         /* Expression Block Node */
         struct {
             size_t nexprs;
             struct AST** exprs;
         } explist;
-        /* If Expression Node */
-        struct {
-            struct AST* cond;
-            struct AST* b1;
-            struct AST* b2;
-        } ifexp;
-        /* Application Node */
-        struct {
-            struct AST* func;
-            struct AST* args;
-        } apply;
         /* String, Symbol, Identifier */
         char* text;
-        /* Character */
-        uint32_t character;
         /* Integer */
         intptr_t integer;
         /* Float */
         double floating;
-        /* Bool */
-        bool boolean;
     } value;
 } AST;
 
index 7b4514d78ea52f0526a4766490da19a460e9f615..257b5f6ee76738c6ea533e17d10c3ac4806adbc8 100644 (file)
--- a/src/ast.c
+++ b/src/ast.c
@@ -77,7 +77,6 @@ AST* Var(char* name, AST* value, AST* type, int flags) {
     AST* node = ast(AST_VAR);
     node->value.var.name = name;
     node->value.var.value = value;
-    node->value.var.type = type;
     node->value.var.flags = flags;
     return node;
 }
@@ -100,19 +99,19 @@ bool var_flagset(AST* var, int mask) {
 AST* Func(AST* args, AST* body, AST* type)
 {
     AST* node = ast(AST_FUNC);
-    node->value.func.args = args;
-    node->value.func.body = body;
+    node->value.nodes[0] = args;
+    node->value.nodes[1] = body;
     return node;
 }
 
 AST* func_args(AST* func) {
     assert(func->nodetype == AST_FUNC);
-    return func->value.func.args;
+    return func->value.nodes[0];
 }
 
 AST* func_body(AST* func) {
     assert(func->nodetype == AST_FUNC);
-    return func->value.func.body;
+    return func->value.nodes[1];
 }
 
 AST* ExpList(void) {
@@ -145,40 +144,40 @@ void explist_prepend(AST* explist, AST* expr) {
 
 AST* If(AST* cond, AST* b1, AST* b2) {
     AST* node = ast(AST_IF);
-    node->value.ifexp.cond = cond;
-    node->value.ifexp.b1 = b1;
-    node->value.ifexp.b2 = b2;
+    node->value.nodes[0] = cond;
+    node->value.nodes[1] = b1;
+    node->value.nodes[2] = b2;
     return node;
 }
 
 AST* if_cond(AST* ifexp) {
     assert(ifexp->nodetype == AST_IF);
-    return ifexp->value.ifexp.cond;
+    return ifexp->value.nodes[0];
 }
 
 AST* if_then(AST* ifexp) {
     assert(ifexp->nodetype == AST_IF);
-    return ifexp->value.ifexp.b1;
+    return ifexp->value.nodes[1];
 }
 
 AST* if_else(AST* ifexp) {
     assert(ifexp->nodetype == AST_IF);
-    return ifexp->value.ifexp.b2;
+    return ifexp->value.nodes[2];
 }
 
 AST* Apply(AST* func, AST* args) {
     AST* node = ast(AST_APPLY);
-    node->value.apply.func = func;
-    node->value.apply.args = args;
+    node->value.nodes[0] = func;
+    node->value.nodes[1] = args;
     return node;
 }
 
 AST* apply_func(AST* apply) {
     assert(apply->nodetype == AST_APPLY);
-    return apply->value.apply.func;
+    return apply->value.nodes[0];
 }
 
 AST* apply_args(AST* apply) {
     assert(apply->nodetype == AST_APPLY);
-    return apply->value.apply.args;
+    return apply->value.nodes[1];
 }
index 96dd223a9ee1b8e22e63c37d5826c113f4893b8b..358eff8538de4ff489f0f7e91346e50458969828 100644 (file)
@@ -16,7 +16,7 @@ static AST* if_expression(Parser* p);
 static AST* identifier(Parser* p);
 static AST* expr_list(Parser* p, int firstc, int endc);
 
-#define TRACE
+//#define TRACE
 #ifdef TRACE
 static int Indent = 0;
 #define parse_enter() \