Skip to content

Instantly share code, notes, and snippets.

@bebraw

bebraw/gist:255410

Created Dec 13, 2009
Embed
What would you like to do?
from placidity.interpreter import Interpreter
class TestInterpreter:
def setup_method(self, method):
interpreter = Interpreter()
self.interpret = interpreter.interpret
class TestAccessVariable:
def test_set_variable(self):
self.interpret('a=6')
assert self.interpret('a') == 6
def test_unset_variable(self):
# Note that we could use some other convention as well.
# Zero will do for now
assert self.interpret('a') == 0
TestAccessVariable.setup_method = setup_method
... # the math tests!
...
class TestUnsetVariable(TestInterpreter):
def test_unset_variable(self):
assert self.interpret('a') == 'null'
def test_variable_in_expression(self):
assert self.interpret('a+3') == 'null'
...
class Interpreter:
def __init__(self):
self.vars = {}
def interpret(self, expression):
try:
return eval(expression, self.vars)
except NameError:
return 'null'
except SyntaxError:
l_value, r_value = expression.split('=')
try:
self.vars[l_value] = int(r_value)
except ValueError:
self.vars[l_value] = self.interpret(r_value)
class Interpreter:
def __init__(self):
self.vars = {}
def interpret(self, expression):
try:
return eval(expression, self.vars)
except SyntaxError:
l_value, r_value = expression.split('=')
self.vars[l_value] = int(r_value)
except NameError:
return 0
from placidity.interpreter import Interpreter
from placidity.utils import Operations
class TestInterpreter:
... # access variable tests and setup method
class TestMath:
operations = Operations(('1+1', 2), ('5-1', 4), ('3*5', 15), ('12/4', 3), )
def test_operations(self):
for operation in self.operations:
assert self.interpret(operation.expression) == operation.result
TestMath.setup_method = setup_method
class Operations(tuple):
def __new__(cls, *args):
class Operation:
def __init__(self, expression, result):
self.expression = expression
self.result = result
operations = []
for arg in args:
operations.append(Operation(*arg))
return tuple.__new__(cls, operations)
from placidity.interpreter import Interpreter
from placidity.utils import Operations
class TestInterpreter:
def setup_method(self, method):
interpreter = Interpreter()
self.interpret = interpreter.interpret
class TestSetVariable(TestInterpreter):
def test_set(self):
self.interpret('a=6')
assert self.interpret('a') == 6
def test_variable_in_expression(self):
self.interpret('a=12')
assert self.interpret('a+3') == 15
class TestUnsetVariable(TestInterpreter):
def test_unset_variable(self):
# Note that we could use some other convention as well.
# Zero will do for now
assert self.interpret('a') == 0
def test_variable_in_expression(self):
assert self.interpret('a+3') == 3
class TestMath(TestInterpreter):
operations = Operations(('1+1', 2), ('5-1', 4), ('3*5', 15), ('12/4', 3), )
def test_operations(self):
for operation in self.operations:
assert self.interpret(operation.expression) == operation.result
class Variables(dict):
def __getitem__(self, key):
return super(Variables, self).__getitem__(key) if key in self else 0
class Interpreter:
def __init__(self):
self.vars = Variables()
def interpret(self, expression):
try:
return eval(expression, self.vars)
except SyntaxError:
l_value, r_value = expression.split('=')
self.vars[l_value] = int(r_value)
...
class TestSetVariable(TestInterpreter):
def test_set(self):
self.interpret('a=6')
assert self.interpret('a') == 6
def test_set_expression(self):
self.interpret('a=4*3')
assert self.interpret('a') == 12
def test_set_variable(self):
self.interpret('a=8')
self.interpret('b=a')
assert self.interpret('b') == 8
def test_variable_in_expression(self):
self.interpret('a=12')
assert self.interpret('a+3') == 15
...
...
class Interpreter:
def __init__(self):
self.vars = Variables()
def interpret(self, expression):
try:
return eval(expression, self.vars)
except SyntaxError:
l_value, r_value = expression.split('=')
try:
self.vars[l_value] = int(r_value)
except ValueError:
try:
self.vars[l_value] = eval(r_value, self.vars)
except SyntaxError:
self.vars[l_value] = self.vars[r_value]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment