Пример #1
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
                // ToDo: Maybe introduce a 'Duration' type to handle this!
                return(new Date(value + TimeSpan.FromDays(operand.AsDouble)));

            case BinaryOperator.Minus:
                // ToDo: Maybe introduce a 'Duration' type to handle this!
                return(new Float((value - operand.AsDateTime).TotalDays));

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(value < operand.AsDateTime));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(value <= operand.AsDateTime));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(value > operand.AsDateTime));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(value >= operand.AsDateTime));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #2
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
            {
                var result = new List <Dynamic>(list);
                result.AddRange(operand.AsList);
                return(new List(result));
            }

            case BinaryOperator.Times:
            {
                var result = new List <Dynamic>();
                int n      = operand.AsInt32;
                for (int i = 0; i < n; ++i)
                {
                    result.AddRange(list);
                }
                return(new List(result));
            }

            case BinaryOperator.Contains:
                return(Boolean.FromBool(list.Contains(operand)));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #3
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            try
            {
                switch (_operator)
                {
                case BinaryOperator.Plus:
                    return(new Integer(checked (value + operand.AsInt32)));

                case BinaryOperator.Minus:
                    return(new Integer(value - operand.AsInt32));

                case BinaryOperator.Times:
                    return(new Integer(checked (value * operand.AsInt32)));

                case BinaryOperator.Divide:
                    return(Rational.Simplify(new Rational32(value, operand.AsInt32)));

                case BinaryOperator.Modulo:
                    return(new Integer(value % operand.AsInt32));

                case BinaryOperator.Power:
                    return(new Integer(MathUtil.Power(value, operand.AsInt32)));

                case BinaryOperator.And:
                    return(new Integer(value & operand.AsInt32));

                case BinaryOperator.Or:
                    return(new Integer(value | operand.AsInt32));

                case BinaryOperator.ExclusiveOr:
                    return(new Integer(value ^ operand.AsInt32));

                case BinaryOperator.ShiftLeft:
                    return(new Integer(value << operand.AsInt32));

                case BinaryOperator.ShiftRight:
                    return(new Integer(value >> operand.AsInt32));

                case BinaryOperator.LessThan:
                    return(Boolean.FromBool(value < operand.AsInt32));

                case BinaryOperator.LessThanOrEqual:
                    return(Boolean.FromBool(value <= operand.AsInt32));

                case BinaryOperator.GreaterThan:
                    return(Boolean.FromBool(value > operand.AsInt32));

                case BinaryOperator.GreaterThanOrEqual:
                    return(Boolean.FromBool(value >= operand.AsInt32));

                default:
                    return(base.BinaryOperation(_operator, operand));
                }
            }
            catch (OverflowException)
            {
                return(new Long(value).BinaryOperation(_operator, operand));
            }
        }
