Exemplo n.º 1
0
 /// <summary>if伪函数表达式</summary>
 public static Expr CreateIf(IfExpr Value)
 {
     return(new Expr {
         _Tag = ExprTag.If, If = Value
     });
 }
Exemplo n.º 2
0
        private Expr TranslateExpr(SyntaxExpr e)
        {
            Expr m;

            if (e.OnLiteral)
            {
                m = Expr.CreateLiteral(TranslateLiteral(e.Literal.Literal));
            }
            else if (e.OnFunction)
            {
                var Parameters = new LinkedList <Expr>();
                var p          = e.Function.ParameterList;
                if (p.OnNull)
                {
                }
                else if (p.OnNonnull)
                {
                    var n = p.Nonnull.NonnullParameterList;
                    while (true)
                    {
                        if (n.OnSingle)
                        {
                            Parameters.AddFirst(TranslateExpr(n.Single.Expr));
                            break;
                        }
                        else if (n.OnMultiple)
                        {
                            Parameters.AddFirst(TranslateExpr(n.Multiple.Expr));
                            n = n.Multiple.NonnullParameterList;
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException();
                }
                var Name = e.Function.Identifier.Name;
                if (Name == "if" && Parameters.Count == 3)
                {
                    var ie = new IfExpr {
                        Condition = Parameters.First.Value, TruePart = Parameters.First.Next.Value, FalsePart = Parameters.First.Next.Next.Value
                    };
                    Positions.Add(ie, Positions[e]);
                    m = Expr.CreateIf(ie);
                }
                else
                {
                    var fe = new FunctionExpr {
                        Name = Name, Parameters = Parameters.ToList()
                    };
                    Positions.Add(fe, Positions[e]);
                    m = Expr.CreateFunction(fe);
                }
            }
            else if (e.OnVariable)
            {
                var ve = new VariableExpr {
                    Name = e.Variable.Identifier.Name
                };
                Positions.Add(ve, Positions[e]);
                m = Expr.CreateVariable(ve);
            }
            else if (e.OnParen)
            {
                return(TranslateExpr(e.Paren.Expr));
            }
            else if (e.OnUnaryOperator)
            {
                var fe = new FunctionExpr {
                    Name = e.UnaryOperator.UnaryOperator.Name, Parameters = new List <Expr> {
                        TranslateExpr(e.UnaryOperator.Expr)
                    }
                };
                Positions.Add(fe, Positions[e]);
                m = Expr.CreateFunction(fe);
            }
            else if (e.OnBinaryOperator)
            {
                var Name = e.BinaryOperator.BinaryOperator.Name;
                if (Name == "&&")
                {
                    var aae = new AndAlsoExpr {
                        Left = TranslateExpr(e.BinaryOperator.Left), Right = TranslateExpr(e.BinaryOperator.Right)
                    };
                    Positions.Add(aae, Positions[e]);
                    m = Expr.CreateAndAlso(aae);
                }
                else if (Name == "||")
                {
                    var oee = new OrElseExpr {
                        Left = TranslateExpr(e.BinaryOperator.Left), Right = TranslateExpr(e.BinaryOperator.Right)
                    };
                    Positions.Add(oee, Positions[e]);
                    m = Expr.CreateOrElse(oee);
                }
                else
                {
                    var fe = new FunctionExpr {
                        Name = Name, Parameters = new List <Expr> {
                            TranslateExpr(e.BinaryOperator.Left), TranslateExpr(e.BinaryOperator.Right)
                        }
                    };
                    Positions.Add(fe, Positions[e]);
                    m = Expr.CreateFunction(fe);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
            Positions.Add(m, Positions[e]);
            return(m);
        }