diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-06-26 23:50:37 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-06-26 23:53:08 +0200 |
commit | c0895afd99c8befd0d89580bfaa71bb532ba9c7f (patch) | |
tree | 97ea69dccef8cf5e994c265f723ff9c5d6940ad8 /src/parser.c | |
parent | 101db2a43b1b3bb4c1c9c120cd5722f17a5f262d (diff) |
Support unary plus and minus
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c index fa1c720..cb8aa8f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -61,6 +61,7 @@ fwdnode(ast_t ast, idx_t i) case ASTBINSUB: case ASTCDECL: case ASTFN: + case ASTUNNEG: i = ast.kids[i].rhs; break; case ASTIDENT: @@ -219,6 +220,13 @@ parseexpr(ast_t *ast, aux_t *aux, lexemes_t toks) { (void)aux; idx_t i = astalloc(ast); + + /* Unary plus is kind of a fake syntactic construct. We just pretend + like it doesn’t exist, but allow it in the syntax to be consistent + with unary negation. */ + while (toks.kinds[toksidx] == LEXPLUS) + toksidx++; + ast->lexemes[i] = toksidx; switch (toks.kinds[toksidx]) { @@ -230,6 +238,13 @@ parseexpr(ast_t *ast, aux_t *aux, lexemes_t toks) toksidx++; ast->kinds[i] = ASTIDENT; break; + case LEXMINUS: { + toksidx++; + ast->kinds[i] = ASTUNNEG; + idx_t rhs = parseexpr(ast, aux, toks); + ast->kids[i].rhs = rhs; + break; + } default: err("parser: Expected expression"); } |