def test_eval_define_evaluateSecondArg(self): initialize.initialize() eval_string('(define a 1)') eval_string('(define b a)') obj = eval_string('b') self.assertEqual(obj.type, 'schemeNumber', 'b should be bound to type schemeNumber') self.assertEqual(obj.value, 1, 'b should be bound to schemeNumber 1')
def test_eval_set(self): initialize.initialize() eval_string('(define x 3)') obj = eval_string('(set! x 42)') self.assertIsNotNone(obj, 'syntax set! should not return None.') self.assertEqual(obj.type, 'schemeVoid','syntax set! should return schemeVoid.') self.assertEqual(eval_string('x'), SchemeNumber(42), 'syntax set! does not work. b should be bound to 42 in the global env.')
def repl(): initialize.initialize() e = evaluator.SchemeEvaluator() r = reader.SchemeReader() initCode = "" with open("init.lisp", "r") as initfile: initCode = initfile.read() initCode = "(begin {0} )".format(initCode) stream = scheme.SchemeStringStream(initCode) initEval = r.read(stream) initPrint = e.evaluate(initEval) if initPrint.type != "schemeVoid": print(initPrint) while True: try: toRead = input("> ") stream = scheme.SchemeStringStream(toRead) syntaxResult = r.checkSyntax(stream) while not syntaxResult.isBalanced() and syntaxResult.isValid(): toRead += "\n" + input() stream = scheme.SchemeStringStream(toRead) syntaxResult = r.checkSyntax(stream) while not stream.isAtEndOfStream(): toEval = r.read(stream) toPrint = e.evaluate(toEval) if toPrint.type != "schemeVoid": print(toPrint) except schemeExceptions.SchemeException as exception: print("{0}: {1}".format(type(exception).__name__, exception)) except Exception as exception: print("Critical error: {0}".format(exception))
def test_eval_or(self): initialize.initialize() obj = eval_string('(or #t #f)') self.assertEqual(obj.type, 'schemeTrue', '(or #t #f) should result in schemeTrue') obj = eval_string('(or #f #f)') self.assertEqual(obj.type, 'schemeFalse', '(or #f #f) should result in schemeFalse') obj = eval_string('(or (= 3 3) (= 4 4))') self.assertEqual(obj.type, 'schemeTrue', '(or (= 3 3) (= 4 4)) should result in schemeTrue')
def test_eval_symbol(self): initialize.initialize() env = SchemeEnvironment() sym1 = SchemeSymbol('a') env.addBinding(sym1, SchemeNumber(10)) obj = eval_string('a', env) self.assertEqual(obj.type, 'schemeNumber', 'A symbol should evaluate to its binding.') self.assertEqual(obj.value, 10, "The Binding of the symbol 'a' does not have the right value. Should be 10, is {0}".format(obj.value))
def test_eval_define(self): initialize.initialize() self.assertRaises(schemeExceptions.NoBindingException, eval_string, 'b') # with global env. obj = eval_string('(define b 3)') self.assertIsNotNone(obj, 'syntax define should not return None.') self.assertEqual(obj.type, 'schemeVoid','syntax define should return schemeVoid.') self.assertEqual(eval_string('b'), SchemeNumber(3), 'syntax define does not work. b should be bound to 3 in the global env.')
def test_eval_setBindingInOuterEnv(self): initialize.initialize() eval_string('(define x 1)') eval_string('(define f (lambda (z) (set! x z)))') eval_string('(f 3)') obj = eval_string('x') self.assertEqual(obj.type, 'schemeNumber', 'x should be bound to a schemeNumber type.') self.assertEqual(obj.value, 3, 'after f is called, x should be bound to 3.')
def test_eval_shortHandLambda_MoreThanOneBodyElement(self): initialize.initialize() eval_string('(define (f n) (define x 1) (+ n x))') obj = eval_string('f') self.assertEqual(obj.type, 'schemeUserDefinedFunction', 'the lambda shorthand syntax should create a user defined function') obj2 = eval_string('(f 3)') self.assertEqual(obj2.type, 'schemeNumber', 'f should return a number type') self.assertEqual(obj2.value, 4, 'f should return 4 if given 3.')
def test_eval_shortHandLambda(self): initialize.initialize() eval_string('(define (add1 n) (+ n 1))') obj = eval_string('add1') self.assertEqual(obj.type, 'schemeUserDefinedFunction', 'the lambda shorthand syntax should create a user defined function') obj2 = eval_string('(add1 3)') self.assertEqual(obj2.type, 'schemeNumber', 'add1 should return a number type') self.assertEqual(obj2.value, 4, 'add1 should return 4 if given 3.')
def test_lambda_makeAdder(self): initialize.initialize() eval_string('(define make-adder (lambda(n) (lambda(x) (+ x n))))') eval_string('(define add10 (make-adder 10))') lambdaObject = eval_string('add10') self.assertRaises(schemeExceptions.NoBindingException, eval_string, 'n') # no global n obj = call_lambda(lambdaObject, '5') self.assertEqual(obj.type, 'schemeNumber', 'lambda should return type schemeNumber.') self.assertEqual(obj.value, 15, 'lambda should return schemeNumber 15.')
def test_eval_begin(self): initialize.initialize() obj = eval_string('(begin (define a 1) (define b "hello") 3)') self.assertEqual(obj.type, 'schemeNumber', 'syntax begin should evaluate all arguments and return the return value of last one.') self.assertEqual(obj.value, 3, 'syntax begin should return the return value of the last evaluated arg') obj = eval_string('a') self.assertEqual(obj.type, 'schemeNumber', 'after the begin statement, a should be bound to schemeNumber 1') self.assertEqual(obj.value, 1, 'after the begin statement, a should be bound to schemeNumber 1') obj = eval_string('b') self.assertEqual(obj.type, 'schemeString', 'after the begin statement, a should be bound to schemeString "hello"') self.assertEqual(obj.value, 'hello', 'after the begin statement, a should be bound to schemeString "hello"')
def test_lambda_tak(self): initialize.initialize() eval_string('(define counter 0)') eval_string(''' (define (tak x y z) (set! counter (+ counter 1)) (if (< y x) (tak (tak (- x 1) y z) (tak (- y 1) z x) (tak (- z 1) x y)) z ) )''' ) obj = eval_string('(tak 4 0 1)') self.assertEqual(obj.type, 'schemeNumber', 'tak should return a schemeNumber') self.assertEqual(obj.value, 0, 'tak should return schemeNumber 1 for arguments 4 0 1') c = eval_string('counter') self.assertEqual(c.type, 'schemeNumber', 'counter should be a number.') self.assertEqual(c.value, 17, 'the counter should be 17 for tak arguments 4 0 1')
def test_eval_map_listsNotSameSize(self): initialize.initialize() eval_string('(define addElementWise (lambda (n m) (+ n m)))') self.assertRaises(schemeExceptions.InvalidInputException, eval_string, '(map addElementWise (list 1 2 3 4) (list 2 3 4))')
def test_eval_map_nils(self): initialize.initialize() eval_string('(define addElementWise (lambda (n m) (+ n m)))') obj = eval_string('(map addElementWise () ())') self.assertEqual(obj.type, 'schemeNil', 'map should return schemeNil if applied to empty lists.')
def test_eval_map_noLists(self): initialize.initialize() eval_string('(define addElementWise (lambda (n m) (+ n m)))') self.assertRaises(schemeExceptions.ArgumentTypeException, eval_string, '(map addElementWise 3 4)') self.assertRaises(schemeExceptions.ArgumentTypeException, eval_string, '(map addElementWise (list 1 2) 4)') self.assertRaises(schemeExceptions.ArgumentTypeException, eval_string, '(map addElementWise 3 (list 1 2))')
def test_eval_map(self): initialize.initialize() eval_string('(define plus1 (lambda (n) (+ n 1)))') obj = eval_string('(map plus1 (list 1 2 3 4))') self.assertEqual(obj.type, 'schemeCons', 'function map should return a list') self.assertEqual(str(obj), '(2 3 4 5)', 'function map should apply plus1 to each given list element.')
def test_eval_set_noBinding(self): initialize.initialize() self.assertRaises(schemeExceptions.NoBindingException, eval_string, '(set! c 5)')
def test_eval_map_functionCall_tooManyArguments(self): initialize.initialize() eval_string('(define addElementWise (lambda (n m) (+ n m)))') self.assertRaises(schemeExceptions.ArgumentCountException, eval_string, '(map addElementWise (list 1 2 3) (list 2 3 4) (list 3 4 5))')
def test_eval_symbol_no_binding(self): initialize.initialize() env = SchemeEnvironment() sym1 = SchemeSymbol('a') env.addBinding(sym1, SchemeNumber(10)) self.assertRaises(schemeExceptions.NoBindingException, eval_string, 'b', env)
def test_lambda_equal(self): initialize.initialize() eval_string('(define (f) 1)') eval_string('(define (g) 1)') obj = eval_string('(eq? g f)') self.assertEqual(obj.type, 'schemeFalse', 'g und f should not be equal.')
def test_eval_map_notCallable(self): initialize.initialize() self.assertRaises(schemeExceptions.ArgumentTypeException, eval_string, '(map 3 (list 2 3 4))')
def test_evaluate_noEnv(self): initialize.initialize() evaluator.SchemeEvaluator.globalEnv.addBinding(SchemeSymbol('a'), SchemeNumber(3)) obj = evaluator.SchemeEvaluator().evaluate(SchemeSymbol('a')) self.assertEqual(obj.type, 'schemeNumber', 'evaluate should find a in the global environment. a should be bound to schemeNumber') self.assertEqual(obj.value, 3, 'a should be bound to 3 in global Environment.')
from interpreter.kmp_py import reader, evaluator,schemeExceptions, initialize from interpreter.kmp_py.scheme import * from unittest import TestCase import sys # initialize global environments and adds initial bindings to globalEnv and syntaxEnv initialize.initialize() def eval_string(string, env = evaluator.SchemeEvaluator.globalEnv): r = reader.SchemeReader() e = evaluator.SchemeEvaluator() obj = r.read(SchemeStringStream(string)) return e.evaluate(obj, env) class SchemeEvaluatorArithmetic(TestCase): def test_eval_number(self): obj = eval_string('1') self.assertEqual(obj.type, 'schemeNumber', 'A number should evaluate to itself.') self.assertEqual(obj.value, 1, 'Evaluated number does not have the right value.') def test_eval_number_negative(self): obj = eval_string('-3') self.assertEqual(obj.type, 'schemeNumber', 'A number should evaluate to itself.') self.assertEqual(obj.value, -3, 'Evaluated number does not have the right value.') def test_eval_float(self): obj = eval_string('3.14') self.assertEqual(obj.type, 'schemeNumber', 'A number should evaluate to itself.') self.assertEqual(obj.value, 3.14, 'Evaluated number does not have the right value.')
def test_eval_or_secondArgumentNotEvaluated(self): initialize.initialize() obj = eval_string('(or #t (aSymbolThatCannotPossiblyHaveABindingAndWillNeverBeEvaluated x y z))') self.assertEqual(obj.type, 'schemeTrue', 'If a argument is true the others should not be evaluated.')