Ejemplo n.º 1
0
        public void ExpressionCalculatorUnary()
        {
            var ast = new AstUnary("-", new AstConstant(1));
            var f   = MakeFunc <Func <object> >(ast);

            Assert.AreEqual(-1, f());
        }
Ejemplo n.º 2
0
        public Expression CompileUnOp(AstUnary uo)
        {
            var operand = CompileExpression(uo.Operand);

            if (operand.IsInvalid)
            {
                return(Expression.Invalid);
            }

            switch (uo.Type)
            {
            case AstUnaryType.IncreasePrefix:
            case AstUnaryType.DecreasePrefix:
            case AstUnaryType.IncreasePostfix:
            case AstUnaryType.DecreasePostfix:
                return(!operand.ReturnType.IsIntegralType && !operand.ReturnType.IsFloatingPointType
                        ? Error(uo.Source, ErrorCode.E2063, "Incremental operators can only be used on integers")
                        : new FixOp(uo.Source, (FixOpType)uo.Type, operand));
            }

            var args = new[] { operand };
            var opOp = uo.Type.ToSymbol();
            var op   = TryResolveOperatorOverload(uo.Source, NameResolver.GetTypeOperators(operand.ReturnType, opOp), args);

            return(op != null
                ? new CallUnOp(uo.Source, op, args[0])
                : Error(uo.Source, ErrorCode.E2065, operand.ReturnType.Quote() + " has no operators matching the argument list"));
        }
Ejemplo n.º 3
0
            public Expression Visit(AstUnary unary)
            {
                switch (unary.Name)
                {
                case "-":
                case "!":
                    return(Expression.Call(typeof(Functions).GetMethod("Negate", BindingFlags.Static | BindingFlags.Public), unary.Operand.Accept(this)));

                default:
                    throw new NotSupportedException(string.Format("Operator {0} is not supported", unary.Name));
                }
            }
Ejemplo n.º 4
0
            public object Visit(AstUnary unary)
            {
                switch (unary.Name)
                {
                case "-":
                    return(-Convert.ToDecimal(unary.Operand.Accept(this)));

                case "!":
                    return(!(bool)unary.Operand.Accept(this));

                default:
                    throw new NotSupportedException(string.Format("Unary operator is not supported: {0}", unary.Name));
                }
            }
Ejemplo n.º 5
0
 public string Visit(AstUnary unary)
 {
     return(string.Format("{0} {1}", unary.Name, unary.Operand.Accept(this)));
 }
Ejemplo n.º 6
0
 public void WriteUnary(AstUnary a)
 {
     Write(a.Source);
     Write(a.Operand);
 }
Ejemplo n.º 7
0
 public T Visit(AstUnary unary)
 {
     throw new System.NotImplementedException();
 }