示例#1
0
文件: Program.cs 项目: juliusf/lambda
        static void Main(string[] args)
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            var bar = new SchemeCore.tests.BaseFunctionTests();
            bar.SchemeReaderTest();
            bar.SchemeASTTest();

               // bar.();

            var foo = new SchemeCore.tests.SchemeBuiltinFunctionTest();
            foo.schemeBuiltInAndTest();

            //foo.schemeBuiltinIfTest();
            //foo.schemeBuiltinModuloTest();

            if( System.Diagnostics.Debugger.IsAttached )
            {
                SchemeCore.helper.Logger.enableConsoleLog = true;

            }

            //SchemeCore.helper.Logger.enableLogfile = true;
            //load the std

            eval.loadSchemeLibrary( "std.sch" );

            while( true )
            {
                Console.Write( ">" );
                var ast = reader.parseString( Console.ReadLine() );
                try
                {
                    var result = eval.evaluate( ast );
                    foreach( SchemeObject res in result)
                    {
                        Console.WriteLine( res );
                    }

                }
                catch( SchemeCore.helper.SchemeException e)
                {
                Console.WriteLine(e.Message);
                }
            }
        }
示例#2
0
        public void complexEnviornmentTest()
        {
            var root = SchemeEnvironmentRoot.instance;
            var reader = new SchemeReader();
            var evaluator = new SchemeEvaluator();

            var ast = reader.parseString( "(define foo (lambda (param) (bar param param))" );
            evaluator.evaluate( ast );
            Assert.AreEqual( root, evaluator.environment );
            //Assert.AreEqual( 1, evaluator.environment.getDict().Count );
            Assert.AreEqual( evaluator.environment.getDict().ContainsKey( "foo" ), true );
            Assert.AreEqual( evaluator.environment.getDict()["foo"] is  SchemeLambda, true  );

             ast = reader.parseString( "(define bar +) (foo 1)" );
             evaluator.evaluate( ast );

             //Assert.AreEqual( root, evaluator.environment );
             Assert.AreEqual( evaluator.environment.getDict()["bar"].GetType(), typeof( SchemeBuiltInPlus ) );
             Assert.AreEqual( true, ( (SchemeLambda) evaluator.environment.getDict()["foo"] )._lambdaEnv.has( new SchemeSymbol( "bar" ) ) );
        }
示例#3
0
        public void evaluate()
        {
            var reader = new SchemeReader();
            var evaluator = new SchemeEvaluator();

            //var ast = reader.parseString( ActiveEditor.Text );
            var ast = reader.parseStringWithPos( ActiveEditor.Text, ActiveEditor.Name );
            List<SchemeObject> res;
            try
            {
                 res = evaluator.evaluate(ast);
            }
            catch (SchemeException e)
            {
                var document = codeWindow.Document;
                var marker = new TextMarker(e.offset, e.length, TextMarkerType.SolidBlock);
                document.MarkerStrategy.AddMarker(marker);
                var update = new TextAreaUpdate(TextAreaUpdateType.WholeTextArea);
                document.RequestUpdate(update);
                document.CommitUpdate();

                int length = resultWindow.Text.Length;
                resultWindow.AppendText(e.Message);
                resultWindow.Select(length, (resultWindow.Text.Length - length));
                resultWindow.SelectionColor = Color.Red;

                return;

            }

            // resultWindow.
            foreach( SchemeObject obj in res )
            {
                resultWindow.AppendText( obj.ToString() + "\n" );
            }
        }
示例#4
0
        public void schemeEnvironentTest()
        {
            var root = SchemeEnvironmentRoot.instance;

            var testSymbol = new SchemeSymbol( "foo" );

            SchemeEnvironment e1 = new SchemeEnvironment( root );
            SchemeEnvironment e2 = new SchemeEnvironment( e1 );

            root.set( testSymbol, new SchemeInteger( 1 ) );

            Assert.AreEqual( root.get( testSymbol ), new SchemeInteger ( 1 ) );
            Assert.AreEqual( e1.get( testSymbol ), new SchemeInteger( 1 ) );
            Assert.AreEqual( e2.get( testSymbol ), new SchemeInteger( 1 ) );

            var e1foo = e1.get( testSymbol );

            Assert.IsTrue( ( (SchemeInteger) e1foo ).value == 1 );

            e2.set( testSymbol, new SchemeInteger( 2 ) );
            root.set( testSymbol, new SchemeInteger( 4 ) );

            Assert.IsTrue( e2.get( testSymbol ) != root.get( testSymbol ) );

            //test correctness in in evaluator

            var reader = new SchemeReader();
            var evaluator = new SchemeEvaluator();

            var ast = reader.parseString( "(define x 1)" );
            evaluator.evaluate( ast );

            ast = reader.parseString( "x" );
            Assert.AreEqual( evaluator.evaluate( ast )[0], new SchemeInteger( 1 ) );

            ast = reader.parseString( "(define bar (lambda () (define x 23) (x)))" );
            evaluator.evaluate( ast );

            ast = reader.parseString( "(bar)" );
            Assert.AreEqual( evaluator.evaluate( ast )[1], new SchemeInteger( 23 ) );

            ast = reader.parseString( "x" );
            Assert.AreEqual( evaluator.evaluate( ast )[0], new SchemeInteger( 1 ) );
        }
示例#5
0
        public void SchemeEvaluatorTest()
        {
            SchemeReader reader = new SchemeReader();
            SchemeEvaluator eval = new SchemeEvaluator();

            SchemeAST ast = reader.parseString( "(+ 1 (+ 1 1) (+ 4 (+ 12 3))" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 22 ) );

            ast = reader.parseString( "(define x 13)" );
            eval.evaluate( ast );
            ast = reader.parseString( "x" );
            Assert.AreEqual( eval.evaluate( ast )[0], new SchemeInteger( 13 ) );

            ast = reader.parseString( "1 2" );
        }