示例#1
0
        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));
            }
        }
示例#2
0
文件: AST.cs 项目: JustCodiex/H-Sharp
 public void SetSource(SourceProjectFile src)
 {
     if (this.Source is null)
     {
         this.Source = src;
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
示例#3
0
 public static SourceProject FromText(IEnumerable <string> content, string name, string output, SourceProjectType projectType)
 => new SourceProject(name, output, projectType, SourceProjectFile.FromText(ToSingleText(content)));