Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create z3_pass #36

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions solver/passes/z3_pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from typing import override
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved
from parser.ast import Expr
from parser.ast import AndExpr, OrExpr, Expr, NotExpr, ParenExpr, Var, VarExpr
from parser.visitor import Visitor, RetVisitor
import z3

from parser.lex import Lexer
from parser.parse import Parser


def run_pass(ast: Expr) -> Expr:
v: Z3MappingVisitor = Z3MappingVisitor()
ast.accept(v)

t: TranslateToZ3 = TranslateToZ3(v.symbols)
p: z3.ExprRef = ast.acceptRet(t)
simplifiedExpr = z3.simplify(p)
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved
simplifiedStr: str = str(simplifiedExpr)

l: Lexer = Lexer()
l.lex(simplifiedStr)

pa: Parser = Parser()
ast = pa.parse(l.tokens)
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved

return ast


class Z3MappingVisitor(Visitor):
def __init__(self):
self.symbols: dict[str, z3.Bool] = {}

@override
def visitVar(self, va: Var):
self.symbols[va.name] = z3.Bool(va.name)


class TranslateToZ3(RetVisitor[z3.ExprRef]):

def __init__(self, symbols: dict[str, z3.Bool]):
self.symbols = symbols
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved

@override
def visitVarExpr(self, vex: VarExpr) -> z3.ExprRef:
first: z3.ExprRef = vex.first.acceptRet(self)
if vex.second:
second: z3.ExprRef = vex.second.acceptRet(self)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very close. I think the idea is actually to call AcceptRet on vex.second.first. We want to skip over the AndExpr and OrExpr since these won't return enough context to initialize the Z3 Object.

if isinstance(vex.second, AndExpr):
return z3.And(first, second)
elif isinstance(vex.second, OrExpr):
return z3.Or(first, second)
return first

@override
def visitNotExpr(self, nex: NotExpr) -> z3.ExprRef:
return z3.Not(nex.expr.acceptRet(self))
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved

@override
def visitParenExpr(self, pex: ParenExpr) -> z3.ExprRef:
return pex.expr.acceptRet(self)
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved

@override
def visitAndExpr(self, aex: AndExpr) -> z3.ExprRef:
sanjanamandadi marked this conversation as resolved.
Show resolved Hide resolved
return z3.And(aex.first.acceptRet(self))

@override
def visitOrExpr(self, oex: OrExpr) -> z3.ExprRef:
return z3.Or(oex.first.acceptRet(self))

@override
def visitVar(self, va: Var) -> z3.ExprRef:
return self.symbols[va.name]

if __name__ == "__main__":

prog: str = "A | B & C"
l: Lexer = Lexer()
l.lex(prog)

p: Parser = Parser()
ast: Expr = p.parse(l.tokens)

run_pass(ast)