public static decimal? Compile(string sourceCode) { // create a compiler from the grammar FlGrammar grammar = new FlGrammar(); LanguageCompiler compiler = new LanguageCompiler(grammar); // Attempt to compile into an Abstract Syntax Tree. Because FLGrammar // defines the root node as ProgramNode, that is what will be returned. // This happens to implement IJavaScriptGenerator, which is what we need. IExpressionGenerator program = (IExpressionGenerator)compiler.Parse(sourceCode); if (program == 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]; throw new CompilationException(message); } // now just instruct the compilation of to javascript //StringBuilder js = new StringBuilder(); var expression = program.GenerateExpression(null); return ((Expression<Func<decimal?>>)expression).Compile()(); }
//Used in unit tests public static LanguageCompiler CreateDummy() { GrammarData data = new GrammarData(); data.Grammar = new Grammar(); LanguageCompiler compiler = new LanguageCompiler(data); return(compiler); }
public SearchHelper(IClientDocumentRepository clientDocumentRepository) { this.clientDocumentRepository = clientDocumentRepository; _grammar = new SearchGrammar(); _compiler = new LanguageCompiler(_grammar); Irony.StringSet errors = _compiler.Parser.GetErrors(); if (errors.Count > 0) { throw new ApplicationException("SearchGrammar contains errors. Investigate using GrammarExplorer." + errors.ToString()); } }
//Used in unit tests public static CompilerContext CreateDummy() { CompilerContext ctx = new CompilerContext(LanguageCompiler.CreateDummy()); return(ctx); }
public CompilerContext(LanguageCompiler compiler) { this.Compiler = compiler; }
//Used in unit tests public static LanguageCompiler CreateDummy() { GrammarData data = new GrammarData(); data.Grammar = new Grammar(); LanguageCompiler compiler = new LanguageCompiler(data); return compiler; }
/// <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 }; }