feat(parser): parse constraint statements
This commit is contained in:
@@ -27,6 +27,9 @@ class Stmt(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def visit_op_stmt(self, stmt: OpStmt) -> T: ...
|
def visit_op_stmt(self, stmt: OpStmt) -> T: ...
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def visit_constraint_stmt(self, stmt: ConstraintStmt) -> T: ...
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class TypeStmt(Stmt):
|
class TypeStmt(Stmt):
|
||||||
@@ -58,6 +61,15 @@ class OpStmt(Stmt):
|
|||||||
return visitor.visit_op_stmt(self)
|
return visitor.visit_op_stmt(self)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ConstraintStmt(Stmt):
|
||||||
|
name: Token
|
||||||
|
constraint: ConstraintExpr
|
||||||
|
|
||||||
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
||||||
|
return visitor.visit_constraint_stmt(self)
|
||||||
|
|
||||||
|
|
||||||
# Expressions
|
# Expressions
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -179,6 +179,15 @@ class MidasAstPrinter(AstPrinter, m.Expr.Visitor[None], m.Stmt.Visitor[None]):
|
|||||||
self._mark_last()
|
self._mark_last()
|
||||||
stmt.result.accept(self)
|
stmt.result.accept(self)
|
||||||
|
|
||||||
|
def visit_constraint_stmt(self, stmt: m.ConstraintStmt):
|
||||||
|
self._write_line("ConstraintStmt")
|
||||||
|
with self._child_level():
|
||||||
|
self._write_line(f'name: "{stmt.name.lexeme}"')
|
||||||
|
self._write_line("constraint", last=True)
|
||||||
|
with self._child_level():
|
||||||
|
self._mark_last()
|
||||||
|
stmt.constraint.accept(self)
|
||||||
|
|
||||||
def visit_type_expr(self, expr: m.TypeExpr):
|
def visit_type_expr(self, expr: m.TypeExpr):
|
||||||
self._write_line("TypeExpr")
|
self._write_line("TypeExpr")
|
||||||
with self._child_level():
|
with self._child_level():
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from typing import Optional
|
|||||||
|
|
||||||
from core.ast.midas import (
|
from core.ast.midas import (
|
||||||
ConstraintExpr,
|
ConstraintExpr,
|
||||||
|
ConstraintStmt,
|
||||||
OpStmt,
|
OpStmt,
|
||||||
PropertyStmt,
|
PropertyStmt,
|
||||||
Stmt,
|
Stmt,
|
||||||
@@ -42,8 +43,8 @@ class MidasParser(Parser):
|
|||||||
return self.type_declaration()
|
return self.type_declaration()
|
||||||
if self.match(TokenType.OP):
|
if self.match(TokenType.OP):
|
||||||
return self.op_declaration()
|
return self.op_declaration()
|
||||||
# if self.match(TokenType.CONSTRAINT):
|
if self.match(TokenType.CONSTRAINT):
|
||||||
# return self.constraint_declaration()
|
return self.constraint_declaration()
|
||||||
except ParsingError:
|
except ParsingError:
|
||||||
self.synchronize()
|
self.synchronize()
|
||||||
return None
|
return None
|
||||||
@@ -109,3 +110,9 @@ class MidasParser(Parser):
|
|||||||
self.consume(TokenType.GREATER, "Expected '>' after result type")
|
self.consume(TokenType.GREATER, "Expected '>' after result type")
|
||||||
|
|
||||||
return OpStmt(left=left, op=op, right=right, result=result)
|
return OpStmt(left=left, op=op, right=right, result=result)
|
||||||
|
|
||||||
|
def constraint_declaration(self) -> ConstraintStmt:
|
||||||
|
name: Token = self.consume(TokenType.IDENTIFIER, "Expected constraint name")
|
||||||
|
self.consume(TokenType.EQUAL, "Expected '=' after constraint name")
|
||||||
|
constraint: ConstraintExpr = self.constraint_expr()
|
||||||
|
return ConstraintStmt(name=name, constraint=constraint)
|
||||||
|
|||||||
Reference in New Issue
Block a user