コード例 #1
0
 public static bool declareArray(CMArray vari, ref IdentiTable table)
 {
     foreach (Variables declared in table.declaredVaries)
     {
         if (declared.name == vari.name)
         {
             return(false);
         }
     }
     table.declaredVaries.Add(vari);
     return(true);
 }
コード例 #2
0
        public static Variables FetchArray(CMArray vari, string i, IdentiTable table)
        {
            CMArray array = (CMArray)FetchVal(vari, table);
            int     index = int.Parse(i);

            array.Lookup(index);
            if (array.Initialized)
            {
                if (array.currIndex >= array.Maxindex)
                {
                    UIHelper.Log("Semantic Error: array overflow");
                    Thread.CurrentThread.Abort();
                }
                return(array.currVari);
            }
            else
            {
                array.currIndex = index;
                return(array);
            }
        }
コード例 #3
0
        //Analyze semantics in current Node.
        public void SemanticAnalysis()
        {
            if (this.thisTok.ttype == Parser.Ttype.Terminal)
            {
                return;
            }
            UnifyScope();
            AnnoTree.NodeOnExecute = this;
            WaitforCommand();
            switch (this.mProduction.SDTRule[1])
            {
            case "fetch":
            {
                geneTok[0].SemanticAnalysis();
                this.mOp.setValue(geneTok[0].mOp.getValue());
                break;
            }

            case "Parseexpr":
            {
                SDTranslator.ParseExpr(this);
                break;
            }

            case "assign":
            {
                this.geneTok[0].SemanticAnalysis();
                this.geneTok[2].SemanticAnalysis();
                this.geneTok[0].mOp.setValue(this.geneTok[2].mOp.getValue());
                break;
            }

            case "getMem":    //fetch an Identifier
            {
                this.mOp = IdentiTable.FetchVal((Variables)this.geneTok[0].mOp, this.varTable);
                break;
            }

            case "getPoint":
            {
                this.geneTok[2].SemanticAnalysis();
                CMArray   arrayname = new CMArray();
                Variables varname   = (Variables)this.geneTok[0].mOp;
                arrayname.name      = varname.name;
                arrayname.Type      = varname.Type;
                this.geneTok[0].mOp = arrayname;
                this.mOp            = IdentiTable.FetchArray((CMArray)this.geneTok[0].mOp, this.geneTok[2].mOp.getValue(), this.varTable);
                break;
            }

            case "addID":
            {
                this.geneTok[1].SemanticAnalysis();

                if (geneTok[1].mOp.GetType().Name == "Variables")
                {
                    Variables newVari = new Variables();
                    newVari.Type = this.geneTok[0].mOp.getValue();
                    newVari.name = ((Variables)this.geneTok[1].mOp).name;

                    if (IdentiTable.declareVariable(newVari, ref this.varTable) == false)
                    {
                        UIHelper.Log("Semantic Error: double declare");
                        Thread.CurrentThread.Abort();
                    }
                    this.mOp = newVari;
                }
                else
                {
                    CMArray newArray = new CMArray();
                    newArray.name      = ((CMArray)this.geneTok[1].mOp).name;
                    newArray.Type      = this.geneTok[0].mOp.getValue();
                    newArray.currIndex = ((CMArray)this.geneTok[1].mOp).currIndex;
                    newArray.initArray(newArray.currIndex);
                    if (IdentiTable.declareArray(newArray, ref this.varTable) == false)
                    {
                        UIHelper.Log("Semantic Error: double declare");
                        Thread.CurrentThread.Abort();
                    }
                    this.mOp = newArray;
                }
                break;
            }

            case "for":
            {
                this.geneTok[2].SemanticAnalysis();
                this.geneTok[4].SemanticAnalysis();
                while (booleanBranch(geneTok[4].mOp.getValue()))
                {
                    geneTok[8].SemanticAnalysis();
                    geneTok[6].SemanticAnalysis();
                    geneTok[4].SemanticAnalysis();
                }

                break;
            }

            case "ifcon":
            {
                this.geneTok[2].SemanticAnalysis();
                if (booleanBranch(geneTok[2].mOp.getValue()))
                {
                    this.geneTok[4].SemanticAnalysis();
                }
                this.mOp.setValue("1");
                break;
            }

            case "elsecon":
            {
                geneTok[0].SemanticAnalysis();
                if (booleanBranch(geneTok[0].mOp.getValue()))
                {
                    break;
                }
                else
                {
                    geneTok[2].SemanticAnalysis();
                }
                break;
            }

            case "while":
            {
                geneTok[2].SemanticAnalysis();
                while (booleanBranch(geneTok[2].mOp.getValue()))
                {
                    this.geneTok[4].SemanticAnalysis();
                }
                break;
            }

            case "pass":
            {
                geneTok[0].SemanticAnalysis();
                break;
            }

            case "addTable":
            {
                geneTok[1].varTable        = new IdentiTable();
                geneTok[1].varTable.Parent = this.varTable;
                geneTok[1].SemanticAnalysis();
                IdentiTable.allTables.RemoveAt(IdentiTable.allTables.Count - 1);
                break;
            }

            case "pass2":
            {
                geneTok[0].SemanticAnalysis();
                geneTok[1].SemanticAnalysis();
                break;
            }

            case "input":
            {
                this.geneTok[2].SemanticAnalysis();
                while (AnnoTree.AttribQueue.Count != 0)
                {
                    Variables frontVari = AnnoTree.AttribQueue.Dequeue();
                    frontVari.setValue(UIHelper.getInput());
                }
                break;
            }

            case "pushStack":
            {
                this.geneTok[0].SemanticAnalysis();
                AnnoTree.AttribQueue.Enqueue((Variables)this.geneTok[0].mOp);
                if (this.geneTok.Length >= 2)
                {
                    this.geneTok[2].SemanticAnalysis();
                }
                break;
            }

            case "output":
            {
                this.geneTok[2].SemanticAnalysis();
                while (AnnoTree.AttribQueue.Count != 0)
                {
                    UIHelper.Output(AnnoTree.AttribQueue.Dequeue().getValue());
                }
                break;
            }

            default:
                break;
            }
        }