diff options
author | Juan J. Martinez <jjm@usebox.net> | 2024-06-18 16:02:46 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2024-06-18 16:03:12 +0100 |
commit | 96362daa03ca36a94ca7c5939446f772d95229b5 (patch) | |
tree | 3b0f129080b82c894ea808fb5d2e9c22f51806c3 | |
parent | f766d64e77654adca9d71a9878f6d5739b3954dd (diff) | |
download | funco-96362daa03ca36a94ca7c5939446f772d95229b5.tar.gz funco-96362daa03ca36a94ca7c5939446f772d95229b5.zip |
Improved parsing
-rw-r--r-- | README.md | 8 | ||||
-rwxr-xr-x | funco | 14 |
2 files changed, 14 insertions, 8 deletions
@@ -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` * `>`, `<`, `>=`, `<=`, `=`, `!=` @@ -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 |