예제 #1
0
        public void schemeBuiltinConsTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString("(car (cons 10 11))");
            Assert.AreEqual(eval.evaluate(ast)[0], new SchemeInteger(10));

            ast = reader.parseString("(cdr (cons 10 11))");
            Assert.AreEqual(eval.evaluate(ast)[0], new SchemeInteger(11));
        }
예제 #2
0
        public void schemeBuiltInEqualsTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString("(= 1 2)");
            Assert.AreEqual(eval.evaluate(ast)[0], SchemeFalse.instance);

            ast = reader.parseString("(= 1 1)");
            Assert.AreEqual(eval.evaluate(ast)[0], SchemeTrue.instance);
        }
예제 #3
0
        public void schemeHigherOrderFunctionTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString( "(define scons (lambda (x y) (lambda (m) (m x y))))" );
            eval.evaluate( ast );
            ast = reader.parseString( "(define scar (lambda (z) (z (lambda (p q) p))))" );
            eval.evaluate( ast );
            ast = reader.parseString( "(scar (scons 10 11))" );

            Assert.AreEqual( new SchemeInteger( 10 ), eval.evaluate( ast )[0] );

            //Double evaluation Bug
            ast = reader.parseString( "(scar (scons 10 11))" );

            Assert.AreEqual( new SchemeInteger( 10 ), eval.evaluate( ast )[0] );
        }
예제 #4
0
        public void schemeBuiltinIfTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString("(if (= 1 1) 1 2)");
            Assert.AreEqual(eval.evaluate(ast)[0], new SchemeInteger(1));

            ast = reader.parseString("(if (= 1 2) 1 2)");
            Assert.AreEqual(eval.evaluate(ast)[0], new SchemeInteger(2));
        }
예제 #5
0
        public void schemeBuiltInPlusTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString("(+ 32 8)");
            Assert.AreEqual(new SchemeInteger(40), eval.evaluate(ast)[0]);
        }
예제 #6
0
        public void schemeBuiltinModuloTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString("(modulo 7 3");
            Assert.AreEqual(eval.evaluate(ast)[0], new SchemeInteger(1));
        }
예제 #7
0
        public void schemeBuiltInLambdaTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var ast = reader.parseString( "(define add (lambda (num1 num2)(+ num1 num2)))" );
            eval.evaluate( ast );
            ast = reader.parseString( "(add 1 2)" );

            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 3 ) );

            ast = reader.parseString( "(define foo (lambda () (+ 1 2))) " );
            eval.evaluate( ast );
            ast = reader.parseString( "(foo)" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 3 ) );

            ast = reader.parseString( "(define bar (lambda (a b) (add a b))" );
            eval.evaluate( ast );

            ast = reader.parseString( "(bar 1 2)" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 3 ) );

            ast = reader.parseString( "(bar (+ 1 2) 2)" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 5 ) );

            ast = reader.parseString( "(define bar (lambda (a) (if (= a 10) a (+ (bar (+ a 1)) 1 ))))" );
            eval.evaluate( ast );

            ast = reader.parseString( "(bar 1)" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 19 ) );
            ast = reader.parseString( "(define foo (lambda (a) (if (= a 10) a (foo (+ a 1)))))" );
            eval.evaluate( ast );

            ast = reader.parseString( "(foo 1)" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 10 ) );
        }
예제 #8
0
        public void schemeSetTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var defineSingletonSet = reader.parseString( "(define singletonSet (lambda (x) (lambda (y) (= y x))))" );
            var defineContains = reader.parseString( "(define contains (lambda (set_ y) (set_ y)))" );
            var defineTestSets = reader.parseString( "(define s1 (singletonSet 1)) (define s2 (singletonSet 2))" +
                   "(define s1 (singletonSet 1)) " +
                    "(define s2 (singletonSet 2)) "  +
                    "(define s3 (lambda (x) (and (>= x 5) (<= x 15))))" +
                    "(define s4 (lambda (x) (and (<= x -5) (>= x -15))))" +
                  ")");

            var test1 = reader.parseString("(contains s1 1)");
            var test2 = reader.parseString("(contains s2 2)");
            var test3 = reader.parseString("(contains s3 5)");
            var test4 = reader.parseString("(contains s4 -5)");
            var test5 = reader.parseString("(contains s4 -22)");

            eval.evaluate( defineSingletonSet );
            eval.evaluate( defineContains );
            eval.evaluate( defineTestSets );

            Assert.AreEqual( SchemeTrue.instance, eval.evaluate( test1 )[0] );
            Assert.AreEqual( SchemeTrue.instance, eval.evaluate( test2 )[0] );
            Assert.AreEqual( SchemeTrue.instance, eval.evaluate( test3 )[0] );
            Assert.AreEqual( SchemeTrue.instance, eval.evaluate( test4 )[0] );
            Assert.AreEqual( SchemeFalse.instance, eval.evaluate( test5 )[0] );
        }