Example #1
0
        private void buildExternalFunctions(routedObject Node, functionList functions)
        {
            if (Node == null)
            {
                return;
            }
            string prefix = Node.node.name; //use to create unique function names

            for (int i = 0; i < Node.node.parameterCount; i++)
            {
                var    param = Node.node.getParamByIdx(i);
                string fName = param.Name.Replace(" ", "");

                if (param.Value != null && param.Value.GetType().IsArray)
                {
                    //function to get array object


                    functions.Add(
                        new bluntFunction("get" + fName,
                                          new bluntParam[] { new bluntParam(vtype.obj)
                                                             {
                                                                 objectType = "rointarray"
                                                             }, new bluntParam(vtype.obj)
                                                             {
                                                                 objectType = prefix
                                                             }, new bluntParam(vtype.constant, i) }, BluntInterp4.OP_get_routed_array));


/*
 *                  functions.Add(prefix + "get" + fName,
 *                      new bluntFunction("OP_get_routed_array_item",
 *                          new bluntParam[] { new bluntParam(vtype.num), new bluntParam(vtype.constant, i), new bluntParam(vtype.num) }, BluntInterp4.OP_get_routed_array_item));
 *
 *                  functions.Add(prefix + "set" + fName,
 *                      new bluntFunction("OP_set_routed_array_item",
 *                          new bluntParam[] { new bluntParam(vtype.num), new bluntParam(vtype.constant, i), new bluntParam(vtype.num), new bluntParam(vtype.num) }, BluntInterp4.OP_set_routed_array_item));
 */
                }
                else
                {
                    functions.Add(
                        new bluntFunction("get" + fName,
                                          new bluntParam[] { new bluntParam(vtype.num), new bluntParam(vtype.obj)
                                                             {
                                                                 objectType = prefix
                                                             }, new bluntParam(vtype.constant, i) }, BluntInterp4.OP_get_routed_int16));


                    functions.Add(
                        new bluntFunction("set" + fName,
                                          new bluntParam[] { new bluntParam(vtype.num), new bluntParam(vtype.obj)
                                                             {
                                                                 objectType = prefix
                                                             }, new bluntParam(vtype.constant, i), new bluntParam(vtype.num) }, BluntInterp4.OP_set_routed_int16));                                                             //3
                }
            }
        }
Example #2
0
        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);
        }