-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving UnaryOp support and test cases
- Loading branch information
Showing
6 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import logging | ||
import z3 | ||
import ast | ||
import pyState | ||
from pyObjectManager.Int import Int | ||
from pyObjectManager.Real import Real | ||
from pyObjectManager.BitVec import BitVec | ||
from copy import deepcopy | ||
|
||
logger = logging.getLogger("pyState:UnaryOp") | ||
|
||
def handle(state,element,ctx=None): | ||
""" | ||
Input: | ||
state = State object | ||
element = ast.UnaryOp element to parse | ||
(optional) ctx = context to resolve UnaryOp in if not current | ||
Action: | ||
Parse out the element with respect to the state | ||
Returns: | ||
pyObjectManager object representing this UnaryOp | ||
""" | ||
ctx = state.ctx if ctx is None else ctx | ||
|
||
assert type(state) == pyState.State | ||
assert type(element) == ast.UnaryOp | ||
|
||
op = element.op | ||
target = state.resolveObject(element.operand) | ||
|
||
if type(target) not in [Int, Real, BitVec]: | ||
err = "handle: unable to resolve UnaryOp target type '{0}'".format(type(target)) | ||
logger.error(err) | ||
raise Exception(err) | ||
|
||
# Get a new variable | ||
t,args = pyState.duplicateSort(target) | ||
newVar = state.getVar("tempUnaryOp",varType=t,kwargs=args) | ||
newVar.increment() | ||
|
||
if type(op) == ast.USub: | ||
state.addConstraint(newVar.getZ3Object() == -target.getZ3Object()) | ||
|
||
elif type(op) == ast.UAdd: | ||
state.addConstraint(newVar.getZ3Object() == target.getZ3Object()) | ||
|
||
elif type(op) == ast.Not: | ||
state.addConstraint(newVar.getZ3Object() == z3.Not(target.getZ3Object())) | ||
|
||
elif type(op) == ast.Invert: | ||
err = "handle: Invert not implemented yet" | ||
logger.error(err) | ||
raise Exception(err) | ||
|
||
else: | ||
# We really shouldn't get here... | ||
err = "handle: {0} not implemented yet".format(type(op)) | ||
logger.error(err) | ||
raise Exception(err) | ||
|
||
return newVar.copy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import sys, os | ||
myPath = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.insert(0, myPath + '/../') | ||
|
||
import logging | ||
import Colorer | ||
logging.basicConfig(level=logging.DEBUG,format='%(name)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | ||
|
||
|
||
import ast | ||
import z3 | ||
from pyPath import Path | ||
from pyPathGroup import PathGroup | ||
import pytest | ||
|
||
test1 = """ | ||
x = -1 | ||
y = -x | ||
z = +x | ||
""" | ||
|
||
def test_pySym_UnaryOp_Simple(): | ||
b = ast.parse(test1).body | ||
p = Path(b,source=test1) | ||
pg = PathGroup(p) | ||
|
||
pg.explore() | ||
assert len(pg.completed) == 1 | ||
assert pg.completed[0].state.any_int('x') == -1 | ||
assert pg.completed[0].state.any_int('y') == 1 | ||
assert pg.completed[0].state.any_int('z') == -1 | ||
|