Exemple #1
0
        private static void LoadImport(string name, AST.Root root)
        {
            var filename = Path.Combine(Environment.CurrentDirectory, CacheDir, name + ".dll");

            if (File.Exists(filename))
            {
                LoadAssembly(name, filename, root);
            }
            else if (BuildLibrary(name))
            {
                LoadAssembly(name, filename, root);
            }
        }
Exemple #2
0
        private static void LoadAssembly(string name, string path, AST.Root root)
        {
            var asm  = Assembly.LoadFile(path);
            var type = asm.GetType(name);
            var attr = type.GetCustomAttributes(typeof(CoreLibrary.TypeAliasAttribute));

            var package = root.GetPackage(name, true);

            foreach (var rawA in attr)
            {
                var a = rawA as TypeAliasAttribute;
                package.AddTypeDeclaration(
                    new AST.TypeDeclaration(
                        a.TypeName,
                        new AST.RealType(a.Type)));
            }
        }
Exemple #3
0
        private static bool BuildLibrary(string name)
        {
            if (IsInteralLibrary(name))
            {
                throw new NotImplementedException();
            }

            var dir = Path.Combine(Environment.CurrentDirectory, StandardLibraryDir, name);

            // create ast root
            var root    = new AST.Root();
            var builder = new AstBuilder(root);

            int count = 42;

            foreach (var file in Directory.EnumerateFiles(dir, "*.go"))
            {
                // parse each file
                var basename = Path.GetFileNameWithoutExtension(file);
                var index    = basename.LastIndexOf('_');
                var fileFlag = string.Empty;
                if (index != -1)
                {
                    fileFlag = basename.Substring(index + 1);
                }

                if (string.IsNullOrEmpty(fileFlag))
                {
                    using (var fileStream = new StreamReader(file, Encoding.UTF8))
                    {
                        var parser = new Parser.GolangParser(
                            new CommonTokenStream(
                                new Parser.GolangLexer(
                                    new AntlrInputStream(fileStream))));

                        parser.RemoveErrorListeners();
                        parser.AddErrorListener(new ErrorHandler(fileStream));
                        builder.Visit(parser.source_file());
                    }

                    if (--count == 0)
                    {
                        break;
                    }
                }
            }

            var tempGatherer = new NodeGatherer <AST.ImportDeclaration>();

            tempGatherer.Process(root);
            var imports = tempGatherer.Results;

            var imported = new HashSet <string>();

            foreach (var i in imports)
            {
                if (imported.Contains(i.Package))
                {
                    continue;
                }

                imported.Add(i.Package);
                LoadImport(i.Package, root);
            }

            var conversionProcessor = new ConversionProcessor();
            var constantEvaluator   = new ConstantEvaluator();
            var resolver            = new AstResolver();
            var typeChecker         = new TypeChecker();
            var translator          = new Translator();
            var compiler            = new Compiler(name, true);

            //new AstPrinter().Process(root);

            conversionProcessor.Process(root);
            constantEvaluator.Process(root);
            resolver.Process(root);
            new AstPrinter(new StreamWriter("log.txt")).Process(root);

            /*typeChecker.Process(root);
             * translator.Process(root);
             * compiler.Process(root);*/

            //Console.WriteLine(root.Packages.First().Value.Imports.Count);
            //compiler.Finalize();
            return(false);
        }