Ejemplo n.º 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();
        }
Ejemplo n.º 2
0
        public void CompileAndRun()
        {
            GameTypes.D.onDLog += Console.WriteLine;

            output = new List<string>();

            RelayTwo relay = new RelayTwo();
            TableTwo programsTable = relay.CreateTable(Program.TABLE_NAME);

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

            Program p1 = new Program();
            p1.CreateNewRelayEntry(programsTable, "Program");
            p1.Init(new ProgramRunner(relay));
            p1.sourceCodeContent = "print(42)";
            p1.FunctionDefinitions.Add(print);
            p1.Compile();
            Assert.AreEqual(0, p1.GetErrors().Length);

            for(int i = 0; i < 100; i++) {
                if(p1.sprakRunner.interpreter != null) {
                    p1.Update(0.1f);
                }
            }

            Assert.AreEqual(1, output.Count);
            Assert.AreEqual("42", output[0]);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 public ExternalFunctionCreator(FunctionDefinition[] functionDefinitions)
 {
     if (functionDefinitions != null)
     {
         foreach (FunctionDefinition f in functionDefinitions)
         {
             defineFunction(f);
         }
     }
 }
Ejemplo n.º 5
0
        private void defineFunction(FunctionDefinition f)
        {
            if (externalFunctions.ContainsKey(f.functionName))
            {
                throw new Error("There is already a function called '" + f.functionName + "'", Error.ErrorType.UNDEFINED, 0, 0);
            }

            AST parameterList = new AST(new Token(Token.TokenType.NODE_GROUP, "<PARAMETER_LIST>"));
            for (int i = 0; i < f.parameterTypes.Length; ++i)
            {
                parameterList.addChild(createParameterDefinition(f.parameterTypes[i], f.parameterNames[i]));
            }

            AST functionNode = createFunctionDefinitionNode(f.returnType, f.functionName, parameterList);

            m_builtInFunctions.Add(functionNode);
            externalFunctions.Add(f.functionName, f.callback);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        private void construct(TextReader stream, FunctionDefinition[] functionDefinitions, VariableDefinition[] variableDefinitions)
        {
            Debug.Assert(stream != null);
            Debug.Assert(functionDefinitions != null);

            m_compileTimeErrorHandler = new ErrorHandler();
            m_runtimeErrorHandler = new ErrorHandler();
            m_tokens = Tokenize(stream);
            m_ast = Parse(m_tokens);
            if(m_compileTimeErrorHandler.getErrors().Count > 0) { m_compileTimeErrorHandler.printErrorsToConsole(); return; }

            AddLocalVariables(m_ast, variableDefinitions);
            ExternalFunctionCreator externalFunctionCreator = AddExternalFunctions(functionDefinitions, m_ast);
            Scope globalScope = CreateScopeTree(m_ast);

            if(m_compileTimeErrorHandler.getErrors().Count > 0) { m_compileTimeErrorHandler.printErrorsToConsole(); return; }

            m_interpreter = new InterpreterTwo(m_ast, globalScope, m_runtimeErrorHandler, externalFunctionCreator);
            m_started = false;

            //PaintAST(m_ast);
        }
Ejemplo n.º 8
0
        private ExternalFunctionCreator AddExternalFunctions(FunctionDefinition[] functionDefinitions, AST ast)
        {
            List<FunctionDefinition> allFunctionDefinitions = new List<FunctionDefinition>();
            allFunctionDefinitions.AddRange(builtInFunctions);
            allFunctionDefinitions.AddRange(functionDefinitions);

            ExternalFunctionCreator externalFunctionCreator = new ExternalFunctionCreator(allFunctionDefinitions.ToArray());
            AST functionList = ast.getChild(1);
            foreach (AST externalFunction in externalFunctionCreator.FunctionASTs)
            {
                functionList.addChild(externalFunction);
            }
            return externalFunctionCreator;
        }
Ejemplo n.º 9
0
 public SprakRunner(TextReader stream, FunctionDefinition[] functionDefinitions, VariableDefinition[] variableDefinitions)
 {
     construct(stream, functionDefinitions, variableDefinitions);
 }
Ejemplo n.º 10
0
 public SprakRunner(TextReader stream, FunctionDefinition[] functionDefinitions)
 {
     construct(stream, functionDefinitions, null);
 }
Ejemplo n.º 11
0
 public bool TryGetFunctionDefinition(string pFunctionName, out FunctionDefinition pOutput)
 {
     foreach (FunctionDefinition f in _functionDefinitions)
         if (f.functionName == pFunctionName)
     {
         pOutput = f;
         return true;
     }
     foreach (FunctionDefinition f in SprakRunner.builtInFunctions)
         if (f.functionName == pFunctionName)
     {
         pOutput = f;
         return true;
     }
     pOutput = new FunctionDefinition();
     return false;
 }
Ejemplo n.º 12
0
        void GenerateProgramAPI(Program pProgram)
        {
            var defs = new List<FunctionDefinition> (FunctionDefinitionCreator.CreateDefinitions (this, typeof(Computer)));

            if (hasGraphicsAPI) {

                var graphicsApi = new GraphicsAPI (this);

                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (graphicsApi, typeof(GraphicsAPI)));

                var linesFn = new FunctionDefinition (
                    "void",
                    "Lines",
                    new string[] { "array" },
                    new string[] { "points" },
                    new ExternalFunctionCreator.OnFunctionCall (graphicsApi.Lines),
                    FunctionDocumentation.Default ());

                defs.Add(linesFn);
            }

            if (hasInternetAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new InternetAPI (this, _tingRunner), typeof(InternetAPI)));

                ConnectionAPI_Optimized connectionApi = new ConnectionAPI_Optimized(this, _tingRunner, pProgram);
                defs.Add(new FunctionDefinition(
                    "number",
                    "Connect",
                    new string[] {"string"},
                    new string[] {"name"},
                    new ExternalFunctionCreator.OnFunctionCall(connectionApi.Connect),
                    FunctionDocumentation.Default()));

                defs.Add(new FunctionDefinition(
                    "void",
                    "DisconnectAll",
                    new string[] {},
                    new string[] {},
                    new ExternalFunctionCreator.OnFunctionCall(connectionApi.DisconnectAll),
                    new FunctionDocumentation("Remove all connections", new string[] {})));

                var rfc = new FunctionDefinition (
                              "number",
                              "RemoteFunctionCall",
                              new string[] { "number", "string", "array" },
                              new string[] { "receiverIndex", "functionName", "arguments" },
                              new ExternalFunctionCreator.OnFunctionCall (connectionApi.RemoteFunctionCall),
                              FunctionDocumentation.Default ());
                rfc.hideInModifier = true;

                defs.Add(rfc);
            }
            if (hasWeatherAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new WeatherAPI (this, _worldSettings), typeof(WeatherAPI)));
            }
            if (hasLampAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new LampAPI (this, _tingRunner), typeof(LampAPI)));
            }
            if (hasDoorAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new DoorAPI (this, _tingRunner, _roomRunner), typeof(DoorAPI)));
            }
            if (hasMemoryAPI) {

                MemoryAPI memoryApi = new MemoryAPI(this, _tingRunner);
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (memoryApi, typeof(MemoryAPI)));

                // Are these added manually because they can take any kind of argument..?

                defs.Add(new FunctionDefinition(
                    "void",
                    "SaveMemory",
                    new string[] {"string", "var"},
                    new string[] {"key", "value"},
                    new ExternalFunctionCreator.OnFunctionCall(memoryApi.SaveMemory),
                    FunctionDocumentation.Default()));

                defs.Add(new FunctionDefinition(
                    "var",
                    "LoadMemory",
                    new string[] {"string"},
                    new string[] {"key"},
                    new ExternalFunctionCreator.OnFunctionCall(memoryApi.LoadMemory),
                    FunctionDocumentation.Default()));
            }
            if (hasVoiceAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new VoiceAPI (this, _tingRunner, _dialogueRunner), typeof(VoiceAPI)));
            }
            if (hasElevatorAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new ElevatorAPI (this, _tingRunner), typeof(ElevatorAPI)));
            }
            if (hasTingrunnerAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new TingrunnerAPI (this, _tingRunner, _roomRunner), typeof(TingrunnerAPI)));
            }
            if (hasTrapAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new TrapAPI (this, _tingRunner, _dialogueRunner), typeof(TrapAPI)));
            }
            if (hasHeartAPI) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new HeartAPI (this, _tingRunner, _dialogueRunner), typeof(HeartAPI)));
            }
            if (true /*hasArcadeMachineAPI*/) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new ArcadeMachineAPI (this), typeof(ArcadeMachineAPI)));
            }
            if (true /*hasFloppyAPI*/) {
                defs.AddRange (FunctionDefinitionCreator.CreateDefinitions (new FloppyAPI (this, _tingRunner), typeof(FloppyAPI)));
            }

            pProgram.FunctionDefinitions = defs;
        }
        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);
        }
Ejemplo n.º 14
0
        private ExternalFunctionCreator AddExternalFunctions(FunctionDefinition[] functionDefinitions, AST ast)
        {
			List<FunctionDefinition> allFunctionDefinitions = new List<FunctionDefinition>();
            allFunctionDefinitions.AddRange(builtInFunctions);
            allFunctionDefinitions.AddRange(functionDefinitions);
            
			// HasFunction requires a reference to the SprakRunner
			FunctionDocumentation functionDoc_HasFunction =
				new FunctionDocumentation("Check if a function exists on the object", new string[] { "The name of the function" });
			allFunctionDefinitions.Add(new FunctionDefinition("bool", "HasFunction", new string[] { "string" }, new string[] { "functionName" }, new ExternalFunctionCreator.OnFunctionCall(API_hasFunction), functionDoc_HasFunction));

			ExternalFunctionCreator externalFunctionCreator = new ExternalFunctionCreator(allFunctionDefinitions.ToArray());
            AST functionList = ast.getChild(1);

            foreach (AST externalFunction in externalFunctionCreator.FunctionASTs)
            {
                functionList.addChild(externalFunction);
            }

            return externalFunctionCreator;
        }