public List <object> Resolve(ITokenErrLog errLog, object scope = null) { List <object> results = new List <object>(); SyntaxTree.ResolveTerms(errLog, scope, tokens, 0, tokens.Count, results); return(results); }
public static List <object> ResolveTerms(ITokenErrLog tok, object scope, List <Token> tokens, ResolvedEnoughDelegate isItResolvedEnough = null) { List <object> results = new List <object>(); ResolveTerms(tok, scope, tokens, 0, tokens.Count, results, isItResolvedEnough); return(results); }
public static void ResolveTerms(ITokenErrLog tok, object scope, List <Token> tokens, int start, int length, List <object> results, ResolvedEnoughDelegate isItResolvedEnough = null) { List <int> found = new List <int>(); FindTerms(tokens, start, length, found); for (int i = 0; i < found.Count; ++i) { Token t = tokens[found[i]]; object result = t.Resolve(tok, scope, isItResolvedEnough); results.Add(result); // if this token is probably a method call, or there are arguments immediately after this token (so maybe a method call) Invocation mc = result as Invocation; if (mc != null || (i < found.Count - 1 && found[i + 1] == found[i] + 1)) { object target = mc != null ? mc.target : scope; object methodName = mc != null ? mc.methodName : result; Token methodArgs = tokens[found[i + 1]]; if (Invocation.TryExecuteFunction(target, methodName, methodArgs, out result, tok, isItResolvedEnough)) { ++i; results[results.Count - 1] = result; } } } }
public string ResolveString(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null) { object result = Resolve(tok, scope, isItResolvedEnough); if (result == null) { return(null); } return(result.ToString()); }
public static bool ShowErrorTo(this ITokenErrLog self, Show.PrintFunc show) { string errStr = self.GetErrorString(); if (string.IsNullOrEmpty(errStr)) { return(false); } show.Invoke(errStr); return(true); }
public void RefreshColumnText(int column, ITokenErrLog errLog) { for (int row = 0; row < contentRectangle.childCount; ++row) { GameObject fieldUi = contentRectangle.GetChild(row).GetChild(column).gameObject; object value = data.RefreshValue(row, column, errLog); if (errLog.HasError()) { return; } if (value != null) { UiText.SetText(fieldUi, value.ToString()); //sb.Append(value.ToString() + ", "); } else { UiText.SetText(fieldUi, ""); } } }
public object Resolve(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null) { DelimOp op = sourceMeta as DelimOp; if (op != null) { return(op.resolve.Invoke(tok, this, scope, isItResolvedEnough)); } List <object> finalTerms = ResolveTerms(tok, scope, tokens, isItResolvedEnough); object result = finalTerms; if (rules != null && rules.Simplify != null) { if (isItResolvedEnough != null && isItResolvedEnough.Invoke(result)) { return(result); } result = rules.Simplify.Invoke(finalTerms); } return(result); }
public object Resolve(ITokenErrLog tok, object scope, ResolvedEnoughDelegate isItResolvedEnough = null) { if (isItResolvedEnough != null && isItResolvedEnough(this)) { return(this); } if (index == -1 && length == -1) { return(meta); } if (meta == null) { throw new NullReferenceException("can't resolve NULL token"); } switch (meta) { case string s: { string str = ToString(s); //Show.Log("@@@ "+str+" "+scope); if (scope != null && (isItResolvedEnough == null || isItResolvedEnough.Invoke(str))) { if (CodeRules.op_SearchForMember(str, out object value, out Type type, scope)) { //Show.Log(str+" "+foundIt+" "+value); return(value); } } return(str); } case TokenSubstitution ss: return(ss.value); case Delim d: return(d.text); case SyntaxTree pce: return(pce.Resolve(tok, scope, isItResolvedEnough)); } throw new DecoderFallbackException(); }
private static int TryConvertArgs(IList <object> args, IList <object> finalArgs, ParameterInfo[] pi, ITokenErrLog tok, Token argsToken) { for (int i = 0; i < args.Count; ++i) { try { finalArgs[i] = Convert.ChangeType(args[i], pi[i].ParameterType); } catch (Exception) { return(i); } } return(args.Count); }
public static ParseError AddError(this ITokenErrLog self, Token token, string message) { return(AddError(self, token.index, message)); }
/// <summary> /// used to resolve tokens /// </summary> /// <param name="token"></param> /// <param name="tokenizer"></param> /// <param name="scope"></param> /// <param name="data"></param> /// <returns></returns> public static bool TryParse(Token token, out object data, object scope, ITokenErrLog tokenizer) { CodeRules.op_ResolveToken(tokenizer, token, scope, out data, out Type resultType); return(resultType != null); }
private static bool DeterminePossibleMethods(object scope, string funcName, out List <MethodInfo> possibleMethods, ITokenErrLog tok, Token argsToken) { if (scope == null) { tok.AddError(argsToken, $"can't execute function \'{funcName}\' without scope"); possibleMethods = null; return(false); } possibleMethods = scope.GetType().GetMethods().FindAll(m => m.Name == funcName); if (possibleMethods.Count == 0) { tok.AddError(argsToken, $"missing function \'{funcName}\' in {scope.GetType()}"); return(false); } return(true); }
private static List <object> ResolveFunctionArgumentList(Token argsToken, object scope, ITokenErrLog tok, ResolvedEnoughDelegate isItResolvedEnough) { object argsRaw = argsToken.Resolve(tok, scope, isItResolvedEnough); if (argsRaw == null) { argsRaw = new List <object>(); } List <object> args = argsRaw as List <object>; if (args == null) { args = new List <object> { argsRaw }; } // remove commas if they are comma tokens before and after being parsed SyntaxTree beforeParse = argsToken.GetAsSyntaxNode(); for (int i = args.Count - 1; i >= 0; --i) { if ((args[i] as string) == "," && beforeParse.tokens[i + 1].StringifySmall() == ",") { args.RemoveAt(i); } } return(args); }
public static ParseError AddError(this ITokenErrLog self, int index, string message) { ParseError e = new ParseError(index, self.GetTextRows(), message); self.AddError(e); return(e); }
private static bool TryExecuteFunction(object scope, string funcName, Token argsToken, out object result, ITokenErrLog tok, ResolvedEnoughDelegate isItResolvedEnough) { result = null; if (!DeterminePossibleMethods(scope, funcName, out List <MethodInfo> possibleMethods, tok, argsToken)) { return(false); } List <object> args = ResolveFunctionArgumentList(argsToken, scope, tok, isItResolvedEnough); if (!DetermineValidMethods(funcName, argsToken, possibleMethods, out List <ParameterInfo[]> validParams, args, tok)) { return(false); } if (!DetermineMethod(args, possibleMethods, validParams, out MethodInfo mi, out object[] finalArgs, tok, argsToken)) { return(false); } return(ExecuteMethod(scope, mi, finalArgs, out result, tok, argsToken)); }
public static object GetValueFromRawPath(object obj, IList <object> rawPath, object defaultValue = null, List <object> out_compiledPath = null, ITokenErrLog errLog = null) { object result = obj; for (int i = 0; i < rawPath.Count; ++i) { string pathStr = rawPath[i].ToString(); result = GetValueIndividual(obj, pathStr, out object path, defaultValue); if (path == null && errLog != null) { errLog.AddError(i, pathStr); } //Show.Log(obj+"["+ pathStr + "] = ("+path+") \'"+result+"\'"); if (out_compiledPath != null) { out_compiledPath.Add(path); } bool done = i == rawPath.Count - 1; if (!done) { if (result == null) { return(defaultValue); } } obj = result; } return(result); }
public static bool TryExecuteFunction(object scope, object resolvedFunctionIdentifier, Token arguments, out object funcResult, ITokenErrLog errLog, ResolvedEnoughDelegate isItResolvedEnough = null) { string funcName = GetMethodCall(resolvedFunctionIdentifier, arguments); if (funcName != null && TryExecuteFunction(scope, funcName, arguments, out funcResult, errLog, isItResolvedEnough)) { return(true); } funcResult = null; return(false); }
private static bool DetermineMethod(List <object> args, List <MethodInfo> possibleMethods, List <ParameterInfo[]> validParams, out MethodInfo mi, out object[] finalArgs, ITokenErrLog tok, Token argsToken) { mi = null; finalArgs = new object[args.Count]; for (int paramSet = 0; paramSet < validParams.Count; ++paramSet) { bool typesOk = true; ParameterInfo[] pi = validParams[paramSet]; int a; if ((a = TryConvertArgs(args, finalArgs, pi, tok, argsToken)) != args.Count // it's only a problem if there are no other options && paramSet == validParams.Count - 1) { tok.AddError(argsToken, $"can't convert \'{args[a]}\' to {pi[a].ParameterType} for {possibleMethods[paramSet].Name}{argsToken.Stringify()}"); } if (typesOk) { mi = possibleMethods[paramSet]; return(true); } } return(false); }
private static bool DetermineValidMethods(string funcName, Token argsToken, List <MethodInfo> possibleMethods, out List <ParameterInfo[]> validParams, IList <object> args, ITokenErrLog tok) { ParameterInfo[] pi; validParams = new List <ParameterInfo[]>(); List <ParameterInfo[]> invalidParams = new List <ParameterInfo[]>(); for (int i = possibleMethods.Count - 1; i >= 0; --i) { pi = possibleMethods[i].GetParameters(); if (pi.Length != args.Count) { possibleMethods.RemoveAt(i); invalidParams.Add(pi); continue; } validParams.Add(pi); } // check arguments. start with the argument count if (possibleMethods.Count == 0) { tok.AddError(argsToken, $"'{funcName}' needs {invalidParams.JoinToString(" or ", par => par.Length.ToString())} arguments, not {args.Count} from {args.StringifySmall()}"); return(false); } return(true); }
public static bool TrySetValueCompiledPath(object scope, IList <object> alreadyCompiledPath, object result, ITokenErrLog errLog = null) { void Err(ITokenErrLog eLog, int index) { if (eLog == null) { return; } string errStr = ""; for (int e = 0; e < index; ++e) { errStr += alreadyCompiledPath[e].ToString() + "."; } eLog.AddError(index, errStr + "failed"); } object cursor = scope; int last = alreadyCompiledPath.Count - 1; for (int i = 0; i < last; ++i) { if (!TryGetValueCompiled(cursor, alreadyCompiledPath[i], out cursor)) { Err(errLog, i); return(false); } } if (!TrySetValueCompiled(cursor, alreadyCompiledPath[last], result)) { Err(errLog, last); return(false); } return(true); }
private static bool ExecuteMethod(object scope, MethodInfo mi, object[] finalArgs, out object result, ITokenErrLog tok, Token argsToken) { try { result = mi.Invoke(scope, finalArgs); } catch (Exception e) { result = null; tok.AddError(argsToken, e.ToString()); return(false); } return(true); }
public static void Log(this ITokenErrLog self) { self.ShowErrorTo(NonStandard.Show.Log); }