Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/GpdlCompiler/Program.cs @ 9573

Last change on this file since 9573 was 9528, checked in by gkronber, 12 years ago

#2026 worked on HL integration of GPDL compiler

File size: 5.8 KB
Line 
1// ATTENTION: Replace all occurences of GpdlCompiler by the compiler name!
2
3// GpdlCompiler.cs                                                        HDO, 2006-08-28
4// -----
5// Main program for compiler generated from GpdlCompiler.atg with Coco-2.
6//=====================================|========================================
7
8using System;
9using System.IO;
10
11using Lex = GPDefLex;
12using Syn = GPDefSyn;
13
14public class GpdlCompiler {
15
16  private static String NAME = "GpdlCompiler";
17  private static bool interactiveMode;
18
19  private static void Abort(String abortKind, String moduleName,
20                            String methName, String descr) {
21    Console.WriteLine();
22    Console.WriteLine("*** {0} in class {1} method {2}",
23                      abortKind, moduleName, methName);
24    Console.WriteLine("*** {0}", descr);
25    Console.WriteLine();
26    Console.WriteLine();
27    Console.WriteLine(NAME + " aborted");
28    Utils.Modules(Utils.ModuleAction.cleanupModule);
29    if (interactiveMode) {
30      Console.Write("type [CR] to quit");
31      string dummy = Console.ReadLine();
32    } // if
33    Environment.Exit(Utils.EXIT_FAILURE);
34  } // Abort
35
36  private static int CompileFile(String srcFileName) {
37    FileStream srcFs = null;
38    try {
39      srcFs = new FileStream(srcFileName, FileMode.Open);
40    }
41    catch (Exception) {
42      Console.WriteLine("*** file \"{0}\" not found", srcFileName);
43      return Utils.EXIT_FAILURE;
44    } // try/catch
45    try {
46      Lex.src = new StreamReader(srcFs);
47      Console.WriteLine("parsing ...");
48      Syn.Parse();
49      Lex.src.Close();
50      Lex.src.Dispose();
51      Lex.src = null;
52      if (Errors.NumOfErrors() > 0) {
53        Console.WriteLine("listing ...");
54        String lstFileName = Path.ChangeExtension(srcFileName, ".lst");
55        FileStream lstFs = null;
56        try {
57          lstFs = new FileStream(lstFileName, FileMode.Create);
58        }
59        catch (Exception) {
60          Utils.FatalError(NAME, "CompileFile", "file \"{0}\" not created", lstFileName);
61          return Utils.EXIT_FAILURE;
62        } // try/catch
63        StreamWriter lstWriter = null;
64        try {
65          lstWriter = new StreamWriter(lstFs);
66          StreamReader srcFsReader = null;
67          try { // open the source file a second time to generate the error listing
68            srcFsReader = new StreamReader(new FileStream(srcFileName, FileMode.Open));
69            lstWriter.WriteLine(NAME + " (file: \"{0}\")", srcFileName);
70            Errors.GenerateListing(srcFsReader, lstWriter, Errors.ListingShape.longListing);
71          }
72          finally {
73            srcFsReader.Close();
74            srcFsReader.Dispose();
75          } // try/finally
76          Console.WriteLine("{0} error(s) detected", Errors.NumOfErrors());
77        }
78        finally {
79          lstWriter.Close();
80          lstWriter.Dispose();
81        } // try/finally
82      } else
83        Console.WriteLine("no errors detected");
84    }
85    finally {
86      if (Lex.src != null) {
87        Lex.src.Close();
88        Lex.src.Dispose();
89        Lex.src = null;
90      } // if
91      Utils.Modules(Utils.ModuleAction.resetModule);
92    } // try/finally to make sure srcFs and srcReader are closed
93    return Utils.EXIT_SUCCESS;
94  } // CompileFile
95
96
97  public static void Main(String[] args) {
98    //-----------------------------------|----------------------------------------
99    int result = 0;
100
101    // --- install modules ---
102    Utils.InstallModule("Utils", new Utils.ModuleMethodDelegate(Utils.UtilsMethod));
103    Utils.InstallModule("Sets", new Utils.ModuleMethodDelegate(Sets.SetsMethod));
104    Utils.InstallModule("Errors", new Utils.ModuleMethodDelegate(Errors.ErrorsMethod));
105
106    Utils.InstallModule("GPDefLex", new Utils.ModuleMethodDelegate(GPDefLex.GPDefLexMethod));
107    Utils.InstallModule("GPDefSem", new Utils.ModuleMethodDelegate(GPDefSem.GPDefSemMethod));
108    Utils.InstallModule("GPDefSyn", new Utils.ModuleMethodDelegate(GPDefSyn.GPDefSynMethod));
109
110    // --- initialize modules ---
111    Utils.Modules(Utils.ModuleAction.initModule);
112
113    Errors.PushAbortMethod(new Errors.AbortMethod(Abort));
114
115    Console.WriteLine("---------------------------");
116    Console.WriteLine(" {0} Compiler {1," + (5 - NAME.Length) + "} Version X  ", NAME, "");
117    Console.WriteLine(" Frontend gen. with Coco-2");
118    Console.WriteLine("---------------------------");
119    Console.WriteLine();
120
121    if (args.Length > 0) { // command line mode
122      interactiveMode = false;
123      Console.WriteLine();
124      int i = 0;
125      do {
126        Console.WriteLine("source file \"{0}\"", args[i]);
127        result = CompileFile(args[i]);
128        if (result != Utils.EXIT_SUCCESS)
129          Environment.Exit(result);
130        Console.WriteLine();
131        i++;
132      } while (i < args.Length);
133    } else { // args.Length == 0, interactive mode
134      interactiveMode = true;
135      for (; ; ) {
136        String srcFileName;
137        Utils.GetInputFileName("source file > ", out srcFileName);
138        if (srcFileName.Length > 0) {
139          result = CompileFile(srcFileName);
140          if (result != Utils.EXIT_SUCCESS)
141            Environment.Exit(result);
142        } // if
143        char answerCh;
144        do {
145          Console.WriteLine();
146          Console.Write("[c]ontinue or [q]uit > ");
147          answerCh = Char.ToUpper(Console.ReadKey().KeyChar);
148        } while (answerCh != 'C' && answerCh != 'Q');
149        if (answerCh == 'Q')
150          break;
151        else // answerCh == 'C'
152          Console.WriteLine();
153      } // for
154    } // else
155
156    Utils.Modules(Utils.ModuleAction.cleanupModule);
157    Environment.Exit(Utils.EXIT_SUCCESS);
158
159  } // Main
160
161} // GpdlCompiler
162
163// End of GpdlCompiler.cs
164//=====================================|========================================
Note: See TracBrowser for help on using the repository browser.