aboutsummaryrefslogtreecommitdiff
path: root/parser.py
diff options
context:
space:
mode:
authormryouse2022-05-10 04:12:04 +0000
committermryouse2022-05-10 04:12:04 +0000
commitdb5564306399f0a3af97eb13808140542aa5b42c (patch)
tree13aca4a0b54c63664e92c988a86975e30d77e95d /parser.py
parente2aff5eee62157d718e88628d9dd7e7a6aa19211 (diff)
nested expressions!
Diffstat (limited to 'parser.py')
-rw-r--r--parser.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/parser.py b/parser.py
index 6360b62..97717bf 100644
--- a/parser.py
+++ b/parser.py
@@ -7,19 +7,26 @@ def peek(inp):
return inp[0]
def parse_expression(tkns):
- # expressions MUST start with a symbol (for now?)
symbol = None
args = []
+ add_idx = 0
for idx, t in enumerate(tkns):
- #if isinstance(t, NebOpen):
+ # if we evaluated a nested expression, skip ahead
+ if add_idx > 0:
+ add_idx -= 1
+ continue
if idx == 0:
if not isinstance(t, NebSymbol):
raise Exception("expressions must start with a symbol")
else:
symbol = t
elif isinstance(t, NebClose):
- return NebExpression(symbol, args), tkns[idx + 1:]
- else: # TODO nested expressions
+ return NebExpression(symbol, args), tkns[idx + 1:], idx + 1
+ # nested expressions
+ elif isinstance(t, NebOpen):
+ expr, remainder, add_idx = parse_expression(tkns[idx + 1:])
+ args.append(expr)
+ else:
args.append(t)
raise Exception("couldn't parse expression!")
@@ -29,7 +36,7 @@ def parse(tkns, parsed):
if nxt is None:
return parsed
if isinstance(nxt, NebOpen):
- expr, remainder = parse_expression(tkns[1:])
+ expr, remainder, _ = parse_expression(tkns[1:])
parsed.append(expr)
return parse(remainder, parsed)
elif isinstance(nxt, NebLiteral):