Skip to content

Commit

Permalink
Merge pull request #33 from bliutech/17-parser-write-a-visitor-to-tra…
Browse files Browse the repository at this point in the history
…verse-the-ast

feat: Update visitor.py
  • Loading branch information
bliutech authored Jul 17, 2024
2 parents a35658b + 47693ea commit 37f8496
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions parser/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Abstract Syntax Tree (AST) for boolean expressions.
"""

from visitor import Visitor


class Var:
"""
Expand All @@ -19,6 +21,9 @@ def __init__(self, name: str):
def __str__(self) -> str:
return self.name

def accept(self, v: Visitor):
v.visitVar(self)


class Expr:
"""
Expand Down Expand Up @@ -52,6 +57,9 @@ def __init__(self, first: Expr):
def __str__(self) -> str:
return f"& {self.first}"

def accept(self, v: Visitor):
v.visitAndExpr(self)


class OrExpr(ExprPrime):
"""
Expand All @@ -69,6 +77,9 @@ def __init__(self, first: Expr):
def __str__(self) -> str:
return f"| {self.first}"

def accept(self, v: Visitor):
v.visitOrExpr(self)


class VarExpr(Expr):
"""
Expand All @@ -93,6 +104,9 @@ def __str__(self) -> str:

return f"{self.first} {self.second}"

def accept(self, v: Visitor):
v.visitVarExpr(self)


class NotExpr(Expr):
"""
Expand All @@ -110,6 +124,9 @@ def __init__(self, first: Expr):
def __str__(self) -> str:
return f"!{self.first}"

def accept(self, v: Visitor):
v.visitNotExpr(self)


class ParenExpr(Expr):
"""
Expand All @@ -126,3 +143,6 @@ def __init__(self, first: Expr):

def __str__(self) -> str:
return f"({self.first})"

def accept(self, v: Visitor):
v.visitParenExpr(self)
21 changes: 21 additions & 0 deletions parser/visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from parser.ast import *


class Visitor:
def visitVarExpr(self, vex: VarExpr):
vex.first.accept(self)

def visitNotExpr(self, nex: NotExpr):
nex.first.accept(self)

def visitParenExpr(self, pex: ParenExpr):
pex.first.accept(self)

def visitAndExpr(self, aex: AndExpr):
aex.first.accept(self)

def visitOrExpr(self, oex: OrExpr):
oex.first.accept(self)

def visitVar():
pass

0 comments on commit 37f8496

Please sign in to comment.