/// <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)); }
/// <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); } }
/// <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]); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <inheritdoc /> protected override object Operate(ScriptContext context) { return(Lhs.Execute(context).ToBoolean() != Rhs.Execute(context).ToBoolean()); }
/// <inheritdoc /> protected override object Compute(ScriptContext context) { return((dynamic)Lhs.Execute(context) & (dynamic)Rhs.Execute(context)); }