Ejemplo n.º 1
0
        private void CompileBinOp(AstBinOp node, Syt syt, StringBuilder sb)
        {
            CompileRecursive(node.exprLeft, syt, sb);
            CompileRecursive(node.exprRight, syt, sb);

            switch (node.KBinop)
            {
            case KBinop.Plus: sb.AppendLine("add"); break;

            case KBinop.Minus: sb.AppendLine("sub"); break;

            case KBinop.And: sb.AppendLine("and"); break;

            case KBinop.Or: sb.AppendLine("or"); break;

            case KBinop.Lt: sb.AppendLine("lt"); break;

            case KBinop.Gt: sb.AppendLine("gt"); break;

            case KBinop.Eq: sb.AppendLine("eq"); break;

            case KBinop.Mul: sb.AppendLine("call Math.multiply 2"); break;

            case KBinop.Div: sb.AppendLine("call Math.divide 2"); break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 2
0
        private AstExpr ParseExpr()
        {
            var astResult = ParseTerm();

            while (true)
            {
                if (Accept(Ktoken.Plus))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Plus, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Minus))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Minus, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Asterix))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Mul, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Slash))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Div, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Or))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Or, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.And))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.And, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Eq))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Eq, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Gt))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Gt, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else if (Accept(Ktoken.Lt))
                {
                    astResult = new AstBinOp {
                        KBinop = KBinop.Lt, exprLeft = astResult, exprRight = ParseTerm()
                    }
                }
                ;
                else
                {
                    break;
                }
            }
            return(astResult);
        }