Esempio n. 1
0
    public void ParseIntermediateCode(string filename)
    {
        bool var_flag   = true;
        int  list_count = 0;
        uint pinID      = 0;

        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

        using (StreamReader streamReader = new StreamReader(fs, Encoding.Default))
        {
            int    lineno = 0;
            string line   = String.Empty;
            while ((line = streamReader.ReadLine()) != null)
            {
                if (line == "[" && list_count == 0)
                {
                    var_flag = false;
                    list_count++;
                    line = streamReader.ReadLine();
                }

                if (var_flag)
                {
                    string[] tokens = line.Split(':');
                    if (tokens[0] != null && tokens[1] != null)
                    {
                        switch (tokens[0])
                        {
                        case "GameObject":
                            symbolTable.Add <GameObject>(tokens[1]);
                            break;

                        case "Component3D":
                            symbolTable.Add <_3DObject>(tokens[1]);
                            break;

                        case "Integer":
                            symbolTable.Add <int>(tokens[1]);
                            break;

                        case "Number":
                            symbolTable.Add <float>(tokens[1]);
                            break;

                        case "ProjectionMap":
                            symbolTable.Add <GameObject>(tokens[1]);
                            break;

                        case "String":
                            symbolTable.Add <string>(tokens[1]);
                            break;

                        case "Bool":
                            symbolTable.Add <bool>(tokens[1]);
                            break;

                        case "ComponentText":
                            symbolTable.Add <Text>(tokens[1]);
                            break;

                        case "Vector":
                            symbolTable.Add <Vector3>(tokens[1]);
                            break;

                        case "Image":
                            symbolTable.Add <Image>(tokens[1]);
                            break;

                        default:
                            Debug.LogError("Invalid type in Symbol Table Entry");
                            break;
                        }
                    }
                }
                else //if node_flag
                {
                    if (line == "{")
                    {
                        string  internal_node_name = streamReader.ReadLine();
                        string  displayname        = streamReader.ReadLine();
                        dynamic nd = createNode(internal_node_name);
                        ((Node)nd).displayname     = displayname;
                        ((Node)nd).internalname    = internal_node_name;
                        ((Node)nd).scriptContainer = this;
                        nodes.Add(nd);

                        string   node_type = streamReader.ReadLine();
                        NodeType nt        = NodeType.Event;
                        switch (node_type)
                        {
                        case "Constant":
                            nt = NodeType.Constant;
                            break;

                        case "Event":
                            nt = NodeType.Event;
                            break;

                        case "FlowNode":
                            nt = NodeType.FlowNode;
                            break;

                        case "Function":
                            nt = NodeType.Function;
                            break;

                        case "Getter":
                            nt = NodeType.Getter;
                            break;

                        case "Setter":
                            nt = NodeType.Setter;
                            break;

                        case "Intermediate":
                            nt = NodeType.Intermediate;
                            break;

                        default:
                            Debug.LogError("Invalid Node Type " + node_type);
                            break;
                        }

                        while (streamReader.ReadLine() != "[")
                        {
                            ;                                    //ignore graphical data
                        }
                        line  = streamReader.ReadLine();
                        pinID = 0;
                        while (line != "]") //read input pins
                        {
                            if (lineno++ >= 2000)
                            {
                                Debug.Log(line);
                                Debug.Log("ERROR");
                                return;
                            }
                            int colonpos   = line.IndexOf(":");
                            int bracketpos = line.IndexOf(" (");

                            // string pin_name = line.Substring(0, colonpos);
                            string pindt = line.Substring(colonpos + 1, bracketpos - colonpos - 1);
                            if (pindt != "Execution") //save pins if not execution
                            {
                                uint   node_index, pin_index, datatype_index_start, datatype_index_end;
                                string datatype;

                                //parse line to find node and pin
                                int p  = (line.IndexOf("node", bracketpos) + 4);
                                int p2 = (line.IndexOf("pin", p));
                                int p3 = (line.IndexOf(")", p2));
                                node_index = Convert.ToUInt32(line.Substring(p, p2 - p - 2));
                                pin_index  = Convert.ToUInt32(line.Substring(p2 + 3, p3 - p2 - 3));

                                PinDataType pdt = PinDataType.Number; //Default

                                switch (pindt)
                                {
                                case "GameObject":
                                    pdt = PinDataType.GameObject;
                                    break;

                                case "Component3D":
                                    pdt = PinDataType.Component3D;
                                    break;

                                case "Integer":
                                    pdt = PinDataType.Integer;
                                    break;

                                case "Number":
                                    pdt = PinDataType.Number;
                                    break;

                                case "ProjectionMap":
                                    pdt = PinDataType.ProjectionMap;
                                    break;

                                case "String":
                                    pdt = PinDataType.String;
                                    break;

                                case "Bool":
                                    pdt = PinDataType.Bool;
                                    break;

                                case "ComponentText":
                                    pdt = PinDataType.ComponentText;
                                    break;

                                case "Image":
                                    pdt = PinDataType.Image;
                                    break;

                                case "VariableReference":
                                    pdt = PinDataType.VariableReference;
                                    break;

                                case "Vector":
                                    pdt = PinDataType.Vector;
                                    break;

                                default:
                                    Debug.LogError("Invalid Pin Datatype: " + pindt);
                                    break;
                                }

                                nd.setInputPin((int)pinID++, node_index, pin_index, pdt);
                            }
                            line = streamReader.ReadLine();
                        }

                        streamReader.ReadLine(); //ignore '['

                        line  = streamReader.ReadLine();
                        pinID = 0;
                        while (line != "]") //read ouput pins
                        {
                            if (lineno++ >= 2000)
                            {
                                Debug.Log(line);
                                Debug.Log("ERROR");
                                return;
                            }

                            int colonpos   = line.IndexOf(":");
                            int bracketpos = line.IndexOf(" [");

                            string pindt = line.Substring(colonpos + 1, bracketpos - colonpos - 1);
                            if (pindt == "Execution") //save pins only if execution
                            {
                                uint node_index, pin_index;

                                if (line.IndexOf("(") != -1)
                                {
                                    //parse line to find node and pin
                                    int    p         = (line.IndexOf("node") + 4);
                                    int    p2        = (line.IndexOf("pin", p));
                                    int    p3        = (line.IndexOf(")", p2));
                                    string nodeidstr = line.Substring(p, p2 - p - 2);
                                    string pinidstr  = line.Substring(p2 + 3, p3 - p2 - 3);
                                    node_index = Convert.ToUInt32(nodeidstr);
                                    pin_index  = Convert.ToUInt32(pinidstr);

                                    //parse line to find node and pin
                                    nd.setOutputPin((int)pinID++, node_index, pin_index);
                                }
                            }

                            line = streamReader.ReadLine();
                        }

                        streamReader.ReadLine(); //ignore '['

                        line  = streamReader.ReadLine();
                        pinID = 0;
                        while (line != "]") //read parameters
                        {
                            if (lineno++ >= 2000)
                            {
                                Debug.Log(line);
                                Debug.Log("ERROR");
                                return;
                            }

                            int    p1       = line.IndexOf(":");
                            int    p2       = line.IndexOf(":", p1 + 1);
                            string typename = line.Substring(p1 + 1, p2 - p1 - 1);
                            string value    = line.Substring(p2 + 1);
                            switch (typename)
                            {
                            case "Integer": {
                                int intval            = Convert.ToInt32(value);
                                ConstantNode <int> cn = (ConstantNode <int>)nd;
                                cn.setConstant(intval);
                                break;
                            }

                            case "Number": {
                                float floatval          = float.Parse(value);
                                ConstantNode <float> cn = (ConstantNode <float>)nd;
                                cn.setConstant(floatval);
                                break;
                            }

                            case "String": {
                                ConstantNode <string> cn = (ConstantNode <string>)nd;
                                cn.setConstant(value);
                                break;
                            }

                            case "Bool": {
                                bool boolval           = value == "on";
                                ConstantNode <bool> cn = (ConstantNode <bool>)nd;
                                cn.setConstant(boolval);
                                break;
                            }

                            case "Vector": {
                                int    pc1 = value.IndexOf(",");
                                int    pc2 = value.LastIndexOf(",");
                                string x   = value.Substring(0, pc1);
                                string y   = value.Substring(pc1 + 1, pc2 - pc1 - 1);
                                string z   = value.Substring(pc2 + 1);
                                float  xf  = float.Parse(x);
                                float  yf  = float.Parse(y);
                                float  zf  = float.Parse(z);
                                ConstantNode <Vector3> cn = (ConstantNode <Vector3>)nd;
                                cn.setConstant(new Vector3(xf, yf, zf));
                                break;
                            }

                            case "VariableReference": {
                                if (nt == NodeType.Getter)
                                {
                                    GetterNode gn = (GetterNode)nd;
                                    gn.setVarName(value);
                                }
                                else if (nt == NodeType.Setter)
                                {
                                    SetterNode sn = (SetterNode)nd;
                                    sn.setVarName(value);
                                }
                                else
                                {
                                    Debug.Log("Unexpected setter!");
                                }
                                break;
                            }

                            default:
                                Debug.LogError("Invalid Parameter Datatype");
                                break;
                            }

                            line = streamReader.ReadLine();
                        }
                        streamReader.ReadLine(); //ignore "}"
                    }
                }
            }
        }


        foreach (Node n in nodes)
        {
            n.setPinValues();
        }
    }
 public void Visit(SetterNode node)
 {
     IndentedAppend($"{node.Name} = ");
     node.Node.Accept(this);
     AppendLine(";");
 }
Esempio n. 3
0
 public void Visit(SetterNode node)
 {
     StringBuilder.Append($"setState(");
     node.Node.Accept(this);
     StringBuilder.Append(") ");
 }