Пример #1
0
        static void Main(string[] args)
        {
            string filename = "../Program1";

            if(args.Length > 0) {
                filename = args[0];
            }

            TextReader tr = File.OpenText(filename);
            //StringReader programString = new StringReader("g()\nfloat g() {\n	print(42)\n }");

            FunctionDefinition[] functionDefinitions = new FunctionDefinition[] {
                new FunctionDefinition("void", "print", new string[] { "string" }, new string[] { "text" }, print, FunctionDocumentation.Default())
            };

            SprakRunner runner = new SprakRunner(tr, functionDefinitions);
            bool success = runner.Start();
            if(success) {
                while(runner.Step() == InterpreterTwo.Status.OK) {
                }
            }

            Console.WriteLine("OUTPUT: ");
            foreach(string s in m_output) {
                Console.WriteLine(s);
            }

            //runner.printTree(true);

            //Console.In.ReadLine();
        }
        public void ArrayAsArgumentListTest()
        {
            DemoClassThree dc3 = new DemoClassThree();
            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc3, typeof(DemoClassThree));
            Assert.AreEqual(1, defs.Length);

            List<FunctionDefinition> moreFunctionDefinitions = new List<FunctionDefinition> {
                GetPrintFunction ()
            };

            moreFunctionDefinitions.AddRange (defs);

            TextReader programString = File.OpenText("code75.txt");
            SprakRunner program = new SprakRunner(programString, moreFunctionDefinitions.ToArray());

            program.run();

            foreach(var e in program.getRuntimeErrorHandler().getErrors()) {
                Console.WriteLine(e);
            }

            foreach(var e in program.getCompileTimeErrorHandler().getErrors()) {
                Console.WriteLine(e);
            }

            Assert.AreEqual (0, program.getRuntimeErrorHandler().getErrors().Count);
            Assert.AreEqual (0, program.getCompileTimeErrorHandler().getErrors().Count);
        }
Пример #3
0
        public DefaultSprakRunner(TextReader stream)
        {
            FunctionDefinition[] functionDefinitions = new FunctionDefinition[] {
                new FunctionDefinition("void", "print", new string[] { "string" }, new string[] { "text" }, new ExternalFunctionCreator.OnFunctionCall(print), FunctionDocumentation.Default()),
                new FunctionDefinition("number", "sqrt", new string[] { "number" }, new string[] { "f" }, new ExternalFunctionCreator.OnFunctionCall(sqrt), FunctionDocumentation.Default())
            };

            m_sprakRunner = new SprakRunner(stream, functionDefinitions);
        }
Пример #4
0
        public DefaultSprakRunner(TextReader stream)
        {
            FunctionDefinition[] functionDefinitions = new FunctionDefinition[] {
                new FunctionDefinition("void", "print", new string[] { "var" }, new string[] { "the thing to print" }, new ExternalFunctionCreator.OnFunctionCall(print), FunctionDocumentation.Default()),
                new FunctionDefinition("number", "sqrt", new string[] { "number" }, new string[] { "f" }, new ExternalFunctionCreator.OnFunctionCall(sqrt), FunctionDocumentation.Default())
            };

            m_sprakRunner = new SprakRunner(stream, functionDefinitions);
        }
Пример #5
0
        static void Main(string[] args)
        {
            string filename = ""; //"../Program1";

            if (args.Length > 0) {
                filename = args [0];
            } else {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine ("No program file given");
                return;
            }

            TextReader tr = File.OpenText(filename);
            //StringReader programString = new StringReader("g()\nfloat g() {\n	print(42)\n }");

            FunctionDefinition[] functionDefinitions = new FunctionDefinition[] {
                new FunctionDefinition("void", "print", new string[] { "string" }, new string[] { "text" }, print, FunctionDocumentation.Default())
            };

            SprakRunner runner = new SprakRunner(tr, functionDefinitions);
            runner.run (int.MaxValue);
        }
Пример #6
0
 void DeleteSprakRunner()
 {
     if (_sprakRunner != null) {
         _sprakRunner.HardReset ();
     }
     _sprakRunner = null;
 }
Пример #7
0
        public Error[] Compile()
        {
            if(!compilationTurnedOn) {
                return new Error[] {
                    new Error("Uncompiled program.")
                };
            }

            //D.Log("Compiling " + this.ToString());

            StopAndReset();

            //_sprakRunner = new SprakRunner(new StringReader(sourceCodeContent), FunctionDefinitions.ToArray(), VariableDefinitions.ToArray());

            if(_sprakRunner == null) {
                //D.Log("Creating new SprakRunner for " + this.ToString());
                _sprakRunner = new SprakRunner(new StringReader(sourceCodeContent), FunctionDefinitions.ToArray(), VariableDefinitions.ToArray());
            }
            else {
                _sprakRunner.Reset();
            }

            PrintErrorsToD();
            return GetErrors();
        }
        public void CallingFunctionWithWrongArgumentType_USING_FUNCTION_DEFINITION_CREATOR()
        {
            TextReader programString = File.OpenText("code73.txt");

            ClassWithFunction c = new ClassWithFunction ();
            FunctionDefinition[] funcDefs = FunctionDefinitionCreator.CreateDefinitions (c, typeof(ClassWithFunction));

            List<FunctionDefinition> moreFunctionDefinitions = new List<FunctionDefinition> {
                GetPrintFunction ()
            };

            moreFunctionDefinitions.AddRange (funcDefs);

            SprakRunner program = new SprakRunner(programString, moreFunctionDefinitions.ToArray());
            program.run();

            Assert.AreEqual (0, program.getCompileTimeErrorHandler().getErrors().Count);
        }
        public void CallingFunctionWithWrongArgumentType_MANUAL_FUNCTION_DEFINITION()
        {
            TextReader programString = File.OpenText("code72.txt");

            FunctionDefinition[] functionDefinitions = new FunctionDefinition[] {
                new FunctionDefinition(
                    "number", "ThisFunctionTakesANumber",
                    new string[] { "number" }, new string[] { "x" },
                ThisFunctionTakesANumber, FunctionDocumentation.Default()),

                GetPrintFunction()
            };

            SprakRunner program = new SprakRunner(programString, functionDefinitions);
            program.run();

            Assert.AreEqual (0, program.getCompileTimeErrorHandler().getErrors().Count);
        }
        public void CallFunctionThatThrowsException()
        {
            DemoClassFour dc3 = new DemoClassFour();
            FunctionDefinition[] defs = FunctionDefinitionCreator.CreateDefinitions(dc3, typeof(DemoClassFour));
            Assert.AreEqual(1, defs.Length);

            List<FunctionDefinition> moreFunctionDefinitions = new List<FunctionDefinition> {
                GetPrintFunction ()
            };

            moreFunctionDefinitions.AddRange (defs);

            TextReader programString = File.OpenText("code77.txt");
            SprakRunner program = new SprakRunner(programString, moreFunctionDefinitions.ToArray());

            program.run();

            Assert.AreEqual (0, program.getCompileTimeErrorHandler().getErrors().Count);
            Assert.AreEqual (1, program.getRuntimeErrorHandler().getErrors().Count);

            program.getRuntimeErrorHandler().printErrorsToConsole();
        }