public void ApplyOperators() { List <int[]> paths = FindTokenPaths(t => t.meta is DelimOp); paths.Sort((a, b) => { int comp; comp = b.Length.CompareTo(a.Length); if (comp != 0) { return(comp); } Context.Entry e = null; Token ta = GetTokenAt(tokens, a, ref e); Token tb = GetTokenAt(tokens, b, ref e); DelimOp da = ta.meta as DelimOp; DelimOp db = tb.meta as DelimOp; comp = da.order.CompareTo(db.order); if (comp == 0) { comp = ta.index.CompareTo(tb.index); } return(comp); }); for (int i = 0; i < paths.Count; ++i) { Context.Entry pathNode = null; Token t = GetTokenAt(tokens, paths[i], ref pathNode); DelimOp op = t.meta as DelimOp; Context.Entry opEntry = op.isSyntaxValid.Invoke(this, pathNode.tokens, paths[i][paths[i].Length - 1]); if (pathNode.tokenCount != pathNode.tokens.Count) { pathNode.tokenCount = pathNode.tokens.Count; } } }
public object Resolve(Tokenizer tok, object scope, bool simplify = true) { DelimOp op = sourceMeta as DelimOp; if (op != null) { return(op.resolve.Invoke(tok, this, scope)); } if (IsText()) { return(Unescape()); } return(Resolve(tok, scope, tokens, simplify)); }
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); }
protected void ApplyOperators() { int SortByOrderOfOperations(TokenPath a, TokenPath b) { int comp; comp = b.path.Length.CompareTo(a.path.Length); if (comp != 0) { return(comp); } //Context.Entry e = null; Token ta = a.token; //GetTokenAt(tokens, a, ref e); Token tb = b.token; //GetTokenAt(tokens, b, ref e); DelimOp da = ta.meta as DelimOp; DelimOp db = tb.meta as DelimOp; comp = da.order.CompareTo(db.order); // do the last one in the line first, so future indexes don't get offset as the tokens are combined if (comp == 0) { comp = ta.index.CompareTo(tb.index); } return(comp); } List <TokenPath> paths = FindTokenPaths(t => t.meta is DelimOp); paths.Sort(SortByOrderOfOperations); List <int> finishedTokens = new List <int>(); bool operatorWasLostInTheShuffle; do { operatorWasLostInTheShuffle = false; for (int i = 0; i < paths.Count; ++i) { SyntaxTree pathNode = null; Token t = GetTokenAt(tokens, paths[i].path, ref pathNode); if (t == Token.None) { t = pathNode?.GetBeginToken() ?? this.tokens[0]; AddError(t, "can't resolve " + t.GetAsSmallText() + ", missing token"); return; } if (paths[i].token.index != t.index) { operatorWasLostInTheShuffle = true; continue; // break instead? } DelimOp op = t.meta as DelimOp; //if(op == null || pathNode == null || paths == null || paths[i].path == null) { // Show.Log("oof"); //} List <Token> listWhereOpWasFound = pathNode != null ? pathNode.tokens : tokens; //Context.Entry opEntry = op.isSyntaxValid.Invoke(this, listWhereOpWasFound, paths[i].path[paths[i].path.Length - 1]); if (pathNode != null && pathNode.TokenCount != pathNode.tokens.Count) { pathNode.tokenCount = pathNode.tokens.Count; } finishedTokens.Add(t.index); } if (operatorWasLostInTheShuffle) { paths = FindTokenPaths(t => t.meta is DelimOp && finishedTokens.IndexOf(t.index) < 0); paths.Sort(SortByOrderOfOperations); } } while (operatorWasLostInTheShuffle); }