From 6e8e8fc4bab63cbb7ad3462ed97edc77a6d7042d Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 20 Apr 2021 16:55:15 -0400 Subject: [PATCH] added scaffolding for arithmetic operators. will fill in details later for constant calculations --- cerise/cerise.h | 5 +++++ cerise/codegen.c | 24 ++++++++++++++++++++++++ cerise/lex.c | 2 +- cerise/parser.c | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/cerise/cerise.h b/cerise/cerise.h index 36f9ce1..cabcd7a 100644 --- a/cerise/cerise.h +++ b/cerise/cerise.h @@ -132,8 +132,13 @@ extern Type BoolType, IntType, RealType, StringType; void codegen_setint(Item* item, Type* type, long long val); void codegen_setreal(Item* item, double val); void codegen_setstr(Item* item, char* val); + void codegen_not(Item* item); +void codegen_add(Item* a, Item* b); +void codegen_sub(Item* a, Item* b); void codegen_mul(Item* a, Item* b); +void codegen_div(Item* a, Item* b); +void codegen_mod(Item* a, Item* b); /* Option Parsing *****************************************************************************/ diff --git a/cerise/codegen.c b/cerise/codegen.c index 48e527c..f00bbad 100644 --- a/cerise/codegen.c +++ b/cerise/codegen.c @@ -119,6 +119,18 @@ void codegen_not(Item* item) } } +void codegen_add(Item* a, Item* b) +{ + (void)a, (void)b; + assert(!"not implemented"); +} + +void codegen_sub(Item* a, Item* b) +{ + (void)a, (void)b; + assert(!"not implemented"); +} + void codegen_imul(Item* a, Item* b) { if ((b->mode == ITEM_CONST) && (b->mode == ITEM_CONST)) @@ -154,3 +166,15 @@ void codegen_mul(Item* a, Item* b) codegen_imul(a,b); } } + +void codegen_div(Item* a, Item* b) +{ + (void)a, (void)b; + assert(!"not implemented"); +} + +void codegen_mod(Item* a, Item* b) +{ + (void)a, (void)b; + assert(!"not implemented"); +} diff --git a/cerise/lex.c b/cerise/lex.c index 2f47a53..5c52bb9 100644 --- a/cerise/lex.c +++ b/cerise/lex.c @@ -39,7 +39,7 @@ static const char Chars[256] = { ['('] = 6, [')'] = 6, ['['] = 6, [']'] = 6, ['{'] = 6, ['}'] = 6, [','] = 6, [':'] = 6, [';'] = 6, ['^'] = 6, ['+'] = 6, ['-'] = 6, ['*'] = 6, ['/'] = 6, - ['|'] = 6, + ['|'] = 6, ['%'] = 6, /* number digits */ ['0'] = 7, ['1'] = 7, ['2'] = 7, ['3'] = 7, ['4'] = 7, diff --git a/cerise/parser.c b/cerise/parser.c index 360c774..b9dedc2 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -333,22 +333,26 @@ RULE(term) // term = factor {MulOperator factor}. // MulOperator = "*" | "/" | "and". factor(p, item); - while (matches_oneof(p, (int[]){'*', '/', AND, 0})) + while (matches_oneof(p, (int[]){'*', '/', '%', AND, 0})) { Item right = { 0 }; int op = consume(p); factor(p, &right); switch(op) { - case '/': + case '*': check_num2(p, item, &right); -// codegen_div(item, &right); - assert(!"not implemented"); + codegen_mul(item, &right); break; - case '*': + case '/': check_num2(p, item, &right); - codegen_mul(item, &right); + codegen_div(item, &right); + break; + + case '%': + check_int2(p, item, &right); + codegen_mod(item, &right); break; case AND: @@ -529,7 +533,7 @@ TEST_SUITE(Grammar) "import A, B = ModB, C;"); } - TEST(Should parse constant declarations) + TEST(Should parse constant declarations and expressions) { parse_rule(const_decl, 0, "FOO = 123"); @@ -552,9 +556,27 @@ TEST_SUITE(Grammar) parse_rule(const_decl, 0, "FOO = -1"); parse_rule(const_decl, 0, - "FOO = 1 + 2 + 3"); + "FOO = +1.0"); + parse_rule(const_decl, 0, + "FOO = -1.0"); + parse_rule(const_decl, 0, + "FOO = 1 + 2"); + parse_rule(const_decl, 0, + "FOO = 1.0 + 2.0"); + parse_rule(const_decl, 0, + "FOO = 1 - 2"); + parse_rule(const_decl, 0, + "FOO = 1.0 - 2.0"); + parse_rule(const_decl, 0, + "FOO = 1 * 2"); + parse_rule(const_decl, 0, + "FOO = 1.0 * 2.0"); + parse_rule(const_decl, 0, + "FOO = 1 / 2"); + parse_rule(const_decl, 0, + "FOO = 1.0 / 2.0"); parse_rule(const_decl, 0, - "FOO = 1 + 2 * 4"); + "FOO = 1 % 2"); } } #endif -- 2.49.0