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); }
// Returns the string, reversed. public static CalcValue PreMinusString(CalcObject param, CLLocalStore vars, CLContextProvider context) { CalcString strParam = param as CalcString; char[] chars = strParam.Value.ToCharArray(); Array.Reverse(chars); return(new CalcString(new string(chars))); }
public static CalcValue SignFunction(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length == 0) { throw new CLException("{!sign} requires a number."); } CalcNumber num = NumberAt(pars, 0, "!sign", vars, context); return(new CalcNumber(Math.Sign(num))); }
// Returns the natural logarithm of the parameter. public static CalcValue LogFunction(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length == 0) { throw new CLException("{!log} requires a number."); } CalcNumber num = NumberAt(pars, 0, "!floor", vars, context); return(new CalcNumber((decimal)Math.Log((double)num.Value))); }
private static DiceDie FunctionDie(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length < 2) { throw new CLException("{!die} requires two params: A number and a value."); } CalcNumber num = NumberAt(pars, 0, "!die", vars, context); CalcValue val = pars[1].GetValue(); return(new DiceDie(num.Value, val)); }
public static CalcValue PostFactNumber(CalcObject param, CLLocalStore vars, CLContextProvider context) { CalcNumber num = param as CalcNumber; int o = 1; for (int i = 2; i < num.Value; i++) { o *= i; } return(new CalcNumber(o)); }
// Calculates the angle of a given set of coordinates. public static CalcValue Atan2Function(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length < 2) { throw new CLException("{!atan2} requires two numbers."); } CalcNumber y = NumberAt(pars, 0, "!atan2", vars, context); CalcNumber x = NumberAt(pars, 1, "!atan2", vars, context); return(new CalcNumber((decimal)Math.Atan2((double)y.Value, (double)x.Value))); }
// Returns the list, with all its elements negated. public static CalcValue PreMinusList(CalcObject param, CLLocalStore vars, CLContextProvider context) { CalcList lstParam = param as CalcList; CalcValue[] lstRet = new CalcValue[lstParam.Count]; for (int i = 0; i < lstRet.Length; i++) { lstRet[i] = PrefixMinus.Run(lstParam[i], vars, context); } return(new CalcList(lstRet)); }
/// <summary> /// Deletes a saved variable. /// </summary> /// <param name="name">The name of the variable to delete.</param> /// <param name="contet"> /// An object representing the context in which it's run. /// </param> public static void Delete(string name, CLContextProvider context) { CLVariableDelete data = new CLVariableDelete() { Name = name }; VariableDeleted.Invoke(context, data); if (!data.Deleted) { InternalStorage.Remove(name); } }
/// <summary> /// Saves a <c>CalcObject</c> as a variable. /// </summary> /// <param name="name">The name of the variable to save.</param> /// <param name="val">The value to save.</param> /// <param name="context"> /// An object representing the context in which it's run. /// </param> public static void Save(string name, CalcObject val, CLContextProvider context) { CLVariableSave data = new CLVariableSave() { Name = name, Value = val }; VariableSaved.Invoke(context, data); if (!data.Saved) { InternalStorage[name] = val; } }
// Returns the minimum value out of the list. public static CalcValue MinFunction(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length == 0) { throw new CLException("{!min} requires numbers."); } decimal min = Decimal.MaxValue; for (int i = 0; i < pars.Length; i++) { CalcNumber num = NumberAt(pars, i, "!min", vars, context); min = Math.Min(min, num.Value); } return(new CalcNumber(min)); }
/// <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)); }
/// <summary> /// Loads a variable and returns the <c>CalcObject</c> it represents. /// </summary> /// <param name="name">The name of the variable to load.</param> /// <param name="context"> /// An object representing the context in which it's run. /// </param> public static CalcObject Load(string name, CLContextProvider context) { CLVariableLoad data = new CLVariableLoad() { Name = name }; VariableLoaded.Invoke(context, data); if (data.Value == null) { try { return(InternalStorage[name]); } catch (KeyNotFoundException e) { throw new CLException(e); } } else { return(data.Value); } }
// Returns the number, negated. public static CalcValue PreMinusNumber(CalcObject param, CLLocalStore vars, CLContextProvider context) { CalcNumber numParam = param as CalcNumber; return(new CalcNumber(-numParam)); }
// Returns the quotient of a list's items over a number. public static CalcValue BinDivideList(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcList lstLeft = left as CalcList; CalcNumber numRight = right as CalcNumber; // We will *not* do string checking here // It's entirely possible someone writes a string divider and this function will use it. CalcValue[] lstRet = new CalcValue[lstLeft.Count]; for (int i = 0; i < lstRet.Length; i++) { lstRet[i] = BinaryDivide.Run(lstLeft[i], numRight, vars, context); } return(new CalcList(lstRet)); }
// Adds two numbers. public static CalcValue BinPlusNumbers(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcNumber numLeft = left as CalcNumber; CalcNumber numRight = right as CalcNumber; return(new CalcNumber(numLeft + numRight)); }
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())); }
private static CalcValue CompRerolls(CalcObject left, CLComparison comp, CalcObject right, CLLocalStore vars, CLContextProvider context, bool keep = false, bool recurse = false) { CalcList lstLeft = (CalcList)left; CalcNumber numRight = (CalcNumber)right; List <CalcValue> output = new List <CalcValue>(); DiceContext dc = null; int limit = 0; int limitUsed = 0; // We need to get the limits if they've been set if (context.ContainsDerived(typeof(DiceContext), out Type actualDiceContext)) { dc = (DiceContext)(context.Get(actualDiceContext)); limit = Math.Min(dc.PerRollLimit, dc.PerFunctionLimit - dc.PerFunctionUsed); if (limit == 0) { throw new LimitedDiceException(); } } // Go through the list foreach (CalcValue val in lstLeft) { decimal value; if (val is CalcNumber valNum) { value = valNum.Value; } else if (val is CalcList valList) { value = valList.Sum(); } else { throw new CLException("Re-rolls only work with numeric values."); } // If it's a value we need to re-roll if (comp.CompareFunction(value, numRight.Value)) { // Keep the original value ("x" and "xr" operators) if (keep) { output.Add(val); } // Now make another value (or recurse) bool redo = true; // Now figure out how many sides each die has... int sides = 0; bool list = false; CalcValue dSides; CalcList lstSides = null; if (val is DiceDie die) { dSides = die.Sides; // (Are we using a list or a number for the sides?) if (dSides is CalcNumber nSides) { sides = (int)(nSides.Value); } else if (dSides is CalcList lSides) { lstSides = lSides; sides = lSides.Count; list = true; } } else { decimal valValue = 0; if (val is CalcNumber nVal) { valValue = nVal.Value; } else if (val is CalcList lVal) { valValue = lVal.Sum(); } else { throw new CLException("Reroll only works with numeric values."); } if (valValue < 0) { valValue *= -1; } sides = (valValue <= 6) ? 6 : (valValue <= 20) ? 20 : (valValue <= 100) ? 100 : (valValue <= 1000) ? 1000 : (valValue <= 10000) ? 10000 : (valValue <= 100000) ? 100000 : (valValue <= 1000000) ? 1000000 : (valValue <= 10000000) ? 10000000 : (valValue <= 100000000) ? 100000000 : (valValue <= 1000000000) ? 1000000000 : 2147483647; dSides = new CalcNumber(sides); } // Now we can roll the dice! Random rand = null; if (context.ContainsDerived(typeof(Random), out Type actualRandom)) { rand = (Random)(context.Get(actualRandom)); } else { rand = new Random(); } while (redo && limitUsed < limit) { int choice = rand.Next(sides); limitUsed++; decimal cValue = 0; if (list) { CalcValue cVal = lstSides[choice]; if (cVal is CalcNumber cNum) { cValue = cNum.Value; output.Add(new DiceDie(cValue, lstSides)); } else if (cVal is CalcList cList) { cValue = cList.Sum(); output.Add(new DiceDie(cValue, lstSides)); } } else { cValue = choice + 1; output.Add(new DiceDie(cValue, new CalcNumber(sides))); } // recursion? if (recurse) { redo = comp.CompareFunction(cValue, numRight.Value); } else { redo = false; } } } else { // The reroll comparison wasn't satisfied output.Add(val); } } return(new CalcList(output.ToArray())); }
private static CalcValue CompUntil(CalcObject left, CLComparison comp, CalcObject right, CLLocalStore vars, CLContextProvider context) { int limit = int.MaxValue; DiceContext dc = null; // We need to get the limits if they've been set if (context.ContainsDerived(typeof(DiceContext), out Type actualDiceContext)) { dc = (DiceContext)(context.Get(actualDiceContext)); limit = Math.Min(dc.PerRollLimit, dc.PerFunctionLimit - dc.PerFunctionUsed); if (limit == 0) { throw new LimitedDiceException(); } } CalcNumber numLeft = null; CalcList lstLeft = null; bool list = false; CalcNumber numRight = (CalcNumber)right; // Now figure out how many sides each die has... int sides = 0; // (Are we using a list or a number for the sides?) if (left is CalcNumber) { numLeft = (CalcNumber)left; sides = (int)(numLeft.Value); } else if (left is CalcList) { lstLeft = (CalcList)left; sides = lstLeft.Count; list = true; } // ... and ensure it's at least one. if (sides < 1) { throw new CLException("Dice must have at least one side."); } // Now we can roll the dice! List <CalcValue> lstRet = new List <CalcValue>(); Random rand = null; if (context.ContainsDerived(typeof(Random), out Type actualRandom)) { rand = (Random)(context.Get(actualRandom)); } else { rand = new Random(); } CalcList output = null; Type actual = null; for (int i = 0; i < limit; i++) { // First determine the value int choice = rand.Next(sides); CalcNumber value = null; if (list) { CalcValue val = lstLeft[choice]; if (val is CalcList valList) { value = new DiceDie(valList.Sum(), lstLeft); } else if (val is CalcNumber valNum) { value = new DiceDie(valNum.Value, lstLeft); } } else { value = new DiceDie(choice + 1, new CalcNumber(sides)); } // See if it satisfies the comparison if (comp.CompareFunction(value.Value, numRight.Value)) { vars["_u"] = value; output = new CalcList(lstRet.ToArray()); // Add to roll history if (context.ContainsDerived(typeof(List <(string, CalcList)>), out actual)) { List <(string, CalcList)> history = (List <(string, CalcList)>)context.Get(actual); history.Add(($"{left.ToCode()}u{comp.PostfixSymbol}{right.ToCode()}", output)); history.Add(($"Killed above roll:", ValToList(value))); } // also remember to actually UPDATE the limits! (╯°□°)╯︵ ┻━┻ if (dc != null) { dc.PerFunctionUsed += i + 1; } return(output); } else { lstRet.Add(value); } } vars["_u"] = new CalcNumber(0); output = new CalcList(lstRet.ToArray()); // Add to roll history if (context.ContainsDerived(typeof(List <(string, CalcList)>), out actual)) { List <(string, CalcList)> history = (List <(string, CalcList)>)context.Get(actual); history.Add(($"{left.ToCode()}u{comp.PostfixSymbol}{right.ToCode()}", output)); } // also remember to actually UPDATE the limits! (╯°□°)╯︵ ┻━┻ if (dc != null) { dc.PerFunctionUsed += limit; } return(output); }
// Gets the parameter at a given index in the params list as a value. public static CalcValue ValueAt(CalcObject[] pars, int index, string name, CLLocalStore vars, CLContextProvider context) { if (pars.Length <= index) { throw new CLException($"{name} parameter {index} was not specified."); } CalcValue val = pars[index].GetValue(vars, context); return(val); }
// Gets the parameter at a given index in the params list as a number. internal static CalcNumber NumberAt(CalcObject[] pars, int index, string name, CLLocalStore vars, CLContextProvider context) { if (pars.Length <= index) { throw new CLException(name + " parameter " + index + " was not specified."); } CalcValue val = pars[index].GetValue(vars, context); if (!(val is CalcNumber num)) { throw new CLCastException(name + " parameter " + index + " must be a number."); } return(num); }
public static CalcValue BinIntDivideNumbers(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcNumber numLeft = left as CalcNumber; CalcNumber numRight = right as CalcNumber; return(new CalcNumber(decimal.Floor(numLeft / numRight))); }
// Returns the minimum value out of the list. public static CalcValue MinMagnitudeFunction(CalcObject[] pars, CLLocalStore vars, CLContextProvider context) { if (pars.Length == 0) { throw new CLException("{!minmagnitude} requires numbers."); } decimal min = (decimal)Decimal.MinValue; for (int i = 0; i < pars.Length; i++) { CalcNumber num = NumberAt(pars, i, "!minmagnitude", vars, context); if (Math.Abs(num.Value) < min) { min = num; } } return(new CalcNumber(min)); }
// Concatenates two strings. public static CalcValue BinPlusStrings(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcString strLeft = left as CalcString; CalcString strRight = right as CalcString; return(new CalcString(strLeft + strRight)); }
// Returns the left raised to the power of the right. public static CalcValue BinPowerNumbers(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcNumber numLeft = left as CalcNumber; CalcNumber numRight = right as CalcNumber; return(new CalcNumber((decimal)Math.Pow((double)(numLeft.Value), (double)(numRight.Value)))); }
private static CalcValue BinDice(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { int limit = int.MaxValue; DiceContext dc = null; // We need to get the limits if they've been set if (context.ContainsDerived(typeof(DiceContext), out Type actualDiceContext)) { dc = (DiceContext)(context.Get(actualDiceContext)); limit = Math.Min(dc.PerRollLimit, dc.PerFunctionLimit - dc.PerFunctionUsed); if (limit == 0) { throw new LimitedDiceException(); } } CalcNumber numLeft = (CalcNumber)left; CalcNumber numRight = null; CalcList lstRight = null; bool list = false; // Now figure out how many dice to roll... int count = (int)(numLeft.Value); // ... and whether or not it's within limits (including the limitation that it must be positive) if (count <= 0) { throw new CLException("The number of dice to roll must be positive."); } else if (count > limit) { count = limit; } // also remember to actually UPDATE the limits! (╯°□°)╯︵ ┻━┻ if (dc != null) { dc.PerFunctionUsed += count; } // Now figure out how many sides each die has... int sides = 0; // (Are we using a list or a number for the sides?) if (right is CalcNumber) { numRight = (CalcNumber)right; sides = (int)(numRight.Value); } else if (right is CalcList) { lstRight = (CalcList)right; sides = lstRight.Count; list = true; } // ... and ensure it's at least one. if (sides < 1) { throw new CLException("Dice must have at least one side."); } // Now we can roll the dice! CalcValue[] ret = new CalcValue[count]; Random rand = null; if (context.ContainsDerived(typeof(Random), out Type actualRandom)) { rand = (Random)(context.Get(actualRandom)); } else { rand = new Random(); } for (int i = 0; i < count; i++) { int choice = rand.Next(sides); if (list) { CalcValue val = lstRight[choice]; if (val is CalcNumber valNum) { ret[i] = new DiceDie(valNum.Value, lstRight); } else if (val is CalcList valList) { ret[i] = new DiceDie(valList.Sum(), lstRight); } else { throw new CLException("Dice must be numeric values."); // maybe I'll change this one day } } else { ret[i] = new DiceDie(choice + 1, new CalcNumber(sides)); } } CalcList output = new CalcList(ret); // Add to roll history if (context.ContainsDerived(typeof(List <(string, CalcList)>), out Type actual)) { List <(string, CalcList)> history = (List <(string, CalcList)>)context.Get(actual); history.Add(($"{left.ToCode()}d{right.ToCode()}", output)); } return(output); }
// Multiplies a string by a number. public static CalcValue BinTimesString(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcString strLeft = left as CalcString; CalcNumber numRight = right as CalcNumber; int count = (int)numRight; if (count < 0) { throw new CLException("Strings cannot be repeated negative times."); } string strRet = ""; for (int i = 0; i < count; i++) { strRet += strLeft; } return(new CalcString(strRet)); }
// Concatenates two lists. public static CalcValue BinPlusLists(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) { CalcList lstLeft = left as CalcList; CalcList lstRight = right as CalcList; CalcValue[] lstRet = new CalcValue[lstLeft.Count + lstRight.Count]; int i; for (i = 0; i < lstLeft.Count; i++) { lstRet[i] = lstLeft[i]; } for (int j = 0; j < lstRight.Count; j++) { lstRet[i + j] = lstRight[j]; } return(new CalcList(lstRet)); }
/// <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)); }
// Throws an exception, but exists. Can be overloaded by other modules. public static CalcValue BinDivideStringException(CalcObject left, CalcObject right, CLLocalStore vars, CLContextProvider context) => throw new CLException("Strings cannot be divided by numbers.");