private static CompileResult ParseFile(SourceProjectFile sourceFile, out AST ast) { // If not virtual, make sure the file exists if (!sourceFile.IsVirtual) { if (!File.Exists(sourceFile.Value)) { ast = null; return(new CompileResult(false, $"Source file \"{sourceFile.Value}\" not found.")); } } // Lex program Lexer lexer = new Lexer(); LexToken[] tokens = sourceFile.IsVirtual ? lexer.Lex(sourceFile.Value) : lexer.LexFile(sourceFile.Value); // Build AST and return it ASTBuilder builder = new ASTBuilder(tokens); ParsingResult result = builder.Parse(); if (result.Success) { ast = builder.Build(); ast.SetSource(sourceFile); return(new CompileResult(true)); } else { ast = null; return(new CompileResult(false)); } }
/// <summary> /// From source code, generates the assembly via Roslyn and generates the AST from the assembly using the <see cref="IASTBuilder"/>. /// </summary> /// <param name="source"></param> /// <returns></returns> public static ASTInfo BuildAST(this string source) { IAssemblyProxy assembly = new AsmlDasmlAssemblyLoader(source).Load(); var builder = new ASTBuilder(assembly); return builder.Build(); }
/// <summary> /// From source code, generates the assembly via Roslyn and generates the AST from the assembly using the <see cref="IASTBuilder"/>. /// </summary> /// <param name="source"></param> /// <returns></returns> public static SyntaxNode ExtractASTRoot(this string source) { IAssemblyProxy assembly = new AsmlDasmlAssemblyLoader(source, ScriptNamespaceAttributeHelper.AttributeSourceCode).Load(); var builder = new ASTBuilder(assembly); // ScriptSharp Reflector var astInfo = builder.Build(); // Getting the AST node var generatedTree = astInfo.Tree as CSharpSyntaxTree; if (generatedTree == null) { throw new InvalidOperationException("Invalid generated tree"); } return(generatedTree.GetRoot()); }
public CompiledScript Compile(string source, Options options = Options.None, IDictionary <string, VariableValue> variables = null) { _rpnConverter.Reset(); // Tokenization var tokens = _lexer.Analyze(source); // Parse _tokenParser.Parse(tokens); // Convert to RPN _rpnConverter.Convert(_tokenParser.Elements); // Build AST var rootNode = ASTBuilder.Build(_rpnConverter.Rpn, options, variables, _functions); // Optimize rootNode = rootNode.Optimize(); // Create script wrapper return(new CompiledScript(rootNode)); }
/// <summary> /// 编译FireML /// </summary> public FireMLRoot CompileFireML() { FireMLRoot root = new FireMLRoot(); #region 读取Asset AssetBuilder assetBuilder = new AssetBuilder(this); assetBuilder.Build(assetFiles, root); #endregion #region 构造AST ASTBuilder astBuilder = new ASTBuilder(this); astBuilder.Build(plotFiles, root); #endregion #region AST合法性检查 ASTChecker astChecker = new ASTChecker(this); astChecker.Check(root); #endregion #region 生成ID号 IDGenerator idGenerator = new IDGenerator(); idGenerator.Generate(root); #endregion #region 资源存在性检查 //ContentChecker contentChecker = new ContentChecker(this); //contentChecker.Check(root); #endregion if (errorList.Count > 0) return null; else return root; }
public void AST_Build_Fails(List <Element> elements) { Assert.Throws <RuntimeException>(() => { ASTBuilder.Build(elements, Compiler.Options.Immutable, null, null); }); }