Пример #4
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
                return(new Long(value + operand.AsBigInteger));

            case BinaryOperator.Minus:
                return(new Long(value - operand.AsBigInteger));

            case BinaryOperator.Times:
                return(new Long(value * operand.AsBigInteger));

            case BinaryOperator.Divide:
                return(new Long(value / operand.AsBigInteger));

            case BinaryOperator.Modulo:
                return(new Long(value % operand.AsBigInteger));

            case BinaryOperator.Power:
                return(new Long(BigInteger.Pow(value, operand.AsInt32)));

            case BinaryOperator.And:
                return(new Long(value & operand.AsBigInteger));

            case BinaryOperator.Or:
                return(new Long(value | operand.AsBigInteger));

            case BinaryOperator.ExclusiveOr:
                return(new Long(value ^ operand.AsBigInteger));

            case BinaryOperator.ShiftLeft:
                return(new Long(value << operand.AsInt32));

            case BinaryOperator.ShiftRight:
                return(new Long(value >> operand.AsInt32));

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(value < operand.AsBigInteger));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(value <= operand.AsBigInteger));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(value > operand.AsBigInteger));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(value >= operand.AsBigInteger));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #5
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
            case BinaryOperator.Or:
            {
                var result = new HashSet <Dynamic>(hashSet);
                result.UnionWith(operand.AsHashSet);
                return(new Set(result));
            }

            case BinaryOperator.Minus:
            {
                var result = new HashSet <Dynamic>(hashSet);
                result.ExceptWith(operand.AsHashSet);
                return(new Set(result));
            }

            case BinaryOperator.And:
            {
                var result = new HashSet <Dynamic>(hashSet);
                result.IntersectWith(operand.AsHashSet);
                return(new Set(result));
            }

            case BinaryOperator.ExclusiveOr:
            {
                var result = new HashSet <Dynamic>(hashSet);
                result.SymmetricExceptWith(operand.AsHashSet);
                return(new Set(result));
            }

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(hashSet.IsProperSubsetOf(operand.AsHashSet)));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(hashSet.IsSubsetOf(operand.AsHashSet)));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(hashSet.IsProperSupersetOf(operand.AsHashSet)));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(hashSet.IsSupersetOf(operand.AsHashSet)));

            case BinaryOperator.Contains:
                return(Boolean.FromBool(hashSet.Contains(operand)));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #6
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
                return(new Decimal(value + operand.AsBigDecimal));

            case BinaryOperator.Minus:
                return(new Decimal(value - operand.AsBigDecimal));

            case BinaryOperator.Times:
                return(new Decimal(value * operand.AsBigDecimal));

            case BinaryOperator.Divide:
                return(new Decimal(value / operand.AsBigDecimal));

            case BinaryOperator.Modulo:
                return(new Decimal(value % operand.AsBigDecimal));

            case BinaryOperator.Power:
                return(new Decimal(value.Power(operand.AsInt32)));

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(value < operand.AsBigDecimal));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(value <= operand.AsBigDecimal));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(value > operand.AsBigDecimal));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(value >= operand.AsBigDecimal));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #7
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
                return(new Float(value + operand.AsDouble));

            case BinaryOperator.Minus:
                return(new Float(value - operand.AsDouble));

            case BinaryOperator.Times:
                return(new Float(value * operand.AsDouble));

            case BinaryOperator.Divide:
                return(new Float(value / operand.AsDouble));

            case BinaryOperator.Modulo:
                return(new Float(MathUtil.Modulo(value, operand.AsDouble)));

            case BinaryOperator.Power:
                return(new Float(Math.Pow(value, operand.AsDouble)));

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(value < operand.AsDouble));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(value <= operand.AsDouble));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(value > operand.AsDouble));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(value >= operand.AsDouble));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #8
0
        public override Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Plus:
                return(new String(value + operand));

            case BinaryOperator.Times:
                return(new String(StringUtil.Repeat(value, operand.AsInt32)));

            case BinaryOperator.LessThan:
                return(Boolean.FromBool(string.Compare(value, operand.ToString()) < 0));

            case BinaryOperator.LessThanOrEqual:
                return(Boolean.FromBool(string.Compare(value, operand.ToString()) <= 0));

            case BinaryOperator.GreaterThan:
                return(Boolean.FromBool(string.Compare(value, operand.ToString()) > 0));

            case BinaryOperator.GreaterThanOrEqual:
                return(Boolean.FromBool(string.Compare(value, operand.ToString()) >= 0));

            case BinaryOperator.StartsWith:
                return(Boolean.FromBool(value.StartsWith(operand.ToString())));

            case BinaryOperator.EndsWith:
                return(Boolean.FromBool(value.EndsWith(operand.ToString())));

            case BinaryOperator.Contains:
                return(Boolean.FromBool(value.Contains(operand.ToString())));

            case BinaryOperator.Matches:
                return(Boolean.FromBool(StringUtil.GetRegex(operand.ToString()).IsMatch(value)));

            default:
                return(base.BinaryOperation(_operator, operand));
            }
        }
Пример #9
0
        public virtual Dynamic BinaryOperation(BinaryOperator _operator, Dynamic operand)
        {
            switch (_operator)
            {
            case BinaryOperator.Equal:
                return(Boolean.FromBool(Equals(operand)));

            case BinaryOperator.NotEqual:
                return(Boolean.FromBool(!Equals(operand)));

            case BinaryOperator.Identical:
                return(Boolean.FromBool((Class == operand.Class) && Equals(operand)));

            case BinaryOperator.NotIdentical:
                return(Boolean.FromBool(!((Class == operand.Class) && Equals(operand))));

            default:
                throw new InvalidOperationException(
                          string.Format(Resources.OperatorCantBeApplied,
                                        CodeGenerator.BinaryOperatorToString(_operator),
                                        Class.Name));
            }
        }
Пример #10
0
        /// <summary>
        /// Recognizes atomic expressions like literals, initializers,
        /// simple references, simple calls, conversions, parenthesized
        /// expressions and the <b>typeof</b> expression.
        /// </summary>
        /// <returns>An <see cref="Expression"/></returns>
        protected Expression Atom()
        {
            SkipComments();
            switch (token.TokenID)
            {
            case TokenID.LT_Null:
                return(Literal(null));

            case TokenID.LT_Boolean:
                return(Literal(Boolean.FromBool((bool)token.Value)));

            case TokenID.LT_Integer:
                return(Literal(new Integer((int)token.Value)));

            case TokenID.LT_Long:
                return(Literal(new Long((BigInteger)token.Value)));

            case TokenID.LT_Float:
                return(Literal(new Float((double)token.Value)));

            case TokenID.LT_Decimal:
                return(Literal(new Decimal((BigDecimal)token.Value)));

            case TokenID.LT_Date:
                return(Literal(new Date((DateTime)token.Value)));

            case TokenID.LT_String:
                return(Literal(new String((string)token.Value)));

            case TokenID.KW_This:
                return(ThisReference());

            case TokenID.KW_Super:
                return(AtomStartingWithSuper());

            case TokenID.KW_New:
                return(AtomStartingWithNew());

            case TokenID.KW_TypeOf:
                return(AtomStartingWithTypeOf());

            case TokenID.TypeName:
                return(AtomStartingWithTypeName());

            case TokenID.Identifier:
                return(AtomStartingWithId());

            case TokenID.LeftParenthesis:
                return(AtomStartingWithLParen());

            case TokenID.LeftBrace:
                return(AtomStartingWithLBrace());

            case TokenID.LeftBracket:
                return(ListInitializer());

            case TokenID.VerticalBar:
                return(Lambda());

            case TokenID.KW_Function:
                return(InlineFunction());

            default:
                return(null);
            }
        }
