From 96362daa03ca36a94ca7c5939446f772d95229b5 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 18 Jun 2024 16:02:46 +0100 Subject: Improved parsing --- README.md | 8 +++----- funco | 14 +++++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fcbb5bd..dd6fb76 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,7 @@ end A program can have: * line comments, with `#` -* function definitions -* expressions +* function definitions, that contain other function definitions and expressions A `main()` function is required and will be executed as entry point for the program. @@ -62,10 +61,9 @@ Literals include: * strings: `"this is a string"` * booleans (`true` or `false`), as result of some logical functions; although 0 is false and non-zero is true * lists -* functions -* none for "no value" +* `none` for "no value" -Operators are functions, that includes: +Operators are functions, that include: * `+`, `-`, `*`, `/`, `mod` * `>`, `<`, `>=`, `<=`, `=`, `!=` diff --git a/funco b/funco index aab0a0c..86da6a6 100755 --- a/funco +++ b/funco @@ -237,7 +237,7 @@ class Parser(object): def expect(self, expected): found = self.next() if not isinstance(found, expected): - error(f"{found.pos}: {expected.value} expected, {found.value} found instead") + error(f"{found.pos}: {expected.value} expected, {found.value} found") def parse_parms(self) -> list: parms = [] @@ -263,7 +263,10 @@ class Parser(object): arg = self.next() if isinstance(arg, Rpar): break - args.append(self.parse_expr(arg)) + elif arg.value == "end": + error(f"{arg.pos}: ) expected, end found") + else: + args.append(self.parse_expr(arg)) return Call(ident, args) def parse_tailrec(self): @@ -331,8 +334,13 @@ class Parser(object): return out else: error(f"{token.pos}: end not in open block") - out.append(self.parse_expr(token)) + if in_block: + out.append(self.parse_expr(token)) + else: + error(f"{token.pos}: expression not in a function") except StopIteration: + if in_block: + error("unexpected EOF in open block") return out -- cgit v1.2.3