From: Michael D. Lowis Date: Tue, 2 Apr 2019 01:31:07 +0000 (-0400) Subject: rework ast constructors X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=54cccb20c586017781f80d5173c2ed12624cb6c1;p=proto%2Fsclpl.git rework ast constructors --- diff --git a/inc/sclpl.h b/inc/sclpl.h index b3ea949..5ff583f 100644 --- a/inc/sclpl.h +++ b/inc/sclpl.h @@ -126,27 +126,27 @@ typedef struct AST { } AST; /* String */ -AST* String(Tok* val); +AST* String(char* val); char* string_value(AST* val); /* Character */ -AST* Char(Tok* val); +AST* Char(int val); uint32_t char_value(AST* val); /* Integer */ -AST* Integer(Tok* val); +AST* Integer(int val); intptr_t integer_value(AST* val); /* Float */ -AST* Float(Tok* val); +AST* Float(double val); double float_value(AST* val); /* Bool */ -AST* Bool(Tok* val); +AST* Bool(bool val); bool bool_value(AST* val); /* Ident */ -AST* Ident(Tok* val); +AST* Ident(char* val); char* ident_value(AST* val); /* Definition */ diff --git a/src/ast.c b/src/ast.c index 257b5f6..0a58dd5 100644 --- a/src/ast.c +++ b/src/ast.c @@ -7,9 +7,9 @@ static AST* ast(ASTType type) { return tree; } -AST* String(Tok* val) { +AST* String(char* val) { AST* node = ast(AST_STRING); - node->value.text = val->text; + node->value.text = val; return node; } @@ -18,9 +18,9 @@ char* string_value(AST* val) { return val->value.text; } -AST* Char(Tok* val) { +AST* Char(int val) { AST* node = ast(AST_CHAR); - node->value.integer = val->value.integer; + node->value.integer = val; return node; } @@ -29,9 +29,9 @@ uint32_t char_value(AST* val) { return val->value.integer; } -AST* Integer(Tok* val) { +AST* Integer(int val) { AST* node = ast(AST_INT); - node->value.integer = val->value.integer; + node->value.integer = val; return node; } @@ -40,9 +40,9 @@ intptr_t integer_value(AST* val) { return val->value.integer; } -AST* Float(Tok* val) { +AST* Float(double val) { AST* node = ast(AST_FLOAT); - node->value.floating = val->value.floating; + node->value.floating = val; return node; } @@ -51,9 +51,9 @@ double float_value(AST* val) { return val->value.floating; } -AST* Bool(Tok* val) { +AST* Bool(bool val) { AST* node = ast(AST_BOOL); - node->value.integer = val->value.integer; + node->value.integer = val; return node; } @@ -62,9 +62,9 @@ bool bool_value(AST* val) { return val->value.integer; } -AST* Ident(Tok* val) { +AST* Ident(char* val) { AST* node = ast(AST_IDENT); - node->value.text = val->text; + node->value.text = strdup(val); return node; } diff --git a/src/parser.c b/src/parser.c index 358eff8..0e9e50d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -244,12 +244,12 @@ static AST* constant(Parser* p) { AST* tree = NULL; Tok* tok = peek(p); switch (tok->type) { - case T_BOOL: tree = Bool(tok); break; - case T_CHAR: tree = Char(tok); break; - case T_STRING: tree = String(tok); break; - case T_INT: tree = Integer(tok); break; - case T_FLOAT: tree = Float(tok); break; - case T_ID: tree = Ident(tok); break; + case T_BOOL: tree = Bool(tok->value.integer); break; + case T_CHAR: tree = Char(tok->value.integer); break; + case T_STRING: tree = String(tok->text); break; + case T_INT: tree = Integer(tok->value.integer); break; + case T_FLOAT: tree = Float(tok->value.floating); break; + case T_ID: tree = Ident(tok->text); break; default: error(p, "expected an expression"); } accept(p, tok->type); @@ -325,7 +325,7 @@ static AST* if_expression(Parser* p) { static AST* identifier(Parser* p) { Tok* tok = peek(p); if (tok->type == T_ID) { - AST* ast = Ident(tok); + AST* ast = Ident(tok->text); tok->type = T_NONE; return ast; } else { diff --git a/test/parser.c b/test/parser.c index 92139b6..0b1ab76 100644 --- a/test/parser.c +++ b/test/parser.c @@ -3,6 +3,18 @@ Parser TestCtx = {0}; + +static int astcmp(AST* a, AST* b) { + int result = a->nodetype - b->nodetype; + if (result) return result; + switch (a->nodetype) { + case AST_BOOL: + case AST_CHAR: + case AST_INT: + return (a->value.integer - b->value.integer); + } +} + void lex_string(Parser* ctx, char* text) { LexFile* file = calloc(sizeof(LexFile), 1u); file->path = NULL; @@ -16,11 +28,12 @@ int parse(char* text, AST* expect) { codegen_init(&TestCtx); lex_string(&TestCtx, text); AST* result = expression(&TestCtx); - return 1; + int cmpresult = astcmp(expect, result); + return cmpresult; } TEST_SUITE(ParserTests) { TEST(should parse some stuff) { - CHECK(parse("123", NULL)); + CHECK(!parse("123", Integer(123))); } }