// Well, this is not a namespace creator, instead this will import everything from another file public override GamaNamespace VisitImportStmt([NotNull] GamaParser.ImportStmtContext context) { var file = context.StringLiteral().GetText().Trim('"'); if (GlobalContext.ImportedFiles.Contains(file)) { return(null); // just skip already included files, this is dumb, fix later: TODO: } if (!file.EndsWith(".gm")) { file += ".gm"; } if (!File.Exists(file)) { file = "./include/" + file; // TODO: might need fixing to app path if (!File.Exists(file)) { Console.WriteLine("Imported file not found: '{0}'", file); // GlobalContext.AddError(new ErrorImportFileNotFound(file)); return(null); } } GlobalContext.ImportedFiles.Add(file); var input = new AntlrInputStream(File.ReadAllText(file)); var lexer = new GamaLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new GamaParser(tokens); var program = parser.program(); if (parser.NumberOfSyntaxErrors > 0) { Console.WriteLine("Imported file contains syntax errors, aborting compilation."); return(null); } var unit = new GamaNamespaceCompiler(GlobalContext); unit.Visit(program); return(null); }
public static int Process(Compile args) { if (args.Files.Count() == 0) { Console.WriteLine("Error: No input files provided"); return(1); } string file = args.Files.First(); if (!File.Exists(file)) { Console.WriteLine($"File does not exists: { file }"); return(1); } var ctx = new GamaGlobalContext($"[module/{ file }]"); InstanceTypes.Initialize(); ctx.Root.Types.AddRange(InstanceTypes.All); var sw = new Stopwatch(); sw.Start(); var inputtxt = File.ReadAllText(file); var input = new AntlrInputStream(inputtxt); var lexer = new GamaLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new GamaParser(tokens); var program = parser.program(); if (parser.NumberOfSyntaxErrors > 0) { Console.WriteLine("Code contains syntax errors, aborting compilation."); return(2); } var unit = new GamaNamespaceCompiler(ctx); unit.Visit(program); sw.Stop(); if (!ctx.Module.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out string err)) { ctx.Module.Dump(); if (ctx.ErrorList.Count > 0) { foreach (var e in ctx.ErrorList) { Console.WriteLine(e.ToString()); } } return(3); } else { if (ctx.ErrorList.Count > 0) { foreach (var e in ctx.ErrorList) { Console.WriteLine(e.ToString()); } } else { Console.WriteLine(ctx.Module.PrintToString()); Console.WriteLine($"Compilation took { sw.Elapsed.Seconds } sec, { sw.Elapsed.Milliseconds } ms"); ctx.Module.WriteBitcodeToFile(args.Output); if (args.Link) { } } } return(0); }