Fixed #51 : I can compile simple recognize

This commit is contained in:
2021-03-19 18:06:29 +01:00
parent 36e3ce0bcb
commit 88c96ee9a8
5 changed files with 376 additions and 76 deletions
+40 -23
View File
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import List, Tuple, Union
from typing import List, Union
from core.builtin_concepts_ids import BuiltinConcepts
from core.sheerka.services.SheerkaExecute import ParserInput
@@ -33,15 +33,11 @@ class ParenthesisMismatchError(ParsingError):
token: Token
@dataclass(init=False)
class ExprNode(Node):
"""
Base ExprNode
eval() must be overridden
"""
start: int # index of the first token
end: int # index of the last token
tokens: List[Token]
def __init__(self, start: int, end: int, tokens: List[Token]):
self.start = start
@@ -118,10 +114,7 @@ class NameExprNode(ExprNode):
return UnrecognizedTokensNode(self.start, self.end, [token]).fix_source()
@dataclass(init=False)
class AndNode(ExprNode):
parts: Tuple[ExprNode]
def __init__(self, start, end, tokens, *parts: ExprNode):
super().__init__(start, end, tokens)
self.parts = parts
@@ -154,10 +147,7 @@ class AndNode(ExprNode):
return hash((self.start, self.end, self.parts))
@dataclass(init=False)
class OrNode(ExprNode):
parts: Tuple[ExprNode]
def __init__(self, start, end, tokens, *parts: ExprNode):
super().__init__(start, end, tokens)
self.parts = parts
@@ -190,9 +180,10 @@ class OrNode(ExprNode):
return hash((self.start, self.end, self.parts))
@dataclass()
class NotNode(ExprNode):
node: ExprNode
def __init__(self, start, end, tokens, node: ExprNode):
super().__init__(start, end, tokens)
self.node = node
def eval(self, obj):
return not self.node.eval(obj)
@@ -222,13 +213,15 @@ class NotNode(ExprNode):
return hash((self.start, self.end, self.node))
@dataclass()
class ParenthesisNode(ExprNode):
"""
Contains the boundaries of an expression inside parenthesis
Need it, just to keep track of the boundaries of the parenthesis
"""
node: ExprNode
def __init__(self, start, end, tokens, node: ExprNode):
super().__init__(start, end, tokens)
self.node = node
def __eq__(self, other):
if not isinstance(other, ParenthesisNode):
@@ -291,11 +284,12 @@ class VariableNode(ExprNode):
return [self.name] + self.attributes
@dataclass
class ComparisonNode(ExprNode):
comp: str
left: ExprNode
right: ExprNode
def __init__(self, start, end, tokens, comp: str, left: ExprNode, right: ExprNode):
super().__init__(start, end, tokens)
self.comp = comp
self.left = left
self.right = right
def __eq__(self, other):
if id(self) == id(other):
@@ -338,11 +332,34 @@ class FunctionParameter:
return UnrecognizedTokensNode(self.separator.start, self.separator.end, self.separator.tokens).fix_source()
@dataclass
class FunctionNode(ExprNode):
first: NameExprNode # beginning of the function (it should represent the name of the function)
last: NameExprNode # last part of the function (it should be the trailing parenthesis)
parameters: Union[None, List[FunctionParameter]]
def __init__(self, start, end, tokens,
first: NameExprNode, last: NameExprNode, parameters: Union[None, List[FunctionParameter]]):
super().__init__(start, end, tokens)
self.first = first
self.last = last
self.parameters = parameters
def __eq__(self, other):
if id(self) == id(other):
return True
if not isinstance(other, FunctionNode):
return False
return (self.first == other.first and
self.last == other.last and
self.parameters == other.parameters)
def __hash__(self):
return hash((self.first, self.last, self.parameters))
def __repr__(self):
return f"FunctionNode(start={self.start}, end={self.end}, {self.first!r} {self.last} {self.parameters!r})"
def __str__(self):
return f"{self.first} {self.parameters} {self.last}"
class BaseExpressionParser(BaseParser):