From 61fe5153d93839e79df91b49868ec4f15f5b4216 Mon Sep 17 00:00:00 2001 From: mike lowis Date: Mon, 3 May 2021 23:14:52 -0400 Subject: [PATCH] Added declarations section to procedure declarations --- cerise/src/grammar.c | 80 ++++++++++++++++++++++++++++--------------- cerise/tests/Module.m | 5 ++- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index d0287d0..5ffb654 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -370,33 +370,6 @@ RULE(statement_seq) while (!matches(p, END) && !matches(p, ELSE) && !matches(p, ELSIF) && !matches(p, RETURN)); } -RULE(proc_decl) -{ - expect(p, PROCEDURE); - (void)expect_text(p, IDENT); - (void)accept(p, '*'); - expect(p, '('); - while (!matches(p, ')')) - { - (void)expect_text(p, IDENT); - expect(p, ':'); - type(p, item); - } - expect(p, ')'); - if (accept(p, ':')) - { - type(p, item); - } - expect(p, BEGIN); - statement_seq(p, item); - if (accept(p, RETURN)) - { - expression(p, item); - expect(p, ';'); - } - expect(p, END); -} - RULE(var_decl) { (void)item; @@ -471,6 +444,59 @@ RULE(const_decl) while (matches(p, IDENT)); } +RULE(proc_decl) +{ + expect(p, PROCEDURE); + (void)expect_text(p, IDENT); + (void)accept(p, '*'); + + /* construct the proc type */ + expect(p, '('); + while (!matches(p, ')')) + { + (void)expect_text(p, IDENT); + expect(p, ':'); + type(p, item); + } + expect(p, ')'); + if (accept(p, ':')) + { + type(p, item); + } + + /* parse the declarations */ + if (accept(p, CONST)) + { + const_decl(p, item); + } + + if (accept(p, TYPE)) + { + type_decl(p, item); + } + + if (accept(p, VAR)) + { + var_decl(p, item); + } + + while (matches(p, PROCEDURE)) + { + proc_decl(p, item); + } + + /* parse the body of the procedure */ + expect(p, BEGIN); + statement_seq(p, item); + if (accept(p, RETURN)) + { + expression(p, item); + expect(p, ';'); + } + expect(p, END); +} + + RULE(import_list) { (void)item; diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 2cd4eba..9d8dbdf 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -19,7 +19,10 @@ var b : Int c : Int -procedure Foo*(e : Int) +procedure Foo*(e : Int) : Int + const FOO = 2 +# type foo : Int + var z : Int begin c = b * b; return c; -- 2.49.0