/// <summary> /// Invokes our function by using the Variables we have passed in /// </summary> public override void PerformOperation() { base.PerformOperation(); // Maybe we can improve this - store the array rather than create it every time we invoke the function object[] parameters = new object[OrderedParameterList.Count]; ParameterInfo[] methodParameters = Method.GetParameters(); int index = 0; foreach (Variable var in OrderedParameterList) { object parameter = (var.Value as Reference).Value; if (parameter.GetType() != methodParameters[index].ParameterType) { parameter = CelesteBinder.Bind(parameter, methodParameters[index].ParameterType); } parameters[index] = parameter;//new CelesteObject(var.Value as Reference); index++; } object returnedArgument = Method.Invoke(null, parameters); if (Method.ReturnType.Name != "Void") { // If the method does not return void, push it onto the stack CelesteStack.Push(returnedArgument); } }
/// <summary> /// Removes the two objects at the top of the stack and divides them together. /// Then, pushes the result on to the top of the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); // If we have fewer than 2 objects on the stack we cannot do this Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for divide operator"); CelesteObject rhs = CelesteStack.Pop(); CelesteObject lhs = CelesteStack.Pop(); // The stack will wrap our division result in a CelesteObject, so just push the actual value of the division if (lhs.IsNumber() && rhs.IsNumber()) { float rhsAsFloat = rhs.As <float>(); if (rhsAsFloat != 0) { CelesteStack.Push(lhs.As <float>() / rhsAsFloat); } else { Debug.Fail("Division by zero"); } } else { Debug.Fail("Invalid parameters to add operation."); } }
/// <summary> /// Removes the the object at the top of the stack and performs the not operation. /// Then, pushes the result of the equality on to the top of the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); // If we have fewer than 1 object on the stack we cannot perform this operation Debug.Assert(CelesteStack.StackSize >= 1, "Not enough elements on the stack for the '!' operator"); CelesteObject rhs = CelesteStack.Pop(); // The stack will wrap our result in a CelesteObject, so just push the actual result of the equality bool result = false; Reference rhsRef = rhs.AsReference(); // Check to see whether our rhs is a reference if (rhsRef != null) { // Returns the result of the reference being equal to null result = rhsRef.Value == null; } else { // Else returns the logical opposite of the bool value result = (rhs.Value == null) || ((rhs.Value is bool) && !(bool)rhs.Value); } // We then finally push the result of the equality test onto the stack CelesteStack.Push(result); }
/// <summary> /// Removes the two objects at the top of the stack and performs logical operations them. /// Then, pushes the result of the operation on to the top of the stack. /// </summary> public sealed override void PerformOperation() { base.PerformOperation(); // If we have fewer than 2 objects on the stack we cannot perform this operation Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for the equality operator"); CelesteObject rhs = CelesteStack.Pop(); CelesteObject lhs = CelesteStack.Pop(); // The stack will wrap our result in a CelesteObject, so just push the actual result of the operation bool result = false; Reference lhsRef = lhs.AsReference(); Reference rhsRef = rhs.AsReference(); // Check to see whether our lhs is a reference if (lhsRef != null) { if (rhsRef != null) { // If the rhs is a reference too, we compare references result = ReferenceReferenceOperation(lhsRef, rhsRef); } else { // Otherwise we compare the value of the rhs with the value of the lhs' referenced object result = ReferenceValueOperation(lhsRef, rhs.Value); } } else { if (rhsRef != null) { // If the rhs is a reference, we compare the value of the rhs' referenced object with the value of the lhs result = ReferenceValueOperation(rhsRef, lhs.Value); } else { // Otherwise we compare the value of the rhs with the value of the lhs - they are both values result = ValueValueOperation(lhs.Value, rhs.Value); } } // We then finally push the result of the equality test onto the stack CelesteStack.Push(result); }
/// <summary> /// Removes the two objects at the top of the stack and multiplies them together. /// Then, pushes the result on to the top of the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); // If we have fewer than 2 objects on the stack we are el-bonerino-ed Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for multiply operator"); CelesteObject rhs = CelesteStack.Pop(); CelesteObject lhs = CelesteStack.Pop(); // The stack will wrap our multiplication result in a CelesteObject, so just push the actual value of the multiplication if (lhs.IsNumber() && rhs.IsNumber()) { CelesteStack.Push(lhs.As <float>() * rhs.As <float>()); } else { Debug.Fail("Invalid parameters to add operation."); } }
// When we compile we need to check which end the increment is on and then check for a variable name accordingly /// <summary> /// Pops the first element on the top of the stack and checks to see if it is a reference to a number. /// If it is, it increments the number and then pushes the object back onto the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); Debug.Assert(CelesteStack.StackSize > 0, "Must be an object on the stack for the increment operator"); CelesteObject rhs = CelesteStack.Pop(); if (rhs.IsNumber()) { float newFloat = rhs.As <float>(); newFloat++; rhs.Value = newFloat; } else { Debug.Fail("Increment operator can only be applied to a variable with a numerical value"); } CelesteStack.Push(rhs); }
/// <summary> /// Removes the two objects at the top of the stack and subtracts the top most object on the stack from the other. /// Then, pushes the result on to the top of the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); // If we have fewer than 2 objects on the stack we are el-bonerino-ed Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for subtract operator"); CelesteObject rhs = CelesteStack.Pop(); CelesteObject lhs = CelesteStack.Pop(); // The stack will wrap our subtraction result in a CelesteObject, so just push the actual value of the subtraction if (lhs.IsNumber() && rhs.IsNumber()) { CelesteStack.Push(lhs.As <float>() - rhs.As <float>()); } else if (lhs.IsString() && rhs.IsString()) { string lhsAsString = lhs.As <string>(); string rhsAsString = rhs.As <string>(); if (lhsAsString.Contains(rhsAsString)) { // Push the lhs string having removed the rhs string CelesteStack.Push(lhsAsString.Remove(lhsAsString.IndexOf(rhsAsString), rhsAsString.Length)); } else { // If our lhs does not contain our rhs, we just push the original unaltered lhs string CelesteStack.Push(lhsAsString); } } else if (lhs.IsList() && rhs.IsList()) { // The subtract operator for lists removes all elements in the first list who are equal to an element in the second, either by reference or value List <object> lhsList = lhs.AsList <object>(); List <object> rhsList = rhs.AsList <object>(); // Remove ALL occurrences in the list of any object in the rhs list - otherwise there is non-deterministic behaviour in which instance to remove lhsList.RemoveAll(x => rhsList.Exists(y => y.Equals(x) || y.ValueEquals(x))); CelesteStack.Push(lhs); } else if (lhs.IsTable() && rhs.IsList()) { // Subtraction of tables does not make sense // What does make sense, is subtracting elements in a table using a list of keys // Subtracting a list from a table will remove any elements in the table with a matching key as an element in our list Dictionary <object, object> lhsTable = lhs.AsTable(); List <object> rhsList = rhs.AsList <object>(); foreach (object obj in rhsList) { if (lhsTable.Contains(new KeyValuePair <object, object>(obj, null), new TableKeyComparer())) { lhsTable.Remove(lhsTable.First(x => x.Key.Equals(obj) || x.Key.ValueEquals(obj))); } } CelesteStack.Push(lhs); } else { Debug.Fail("Invalid parameters to subtract operation."); } }
/// <summary> /// Pushes our stored object onto the stack /// </summary> public override void PerformOperation() { base.PerformOperation(); CelesteStack.Push(_Value); }
/// <summary> /// Removes the two objects at the top of the stack and adds them together. /// Then, pushes the result on to the top of the stack. /// </summary> public override void PerformOperation() { base.PerformOperation(); // If we have fewer than 2 objects on the stack we are el-bonerino-ed Debug.Assert(CelesteStack.StackSize >= 2, "Not enough elements on the stack for add operator"); CelesteObject rhs = CelesteStack.Pop(); CelesteObject lhs = CelesteStack.Pop(); // The stack will wrap our addition result in a CelesteObject, so just push the actual value of the addition if (lhs.IsNumber() && rhs.IsNumber()) { CelesteStack.Push(lhs.As <float>() + rhs.As <float>()); } else if (lhs.IsString() && rhs.IsString()) { CelesteStack.Push(lhs.As <string>() + rhs.As <string>()); } else if (lhs.IsList() && rhs.IsList()) { lhs.AsList <object>().AddRange(rhs.AsList <object>()); CelesteStack.Push(lhs); } else if (lhs.IsTable() && rhs.IsTable()) { Dictionary <object, object> lhsTable = lhs.AsTable(); Dictionary <object, object> rhsTable = rhs.AsTable(); // Tables can be added, but only if they are all indexed by number or string completely // No other way can be used because the reference type of 'object' makes value equality difficult // These two types of adding are the only two we can realistically support // Check whether our lhs table has all number keys if (lhsTable.Keys.Count(x => x is float) == lhsTable.Keys.Count) { // If it does, check the rhs table too if (rhsTable.Keys.Count(x => x is float) == rhsTable.Keys.Count) { // We now go through and see if any keys overlap in the two tables if (lhsTable.Intersect(rhsTable, new TableKeyComparer()).Count() == 0) { foreach (KeyValuePair <object, object> pair in rhsTable) { lhsTable.Add((float)pair.Key, pair.Value); } } else { Debug.Fail("Invalid parameters to add operation. Right hand side table has at least one key the same as the left hand side table"); } } else { Debug.Fail("Invalid parameters to add operation. Right hand side table has inconsistent key types (should be numbers)"); } } // Check whether our lhs table has all string keys else if (lhsTable.Keys.Count(x => x is string) == lhsTable.Keys.Count) { // If it does, check the rhs table too if (rhsTable.Keys.Count(x => x is string) == rhsTable.Keys.Count) { // We now go through and see if any keys overlap in the two tables if (lhsTable.Intersect(rhsTable, new TableKeyComparer()).Count() == 0) { foreach (KeyValuePair <object, object> pair in rhsTable) { lhsTable.Add((string)pair.Key, pair.Value); } } else { Debug.Fail("Invalid parameters to add operation. Right hand side table has at least one key the same as the left hand side table"); } } else { Debug.Fail("Invalid parameters to add operation. Right hand side table has inconsistent key types (should be strings)"); } } else { Debug.Fail("Invalid parameters to add operation. Left hand side table has inconsistent key types (should be numbers or strings)"); } CelesteStack.Push(lhs); } else { Debug.Fail("Invalid parameters to add operation."); } }