Beispiel #1
0
        public CompileResult Compile(string code)
        {
            CompileResult compileResult = new CompileResult();

            runningPhase = RunningPhaseType.Global;

            if (string.IsNullOrEmpty(code))
            {
                return(compileResult);
            }

            // export
            List <byte[]> data = new List <byte[]>();

            try
            {
                var lexer  = new Lexer();
                var tokens = lexer.Analyze(code);

                var parser   = new Parser();
                var sentense = parser.Parse(tokens.Tokens);

                var ssaGenerator = new SSAGenerator();
                ssaGenerator.Eval(sentense);
                var outputAttribute = ssaGenerator.RootTable.Tables["@O"].Value;
                var outputValues    = new SSAGenerator.Value[4];
                if (outputAttribute is SSAGenerator.Value)
                {
                    outputValues[0] = outputAttribute as SSAGenerator.Value;
                    outputValues[1] = outputAttribute as SSAGenerator.Value;
                    outputValues[2] = outputAttribute as SSAGenerator.Value;
                    outputValues[3] = outputAttribute as SSAGenerator.Value;
                }
                else if (outputAttribute is SSAGenerator.SymbolTable)
                {
                    outputValues[0] = (outputAttribute as SSAGenerator.SymbolTable).Tables["x"].Value as SSAGenerator.Value;
                    outputValues[1] = (outputAttribute as SSAGenerator.SymbolTable).Tables["y"].Value as SSAGenerator.Value;
                    outputValues[2] = (outputAttribute as SSAGenerator.SymbolTable).Tables["z"].Value as SSAGenerator.Value;
                    outputValues[3] = (outputAttribute as SSAGenerator.SymbolTable).Tables["w"].Value as SSAGenerator.Value;
                }

                var nodes = ssaGenerator.GatherNodes(outputValues);

                var registerOffset = 0;
                var validNodeCount = 0;

                foreach (var node in nodes)
                {
                    if (node is SSAGenerator.NodePredefined)
                    {
                        var node_ = node as SSAGenerator.NodePredefined;
                        foreach (var v in node.Outputs)
                        {
                            v.RegisterIndex = GetInputIndex(node_.Value);
                        }
                    }
                    else
                    {
                        foreach (var v in node.Outputs)
                        {
                            v.RegisterIndex = registerOffset;
                            registerOffset += 1;
                        }
                        validNodeCount++;
                    }
                }

                // Operators
                List <byte[]> dataOp = new List <byte[]>();

                foreach (var node in nodes)
                {
                    if (node is SSAGenerator.NodeConstantNumber)
                    {
                        var node_ = node as SSAGenerator.NodeConstantNumber;

                        dataOp.Add(BitConverter.GetBytes((int)OperatorType.Constant));
                        dataOp.Add(BitConverter.GetBytes((int)node.Inputs.Count));
                        dataOp.Add(BitConverter.GetBytes((int)node.Outputs.Count));
                        dataOp.Add(BitConverter.GetBytes(1));

                        foreach (var o in node.Inputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        foreach (var o in node.Outputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        dataOp.Add(BitConverter.GetBytes(node_.Value));
                    }
                    else if (node is SSAGenerator.NodePredefined)
                    {
                        continue;
                    }
                    else if (node is SSAGenerator.NodeOperator)
                    {
                        var node_ = node as SSAGenerator.NodeOperator;

                        if (node_.Type == OperatorType.Rand)
                        {
                            runningPhase = RunningPhaseType.Local;
                        }

                        dataOp.Add(BitConverter.GetBytes((int)node_.Type));
                        dataOp.Add(BitConverter.GetBytes((int)node.Inputs.Count));
                        dataOp.Add(BitConverter.GetBytes((int)node.Outputs.Count));
                        dataOp.Add(BitConverter.GetBytes(0));

                        foreach (var o in node.Inputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        foreach (var o in node.Outputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }
                    }
                }

                int version = 0;
                data.Add(BitConverter.GetBytes(version));
                data.Add(BitConverter.GetBytes((int)runningPhase));
                data.Add(BitConverter.GetBytes(registerOffset));
                data.Add(BitConverter.GetBytes(validNodeCount));
                data.Add(BitConverter.GetBytes(outputValues[0].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[1].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[2].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[3].RegisterIndex));
                data.Add(dataOp.SelectMany(_ => _).ToArray());
            }
            catch (CompileException e)
            {
                compileResult.Error = e;
                return(compileResult);
            }

            compileResult.Bytecode     = data.SelectMany(_ => _).ToArray();
            compileResult.RunningPhase = runningPhase;
            return(compileResult);
        }
Beispiel #2
0
        public CompileResult Compile(string code)
        {
            CompileResult compileResult = new CompileResult();

            runningPhase = RunningPhaseType.Global;

            if (string.IsNullOrEmpty(code))
            {
                return(compileResult);
            }

            // export
            List <byte[]> data = new List <byte[]>();

            try
            {
                var lexer  = new Lexer();
                var tokens = lexer.Analyze(code);

                var parser   = new Parser();
                var sentense = parser.Parse(tokens.Tokens);

                var ssaGenerator = new SSAGenerator();
                ssaGenerator.Eval(sentense);
                var outputAttribute = ssaGenerator.RootTable.Tables["@O"].Value;
                var outputValues    = new SSAGenerator.Value[4];
                if (outputAttribute is SSAGenerator.Value)
                {
                    outputValues[0] = outputAttribute as SSAGenerator.Value;
                    outputValues[1] = outputAttribute as SSAGenerator.Value;
                    outputValues[2] = outputAttribute as SSAGenerator.Value;
                    outputValues[3] = outputAttribute as SSAGenerator.Value;
                }
                else if (outputAttribute is SSAGenerator.SymbolTable)
                {
                    outputValues[0] = (outputAttribute as SSAGenerator.SymbolTable).Tables["x"].Value as SSAGenerator.Value;
                    outputValues[1] = (outputAttribute as SSAGenerator.SymbolTable).Tables["y"].Value as SSAGenerator.Value;
                    outputValues[2] = (outputAttribute as SSAGenerator.SymbolTable).Tables["z"].Value as SSAGenerator.Value;
                    outputValues[3] = (outputAttribute as SSAGenerator.SymbolTable).Tables["w"].Value as SSAGenerator.Value;
                }

                var nodes = ssaGenerator.GatherNodes(outputValues);

                var registerOffset = 0;
                var validNodeCount = 0;

                foreach (var node in nodes)
                {
                    if (node is SSAGenerator.NodePredefined)
                    {
                        var node_ = node as SSAGenerator.NodePredefined;
                        foreach (var v in node.Outputs)
                        {
                            v.RegisterIndex = GetInputIndex(node_.Value);
                        }
                    }
                    else
                    {
                        foreach (var v in node.Outputs)
                        {
                            v.RegisterIndex = registerOffset;
                            registerOffset += 1;
                        }
                        validNodeCount++;
                    }
                }

                // Operators
                List <byte[]> dataOp = new List <byte[]>();

                foreach (var node in nodes)
                {
                    if (node is SSAGenerator.NodeConstantNumber)
                    {
                        var node_ = node as SSAGenerator.NodeConstantNumber;

                        dataOp.Add(BitConverter.GetBytes((int)OperatorType.Constant));
                        dataOp.Add(BitConverter.GetBytes((int)node.Inputs.Count));
                        dataOp.Add(BitConverter.GetBytes((int)node.Outputs.Count));
                        dataOp.Add(BitConverter.GetBytes(1));

                        foreach (var o in node.Inputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        foreach (var o in node.Outputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        dataOp.Add(BitConverter.GetBytes(node_.Value));
                    }
                    else if (node is SSAGenerator.NodePredefined)
                    {
                        continue;
                    }
                    else if (node is SSAGenerator.NodeOperator)
                    {
                        var node_ = node as SSAGenerator.NodeOperator;

                        dataOp.Add(BitConverter.GetBytes((int)node_.Type));
                        dataOp.Add(BitConverter.GetBytes((int)node.Inputs.Count));
                        dataOp.Add(BitConverter.GetBytes((int)node.Outputs.Count));
                        dataOp.Add(BitConverter.GetBytes(0));

                        foreach (var o in node.Inputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }

                        foreach (var o in node.Outputs)
                        {
                            dataOp.Add(BitConverter.GetBytes((int)o.RegisterIndex));
                        }
                    }
                }

                int version = 0;
                data.Add(BitConverter.GetBytes(version));
                data.Add(BitConverter.GetBytes((int)runningPhase));
                data.Add(BitConverter.GetBytes(registerOffset));
                data.Add(BitConverter.GetBytes(validNodeCount));
                data.Add(BitConverter.GetBytes(outputValues[0].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[1].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[2].RegisterIndex));
                data.Add(BitConverter.GetBytes(outputValues[3].RegisterIndex));
                data.Add(dataOp.SelectMany(_ => _).ToArray());

                /*
                 * Compile(sentense[0]);
                 *
                 * Dictionary<string, int> variableList = new Dictionary<string, int>();
                 * foreach (var opt in operators)
                 * {
                 *      foreach (var o in opt.Inputs.Concat(opt.Outputs))
                 *      {
                 *              if (IsValidLabel(o)) continue;
                 *
                 *              if (!variableList.ContainsKey(o))
                 *              {
                 *                      variableList.Add(o, variableList.Count);
                 *              }
                 *      }
                 * }
                 *
                 * int outputIndex = -1;
                 *
                 * // Output register
                 * var outputName = GetOutputName(sentense[0]);
                 * if (variableList.ContainsKey(outputName))
                 * {
                 *      outputIndex = variableList[outputName];
                 * }
                 * else
                 * {
                 *      outputIndex = GetInputIndex(outputName);
                 * }
                 *
                 * // Operators
                 * List<byte[]> dataOp = new List<byte[]>();
                 *
                 * foreach (var op in operators)
                 * {
                 *      dataOp.Add(BitConverter.GetBytes((int)op.Type));
                 *      dataOp.Add(BitConverter.GetBytes((int)op.Inputs.Count));
                 *      dataOp.Add(BitConverter.GetBytes((int)op.Outputs.Count));
                 *
                 *      // Attribute
                 *      if (op.Type == OperatorType.Constant)
                 *      {
                 *              dataOp.Add(BitConverter.GetBytes(1));
                 *      }
                 *      else
                 *      {
                 *              dataOp.Add(BitConverter.GetBytes(0));
                 *      }
                 *
                 *      foreach (var o in op.Inputs)
                 *      {
                 *              if (variableList.ContainsKey(o))
                 *              {
                 *                      var index = variableList[o];
                 *                      dataOp.Add(BitConverter.GetBytes(index));
                 *              }
                 *              else
                 *              {
                 *                      var index = GetInputIndex(o);
                 *                      dataOp.Add(BitConverter.GetBytes(index));
                 *              }
                 *      }
                 *
                 *      foreach (var o in op.Outputs)
                 *      {
                 *              if (variableList.ContainsKey(o))
                 *              {
                 *                      var index = variableList[o];
                 *                      dataOp.Add(BitConverter.GetBytes(index));
                 *              }
                 *              else
                 *              {
                 *                      var index = GetInputIndex(o);
                 *                      dataOp.Add(BitConverter.GetBytes(index));
                 *              }
                 *      }
                 *
                 *      // Attribute
                 *      if (op.Type == OperatorType.Constant)
                 *      {
                 *              var value = (float)op.Attributes["Constant"];
                 *              dataOp.Add(BitConverter.GetBytes(value));
                 *      }
                 * }
                 *
                 * int version = 0;
                 * data.Add(BitConverter.GetBytes(version));
                 * data.Add(BitConverter.GetBytes((int)runningPhase));
                 * data.Add(BitConverter.GetBytes(variableList.Count));
                 * data.Add(BitConverter.GetBytes(operators.Count));
                 * data.Add(BitConverter.GetBytes(outputIndex));
                 * data.Add(dataOp.SelectMany(_=>_).ToArray());
                 */
            }
            catch (CompileException e)
            {
                compileResult.Error = e;
                return(compileResult);
            }

            compileResult.Bytecode     = data.SelectMany(_ => _).ToArray();
            compileResult.RunningPhase = runningPhase;
            return(compileResult);
        }