Пример #1
0
 public YuSymbolTableRow(ESymbolType stype, YuToken token)
 {
     this.SymbolType   = stype;
     this.SymbolToken  = token;
     this.ProcTree     = null;
     this.AbsoluteAddr = -1;
 }
Пример #2
0
 public YuSymbolTableRow(ESymbolType stype, YuToken token, YuTreeNode tree)
 {
     this.SymbolType   = stype;
     this.SymbolToken  = token;
     this.ProcTree     = tree;
     this.AbsoluteAddr = -1;
 }
Пример #3
0
        public YuTreeNode ExpStat()
        {
            YuToken    a   = this.Term();
            YuToken    exp = null;
            YuToken    b   = null;
            YuTreeNode ret = null;

            while (this.CurrToken.Type != ETokenType.SemiColon && this.CurrToken.Type != ETokenType.Eof)
            {
                exp = this.ExpAdd();
                b   = this.Term();

                if (ret == null)
                {
                    ret = new YuTreeNode(exp);
                    ret.Children.Add(new YuTreeNode(a));
                    ret.Children.Add(new YuTreeNode(b));
                }
                else
                {
                    YuTreeNode tmp = new YuTreeNode(exp);
                    tmp.Children.Add(ret);
                    tmp.Children.Add(new YuTreeNode(b));
                    ret = tmp;
                }
            }
            this.SemiColon();

            if (exp == null)
            {
                ret = new YuTreeNode(a);
            }

            return(ret);
        }
Пример #4
0
        private void RecTree(YuTreeNode node)
        {
            if (node.Children[0].Token.Type == ETokenType.ExpAdd)
            {
                this.RecTree(node.Children[0]);
            }
            else
            {
                if (node.Children[0].Token.Type == ETokenType.Symbol)
                {
                    YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token);
                    if (this.SymbolTable.IsContain(row) == false)
                    {
                        throw new YuSymbolUndefinedException();
                    }

                    VMOp op = new VMOp(EOpCode.PushAddr, -1);
                    this.OpLst.Add(op);
                    this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token));
                }
                else
                {
                    this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[0].Token.Text)));
                }
            }

            if (node.Children[1].Token.Type == ETokenType.ExpAdd)
            {
                this.RecTree(node.Children[1]);
            }
            else
            {
                if (node.Children[1].Token.Type == ETokenType.Symbol)
                {
                    YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[1].Token);
                    if (this.SymbolTable.IsContain(row) == false)
                    {
                        throw new YuSymbolUndefinedException();
                    }

                    VMOp op = new VMOp(EOpCode.PushAddr, -1);
                    this.OpLst.Add(op);
                    this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[1].Token));
                }
                else
                {
                    this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Token.Text)));
                }
            }

            this.OpLst.Add(new VMOp(EOpCode.Add, 0));
        }
Пример #5
0
        /*
         * public YuTreeNode CalcStat()
         * {
         *  YuToken a = this.Number();
         *  YuToken b = this.ExpAdd();
         *  YuToken c = this.Number();
         *  this.SemiColon();
         *
         *  YuTreeNode ret = new YuTreeNode(b);
         *  ret.Children.Add(new YuTreeNode(a));
         *  ret.Children.Add(new YuTreeNode(c));
         *
         *  return ret;
         * }
         *
         * public YuTreeNode VCalcStat()
         * {
         *  YuToken a = this.Symbol();
         *  YuToken b = this.ExpAdd();
         *  YuToken c = this.Number();
         *  this.SemiColon();
         *
         *  YuTreeNode ret = new YuTreeNode(b);
         *  ret.Children.Add(new YuTreeNode(a));
         *  ret.Children.Add(new YuTreeNode(c));
         *
         *  return ret;
         * }
         */

        public YuTreeNode VarDefStat()
        {
            YuToken a = this.Var();
            YuToken b = this.Symbol();

            this.SemiColon();

            YuTreeNode ret = new YuTreeNode(a);

            ret.Children.Add(new YuTreeNode(b));

            return(ret);
        }
Пример #6
0
        public void TranslateTree(YuTreeNode node)
        {
            if (node.Token.Type == ETokenType.ExpAdd)
            {
                this.RecTree(node);
            }
            else if (node.Token.Type == ETokenType.Var)
            {
                YuSymbolTableRow sr = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token);
                this.SymbolTable.Add(sr);
            }
            else if (node.Token.Type == ETokenType.Equal)
            {
                /*
                 * if (node.Children[1].Children[0].Token.Type == ETokenType.Number)
                 * {
                 *  this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Children[0].Token.Text)));
                 *  this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Children[1].Token.Text)));
                 *  this.OpLst.Add(new VMOp(EOpCode.Add, 0));
                 * }
                 */
                this.RecTree(node.Children[1]);

                YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token);
                if (this.SymbolTable.IsContain(row) == false)
                {
                    throw new YuSymbolUndefinedException();
                }

                VMOp op = new VMOp(EOpCode.Pop, -1);

                this.OpLst.Add(op);
                this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token));
            }
            else if (node.Token.Type == ETokenType.Def)
            {
                YuSymbolTableRow sr = new YuSymbolTableRow(ESymbolType.Proc, node.Children[0].Token, node);
                this.SymbolTable.Add(sr);
            }
            else if (node.Token.Type == ETokenType.Call)
            {
                VMOp op = new VMOp(EOpCode.Call, -1);
                this.OpLst.Add(op);
                this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token));
            }
            else if (node.Token.Type == ETokenType.Eof)
            {
                this.OpLst.Add(new VMOp(EOpCode.End, 0));
            }
        }
Пример #7
0
        public YuTreeNode CallStat()
        {
            YuToken a = this.Call();
            YuToken b = this.Symbol();

            this.LParen();
            this.RParen();
            this.SemiColon();

            YuTreeNode ret = new YuTreeNode(a);

            ret.Children.Add(new YuTreeNode(b));

            return(ret);
        }
Пример #8
0
        public YuTreeNode ProcDefStat()
        {
            YuToken a = this.Def();
            YuToken b = this.Symbol();

            YuTreeNode ret = new YuTreeNode(a);

            ret.Children.Add(new YuTreeNode(b));
            this.LBrace();

            while (this.CurrToken.Type != ETokenType.RBrace && this.CurrToken.Type != ETokenType.Eof)
            {
                ret.Children.Add(this.Statement());
            }

            this.RBrace();

            return(ret);
        }
Пример #9
0
        public YuTreeNode AssignStat()
        {
            this.Let();
            YuToken    a = this.Symbol();
            YuToken    b = this.Equal();
            YuTreeNode n = this.ExpStat();

            /*
             * if (this.CurrToken.Type == ETokenType.Number)
             *  n = this.CalcStat();
             * else
             *  n = this.VCalcStat();
             */


            YuTreeNode ret = new YuTreeNode(b);

            ret.Children.Add(new YuTreeNode(a));
            ret.Children.Add(n);

            return(ret);
        }