public static bool IsValidPlace(string place, FiMClass reportClass, out object index) { if (IsValidNumberPlace(place, out var nIndex)) { index = nIndex; return(true); } if (reportClass.GetVariable(place) != null) { var variable = reportClass.GetVariable(place); if (!variable.Constant) { throw new FiMException("Cannot use a non-constant variable as a case"); } if (FiMHelper.IsTypeArray(variable.Type)) { throw new FiMException("Can only use non-array variables as a case"); } index = variable.Value; return(true); } index = null; return(false); }
public override object Execute(FiMClass reportClass, params object[] args) { object[] sanitizedArgs = null; if (this.Arguments?.Count > 0) { sanitizedArgs = new object[this.Arguments.Count]; for (int i = 0; i < this.Arguments.Count; i++) { if (i < args.Length) { if (this.Arguments[i] == KirinVariableType.EXPERIMENTAL_DYNAMIC_ARRAY) { if (!FiMHelper.IsTypeArray(args[i])) { throw new FiMException("Expected an array, got " + FiMHelper.AsVariableType(args[i]).AsNamedString()); } } else if (FiMHelper.AsVariableType(args[i]) != this.Arguments[i]) { throw new FiMException("Expected " + this.Arguments[i].AsNamedString() + ", got " + FiMHelper.AsVariableType(args[i]).AsNamedString()); } sanitizedArgs[i] = args[i]; } else { sanitizedArgs[i] = FiMHelper.GetDefaultValue(this.Arguments[i]); } } } object result; try { result = this.Function.DynamicInvoke(sanitizedArgs); } catch (Exception ex) { throw new Exception("An error has occured while running a custom method\n\n" + ex.ToString()); } if (result != null && this.Returns == null) { throw new FiMException("Non-value returning function returned value"); } if (result != null && this.Returns != null && this.Returns != KirinVariableType.UNKNOWN) { if (FiMHelper.AsVariableType(result) != this.Returns) { throw new FiMException("Expected " + ((KirinVariableType)this.Returns).AsNamedString() + ", got " + FiMHelper.AsVariableType(result).AsNamedString()); } return(result); } return(null); }
public KirinValue Load() { if (this.Raw == null) { if (FiMHelper.IsTypeArray(this.Type)) { this._Value = FiMHelper.GetDefaultValue(this.Type); } else { if (this.ForcedType == null) { throw new FiMException("Value is null"); } this._Value = FiMHelper.GetDefaultValue((KirinVariableType)this.ForcedType); } } else { string raw = this.Raw; var eType = FiMHelper.DeclarationType.Determine(" " + raw, out string eKeyword, false); if (eType != KirinVariableType.UNKNOWN) { raw = raw.Substring(eKeyword.Length); this.ForceType(eType); } if (this.ForcedType != null) { eType = (KirinVariableType)this.ForcedType; } object value; if (KirinLiteral.TryParse(raw, out object lResult)) { value = lResult; } else { value = KirinValue.Evaluate(Class, raw, out var returnedType, ForcedType); this.ForceType(returnedType); } if (eType != KirinVariableType.UNKNOWN && FiMHelper.AsVariableType(value) != eType) { throw new FiMException("Expected " + eType.AsNamedString() + ", got " + FiMHelper.AsVariableType(value)); } this._Value = value; } return(this); }
public override object Eval(FiMClass reportClass) { var lv = Left.Eval(reportClass); var rv = Right.Eval(reportClass); var lvt = FiMHelper.AsVariableType(lv); var rvt = FiMHelper.AsVariableType(rv); if (FiMHelper.IsTypeArray(lvt) || FiMHelper.IsTypeArray(rvt)) { throw new FiMException("Cannot execute conditional with an array"); } if (this.Condition == "==") { return(IsEqual(lv, rv)); } if (this.Condition == "!=") { return(!IsEqual(lv, rv)); } if (this.Condition == "&&") { return(Convert.ToBoolean(lv) && Convert.ToBoolean(rv)); } if (this.Condition == "||") { return(Convert.ToBoolean(lv) || Convert.ToBoolean(rv)); } if (lvt != KirinVariableType.NUMBER || rvt != KirinVariableType.NUMBER) { throw new FiMException("Expected number value in conditional"); } double lvd = Convert.ToDouble(lv); double rvd = Convert.ToDouble(rv); switch (this.Condition) { case ">=": return(lvd >= rvd); case "<=": return(lvd <= rvd); case ">": return(lvd > rvd); case "<": return(lvd < rvd); default: throw new FiMException("Invalid expression " + this.Condition); } }
public override object Execute(FiMClass reportClass) { if (reportClass.Variables.Has(this.VariableName)) { throw new FiMException("Variable " + this.VariableName + " already exists"); } var varArray = new KirinValue(this.RawValue, reportClass); if (!FiMHelper.IsTypeArray(varArray.Type) && varArray.Type != KirinVariableType.STRING) { throw new FiMException("Expected type array on for-in loops"); } if (varArray.Type == KirinVariableType.STRING) { string str = Convert.ToString(varArray.Value); reportClass.Variables.Push(new FiMVariable(this.VariableName, str[0])); foreach (char c in str) { reportClass.Variables.Get(this.VariableName).Value = c; var value = this.Statement.Execute(reportClass); if (value != null) { reportClass.Variables.Pop(false); return(value); } } } else { var list = varArray.Value as System.Collections.IDictionary; var sortedKeys = list.Keys.Cast <int>().ToArray().OrderBy(k => k).ToArray(); reportClass.Variables.Push(new FiMVariable(this.VariableName, list[sortedKeys[0]])); foreach (int k in sortedKeys) { reportClass.Variables.Get(this.VariableName).Value = list[k]; var value = this.Statement.Execute(reportClass); if (value != null) { reportClass.Variables.Pop(false); return(value); } } } reportClass.Variables.Pop(false); return(null); }
public override object Execute(FiMClass reportClass) { if (!this.Complete) { throw new FiMException("Executing an incomplete switch statement"); } var variable = reportClass.GetVariable(this.RawVariable); if (variable == null) { throw new FiMException("Varible " + this.RawVariable + " does not exist"); } if (FiMHelper.IsTypeArray(variable)) { throw new FiMException("Cannot use array on a switch"); } Dictionary <object, string> CasesLookup = new Dictionary <object, string>(); foreach (var key in Cases.Keys) { if (!KirinSwitchCase.IsValidPlace(key, reportClass, out object value)) { throw new FiMException("Invalid case " + key); } if (CasesLookup.Keys.Any(k => KirinValue.IsEqual(k, value))) { throw new FiMException("Duplicate case value " + key); } CasesLookup.Add(value, key); } KirinStatement s = DefaultCase; if (CasesLookup.Keys.Any(k => KirinValue.IsEqual(k, variable.Value))) { s = Cases[CasesLookup.First(l => KirinValue.IsEqual(l.Key, variable.Value)).Value]; } if (s != null) { return(s.Execute(reportClass)); } return(null); }
public override object Execute(FiMClass reportClass) { var result = (new KirinValue(RawParameters, reportClass)).Value; if (result == null) { return(null); } if (FiMHelper.IsTypeArray(result)) { throw new FiMException("Cannot print an array"); } reportClass.Report.Output(Convert.ToString(result) + (NewLine ? "\n" : "")); return(null); }
public override object Execute(FiMClass reportClass) { var variable = reportClass.GetVariable(this.RawVariable); if (variable == null) { throw new Exception("Variable " + this.RawVariable + " does not exist"); } if (FiMHelper.IsTypeArray(variable.Type)) { throw new Exception("Cannot input into an array"); } string prompt = ""; if (!string.IsNullOrWhiteSpace(this.PromptString)) { prompt = this.PromptString; } string input = reportClass.Report.Input(prompt, this.RawVariable); if (variable.Type == KirinVariableType.STRING) { input = $"\"{input}\""; } else if (variable.Type == KirinVariableType.CHAR) { input = $"'{input}'"; } if (!KirinLiteral.TryParse(input, out object value)) { throw new Exception("Invalid input " + input); } if (FiMHelper.AsVariableType(value) != variable.Type) { throw new Exception("Input type doesnt match variable type"); } variable.Value = value; return(null); }
/// <summary> /// Evaluates raw FiM++ string into a value /// </summary> public static object Evaluate( FiMClass reportClass, string evaluatable, out KirinVariableType returnedType, KirinVariableType?expectedType = null ) { returnedType = KirinVariableType.UNKNOWN; // Nothing if (evaluatable == "nothing" && expectedType != null) { returnedType = (KirinVariableType)expectedType; return(FiMHelper.GetDefaultValue(returnedType)); } // Calling an existing variable if (reportClass.GetVariable(evaluatable) != null) { var variable = reportClass.GetVariable(evaluatable); returnedType = variable.Type; return(variable.Value); } // Calling an existing method if (reportClass.GetParagraphLazy(evaluatable) != null) { KirinValue[] args = null; string pName = evaluatable; if (pName.Contains(KirinFunctionCall.FunctionParam)) { int pIndex = pName.IndexOf(KirinFunctionCall.FunctionParam); pName = pName.Substring(0, pIndex); args = KirinFunctionCall.ParseCallArguments( evaluatable.Substring(pName.Length + KirinFunctionCall.FunctionParam.Length), reportClass ).ToArray(); } var paragraph = reportClass.GetParagraph(pName); if (paragraph == null) { throw new FiMException("Paragraph " + pName + " not found"); } if (paragraph.ReturnType == KirinVariableType.UNKNOWN) { throw new FiMException("Paragraph returns nothing"); } returnedType = paragraph.ReturnType; return(paragraph.Execute(args)); } // Array if (expectedType != null && FiMHelper.IsTypeArray((KirinVariableType)expectedType)) { System.Collections.IDictionary dict = null; var args = KirinFunctionCall.ParseCallArguments(evaluatable, reportClass); if (!FiMHelper.IsTypeOfArray(args[0].Type, (KirinArrayType)expectedType)) { throw new FiMException("Invalid list value type"); } if (!args.All(a => a.Type == args[0].Type)) { throw new FiMException("Unidentical list value type"); } int i = 1; if (expectedType == KirinVariableType.STRING_ARRAY) { dict = new Dictionary <int, string>(); args.ForEach(kv => dict.Add(i++, Convert.ToString(kv.Value))); } else if (expectedType == KirinVariableType.NUMBER_ARRAY) { dict = new Dictionary <int, double>(); args.ForEach(kv => dict.Add(i++, Convert.ToDouble(kv.Value))); } else if (expectedType == KirinVariableType.BOOL_ARRAY) { dict = new Dictionary <int, bool>(); args.ForEach(kv => dict.Add(i++, Convert.ToBoolean(kv.Value))); } returnedType = (KirinVariableType)expectedType; return(dict); } // Array index if (FiMHelper.ArrayIndex.IsArrayIndex(evaluatable, reportClass)) { var match = FiMHelper.ArrayIndex.GetArrayIndex(evaluatable, reportClass); var varIndex = new KirinValue(match.RawIndex, reportClass); if (varIndex.Type != KirinVariableType.NUMBER) { throw new FiMException("Invalid index value"); } int index = Convert.ToInt32(varIndex.Value); string strVar = match.RawVariable; if (!reportClass.Variables.Has(strVar)) { throw new FiMException("Variable " + strVar + " does not exist"); } var variable = reportClass.Variables.Get(strVar); if (!FiMHelper.IsTypeArray(variable.Type) && variable.Type != KirinVariableType.STRING) { throw new FiMException("Cannot index a non-array variable"); } if (variable.Type == KirinVariableType.STRING) { returnedType = KirinVariableType.CHAR; return(Convert.ToString(variable.Value)[index - 1]); } var dict = variable.Value as System.Collections.IDictionary; if (variable.Type == KirinVariableType.STRING_ARRAY) { returnedType = KirinVariableType.STRING; return(Convert.ToString(dict[index])); } if (variable.Type == KirinVariableType.BOOL_ARRAY) { returnedType = KirinVariableType.BOOL; return(Convert.ToBoolean(dict[index])); } if (variable.Type == KirinVariableType.NUMBER_ARRAY) { returnedType = KirinVariableType.NUMBER; return(Convert.ToDouble(dict[index])); } } // Arithmetic if (KirinArithmetic.IsArithmetic(evaluatable, out var arithmeticResult)) { var arithmetic = new KirinArithmetic(arithmeticResult); var value = arithmetic.GetValue(reportClass); returnedType = FiMHelper.AsVariableType(value.GetType()); return(value); } // Conditional if (KirinConditional.IsConditional(evaluatable, out var conditionalResult)) { var conditional = new KirinConditional(conditionalResult); returnedType = KirinVariableType.BOOL; return(conditional.GetValue(reportClass)); } throw new FiMException("Cannot evaluate " + evaluatable); }
public override object Execute(FiMClass reportClass) { var variable = reportClass.GetVariable(this.LeftOp); if (variable == null) { throw new FiMException("Variable " + this.LeftOp + " does not exist"); } if (!FiMHelper.IsTypeArray(variable.Type) && variable.Type != KirinVariableType.STRING) { throw new FiMException("Variable " + this.LeftOp + " is not an array"); } var kIndex = new KirinValue(this.RawIndex, reportClass); if (kIndex.Type != KirinVariableType.NUMBER) { throw new FiMException("Invalid index " + kIndex.Value); } var iValue = Convert.ToInt32(kIndex.Value); var value = new KirinValue(this.RightOp, reportClass); if (variable.Type == KirinVariableType.STRING) { if (value.Type != KirinVariableType.CHAR) { throw new FiMException("Invalid array modify value"); } var sb = new StringBuilder(variable.Value as string); sb[iValue] = (char)variable.Value; variable.Value = sb.ToString(); } else { if (!FiMHelper.IsTypeOfArray(value.Type, (KirinArrayType)variable.Type)) { throw new FiMException("Invalid array modify value"); } dynamic dict; int index = Convert.ToInt32(kIndex.Value); if (variable.Type == KirinVariableType.STRING_ARRAY) { dict = variable.Value as Dictionary <int, string>; dict[index] = Convert.ToString(value.Value); } else if (variable.Type == KirinVariableType.NUMBER_ARRAY) { dict = variable.Value as Dictionary <int, double>; dict[index] = Convert.ToDouble(value.Value); } else if (variable.Type == KirinVariableType.BOOL_ARRAY) { dict = variable.Value as Dictionary <int, bool>; dict[index] = Convert.ToBoolean(value.Value); } } ; return(null); }
private static string SanitizeValue(string value, JavascriptContainer container, KirinVariableType?expectedType = null) { var eType = FiMHelper.DeclarationType.Determine(" " + value, out string eKeyword, strict: false); if (eType != KirinVariableType.UNKNOWN) { value = value.Substring(eKeyword.Length); expectedType = eType; } // Nothing if ((value == null || value == "nothing") && expectedType != null) { return(DefaultValue((KirinVariableType)expectedType)); } if (KirinLiteral.TryParse(value, out object val)) { if (val.GetType() == typeof(bool)) { return((bool)val == true ? "true" : "false"); } return(value); } // Calling an existing method if (container.report.GetParagraphLazy(value) != null) { string args = ""; string pName = value; if (pName.Contains(KirinFunctionCall.FunctionParam)) { int pIndex = pName.IndexOf(KirinFunctionCall.FunctionParam); pName = pName.Substring(0, pIndex); args = string.Join(", ", value .Substring(pName.Length + KirinFunctionCall.FunctionParam.Length) .Split(new string[] { " and " }, StringSplitOptions.None) .Select(v => SanitizeValue(v, container)) ); } if (container.internalFunctions.ContainsKey(pName)) { return($"{ container.internalFunctions[pName] }({ args })"); } return($"this.{ SanitizeName(pName, container) }({ args })"); } // Array if (expectedType != null && FiMHelper.IsTypeArray((KirinVariableType)expectedType)) { string args = string.Join(", ", value .Split(new string[] { " and " }, StringSplitOptions.None) .Select(v => SanitizeValue(v, container)) ); return($"[, { args }]"); } // Array index if (IsArrayIndex(value)) { var match = GetArrayIndex(value); var index = SanitizeValue(match.RawIndex, container); var variable = SanitizeValue(match.RawVariable, container); return($"{ variable }[ fim_index({ variable }, {index}) ]"); } // Arithmetic if (KirinArithmetic.IsArithmetic(value, out var arithmeticResult)) { return(ParseArithmeticNodes(arithmeticResult, container)); } // Conditional if (KirinConditional.IsConditional(value, out var conditionalResult)) { return(ParseConditionalNodes(conditionalResult, container)); } // Calling an existing variable (hopefully) return(SanitizeName(value, container)); }