Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package IC;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5.  
  6. import java_cup.runtime.Symbol;
  7. import IC.AST.ICClass;
  8. import IC.AST.Method;
  9. import IC.AST.PrettyPrinter;
  10. import IC.AST.Program;
  11. import IC.Parser.Parser;
  12. import IC.SemanticChecks.CheckMain;
  13. import IC.SemanticChecks.CheckReturn;
  14. import IC.SemanticChecks.ScopeRules;
  15. import IC.SemanticChecks.TypeChecking;
  16. import IC.SymbolTable.SymbolTable;
  17. import IC.TypeTable.TypeTable;
  18.  
  19. public class Compiler {
  20.  
  21. private static Program program;
  22. private static ICClass lib;
  23.  
  24. public static void main(String[] args) throws Exception {
  25.  
  26. boolean hasLib = false; //indicates that the library path was passed by the user
  27.  
  28. Parse(args[0], 0); //parse the source program
  29.  
  30. //parse the library
  31. if (args.length >= 2) {
  32. if (args[1].contains("-L")) {
  33. Parse(args[1].substring(2), 1);
  34. hasLib = true;
  35. } else {
  36. Parse("libic.sig", 1); //we assume that the library is in the current dir
  37. }
  38. }
  39.  
  40. else {
  41. Parse("libic.sig", 1);
  42. }
  43.  
  44. SymbolTable symTable = SymbolTable.createTableFromTree(program, lib,
  45. args[0]); //create symbol tables according to ast
  46.  
  47. //check that the program contains syngle main function
  48. Method main = CheckMain.checkSingleMain(program, args[0]);
  49.  
  50. //create type table
  51. TypeTable t = new TypeTable(program, lib, main);
  52. ScopeRules.typeTable = t;
  53.  
  54. TypeChecking typeChecker = new TypeChecking(t);
  55.  
  56. ScopeRules.checkScopeRules(program); //check scope rules (section 11 in the spec)
  57.  
  58. typeChecker.checkTypeRules(program); //check correctness of types
  59. typeChecker.checkOverriding(program); //check correctness of overriding functions (in inheritance)
  60.  
  61. CheckReturn.checkReturn(program); //check that the functions that return value realy do that (bonus!)
  62.  
  63. //if we get some options to print in the arguments
  64. if (args.length > 2 && hasLib) {
  65.  
  66. if (args[2].equals("-print-ast")) { //print ast
  67. PrettyPrinter x = new PrettyPrinter(args[0]);
  68.  
  69. System.out.println(x.visit(program).toString());
  70. System.out.println();
  71.  
  72. } else if (args[2].equals("-dump-symtab")) { //print sym table
  73. SymbolTable.PrintSymbolTable(symTable);
  74. t.prettyPrint(args[0]);
  75. }
  76.  
  77. if (args.length > 3) {
  78.  
  79. if (args[3].equals("-print-ast")) { //print ast
  80. PrettyPrinter x = new PrettyPrinter(args[0]);
  81.  
  82. System.out.println(x.visit(program).toString());
  83. System.out.println();
  84. } else if (args[3].equals("-dump-symtab")) { //print sym table
  85. SymbolTable.PrintSymbolTable(symTable);
  86. t.prettyPrint(args[0]);
  87. }
  88. }
  89. }
  90.  
  91. else if (args.length > 1 && !hasLib) {
  92.  
  93. if (args[1].equals("-print-ast")) { //print ast
  94. PrettyPrinter x = new PrettyPrinter(args[0]);
  95.  
  96. System.out.println(x.visit(program).toString());
  97. System.out.println();
  98.  
  99. } else if (args[1].equals("-dump-symtab")) { //print sym table
  100. SymbolTable.PrintSymbolTable(symTable);
  101. t.prettyPrint(args[0]);
  102. }
  103.  
  104. if (args.length > 2) {
  105.  
  106. if (args[2].equals("-print-ast")) { //print ast
  107. PrettyPrinter x = new PrettyPrinter(args[0]);
  108.  
  109. System.out.println(x.visit(program).toString());
  110. System.out.println();
  111. } else if (args[2].equals("-dump-symtab")) { //print sym table
  112. SymbolTable.PrintSymbolTable(symTable);
  113. t.prettyPrint(args[0]);
  114. }
  115. }
  116. }
  117.  
  118. }
  119.  
  120. //this function create the tokens (lexer) and ast (parser)
  121. public static void Parse(String input, int code)
  122. throws FileNotFoundException {
  123. java_cup.runtime.lr_parser parser;
  124. FileReader txtFile = new FileReader(input);
  125. if (code == 0) { //for user program
  126. IC.Parser.Lexer scanner = new IC.Parser.Lexer(txtFile); //lexer
  127. parser = new Parser(scanner); //parser
  128. } else { //for library
  129. IC.Parser.LibLexer scanner = new IC.Parser.LibLexer(txtFile); //lexer
  130. parser = new IC.Parser.LibParser(scanner); //parser
  131. }
  132.  
  133. try {
  134. Symbol parseSym = parser.parse();
  135. if (code == 0) {
  136. program = (IC.AST.Program) parseSym.value;
  137. } else {
  138. lib = (IC.AST.ICClass) parseSym.value;
  139. }
  140.  
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144.  
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement