From 7de4815ab8be342504fd1c07e50158539d0caf9c Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 2 Mar 2024 19:53:47 +0100 Subject: [PATCH] feat: Add negation to grammar --- compiler/src/ast.h | 1 + compiler/src/lexer.l | 1 + compiler/src/parser.y | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/compiler/src/ast.h b/compiler/src/ast.h index 7fa1906..f25b3ed 100644 --- a/compiler/src/ast.h +++ b/compiler/src/ast.h @@ -60,6 +60,7 @@ enum calc_operator { NOT_EQUALS, CONJUNCTION, DISJUNCTION, + NEGATION, }; struct calc_expression { diff --git a/compiler/src/lexer.l b/compiler/src/lexer.l index d76782b..d48e8a5 100644 --- a/compiler/src/lexer.l +++ b/compiler/src/lexer.l @@ -83,6 +83,7 @@ strbuf_t strbuf = NULL; "!=" { return NOT_EQUAL; } "&&" { return AND; } "||" { return OR; } +"!" { return NOT; } "(" { return OPENING_BRACKETS; } ")" { return CLOSING_BRACKETS; } diff --git a/compiler/src/parser.y b/compiler/src/parser.y index f165362..1361b48 100644 --- a/compiler/src/parser.y +++ b/compiler/src/parser.y @@ -54,6 +54,7 @@ extern struct block* program; %token NOT_EQUAL %token AND %token OR +%token NOT %token OPENING_BRACKETS %token CLOSING_BRACKETS @@ -162,6 +163,10 @@ calcexpr: OPENING_BRACKETS expr op expr CLOSING_BRACKETS { $$ = calc_expression_new($2, $4, $3); } + | NOT expr + { + $$ = calc_expression_new($2, NULL, NEGATION); + } ; op: PLUS