Example #1
0
        public override bool Execute(IDictionary <string, EntityProperty> data, out string error)
        {
            var leftResult = Lhs.Execute(data, out error);

            if (error != null)
            {
                return(false);
            }

            if (Op.TokenType == TokenType.LogicalOr)
            {
                if (leftResult)
                {
                    return(true);
                }

                return(Rhs.Execute(data, out error));
            }

            if (Op.TokenType == TokenType.LogicalAnd)
            {
                if (!leftResult)
                {
                    return(false);
                }

                return(Rhs.Execute(data, out error));
            }

            error = $@"Invalid relational token ""{Op.Text}"".";
            return(false);
        }
Example #2
0
        /// <summary>
        /// Executes sub-expressions and checks resulting <see cref="Value"/>s for equality.
        /// </summary>
        /// <param name="vm">The environment in which to execute the operation.</param>
        /// <returns>A <see cref="BooleanValue"/>. True if equal. False if not.</returns>
        public override Value Execute(VM vm)
        {
            Value a     = Lhs.Execute(vm);
            Value b     = Rhs.Execute(vm);
            bool  equal = a.Equal(b);

            return(new BooleanValue(equal));
        }
Example #3
0
 /// <inheritdoc />
 protected override object ExecuteToken(ScriptContext context)
 {
     try {
         object lhs = Lhs.Execute(context);
         object rhs = Rhs.Execute(context);
         TypeInformation.ConvertOperands(ref lhs, ref rhs);
         return(Operate(lhs, rhs, context));
     }
     catch (RuntimeBinderException e) {
         throw new ScriptRuntimeException(e.Message, null, e);
     }
 }
Example #4
0
        /// <summary>
        /// Returns the <see cref="Value"/> in the list gotten by Lhs at the index gotten by Rhs.
        /// </summary>
        /// <param name="vm">The environment in which to execute the operation.</param>
        /// <returns>The element in the list.</returns>
        /// <exception cref="Exception">If there was a type error.</exception>
        public override Value Execute(VM vm)
        {
            Value list = Lhs.Execute(vm);
            Value idx  = Rhs.Execute(vm);

            Value.AssertType(Value.ValueType.List, list,
                             "First operand to '[]' expected to be 'List' but was given '{0}'.", list.Type);
            Value.AssertType(Value.ValueType.Number, idx,
                             "Second operand to '[]' expected to be 'Number' but was given '{0}'.", idx.Type);

            List <Value> values = list.List;
            float        i      = idx.Number;

            return(values[(int)i]);
        }
Example #5
0
        /// <summary>
        /// Evaluates Lhs > Rhs and returns the result.
        /// </summary>
        /// <param name="vm">The environment in which to execute the operation.</param>
        /// <returns>A <see cref="BooleanValue"/>. True if Lhs > Rhs. False if not.</returns>
        /// <exception cref="Exception">If either sub-expression doesn't return a <see cref="NumberValue"/>.</exception>
        public override Value Execute(VM vm)
        {
            Value a = Lhs.Execute(vm);
            Value b = Rhs.Execute(vm);

            Value.AssertType(Value.ValueType.Number, a,
                             "First operand of '>' expected to be 'Number' but was given '{0}'.", a.Type);
            Value.AssertType(Value.ValueType.Number, b,
                             "Second operand of '>' expected to be 'Number' but was given '{0}'.", b.Type);

            float numA = a.Number;
            float numB = b.Number;

            return(new BooleanValue(numA > numB));
        }
Example #6
0
        /// <summary>
        /// Executes sub-expressions and returns the sum of the resulting <see cref="NumberValue"/>s.
        /// </summary>
        /// <param name="vm">The environment in which to execute the operation.</param>
        /// <returns>A <see cref="NumberValue"/>. The sum of the sub-expressions.</returns>
        /// <exception cref="Exception">If either sub-expression doesn't return a <see cref="NumberValue"/>.</exception>
        public override Value Execute(VM vm)
        {
            Value a = Lhs.Execute(vm);
            Value b = Rhs.Execute(vm);

            Value.AssertType(Value.ValueType.Number, a,
                             "First argument to '+' expected to be 'Number' but was given '{0}'.", a.Type);
            Value.AssertType(Value.ValueType.Number, b,
                             "Second argument to '+' expected to be 'Number' but was given '{0}'.", b.Type);

            float numA = a.Number;
            float numB = b.Number;
            float numR = numA + numB;

            return(new NumberValue(numR));
        }
