public void TestLine(string input, bool resolve = true) { // First, let's parse it CalcObject obj1 = CLInterpreter.Interpret(input); // And put it back to string and back string code2 = obj1.ToCode(); CalcObject obj3 = CLInterpreter.Interpret(code2); string code4 = obj3.ToCode(); // code2 and code4 need to match Assert.AreEqual(code2, code4, "code2 and code4 don't match on line: " + input); // We'll stop here if we're testing non-deterministic functions if (!resolve) { return; } // Then, let's get the value CalcValue val5 = obj1.GetValue(); string code6 = val5.ToCode(); CalcObject obj7 = CLInterpreter.Interpret(code6); string code8 = obj7.ToCode(); CalcValue val9 = obj7.GetValue(); // code6 and code8 need to match Assert.AreEqual(code6, code8, "code6 and code8 don't match on line: " + input); // val5 and val9 also need to match Assert.AreEqual(val5, val9, "val5 and val9 don't match on line: " + input); }
private static CalcValue BinRepeat(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { List <CalcValue> ret = new List <CalcValue>(); CalcNumber numRight = (CalcNumber)right; int count = (int)numRight.Value; CalcObject _i = null; if (vars.ContainsVar("_i")) { _i = vars["_i"]; } for (int i = 0; i < count; i++) { vars["_i"] = new CalcNumber(i); ret.Add(left.GetValue(vars, context)); } if (_i != null) { vars["_i"] = _i; } return(new CalcList(ret.ToArray())); }
/// <summary>Runs the Operator on two operands.</summary> /// <param name="left">The left operand.</param> /// <param name="right">The right operand.</param> /// <param name="vars">The local variable storage.</param> /// <param name="context">An object representing context.</param> public CalcValue Run(CalcObject left, CalcObject right, CLLocalStore vars = null, CLContextProvider context = null) { // If the operator is value-based, we'll automatically convert expressions. if (ValueBasedLeft) { left = left.GetValue(vars, context); } if (ValueBasedRight) { right = right.GetValue(vars, context); } // Now get the func. CLBinaryOperatorFunc func = this[left.GetType(), right.GetType()]; // If it's null, we'll throw an exception. if (func == null) { throw new CLException( "Binary operator " + Symbol + " doesn't support parameters " + left.GetType().Name + " and " + right.GetType().Name ); } // Now let's run it. return(func(left, right, vars, context)); }
public string TestLine(string line, CLLocalStore vars, CLContextProvider context, string expected) { // We'll parse the line as usual CalcObject obj1 = CLInterpreter.Interpret(line); string code2 = obj1.ToCode(); CalcObject obj3 = CLInterpreter.Interpret(code2); string code4 = obj3.ToCode(); // Make sure things look right Assert.AreEqual(code2, code4); // Now get the value out of it CalcValue val5 = obj3.GetValue(vars, context); string code6 = val5.ToCode(); CalcObject obj7 = CLInterpreter.Interpret(code6); string code8 = val5.ToCode(); CalcValue val9 = obj7.GetValue(vars, context); // Make sure things still look right Assert.AreEqual(code6, code8); Assert.AreEqual(val5, val9); // Now we should also make sure the values reported are as expected. Assert.AreEqual(code8, expected); // And we'll return the final value so it can be used later! return(code8); }
/// <summary>Runs the Operator on two operands.</summary> /// <param name="param">The right operand.</param> /// <param name="vars">The local variable storage.</param> /// <param name="context">An object representing context.</param> public CalcValue Run(CalcObject param, CLLocalStore vars = null, CLContextProvider context = null) { // If the operator is value-based, we'll automatically convert expressions. if (ValueBased) { param = param.GetValue(vars, context); } // Now get the func. CLUnaryOperatorFunc func = this[param.GetType()]; // If it's null, we'll throw an exception. if (func == null) { throw new CLException( "Binary operator " + Symbol + " doesn't support parameter " + param.GetType().Name ); } // Now let's run it. return(func(param, vars, context)); }