dijkstra.Dijkstra.java Source code

Java tutorial

Introduction

Here is the source code for dijkstra.Dijkstra.java

Source

package dijkstra;
/*******************************************************************************
 * Copyright (c) 2015 Gary F. Pollice
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Used in CS4533/CS544 at Worcester Polytechnic Institute
 *******************************************************************************/

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.antlr.v4.runtime.*;

import dijkstra.ast.*;
import dijkstra.ast.ASTNodeFactory.ClassDeclarationNode;
import dijkstra.ast.ASTNodeFactory.ProgramNode;
import dijkstra.ast.ASTNodeFactory.RootNode;
import dijkstra.gen.DijkstraCodeGenerator;
import dijkstra.lexparse.DijkstraParser;
import dijkstra.semantic.TypeChecker;
import dijkstra.symbol.SymbolCreator;
import dijkstra.utility.DijkstraFactory;

/**
 * This is the driver for the Dijkstra compiler.
 * @version October 22, 2012
 */
public class Dijkstra {
    private static String programName = new String();
    private static String customPackage = "djkcode"; // default
    private static String outputDirectory = "."; // default
    private static String fileContents = null;

    /**
     * Main program to run compiler.
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        final String fileName = parseArgs(args);
        //read the input file containing the actual Dijkstra code
        try {
            fileContents = new Scanner(new File(fileName)).useDelimiter("\\A").next();
        } catch (Exception e) {
            System.out.println("Either no input file was specified or it could not be read.");
            showHelp();
            System.exit(1);
        }

        doCompile();
    }

    /**
     * Parser for arguments to the main function
     * @param args Array of strings
     * @return name of the source file to compile from
     */
    static private String parseArgs(String[] args) {
        int i = 0;
        String s = null;
        while (i < args.length) {
            s = args[i++];
            if (s.equals("-h")) {
                showHelp();
                System.exit(0);
            } else if (s.equals("-o")) {
                outputDirectory = args[i++];
            } else if (s.equals("-p")) {
                customPackage = args[i++];
            }
        }
        return s;
    }

    /**
     * Prints help information
     */
    static private void showHelp() {
        System.out.println("Arguments: [-h] [-cp CLASSPATH] [-p PACKAGE_NAME] sourceFile");
        System.out.println(
                "Takes in Base Dijkstra code written in sourceFile and prints compiled code to the screen.\n"
                        + "\t-h Show this text\n" + "\t-o<directory> compiles to the specified directory\n"
                        + "\t-p<package> Sets the package to <package>\n");
    }

    static private void doCompile() {
        DijkstraParser parser = DijkstraFactory.makeParser(new ANTLRInputStream(fileContents)); // $codepro.audit.disable variableShouldBeFinal
        ParserRuleContext tree = parser.dijkstraText();
        ASTNode ast = tree.accept(new ASTCreator());
        ast.accept(new SymbolCreator());
        TypeChecker checker = new TypeChecker();
        do {
            ast.accept(checker);
        } while (checker.checkAgain());
        // get the class name
        programName = ((ProgramNode) ((RootNode) ast).getProgramNode()).programName;
        List<byte[]> codeList = new ArrayList<byte[]>();
        DijkstraCodeGenerator codeGen = new DijkstraCodeGenerator();
        if (customPackage != null) {
            codeGen.setClassPackage(customPackage);
        }
        for (ASTNode classDef : ast.children) {
            codeList.add(classDef.accept(codeGen));
        }
        for (int i = 0; i < ast.children.size(); i++) {
            String className;
            if (ast.children.get(i) instanceof ClassDeclarationNode) {
                className = ((ClassDeclarationNode) ast.children.get(i)).getID().getName();
            } else {
                className = ((ProgramNode) ast.children.get(i)).programName;
            }
            byte[] compiledCode = codeList.get(i);
            try (FileOutputStream fos = new FileOutputStream("djkcode/" + className + ".class")) {
                fos.write(compiledCode);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}