From c0895afd99c8befd0d89580bfaa71bb532ba9c7f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 26 Jun 2024 23:50:37 +0200 Subject: Support unary plus and minus --- src/parser.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/parser.c') 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"); } -- cgit v1.2.3