public static void Execute(string code, MethodCallDelegate MethodCall, TransformCompiledDelegate TransformCompiled, out string output, out string warnings) { output = ""; warnings = ""; // Tokenize code Tokenizer.Fragment[] fragments = Tokenizer.Tokenize(code); if(fragments.Length == 0) return; /*string strtokens = ""; foreach(Tokenizer.Fragment fragment in fragments) strtokens += fragment.ToString() + " "; //strtokens += code.Substring(fragment.startidx, fragment.endidx - fragment.startidx) + " "; System.Windows.Forms.MessageBox.Show(code + "\n\n" + strtokens);*/ // Interpret first fragment as statement string statement = fragments[0].GetString(code); if(fragments[0].token != Tokenizer.Token.Var) throw new Exception("Expected statement instead of " + statement); int creator; if(transformCreators.TryGetValue(statement, out creator)) { // Treat code as transform creator statement //EDIT } else { // Treat code as method call //EDIT } // Interpret clauses IEnumerable<csharp_viewer.TransformedImage> scope = null; string[] byExpr = null; HashSet<int> byExpr_usedArgumentIndices = null; bool byExpr_isTemporal = false; int lastfragment = fragments.Length - 1; for(int i = fragments.Length - 1; i > 0; --i) { if(!Tokenizer.IsClause(fragments[i].token)) continue; if(i == lastfragment) throw new Exception(string.Format("Expected expression after '{0}' clause", fragments[i].GetString(code))); if(fragments[i].token == Tokenizer.Token.Where) { // Compile 'where' block into imageEnumerator scope = CompileScopeCondition(code, fragments, i + 1, lastfragment, ref warnings); } else if(fragments[i].token == Tokenizer.Token.By) { // Parse 'by' block into byExpr byExpr_usedArgumentIndices = new HashSet<int>(); byExpr = ParseExpression(code, fragments, i + 1, lastfragment, byExpr_usedArgumentIndices, out byExpr_isTemporal); } lastfragment = i - 1; } if(scope == null && fragments[lastfragment].token == Tokenizer.Token.Var && csharp_viewer.Viewer.groups.TryGetValue((string)fragments[lastfragment].value, out scope)) --lastfragment; List<object> args = new List<object>(); for(int i = 1; i <= lastfragment; ++i) { if(fragments[i].token == Tokenizer.Token.Str) args.Add(((string)fragments[i].value).Replace("\\\"", "\"")); if(fragments[i].token == Tokenizer.Token.TfId) args.Add(fragments[i].value); } if(args.Count == 0 && lastfragment >= 1) { CompilerErrorCollection errors; object[] newargs; if(csharp_viewer.Console.Eval(code.Substring(fragments[1].startidx, fragments[lastfragment].endidx - fragments[1].startidx), out newargs, out errors, null, new string[] { System.Reflection.Assembly.GetEntryAssembly().Location })) args.AddRange(newargs); else { foreach(CompilerError error in errors) output += error.ErrorText + '\n'; output.TrimEnd(new char[] { '\n' }); } } if(byExpr != null) { args.Add(byExpr); args.Add(byExpr_usedArgumentIndices); args.Add(byExpr_isTemporal); } if(scope != null) args.Add(scope); output += MethodCall(statement, args.ToArray(), code); }
public static void Execute(string code, MethodCallDelegate MethodCall, TransformCompiledDelegate TransformCompiled, out string output, out string warnings) { output = ""; warnings = ""; // Tokenize code Tokenizer.Fragment[] fragments = Tokenizer.Tokenize(code); if (fragments.Length == 0) { return; } /*string strtokens = ""; * foreach(Tokenizer.Fragment fragment in fragments) * strtokens += fragment.ToString() + " "; * //strtokens += code.Substring(fragment.startidx, fragment.endidx - fragment.startidx) + " "; * * System.Windows.Forms.MessageBox.Show(code + "\n\n" + strtokens);*/ // Interpret first fragment as statement string statement = fragments[0].GetString(code); if (fragments[0].token != Tokenizer.Token.Var) { throw new Exception("Expected statement instead of " + statement); } int creator; if (transformCreators.TryGetValue(statement, out creator)) { // Treat code as transform creator statement //EDIT } else { // Treat code as method call //EDIT } // Interpret clauses IEnumerable <csharp_viewer.TransformedImage> scope = null; string[] byExpr = null; HashSet <int> byExpr_usedArgumentIndices = null; bool byExpr_isTemporal = false; int lastfragment = fragments.Length - 1; for (int i = fragments.Length - 1; i > 0; --i) { if (!Tokenizer.IsClause(fragments[i].token)) { continue; } if (i == lastfragment) { throw new Exception(string.Format("Expected expression after '{0}' clause", fragments[i].GetString(code))); } if (fragments[i].token == Tokenizer.Token.Where) { // Compile 'where' block into imageEnumerator scope = CompileScopeCondition(code, fragments, i + 1, lastfragment, ref warnings); } else if (fragments[i].token == Tokenizer.Token.By) { // Parse 'by' block into byExpr byExpr_usedArgumentIndices = new HashSet <int>(); byExpr = ParseExpression(code, fragments, i + 1, lastfragment, byExpr_usedArgumentIndices, out byExpr_isTemporal); } lastfragment = i - 1; } if (scope == null && fragments[lastfragment].token == Tokenizer.Token.Var && csharp_viewer.Viewer.groups.TryGetValue((string)fragments[lastfragment].value, out scope)) { --lastfragment; } List <object> args = new List <object>(); for (int i = 1; i <= lastfragment; ++i) { if (fragments[i].token == Tokenizer.Token.Str) { args.Add(((string)fragments[i].value).Replace("\\\"", "\"")); } if (fragments[i].token == Tokenizer.Token.TfId) { args.Add(fragments[i].value); } } if (args.Count == 0 && lastfragment >= 1) { CompilerErrorCollection errors; object[] newargs; if (csharp_viewer.Console.Eval(code.Substring(fragments[1].startidx, fragments[lastfragment].endidx - fragments[1].startidx), out newargs, out errors, null, new string[] { System.Reflection.Assembly.GetEntryAssembly().Location })) { args.AddRange(newargs); } else { foreach (CompilerError error in errors) { output += error.ErrorText + '\n'; } output.TrimEnd(new char[] { '\n' }); } } if (byExpr != null) { args.Add(byExpr); args.Add(byExpr_usedArgumentIndices); args.Add(byExpr_isTemporal); } if (scope != null) { args.Add(scope); } output += MethodCall(statement, args.ToArray(), code); }