From 43ad43fa1d95372b5762118d17bccb0962c09c43 Mon Sep 17 00:00:00 2001 From: mike lowis Date: Wed, 28 Apr 2021 23:28:18 -0400 Subject: [PATCH] added if statement to parser. need to add codegen hooks --- cerise/backend/x86_64/codegen.c | 1 + cerise/oberon0/OSP.Mod | 1 + cerise/src/parser.c | 20 +++++++++-- cerise/tests/Module.m | 64 ++++++++++++++++++++------------- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/cerise/backend/x86_64/codegen.c b/cerise/backend/x86_64/codegen.c index ac961a6..4f8602c 100644 --- a/cerise/backend/x86_64/codegen.c +++ b/cerise/backend/x86_64/codegen.c @@ -238,6 +238,7 @@ void codegen_main(Parser* p) void codegen_startproc(Parser* p, char* name, bool exported) { + (void)exported; printf(" .text\n"); if (name) { diff --git a/cerise/oberon0/OSP.Mod b/cerise/oberon0/OSP.Mod index 4433549..7b0fb37 100644 --- a/cerise/oberon0/OSP.Mod +++ b/cerise/oberon0/OSP.Mod @@ -335,6 +335,7 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) OSS.Mark("not a procedure") END END + ELSIF sym = OSS.if THEN OSS.Get(sym); expression(x); diff --git a/cerise/src/parser.c b/cerise/src/parser.c index 508cccb..a458a55 100644 --- a/cerise/src/parser.c +++ b/cerise/src/parser.c @@ -560,14 +560,30 @@ RULE(statement_seq) } else if (matches(p, IF)) { - assert("!not implemented"); + expect(p, IF); + expression(p, item); + check_bool(p, item); + expect(p, THEN); + statement_seq(p, &(Item){0}); + while (accept(p, ELSIF)) + { + expression(p, item); + check_bool(p, item); + expect(p, THEN); + statement_seq(p, &(Item){0}); + } + if (accept(p, ELSE)) + { + statement_seq(p, &(Item){0}); + } + expect(p, END); } else { error(p, "expected a statement"); } - if (matches(p, END)) + if (matches(p, END) || matches(p, ELSE) || matches(p, ELSIF)) { break; } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index c78051e..57fcc05 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -14,29 +14,45 @@ var c : Int begin - a = true; - a = A; - b = 24; - b = B; - - # Unary ops - b = +b; - b = -b; - - # Immediate ops - c = b + 1; - c = b - 1; - c = b * 1; - +# a = true; +# a = A; +# b = 24; +# b = B; +# +# # Unary ops +# b = +b; +# b = -b; +# +# # Arithmetic ops +# c = b + 1; +# c = b - 1; +# c = b * 1; +# c = b / 1; +# c = b % 1; +# +# # Comparison ops # c = b == 1; - - # Register ops - c = 1 + b; - c = 1 - b; - c = 1 * b; - - c = b + b; - - # Complex arithmetic - c = b + b * b + b; +# c = b != 1; +# c = b < 1; +# c = b > 1; +# c = b <= 1; +# c = b >= 1; +# +# # Register ops +# c = 1 + b; +# c = 1 - b; +# c = 1 * b; +# +# c = b + b; +# +# # Complex arithmetic +# c = b + b * b + b; + + if 1 == 1 then + c = 1; + elsif 2 == 2 then + c = 2; + else + c = 3; + end end -- 2.49.0