Example #1
0
 public static void Import(string moduleName)
 {
     if (!ModuleHeaders.ContainsKey(moduleName))
     {
         // 避免重复导入
         if (File.Exists(Path.Combine(DirName, moduleName + ".xibc")))
         {
             ModuleHeaders.Add(moduleName, BinaryModule.Load(Path.Combine(DirName, moduleName + ".xibc")));
         }
         else if (File.Exists(Path.Combine(DirName, moduleName + ".xi")))
         {
             // 可能需要共同编译,因为也许互相有依赖
             // 一个可能的想法是层序遍历,先声明所有的class,再声明所有的类成员,再定义所有的类成员
             throw new NotImplementedException();
         }
         else
         {
             throw new XiLangError($"{moduleName} not found");
         }
     }
 }
Example #2
0
        public static void Main(string[] args)
        {
            ArgumentParser  argumentParser = new ArgumentParser(new ConsoleArgument());
            ConsoleArgument dirArg         = new ConsoleArgument("d", ArgumentValueType.STRING);
            ConsoleArgument verboseArg     = new ConsoleArgument("verbose");

            argumentParser.AddArgument(dirArg);
            argumentParser.AddArgument(verboseArg);

            argumentParser.Parse(args);

            string moduleName = argumentParser.DefaultRule.StringValue;

            if (dirArg.IsSet)
            {
                DirName = dirArg.StringValue;
            }

            string fileName = null;

            foreach (string f in Directory.EnumerateFiles(DirName))
            {
                string fname = Path.GetFileName(f).ToString();
                if (fname.StartsWith(moduleName + "."))
                {
                    fileName = f;
                    break;  // TODO 不break,支持同模块多文件
                }
            }

            if (string.IsNullOrEmpty(fileName))
            {
                Console.Error.WriteLine($"Module {moduleName} not found in {DirName}");
                return;
            }

            string text = File.ReadAllText(fileName);

            TokenPassManager tokenPasses = new TokenPassManager(text);

            // 解析类信息之外的部分并生成AST
            AST root = (AST)tokenPasses.Run(new Parser());

            ASTPassManager astPasses = new ASTPassManager(root);

            //, 打印json文件
            if (verboseArg.IsSet)
            {
                string json = (string)astPasses.Run(new JsonPass());
                File.WriteAllText(fileName + ".ast.json", json);
            }

            // 编译生成ir与字节码,编译阶段会完成常量表达式的估值
            ModuleConstructor constructor = new ModuleConstructor(moduleName);

            ModuleHeaders.Add(moduleName, constructor.Module);
            List <Class> classes = (List <Class>)astPasses.Run(new ClassDeclarationPass(constructor));

            (List <Field> fields, List <Method> methods) = ((List <Field> fields, List <Method> methods))
                                                           astPasses.Run(new MemberDeclarationPass(constructor, classes));
            astPasses.Run(new CodeGenPass(constructor, classes, fields, methods));

            // 输出生成字节码
            constructor.Dump(DirName, verboseArg.IsSet);
        }