Exemple #1
0
    def test_string(self):
        """test string"""
        input1 = """
        (def a \'str\')
        (assert-equal! a 'str')
        """

        output1 = evaluator.evaluate(input1, None, True)
        output2 = evaluator.evaluate(input1, None, False)

        self.assertEqual(list(output1), [None, None])
        self.assertEqual(list(output2), [None])
    def test_simple(self):
        """test string"""
        input1 = '((lambda (x) x) 1)'
        input2 = '((lambda (x) (+ x 1)) 1)'
        input3 = '(((lambda (x) x) +) 1 2)'

        output1 = list(evaluator.evaluate(input1))
        output2 = list(evaluator.evaluate(input2))
        output3 = list(evaluator.evaluate(input3))

        self.assertEqual(output1, [1])
        self.assertEqual(output2, [2])
        self.assertEqual(output3, [3])
Exemple #3
0
    def test_python_import(self):
        """test string"""
        input1 = '(or? 1 2 3)'
        input2 = '(or? #t #f)'
        input3 = '(or? #f #f)'

        output1 = list(evaluator.evaluate(input1))
        output2 = list(evaluator.evaluate(input2))
        output3 = list(evaluator.evaluate(input3))

        self.assertEqual(output1[0], True)
        self.assertEqual(output2[0], True)
        self.assertEqual(output3[0], False)
Exemple #4
0
    def simple_test(self):
        input1 = """(quote (1 2 3))"""
        input2 = """(quote ((1 2) (3 4)))"""
        input3 = """(quote ahoj)"""
        input4 = """(quote 1)"""

        output1 = list(evaluator.evaluate(input1))
        output2 = list(evaluator.evaluate(input2))
        output3 = list(evaluator.evaluate(input3))
        output4 = list(evaluator.evaluate(input4))

        self.assertEqual(output1[0], [1, 2, 3])
        self.assertEqual(output2[0], [[1, 2], [3, 4]])
        self.assertEqual(output3[0], 'ahoj')
        self.assertEqual(output4[0], 1)
Exemple #5
0
    def test_string(self):
        """test string"""
        input1 = '\'str\''
        input2 = '\"str\"'
        input3 = '(def s \'ahoj\')\n(== s \'ahoj\')'
        input4 = '(def s \'ahoj\')\ns'

        output1 = list(evaluator.evaluate(input1))
        output2 = list(evaluator.evaluate(input2))
        output3 = list(evaluator.evaluate(input3))
        output4 = list(evaluator.evaluate(input4))

        self.assertEqual(output1, ['str'])
        self.assertEqual(output2, ['str'])
        self.assertEqual(output3, [None, True])
        self.assertEqual(output4, [None, 'ahoj'])
Exemple #6
0
    def test_python_import(self):
        import sys
        """test string"""
        input1 = '(import "sys")'

        output1 = list(evaluator.evaluate(input1))

        self.assertEqual(type(output1[0]), type(sys))
Exemple #7
0
    def test_import_python_function(self):
        """test mplisp import"""
        input1 = '''
        (def os-mod (import "os"))
        ((get-attribute os-mod "getcwd"))
        '''

        output1 = list(evaluator.evaluate(input1))
        self.assertTrue("mplisp/tests" in output1[1])
Exemple #8
0
    def test_import_mplisp(self):
        """test mplisp import"""
        input1 = '''
        (import "scripts/testlib")
        (nice-string "test")
        '''

        output1 = list(evaluator.evaluate(input1))
        self.assertEqual(output1[1], ">> test <<")
Exemple #9
0
    def test_python_apply(self):
        """test string"""
        input1 = '''
        (get-attribute "sys" "argv")
        '''

        output1 = list(evaluator.evaluate(input1))

        self.assertTrue(isinstance(output1[0], list))
Exemple #10
0
def main():
    for filename in sys.argv[1:]:
        with open(filename) as file_object:
            try:
                result = evaluator.evaluate(file_object.read())

                for line in result:
                    if line is not None:
                        print(line)
            except ValueError as e:
                print(e.args[0])
Exemple #11
0
    def test_define_list_length_implementation(self):
        """test string"""
        input1 = """
        (def list-len
            (lambda (l)
                (if (== l `()) 0
                    (apply + (map (lambda (x) 1) l)))))
        
        (list-len `(1 2 3 4))
        """

        output1 = list(evaluator.evaluate(input1))
        self.assertEqual(output1, [None, 4])
    def test_factorial(self):
        input1 = """
        (def fact 
            (lambda (x)
                (if (> x 1)
                    (* x (fact (- x 1)))
                    1)))
        (fact 1)
        (fact 2)
        (fact 3)
        (fact 4)
        (fact 5)
        """

        output1 = list(evaluator.evaluate(input1))

        self.assertEqual(output1, [None, 1, 2, 6, 24, 120])
    def test_generator(self):
        input1 = """
        (def fc 
            (lambda (a)
                (lambda (x) (+ x a))))
                
        ((fc 1) 1)
        ((fc 2) 1)
        ((fc 1) 2)
        
        (def +2 (fc 2))
        (def +1 (fc 1))
        
        (+1 1)
        (+1 -2)
        (+2 -2)
        (+2 1)
        (+2 2)
        """

        output1 = list(evaluator.evaluate(input1))

        self.assertEqual(output1, [None, 2, 3, 3, None, None, 2, -1, 0, 3, 4])
Exemple #14
0
def import_module(args: List, node):
    """
    Import all functions from a module and add corresponding symbols
    to the parent environment.
    """
    module_name = evaluator.evaluate_node(args[0])

    if not isinstance(module_name, str):
        evaluator.error(
            "(special_forms.module_import.import) 1st param must be of type str",
            node)

    local_path = module_name + ".mplisp"
    absolute_path = os.path.join("/usr/lib/mplisp", local_path)
    path = ''

    if os.path.isfile(local_path):
        path = local_path
    elif os.path.isfile(absolute_path):
        path = absolute_path

    if path:
        with open(local_path) as file_object:
            list(evaluator.evaluate(file_object.read(), node.getenv()))
            return None

    mplispstd = module_name.replace("std", "mplispstd")

    if module_exists(mplispstd):
        module_name = mplispstd
    elif not module_exists(module_name):
        evaluator.error(
            "(special_forms.module_import.import) package {} not found".format(
                module_name), node)

    mod = importlib.import_module(module_name)
    return mod
Exemple #15
0
    def quote_syntax_test(self):
        input1 = "`(1 2 3)"
        input2 = "`((1 2) (3 4))"
        input3 = "`ahoj"
        input4 = "`1"
        input5 = "`()"
        input6 = "`(() (()))"

        output1 = list(evaluator.evaluate(input1))
        output2 = list(evaluator.evaluate(input2))
        output3 = list(evaluator.evaluate(input3))
        output4 = list(evaluator.evaluate(input4))
        output5 = list(evaluator.evaluate(input5))
        output6 = list(evaluator.evaluate(input6))

        self.assertEqual(output1[0], [1, 2, 3])
        self.assertEqual(output2[0], [[1, 2], [3, 4]])
        self.assertEqual(output3[0], 'ahoj')
        self.assertEqual(output4[0], 1)
        self.assertEqual(output5[0], [])
        self.assertEqual(output6[0], [[], [[]]])
    def test_generator_2(self):
        input1 = """
        (lambda (a) (lambda (x) (+ x a)))
        """

        output1 = list(evaluator.evaluate(input1))