void ProcessCommand(string line) { Match m = cmdSplit.Match(line); string cmd = m.Groups[1].ToString(); string arg = m.Groups[2].ToString().TrimStart(null); switch (cmd) { case "n": AddNamespace(arg); break; case "r": AddReference(arg); break; case "v": foreach (string v in varTable.Keys) { Utils.Print(v + " = " + varTable[v]); } break; case "dcl": MustDeclare = !MustDeclare; break; case "code": // show code sent to compiler! showCode = !showCode; break; default: // a macro may be used as a command; the line is split up and // and supplied as arguments. // For macros taking one argument, the whole line is supplied. MacroEntry me = macro.Lookup(cmd); if (me != null && me.Parms != null) { string[] parms; if (me.Parms.Length > 1) { parms = spaces.Split(arg); } else { parms = new string[] { arg } }; string s = macro.ReplaceParms(me, parms); ExecuteLine(s); } else { Utils.Print("unrecognized command, or bad macro"); } break; } }
CompilerResults CompileLine(string codeStr, CHash type, string assemblyName, string className) { CompilerParameters cp = new CompilerParameters(); if (type == CHash.Function) { cp.OutputAssembly = assemblyName; } else { cp.GenerateInMemory = true; } foreach (string r in referenceList) { #if DEBUG if (!System.Diagnostics.Debugger.IsAttached) { Utils.Print(r); } #endif cp.ReferencedAssemblies.Add(r); } string exprStr = codeStr; returnsValue = false; if (type == CHash.Expression) { if (codeStr[0] != '{' && !word_within(firstToken(codeStr), keywords)) { returnsValue = true; exprStr = "V[\"_\"] = " + codeStr; } } CompilerResults cr = CompileTemplate(cp, exprStr, type, className); if (cr.Errors.HasErrors) { if (returnsValue) { // we assumed that this expression did return a value; we were wrong. // Try it again, without assignment to $_ returnsValue = false; cp.OutputAssembly = null; // Reset value, which is needed for Mono to work cr = CompileTemplate(cp, codeStr, CHash.Expression, ""); if (!cr.Errors.HasErrors) { return(cr); } } ShowErrors(cr, codeStr); return(null); } else { return(cr); } }
void ShowErrors(CompilerResults cr, string codeStr) { StringBuilder sbErr; sbErr = new StringBuilder("Compiling string: "); sbErr.AppendFormat("'{0}'\n\n", codeStr); foreach (CompilerError err in cr.Errors) { sbErr.AppendFormat("{0}\n", err.ErrorText); } Utils.Print(sbErr.ToString()); }
void ShowErrors(CompilerResults cr, string codeStr) { StringBuilder sbErr; sbErr = new StringBuilder("Compiling string: "); sbErr.AppendFormat("'{0}'\n\n", codeStr); foreach (CompilerError err in cr.Errors) { sbErr.AppendFormat( "{0}{1}\n", (err.ErrorText ?? string.Empty).Trim(), (string.IsNullOrEmpty(err.ErrorNumber) ? string.Empty : (" [" + err.ErrorNumber + "]"))); } Utils.Print(sbErr.ToString()); }
CompilerResults CompileTemplate(CompilerParameters cp, string codeStr, CHash type, string className) { if (showCode) { Utils.Print("code:", codeStr); } string finalSource = CodeChunk.Template; if (type == CHash.Function) { finalSource = CsiFunctionContext.Template; } finalSource = finalSource.Replace("$USES$", namespaceString); finalSource = finalSource.Replace("$BODY$", codeStr); if (type == CHash.Function) { finalSource = finalSource.Replace("$CLASS$", className); } return(prov.CompileAssemblyFromSource(cp, finalSource)); }
CompilerResults CompileLine(string codeStr, CHash type, string assemblyName, string className) { CompilerParameters cp = new CompilerParameters(); if (type == CHash.Function) { cp.OutputAssembly = assemblyName; } else { cp.GenerateInMemory = true; } foreach (string r in referenceList) { #if DEBUG if (!System.Diagnostics.Debugger.IsAttached) { Utils.Print(r); } #endif cp.ReferencedAssemblies.Add(r); } string exprStr = codeStr; returnsValue = false; if (type == CHash.Expression) { if (codeStr[0] != '{' && !word_within(firstToken(codeStr), keywords)) { returnsValue = true; exprStr = "V[\"_\"] = " + codeStr; } } CompilerResults cr = CompileTemplate(cp, exprStr, type, className); if (cr.Errors.HasErrors) { if (returnsValue) { // we assumed that this expression did return a value; we were wrong. // Try it again, without assignment to $_ returnsValue = false; cp.OutputAssembly = null; // Reset value, which is needed for Mono to work CompilerResults cr2 = CompileTemplate(cp, codeStr, CHash.Expression, ""); if (!cr2.Errors.HasErrors) { return(cr2); } try { bool firstErrorIsTypeConversion = false; foreach (CompilerError err in cr.Errors) { // Check for "Cannot implicitly convert type `void' to `object'" if (string.Equals("CS0029", err.ErrorNumber, StringComparison.OrdinalIgnoreCase) && (!string.IsNullOrEmpty(err.ErrorText)) && (err.ErrorText.IndexOf("void", 0, StringComparison.OrdinalIgnoreCase) >= 0)) { firstErrorIsTypeConversion = true; break; } } bool secondErrorIsTooCommon = false; foreach (CompilerError err in cr2.Errors) { // Check for "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" if (string.Equals("CS0201", err.ErrorNumber, StringComparison.OrdinalIgnoreCase)) { secondErrorIsTooCommon = true; break; } } // Usually show the second error, unless it is not very // informative and the first error is unlikely to have // been caused by our editing of the expression string if ((!secondErrorIsTooCommon) || (firstErrorIsTypeConversion)) { cr = cr2; } } catch { // Assume that most recent error is mostly appropriate cr = cr2; } } ShowErrors(cr, codeStr); return(null); } else { return(cr); } }