From: Michael D. Lowis Date: Mon, 19 Jul 2021 03:18:17 +0000 (-0400) Subject: removed distinction between unop and binop in API X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=bd27559e7a021529af594959b9d261f7e5d25c1b;p=proto%2Fobnc.git removed distinction between unop and binop in API --- diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 08f1474..40b55a6 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -205,8 +205,7 @@ bool ast_asbool(AstNode* node); long long ast_asint(AstNode* node); double ast_asreal(AstNode* node); -AstNode* ast_binop(int op, AstNode* left, AstNode* right); -AstNode* ast_unop(int op, AstNode* operand); +AstNode* ast_op(int op, AstNode* left, AstNode* right); AstNode* ast_store(AstNode* dest, AstNode* value); AstNode* ast_fieldref(Parser* p, AstNode* record, char* fname); AstNode* ast_index(Parser* p, AstNode* array, AstNode* index); diff --git a/cerise/src/ast.c b/cerise/src/ast.c index 14defd4..ac1c78a 100644 --- a/cerise/src/ast.c +++ b/cerise/src/ast.c @@ -132,7 +132,7 @@ double ast_asreal(AstNode* node) return ((AstValue*)node)->val.f; } -AstNode* ast_binop(int op, AstNode* left, AstNode* right) +static AstNode* ast_binop(int op, AstNode* left, AstNode* right) { assert(left); assert(right); @@ -259,7 +259,7 @@ AstNode* ast_binop(int op, AstNode* left, AstNode* right) return ret; } -AstNode* ast_unop(int op, AstNode* operand) +static AstNode* ast_unop(int op, AstNode* operand) { assert(operand); AstNode* ret = NULL; @@ -305,6 +305,13 @@ AstNode* ast_unop(int op, AstNode* operand) return ret; } +AstNode* ast_op(int op, AstNode* left, AstNode* right) +{ + return (right + ? ast_binop(op, left, right) + : ast_unop(op, left)); +} + AstNode* ast_store(AstNode* dest, AstNode* value) { /* TODO: validate left-hand side is assignable */ @@ -477,16 +484,22 @@ static void print(Parser* p, AstNode* node, int indent) case IDENT: { Symbol* s = symbol_getbyid(p, ((AstValue*)node)->val.i); - printf("S:%s", s->name); + printf("%s.%lld", s->name, ((AstValue*)node)->tag); } break; case '.': - printf("(field-ref)"); + printf("(field-ref\n"); + print(p, node->links[0], indent+1); + print(p, node->links[1], indent+1); + print_indent(indent, ")"); break; case '[': - printf("(array-index)"); + printf("(array-index\n"); + print(p, node->links[0], indent+1); + print(p, node->links[1], indent+1); + print_indent(indent, ")"); break; case BEGIN: diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index b5dbffa..e7295b6 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -166,7 +166,7 @@ static AstNode* factor(Parser* p) case NOT: consume(p); - expr = ast_unop(NOT, factor(p)); + expr = ast_op(NOT, factor(p), NULL); break; case IDENT: @@ -238,12 +238,12 @@ static AstNode* term(Parser* p) case '/': case '%': check_nums(p, expr, right); - expr = ast_binop(op, expr, right); + expr = ast_op(op, expr, right); break; case AND: check_bools(p, expr, right); - expr = ast_binop(op, expr, right); + expr = ast_op(op, expr, right); break; default: @@ -269,7 +269,7 @@ static AstNode* simple_expr(Parser* p) int op = consume(p); AstNode* operand = term(p); check_num(p, operand); - expr = ast_unop(op, operand); + expr = ast_op(op, operand, NULL); } else { @@ -289,7 +289,7 @@ static AstNode* simple_expr(Parser* p) { check_nums(p, expr, right); } - expr = ast_binop(op, expr, right); + expr = ast_op(op, expr, right); } EXIT_RULE(); @@ -309,7 +309,7 @@ static AstNode* expression(Parser* p) int op = consume(p); AstNode* right = simple_expr(p); check_nums(p, expr, right); - expr = ast_binop(op, expr, right); + expr = ast_op(op, expr, right); } EXIT_RULE(); @@ -338,7 +338,6 @@ static Type* type(Parser* p) long long length = ast_asint(lnode); expect(p, OF); Type* base = type(p); -// ret = calloc(1, sizeof(Type)); ret = symbol_newtype(p); ret->form = FORM_ARRAY; ret->size = length; @@ -347,7 +346,6 @@ static Type* type(Parser* p) else if (accept(p, RECORD)) { long offset = 0; -// ret = calloc(1, sizeof(Type)); ret = symbol_newtype(p); ret->form = FORM_RECORD; @@ -558,7 +556,7 @@ void proc_decl(Parser* p) expect(p, END); symbol_closescope(p, scope); -// ast_print(p, proc->value); + ast_print(p, proc->value); (void)proc->value; EXIT_RULE(); @@ -622,7 +620,7 @@ static void module(Parser* p) if (accept(p, BEGIN)) { AstNode* block = statement_seq(p); -// ast_print(p, block); + ast_print(p, block); (void)block; expect(p, END); } @@ -632,7 +630,7 @@ static void module(Parser* p) error(p, "expected end of file"); } - symbol_export(p, NULL); +// symbol_export(p, NULL); symbol_closescope(p, scope); EXIT_RULE(); } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index dd92fb4..58698a5 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -69,7 +69,7 @@ begin end begin -# h[1].i = 42; + h[1].i = 42; # a = true; # a = A; # b = 24;