Пример #11
0
        public static Dynamic CreateDynamic(object value)
        {
            if (value == null)
            {
                return(Void.Value);
            }
            if (value is Dynamic)
            {
                return((Dynamic)value);
            }
            if (value is Enum)
            {
                return(new String(value.ToString()));
            }
            if (value is char[])
            {
                return(new String(new string((char[])value)));
            }
            if (value is byte[])
            {
                return(new String(StringUtil.ByteArray2String((byte[])value)));
            }
            if (value is Array)
            {
                var array = (Array)value;
                var list  = new List <Dynamic>();

                foreach (object item in array)
                {
                    list.Add(CreateDynamic(item));
                }

                return(new List(list));
            }

            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Boolean:
                return(Boolean.FromBool(Convert.ToBoolean(value)));

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
                return(new Integer(Convert.ToInt32(value)));

            case TypeCode.UInt32:
            case TypeCode.Int64:
                return(new Long(Convert.ToInt64(value)));

            case TypeCode.UInt64:
                return(new Long(Convert.ToUInt64(value)));

            case TypeCode.Single:
            case TypeCode.Double:
                return(new Float(Convert.ToDouble(value)));

            case TypeCode.Decimal:
                return(new Decimal(Convert.ToDecimal(value)));

            case TypeCode.DateTime:
                return(new Date(Convert.ToDateTime(value)));

            case TypeCode.Char:
            case TypeCode.String:
                return(new String(Convert.ToString(value)));

            case TypeCode.Object:
                if (value is BigInteger)
                {
                    return(new Long((BigInteger)value));
                }
                if (value is Rational32)
                {
                    return(new Rational((Rational32)value));
                }
                if (value is BigDecimal)
                {
                    return(new Decimal((BigDecimal)value));
                }
                if (value is Complex64)
                {
                    return(new Complex((Complex64)value));
                }
                if (value is List <Dynamic> )
                {
                    return(new List((List <Dynamic>)value));
                }
                if (value is Dictionary <Dynamic, Dynamic> )
                {
                    return(new Map((Dictionary <Dynamic, Dynamic>)value));
                }
                if (value is HashSet <Dynamic> )
                {
                    return(new Set((HashSet <Dynamic>)value));
                }
                if (value is Queue <Dynamic> )
                {
                    return(new Queue((Queue <Dynamic>)value));
                }
                if (value is Stack <Dynamic> )
                {
                    return(new Stack((Stack <Dynamic>)value));
                }
                if (value is Tuple <Class, Dictionary <string, Dynamic> > )
                {
                    var couple = (Tuple <Class, Dictionary <string, Dynamic> >)value;
                    return(new Object(couple.Item1, couple.Item2));
                }
                if (value is Function)
                {
                    return(new Closure((Function)value));
                }
                return(new Resource(value));

            default:
                return(new Resource(value));
            }
        }
Пример #12
0
        public virtual Dynamic ConvertTo(Class targetClass)
        {
            if (targetClass == Class)
            {
                return(this);
            }

            switch (targetClass.ClassID)
            {
            case ClassID.Boolean:
                return(Boolean.FromBool(AsBoolean));

            case ClassID.Integer:
                return(new Integer(AsInt32));

            case ClassID.Long:
                return(new Long(AsBigInteger));

            case ClassID.Rational:
                return(new Rational(AsRational32));

            case ClassID.Float:
                return(new Float(AsDouble));

            case ClassID.Decimal:
                return(new Decimal(AsBigDecimal));

            case ClassID.Complex:
                return(new Complex(AsComplex64));

            case ClassID.Date:
                return(new Date(AsDateTime));

            case ClassID.String:
                return(new String(ToString()));

            case ClassID.List:
                return(new List(AsList));

            case ClassID.Map:
                return(new Map(AsDictionary));

            case ClassID.Set:
                return(new Set(AsHashSet));

            case ClassID.Queue:
                return(new Queue(AsQueue));

            case ClassID.Stack:
                return(new Stack(AsStack));

            case ClassID.Object:
                return(new Object(AsDynamicObject));

            case ClassID.Resource:
                return(new Resource(AsNativeObject));

            case ClassID.Closure:
                return(new Closure(AsFunction));

            default:
                throw new InvalidCastException();
            }
        }
Пример #13
0
 /// <summary>
 /// Initializes a boolean constant.
 /// </summary>
 /// <param name="value">The constant's value</param>
 public Constant(bool value)
 {
     this.value = Boolean.FromBool(value);
 }