コード例 #1
0
ファイル: scriptEditor.cs プロジェクト: kevinmiles/rc24
        private void run(bool debug)
        {
            string fullSource = source;

            //very crude selective use of library functions
            if (source.Contains("while("))
            {
                fullSource += whileFunction;
            }
            if (source.Contains("if("))
            {
                fullSource += ifFunction;
            }
            if (source.Contains("for("))
            {
                fullSource += forFunction;
            }
            if (source.Contains("loop("))
            {
                fullSource += loopFunction;
            }

            var p = new parser5(fullSource);
            //  var p = new parser(fullSource);

            astNode tree;

            try
            {
                tree = p.parse();
            }
            catch (Exception parseErr)
            {
                MessageBox.Show("Parse Error " + parseErr.Message + " Line " + p.line);
                return;
            }

            if (debug)
            {
                displaySyntaxTree(tree);
            }
            compiler.fixDotNotation(tree);
            if (debug)
            {
                displaySyntaxTree(tree);
            }

            compiler comp = new compiler();

            //get functions from connected devices
            var ExternalFunctions = new functionList();

            dynamic RX = PC.findNode("RX");

            buildExternalFunctions(RX, ExternalFunctions);
            dynamic IMU = PC.findNode("IMU");

            buildExternalFunctions(IMU, ExternalFunctions);
            dynamic pilot = PC.findNode("Pilot");

            buildExternalFunctions(pilot, ExternalFunctions);
            dynamic TX = PC.findNode("TX");

            buildExternalFunctions(TX, ExternalFunctions);

            if (PC.node.parameterCount == 0)
            {
                PC.node.properties.Add("say", new ccParameter(0, "say", 7, 0, PC.node));
                PC.node.parameterCount++;
            }

            buildExternalFunctions(PC, ExternalFunctions);

            int[] bbc;
            try
            {
                bbc = comp.compile(tree, ExternalFunctions);
            }
            catch (Exception ce)
            {
                string msg = "Compilation Error " + ce.Message;
                foreach (var k in ce.Data.Keys)
                {
                    msg += " " + k.ToString() + " " + ce.Data[k].ToString();
                }
                MessageBox.Show(msg);
                return;
            }


            if (debug)
            {
                comp.list(bbc);
                Console.Out.WriteLine(comp.listMethods());
            }

            if (ComboBoxTarget.SelectedItem.ToString() != "PC")
            {
                //run on receiver
                if (pilot != null)
                {
                    try
                    {
                        //stop existing script running
                        pilot.enabled = false;
                        //send to script array on rx
                        pilot.script = bbc;

                        //int[] test = pilot.script;

                        Console.Out.WriteLine("Script Uploaded");

                        //run code
                        pilot.enabled = true;
                    }
                    catch (Exception RXrunEx)
                    {
                        MessageBox.Show("Failed to run exception " + RXrunEx.Message);
                    }
                }
                else
                {
                    Console.Out.WriteLine("Pilot node not found");
                }
            }
            if (ComboBoxTarget.SelectedItem.ToString() == "PC")
            {
                // run script on pc virtual machine

                int[] inputs = new int[] { 0, 1, 2, 3, 4 };
                try
                {
                    comp.run(inputs, debug, new List <object> {
                        PC, TX, RX, pilot, IMU
                    });
                }
                catch (Exception runEx)
                {
                    MessageBox.Show("Interp exception " + runEx.Message);
                }
            }
            //     emitBBC(bbc);
        }