From: Michael D. Lowis Date: Sat, 30 Mar 2019 02:52:27 +0000 (-0400) Subject: simplified AST datatype X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0bab93bba2ed20b8c1336d8c986626b8c75201c2;p=proto%2Fsclpl.git simplified AST datatype --- diff --git a/inc/sclpl.h b/inc/sclpl.h index 82b7d3e..b3ea949 100644 --- a/inc/sclpl.h +++ b/inc/sclpl.h @@ -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; diff --git a/src/ast.c b/src/ast.c index 7b4514d..257b5f6 100644 --- 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]; } diff --git a/src/parser.c b/src/parser.c index 96dd223..358eff8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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() \