feat(parser): parse constraint statements

This commit is contained in:
2026-05-14 02:44:21 +02:00
parent 4b715ed33a
commit 61b36ee50f
3 changed files with 30 additions and 2 deletions

View File

@@ -27,6 +27,9 @@ class Stmt(ABC):
@abstractmethod
def visit_op_stmt(self, stmt: OpStmt) -> T: ...
@abstractmethod
def visit_constraint_stmt(self, stmt: ConstraintStmt) -> T: ...
@dataclass(frozen=True)
class TypeStmt(Stmt):
@@ -58,6 +61,15 @@ class OpStmt(Stmt):
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

View File

@@ -179,6 +179,15 @@ class MidasAstPrinter(AstPrinter, m.Expr.Visitor[None], m.Stmt.Visitor[None]):
self._mark_last()
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):
self._write_line("TypeExpr")
with self._child_level():

View File

@@ -2,6 +2,7 @@ from typing import Optional
from core.ast.midas import (
ConstraintExpr,
ConstraintStmt,
OpStmt,
PropertyStmt,
Stmt,
@@ -42,8 +43,8 @@ class MidasParser(Parser):
return self.type_declaration()
if self.match(TokenType.OP):
return self.op_declaration()
# if self.match(TokenType.CONSTRAINT):
# return self.constraint_declaration()
if self.match(TokenType.CONSTRAINT):
return self.constraint_declaration()
except ParsingError:
self.synchronize()
return None
@@ -109,3 +110,9 @@ class MidasParser(Parser):
self.consume(TokenType.GREATER, "Expected '>' after result type")
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)