private void addError(String message) { var err = new CodeError(SourceCode.Substring(tokenLeft_, currentIdx_ - tokenLeft_), message); err.LineNumber = lineCount_; err.ColumeNumber = tokenLeft_ - currentLineIdx_; ErrorList.Add(err); }
public void WriteErrorMsgs(CodeError code) { switch (code) { case CodeError.InputError: Console.WriteLine("Input Error. Please input as the following: LineIndex,ColIndex"); break; case CodeError.MoveImpossible: Console.WriteLine("Not a Possible Move!\nPlease Input a Possible Move from the List"); break; case CodeError.QuitInputError: Console.WriteLine("Input Error. Press Q to Quit or C to Continue!"); break; } }
public T RequestService <T>(Func <T> action) { try { var result = action(); logger.Info(ControllerContext.ActionDescriptor.ActionName + "API Result: " + JsonConvert.SerializeObject(result, Formatting.None)); return(result); } catch (Exception ex) { var objectReturn = Activator.CreateInstance <T>(); var objectType = objectReturn.GetType(); if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(ApplicationResponse <>)) { objectReturn.GetType().GetProperties().First(x => x.Name == "Message").SetValue(objectReturn, VersatileFunctionsEnum.GetDescription(CodeErrorType.UnexpectedError)); objectReturn.GetType().GetProperties().First(x => x.Name == "Status").SetValue(objectReturn, Enum.ToObject(objectReturn.GetType().GetProperties().First(x => x.Name == "Status").PropertyType, 3)); } else if (objectType.BaseType == typeof(ResultDTO)) { objectReturn.GetType().GetProperties().First(x => x.Name == "ReturnExecution") .SetValue(objectReturn, (int)ReturnExecution.WITH_ERROR); var errors = new List <ErrorResultDTO>() { new ErrorResultDTO() { ErrorCode = CodeError.GetCodeError(CodeErrorType.UnexpectedError), ErrorText = VersatileFunctionsEnum.GetDescription(CodeErrorType.UnexpectedError) } }; objectReturn.GetType().GetProperties().First(x => x.Name == "Errors").SetValue(objectReturn, errors); } else { throw new Exception("BaseController.RequestService exception not handled for type " + objectType.Name); } logger.Info(ControllerContext.ActionDescriptor.ActionName + "API Result: Exception " + ex.GetType().ToString() + " Message: " + ex.Message + (ex.InnerException != null ? " inner ex: " + ex.InnerException.Message : "")); logger.Error("API Result Excepion Detail.", ex); return(objectReturn); } }
public TestCodeViewModel RunCSharp(TestCodeViewModel vm) { SyntaxTree tree = CSharpSyntaxTree.ParseText(vm.TheCode); // find out where all those sweet .dlls live string assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); // references to add to the assembly MetadataReference[] references = new MetadataReference[] { MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.IO.dll")), MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")) }; // set up compiler CSharpCompilation csc = CSharpCompilation.Create("Cts" , new[] { tree } , references , new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var ms = new MemoryStream()) { // attempt compilation EmitResult result = csc.Emit(ms); if (!result.Success) { //compilation failed IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); foreach (Diagnostic diagnostic in failures) { string sPattern = @"\((\d+),"; var lineNumber = int.Parse((Regex.Match(diagnostic.ToString(), sPattern)).Groups[1].Value); CodeError error = new CodeError(lineNumber, diagnostic.Id, diagnostic.GetMessage()); vm.AddError(error); vm.ErrorMessage += "Line: " + error.LineNumber + "\nID: " + error.ID + "\nMessage: " + error.Message + "\n\n"; } return(vm); } else { //compilation succeeded! ms.Seek(0, SeekOrigin.Begin); Assembly assembly = Assembly.Load(ms.ToArray()); TextWriter StandardOut = Console.Out; TextReader StandardIn = Console.In; Type type = assembly.GetType("Cts.Program"); object obj = Activator.CreateInstance(type); //iterate over inputs for (int testCaseNumber = 0; testCaseNumber < vm.TestCases.Count; testCaseNumber++) { MemoryStream mem = new MemoryStream(1000); StreamWriter writer = new StreamWriter(mem); Console.SetOut(writer); TextReader r = new StringReader(vm.TestCases[testCaseNumber].Input); Console.SetIn(r); //TODO: verify that the method exists before invoking it, and throw appropriate error if it's missing // sample code here: http://stackoverflow.com/questions/14479074/c-sharp-reflection-load-assembly-and-invoke-a-method-if-it-exists type.InvokeMember("Run" , BindingFlags.Default | BindingFlags.InvokeMethod , null , obj , null); r.Close(); writer.Close(); string s = Encoding.Default.GetString(mem.ToArray()); mem.Close(); vm.TestCases[testCaseNumber].ActualOutput = s; } Console.SetOut(StandardOut); Console.SetIn(StandardIn); return(vm); } } }