Exemple #1
0
        public AST term()
        {
            AST node = factor();

            while (ct.t_type == "MUL" || ct.t_type == "DIV")
            {
                token = ct;
                if (ct.t_type == "MUL")
                {
                    verify("MUL");
                }
                else if (ct.t_type == "DIV")
                {
                    verify("DIV");
                }
                node = new BinOp(node, token, factor());
            }
            return(node);
        }
Exemple #2
0
        public AST expr()
        {
            AST node = term();

            while (ct.t_type == "ADD" || ct.t_type == "SUB")
            {
                token = ct;
                if (ct.t_type == "ADD")
                {
                    verify("ADD");
                }
                else if (ct.t_type == "SUB")
                {
                    verify("SUB");
                }
                node = new BinOp(node, token, term());
            }
            return(node);
        }
Exemple #3
0
 public int visit_BinOp(BinOp node)
 {
     if (node.Op.t_type == "ADD")
     {
         return(Convert.ToInt32(visit(node.Left)) + Convert.ToInt32(visit(node.Right)));
     }
     else if (node.Op.t_type == "SUB")
     {
         return(Convert.ToInt32(visit(node.Left)) - Convert.ToInt32(visit(node.Right)));
     }
     else if (node.Op.t_type == "MUL")
     {
         return(Convert.ToInt32(visit(node.Left)) * Convert.ToInt32(visit(node.Right)));
     }
     else if (node.Op.t_type == "DIV")
     {
         return(Convert.ToInt32(visit(node.Left)) / Convert.ToInt32(visit(node.Right)));
     }
     return(0);
 }