Example #7
0
        /// <summary>
        /// Evaluates Lhs and Rhs with short-circuiting.
        /// </summary>
        /// <param name="vm">The environment in which to execute the operation.</param>
        /// <returns>A <see cref="BooleanValue"/>. True if both sub-expression are true. False if not.</returns>
        /// <exception cref="Exception">if either sub-expression doesn't return a <see cref="BooleanValue"/>.</exception>
        public override Value Execute(VM vm)
        {
            Value a = Lhs.Execute(vm);

            Value.AssertType(Value.ValueType.Boolean, a,
                             "First operand of 'or' expected to be 'Boolean' but was given '{0}'.", a.Type);
            bool boolA = a.Boolean;

            if (!boolA)
            {
                return(new BooleanValue(boolA));
            }


            Value b = Rhs.Execute(vm);

            Value.AssertType(Value.ValueType.Boolean, b,
                             "Second operand of 'or' expected to be 'Boolean' but was given '{0}'.", b.Type);
            return(new BooleanValue(b.Boolean));
        }
Example #8
0
        /// <summary>
        /// Invokes a value and returns the resulting <see cref="Value"/>.
        /// </summary>
        /// <param name="vm">The environment in which to execute the invocation.</param>
        /// <returns>A <see cref="BooleanValue"/>. True if Lhs less-than Rhs. False if not.</returns>
        /// <exception cref="Exception">If Lhs isn't invocable.</exception>
        public override Value Execute(VM vm)
        {
            if (Lhs is BoundMethod boundMethod)
            {
                return(InvokeBoundMethod(vm, boundMethod));
            }

            Value callee = Lhs.Execute(vm);

            switch (callee.Type)
            {
            case Value.ValueType.Lambda:
                return(InvokeLambda(vm, callee as LambdaValue));

            case Value.ValueType.Class:
                return(InvokeClass(vm, callee as ClassValue));

            default:
                throw new Exception(string.Format("Cannot invoke something of type '{0}'.", callee.Type));
            }
        }
Example #9
0
        /// <summary>
        /// Executes sub-expressions and returns a <see cref="RangeValue"/>.
        /// </summary>
        /// <param name="vm">The environment in which to execute the sub-expressions.</param>
        /// <returns>A <see cref="RangeValue"/>. A range between Lhs and Rhs.</returns>
        /// <exception cref="Exception">If type error.</exception>
        public override Value Execute(VM vm)
        {
            Value start = Lhs.Execute(vm);
            Value end   = Rhs.Execute(vm);

            if (!Value.TypesMatch(start, end))
            {
                throw new Exception("start and end values of 'Range' must be the same type.");
            }

            if (start.Type != Value.ValueType.Number && start.Type != Value.ValueType.Char)
            {
                throw new Exception(string.Format("Cannot make a range over '{0}'.", start.Type));
            }

            if (end.Type != Value.ValueType.Number && end.Type != Value.ValueType.Char)
            {
                throw new Exception(string.Format("Cannot make a range over '{0}'.", end.Type));
            }

            return(new RangeValue(start, end, inclusive));
        }
Example #10
0
 /// <inheritdoc />
 protected override object Operate(ScriptContext context)
 {
     return(Lhs.Execute(context).ToBoolean() != Rhs.Execute(context).ToBoolean());
 }
Example #11
0
 /// <inheritdoc />
 protected override object Compute(ScriptContext context)
 {
     return((dynamic)Lhs.Execute(context) & (dynamic)Rhs.Execute(context));
 }