Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package IC;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java_cup.runtime.Symbol;
- import IC.AST.ICClass;
- import IC.AST.Method;
- import IC.AST.PrettyPrinter;
- import IC.AST.Program;
- import IC.Parser.Parser;
- import IC.SemanticChecks.CheckMain;
- import IC.SemanticChecks.CheckReturn;
- import IC.SemanticChecks.ScopeRules;
- import IC.SemanticChecks.TypeChecking;
- import IC.SymbolTable.SymbolTable;
- import IC.TypeTable.TypeTable;
- public class Compiler {
- private static Program program;
- private static ICClass lib;
- public static void main(String[] args) throws Exception {
- boolean hasLib = false; //indicates that the library path was passed by the user
- Parse(args[0], 0); //parse the source program
- //parse the library
- if (args.length >= 2) {
- if (args[1].contains("-L")) {
- Parse(args[1].substring(2), 1);
- hasLib = true;
- } else {
- Parse("libic.sig", 1); //we assume that the library is in the current dir
- }
- }
- else {
- Parse("libic.sig", 1);
- }
- SymbolTable symTable = SymbolTable.createTableFromTree(program, lib,
- args[0]); //create symbol tables according to ast
- //check that the program contains syngle main function
- Method main = CheckMain.checkSingleMain(program, args[0]);
- //create type table
- TypeTable t = new TypeTable(program, lib, main);
- ScopeRules.typeTable = t;
- TypeChecking typeChecker = new TypeChecking(t);
- ScopeRules.checkScopeRules(program); //check scope rules (section 11 in the spec)
- typeChecker.checkTypeRules(program); //check correctness of types
- typeChecker.checkOverriding(program); //check correctness of overriding functions (in inheritance)
- CheckReturn.checkReturn(program); //check that the functions that return value realy do that (bonus!)
- //if we get some options to print in the arguments
- if (args.length > 2 && hasLib) {
- if (args[2].equals("-print-ast")) { //print ast
- PrettyPrinter x = new PrettyPrinter(args[0]);
- System.out.println(x.visit(program).toString());
- System.out.println();
- } else if (args[2].equals("-dump-symtab")) { //print sym table
- SymbolTable.PrintSymbolTable(symTable);
- t.prettyPrint(args[0]);
- }
- if (args.length > 3) {
- if (args[3].equals("-print-ast")) { //print ast
- PrettyPrinter x = new PrettyPrinter(args[0]);
- System.out.println(x.visit(program).toString());
- System.out.println();
- } else if (args[3].equals("-dump-symtab")) { //print sym table
- SymbolTable.PrintSymbolTable(symTable);
- t.prettyPrint(args[0]);
- }
- }
- }
- else if (args.length > 1 && !hasLib) {
- if (args[1].equals("-print-ast")) { //print ast
- PrettyPrinter x = new PrettyPrinter(args[0]);
- System.out.println(x.visit(program).toString());
- System.out.println();
- } else if (args[1].equals("-dump-symtab")) { //print sym table
- SymbolTable.PrintSymbolTable(symTable);
- t.prettyPrint(args[0]);
- }
- if (args.length > 2) {
- if (args[2].equals("-print-ast")) { //print ast
- PrettyPrinter x = new PrettyPrinter(args[0]);
- System.out.println(x.visit(program).toString());
- System.out.println();
- } else if (args[2].equals("-dump-symtab")) { //print sym table
- SymbolTable.PrintSymbolTable(symTable);
- t.prettyPrint(args[0]);
- }
- }
- }
- }
- //this function create the tokens (lexer) and ast (parser)
- public static void Parse(String input, int code)
- throws FileNotFoundException {
- java_cup.runtime.lr_parser parser;
- FileReader txtFile = new FileReader(input);
- if (code == 0) { //for user program
- IC.Parser.Lexer scanner = new IC.Parser.Lexer(txtFile); //lexer
- parser = new Parser(scanner); //parser
- } else { //for library
- IC.Parser.LibLexer scanner = new IC.Parser.LibLexer(txtFile); //lexer
- parser = new IC.Parser.LibParser(scanner); //parser
- }
- try {
- Symbol parseSym = parser.parse();
- if (code == 0) {
- program = (IC.AST.Program) parseSym.value;
- } else {
- lib = (IC.AST.ICClass) parseSym.value;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement