def testLambda_call_optional_argument(self): """Checks assigning optinal arguments. """ # Makes an instance of Lmabda. forms = Parser.parse('((x &optional y) (if y (* x x) (* x x x))))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks call. # When an optional argumet is not given, Null() is set to an argument. retval = lambda_func(Parser.parse('(2)'), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # retval is result of (* x x x). self.assertTrue(retval is Integer(8)) # When an optional argumet is given, this is set to an argument. retval = lambda_func(Parser.parse('(2 t)'), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # retval is result of (* x x). self.assertTrue(retval is Integer(4))
def testLambda_call_keyword_argument_with_default_value(self): """Checks assigning keyword arguments. """ # Makes an instance of Lmabda. forms = Parser.parse('((x &key (y t)) (if y (* x x) (* x x x))))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks call. # When a keyword argumet is not given, Null() is set to an argument. retval = lambda_func(Parser.parse('(2)'), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # retval is result of (* x x x). self.assertTrue(retval is Integer(4)) # When an keyword argumet is given, this is set to an argument. retval = lambda_func(Parser.parse('(2 :y nil)'), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # retval is result of (* x x). self.assertTrue(retval is Integer(8))
def testLambda_call_expand_argument(self): """Arguments are expanded before the call method is executed. """ # Makes an instance of Lambda. forms = Parser.parse('((x) (* x x x))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Define macro. forms = Parser.parse('(defmacro cube (x) `(* ,x ,x ,x))') exp = Expander.expand(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) Evaluator.eval(exp, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks call. retval = lambda_func( Parser.parse('((cube 2))'), # an argument is expression. PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks return value. self.assertTrue(retval, Integer(512))
def testMulSystemFunction_call(self): # Makes an instance of MulSystemFunction. mul = MulSystemFunction() # Calls mul. forms = Parser.parse('(1 2 3)') retval = mul(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Chesk return value. self.assertEqual(retval, Integer(6))
def testSubSystemFunction_call(self): # Makes an instance of SubSystemFunction. sub = SubSystemFunction() # Calls sub. forms = Parser.parse('(1 2 3)') retval = sub(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Chesk return value. self.assertEqual(retval, Integer(-4))
def testConsSystemFunction_call(self): # Makes an instance of CarSystemFunction. car = CarSystemFunction() # Calls car. forms = Parser.parse("('(1 2))") retval = car(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Chesk return value. self.assertEqual(retval, Integer(1))
def __call__(self, forms, var_env, func_env, macro_env): """Behavior of MulSystemFunction. """ args = self.eval_forms(forms, var_env, func_env, macro_env) # Initializes retval. retval = Integer(1) # The product of numbers. while args is not Null(): retval = retval * args.car args = args.cdr return retval
def testLambdaSystemFunction_call(self): # Makes an instance of LambdaSytemFunction. forms = Parser.parse('((x) (* x x x))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Executes lambda function. retval = lambda_func(Parser.parse("(2)"), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks return value. # exp is (* x x x) and arg is 2, so retval must be Integer(8). self.assertEqual(retval, Integer(8))
def testLambda_call_evaluate_argument(self): """Arguments are evaluated before the call method is executed. """ # Makes an instance of Lambda. forms = Parser.parse('((x) (* x x x))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks call. retval = lambda_func( Parser.parse('((* 2 2 2))'), # an argument is expression. PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks return value. self.assertTrue(retval, Integer(512))
def testLambda_call(self): """Checks call method of Lambda. The body of Lmabda is expanded and executed when the method is called. """ # Makes an instance of Lambda. forms = Parser.parse('((x) (* x x x))') lambda_func = Lambda(forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks call. retval = lambda_func(Parser.parse('(2)'), PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO']) # Checks return value. self.assertTrue(retval, Integer(8))
def test_eval_forms(self): forms = Parser.parse("(1 'hoge \"fuga\")") # Evaluates forms args = SystemFunction.eval_forms( forms, PackageManager.current_package.env['VARIABLE'], PackageManager.current_package.env['FUNCTION'], PackageManager.current_package.env['MACRO'], ) # Checks type of args. self.assertIsInstance(args, Cons) # Checks args self.assertTrue(args.car is Integer(1)) self.assertTrue(args.cdr.car is Symbol('HOGE')) self.assertTrue(args.cdr.cdr.car is String('fuga')) self.assertTrue(args.cdr.cdr.cdr is Null())