/// <summary> /// Generates JavaScript based on BASIC source code, and returns a /// CopmileResult object containing the compiled source code if /// successful, or otherwise error messages. /// </summary> public static CompileResult Generate(string sourceCode) { // Create a BASIC compiler BasicGrammar basicGrammer = new BasicGrammar(); LanguageCompiler compiler = new LanguageCompiler(basicGrammer); // Compile the source code into an Abstract Syntax Tree. ProgramNode rootNode; try { rootNode = (ProgramNode)compiler.Parse(sourceCode + Environment.NewLine); } catch (BasicSyntaxErrorException bsee) { rootNode = null; compiler.Context.Errors.Add(new SyntaxError(new SourceLocation(), bsee.Message, null)); } if (rootNode == null || compiler.Context.Errors.Count > 0) { // Didn't compile. Generate an error message. SyntaxError error = compiler.Context.Errors[0]; string location = string.Empty; if (error.Location.Line > 0 && error.Location.Column > 0) { location = "Line " + (error.Location.Line + 1) + ", column " + (error.Location.Column + 1); } string message = location + ": " + error.Message + ":" + Environment.NewLine; message += sourceCode.Split('\n')[error.Location.Line]; // Return failure. return(new CompileResult() { IsSuccessful = false, ResultMessage = message }); } // Set up the types of lines (e.g. whether a given line is the first, // last, or internal line of a function) and get the starting function // to call. string firstFunctionName = SetLineTypes(rootNode); // Now generate JavaScript from an abstract syntax tree. string code; StringBuilder sb = new StringBuilder(); using (TextWriter tw = new StringWriter(sb, CultureInfo.InvariantCulture)) { rootNode.GenerateJavaScript(new JSContext(), tw); code = sb.ToString(); } // Return the JavaScript code. return(new CompileResult() { IsSuccessful = true, ResultMessage = "Successfully compiled in " + compiler.CompileTime + "ms", JavaScript = code, StartFunction = firstFunctionName }); }
/// <summary> /// Generates JavaScript based on BASIC source code, and returns a /// CopmileResult object containing the compiled source code if /// successful, or otherwise error messages. /// </summary> public static CompileResult Generate(string sourceCode) { // Create a BASIC compiler BasicGrammar basicGrammer = new BasicGrammar(); LanguageCompiler compiler = new LanguageCompiler(basicGrammer); // Compile the source code into an Abstract Syntax Tree. ProgramNode rootNode; try { rootNode = (ProgramNode)compiler.Parse(sourceCode + Environment.NewLine); } catch (BasicSyntaxErrorException bsee) { rootNode = null; compiler.Context.Errors.Add(new SyntaxError(new SourceLocation(), bsee.Message, null)); } if (rootNode == null || compiler.Context.Errors.Count > 0) { // Didn't compile. Generate an error message. SyntaxError error = compiler.Context.Errors[0]; string location = string.Empty; if (error.Location.Line > 0 && error.Location.Column > 0) { location = "Line " + (error.Location.Line + 1) + ", column " + (error.Location.Column + 1); } string message = location + ": " + error.Message + ":" + Environment.NewLine; message += sourceCode.Split('\n')[error.Location.Line]; // Return failure. return new CompileResult() { IsSuccessful = false, ResultMessage = message }; } // Set up the types of lines (e.g. whether a given line is the first, // last, or internal line of a function) and get the starting function // to call. string firstFunctionName = SetLineTypes(rootNode); // Now generate JavaScript from an abstract syntax tree. string code; StringBuilder sb = new StringBuilder(); using (TextWriter tw = new StringWriter(sb, CultureInfo.InvariantCulture)) { rootNode.GenerateJavaScript(new JSContext(), tw); code = sb.ToString(); } // Return the JavaScript code. return new CompileResult() { IsSuccessful = true, ResultMessage = "Successfully compiled in " + compiler.CompileTime + "ms", JavaScript = code, StartFunction = firstFunctionName }; }