示例#1
0
        internal CompileError Parse(NmProgram program, Token token)
        {
            var source = NmSource.FromFile(FileName);

            source.ModuleNames = program.Source.ModuleNames;

            var newProgram = new NmProgram(source)
            {
                Verbose            = program.Verbose,
                MeasureTime        = false,
                IncludeDirectories = program.IncludeDirectories,
                ParentProgram      = program
            };

            if (program.Verbose)
            {
                Console.WriteLine("Compiling {0}", FileName);
            }

            CompileError error;

            if ((error = newProgram.Parse()) != null)
            {
                Console.WriteLine(error);
                return(new CompileError(CompileErrorType.InnerCompileException, token));
            }

            if ((error = newProgram.Expand()) != null)
            {
                Console.WriteLine(error);
                return(new CompileError(CompileErrorType.InnerCompileException, token));
            }

            if (!newProgram.IsModule)
            {
                return(new CompileError(CompileErrorType.NotModuleImport, token));
            }

            Library = newProgram.Module.IsLibrary;
            //todo: library modules

            /*if (newProgram.Module.IsLibrary)
             * {
             *  newProgram.ByteCode.SaveToFile(compilerBinaryName);
             *  if (program.Verbose)
             *      Console.WriteLine("File saved {0}", compilerBinaryName);
             * }*/

            LinkedModule = newProgram.Module;

            return(null);
        }
示例#2
0
        private static void Run(Options options, bool errors)
        {
            if (errors || options == null)
            {
                return;
            }

            if (options.MetadataFileName == null && !options.IgnoreMetadataFiles)
            {
                var splitted = options.InputFileName.Split('.');
                var fn       = string.Join(".", splitted.Take(splitted.Length - 1)) + ".json";
                if (File.Exists(fn))
                {
                    options.MetadataFileName = fn;
                    Console.WriteLine("Using \"{0}\" as metadata file", fn);
                }
            }

            var source = NmSource.FromFile(options.InputFileName);

            var metadata = options.MetadataFileName != null?ParseJSON(options.MetadataFileName) : null;

            if (metadata == null &&
                (options.BinaryName != null ||
                 options.BinaryAuthor != null ||
                 options.BinaryDescription != null ||
                 options.BinaryVersion != null))
            {
                var maj = 1;
                var min = 0;

                if (options.BinaryVersion != null)
                {
                    try
                    {
                        maj = int.Parse(options.BinaryVersion.Split('.')[0]);
                        min = int.Parse(options.BinaryVersion.Split('.')[1]);
                    }
                    catch
                    {
                        Console.WriteLine("\"{0}\" is wrong version format", options.BinaryVersion);
                    }
                }

                metadata = new NmMetadata(options.BinaryName ?? "name", options.BinaryDescription ?? "descr",
                                          options.BinaryAuthor ?? "author", DateTime.Now, (ushort)maj, (ushort)min);
            }

            var includeList = new List <string>();

            if (options.IncludeDirectories != null)
            {
                includeList.AddRange(options.IncludeDirectories.Split(','));
            }
            else
            {
                var dirName = new FileInfo(options.InputFileName).Directory.FullName;
                includeList.Add(dirName);
                Console.WriteLine("Using \"{0}\" as include path", dirName);

                var possibleName = dirName + Path.DirectorySeparatorChar + "modules";
                if (Directory.Exists(possibleName))
                {
                    includeList.Add(possibleName);
                    Console.WriteLine("Using \"{0}\" as include path", possibleName);
                }

                possibleName = dirName + Path.DirectorySeparatorChar + "lib";
                if (Directory.Exists(possibleName))
                {
                    includeList.Add(possibleName);
                    Console.WriteLine("Using \"{0}\" as include path", possibleName);
                }
            }

            NmProgram.InitCompiler();
            var program = new NmProgram(source, metadata)
            {
                SaveDebugInfo       = options.UseDebugChunk,
                Verbose             = options.Verbose,
                DisableOptimization = options.DisableOptimization,
                IncludeDirectories  = includeList,
                MeasureTime         = options.PrintTime
            };

            if (options.LogFileName != null)
            {
                Stream     f      = File.OpenWrite(options.LogFileName);
                TextWriter writer = new StreamWriter(f);
                Console.SetOut(writer);
            }

            CompileError error;

            if ((error = program.Parse()) != null)
            {
                if (options.LogFileName != null)
                {
                    ResetOutput();
                }

                Console.WriteLine("Parse error: {0}", error);
            }
            else if ((error = program.Expand()) != null)
            {
                if (options.LogFileName != null)
                {
                    ResetOutput();
                }

                Console.WriteLine("Expand error: {0}", error);
            }
            else
            {
                if (options.LogFileName != null)
                {
                    ResetOutput();
                }

                if (options.PrintTime)
                {
                    Console.WriteLine("Elapsed Time (Total: {0}): ",
                                      ToPrettyFormat(program.GetElapsedTime(ElapsedTimeType.Total)));
                    var i = 0;
                    foreach (var type in (ElapsedTimeType[])Enum.GetValues(typeof(ElapsedTimeType)))
                    {
                        if (type == ElapsedTimeType.Total)
                        {
                            continue;
                        }
                        var time = program.GetElapsedTime(type);
                        if (time != TimeSpan.Zero)
                        {
                            Console.WriteLine("{0}. {1} - {2}", i++, type, ToPrettyFormat(time));
                        }
                    }
                }

                if (options.PrintByteCode)
                {
                    Console.WriteLine("ASM Program:\n");
                    Console.WriteLine(program.ByteCode.ToSource());
                }
                try
                {
                    using (var f = File.OpenRead(options.OutputFileName));
                }
                catch
                {
                    Console.WriteLine("Unable to write file {0}", options.OutputFileName);
                    return;
                }

                program.ByteCode.SaveToFile(options.OutputFileName);
                Console.WriteLine("File saved!");
            }
        }