🐐 NON operator

This commit is contained in:
2026-04-01 13:21:07 +02:00
parent 0b712f5040
commit 77a4f8ae2b
4 changed files with 64 additions and 2 deletions

View File

@@ -462,6 +462,24 @@ class UnaryMinus(Node):
return vtable, ValInt(-val.value())
class UnaryNot(Node):
def __init__(self, expr):
self.expr = expr
def __eq__(self, other):
return type(self) == type(other) and self.expr == other.expr
def __repr__(self):
return f"UnaryNot({self.expr!r})"
def print(self):
return f"(NON {self.expr.print()})"
def _eval(self, vtable):
vtable, val = self.expr.eval(vtable)
return vtable, ValBool(not bool(val))
class ArrayIndex(Node):
def __init__(self, array, index) -> None:
self.array = array

View File

@@ -17,6 +17,7 @@ keyword_tokens = [("KEYWORD_"+i, i) for i in [
"INVOCA",
"IN",
"MINVS",
"NON",
"NVLLVS",
"PER",
"PLVS",

View File

@@ -15,7 +15,7 @@ class Parser():
('left', ["KEYWORD_PLVS", "KEYWORD_MINVS", "KEYWORD_EST"]),
('left', ["SYMBOL_COLON", "SYMBOL_PLUS", "SYMBOL_MINUS"]),
('left', ["SYMBOL_TIMES", "SYMBOL_DIVIDE"]),
('right', ["UMINUS"]),
('right', ["UMINUS", "UNOT"]),
('left', ["INDEX"]),
]
)
@@ -191,6 +191,10 @@ class Parser():
def unary_minus(tokens):
return ast_nodes.UnaryMinus(tokens[1])
@self.pg.production('expression : KEYWORD_NON expression', precedence='UNOT')
def unary_not(tokens):
return ast_nodes.UnaryNot(tokens[1])
@self.pg.production('expression : KEYWORD_INVOCA id expressions')
def invoca(tokens):
return ast_nodes.Invoca(tokens[1], tokens[2])