001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2007 New York University
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.lang.p2;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.Reader;
024:
025: import xtc.Constants;
026:
027: import xtc.parser.ParseException;
028:
029: import xtc.tree.Node;
030: import xtc.tree.Visitor;
031:
032: import xtc.type.TypePrinter;
033:
034: import xtc.util.SymbolTable;
035: import xtc.util.Tool;
036:
037: /**
038: * The driver for processesing the Overlog language.
039: *
040: * @author Robert Soule
041: * @version $Revision: 1.15 $
042: */
043: public class Overlog extends Tool {
044:
045: /** Create a new driver for Overlog. */
046: public Overlog() {
047: // Nothing to do.
048: }
049:
050: public String getName() {
051: return "xtc Overlog Driver";
052: }
053:
054: public String getCopy() {
055: return Constants.COPY;
056: }
057:
058: public void init() {
059: super .init();
060: runtime
061: .bool("optionTypeAnalyze", "optionTypeAnalyze", false,
062: "Analyze the program's AST.")
063: .bool("optionConcurrency", "optionConcurrency", false,
064: "Augment the program's AST with concurrency meta data.")
065: .bool("printAST", "printAST", false,
066: "Print the program's AST in generic form.")
067: .bool("printSource", "printSource", false,
068: "Print the program's AST in source form.")
069: .bool("printSymbolTable", "printSymbolTable", false,
070: "Print the program's symbol table.");
071:
072: }
073:
074: public Node parse(Reader in, File file) throws IOException,
075: ParseException {
076: Parser parser = new Parser(in, file.toString(), (int) file
077: .length());
078: return (Node) parser.value(parser.pProgram(0));
079: }
080:
081: public void process(Node node) {
082: // Analyze the AST.
083: if (runtime.test("optionTypeAnalyze")) {
084: // Create symbol table.
085: SymbolTable table = new SymbolTable();
086: // Perform type checking.
087: node = new TypeAnalyzer(runtime).analyze(node, table);
088: // Print the symbol table.
089: if (runtime.test("printSymbolTable")) {
090: // Save the registered visitor.
091: Visitor visitor = runtime.console().visitor();
092: // Note that the type printer's constructor registers the just
093: // created printer with the console.
094: new TypePrinter(runtime.console());
095: try {
096: table.root().dump(runtime.console());
097: } finally {
098: // Restore the previously registered visitor.
099: runtime.console().register(visitor);
100: }
101: runtime.console().flush();
102: }
103: }
104: if (runtime.test("optionConcurrency")) {
105: // Perform concurrency checking.
106: node = new ConcurrencyAnalyzer(runtime).analyze(node);
107: }
108:
109: // Print AST.
110: if (runtime.test("printAST")) {
111: runtime.console().format(node).pln().flush();
112: }
113:
114: // Print source.
115: if (runtime.test("printSource")) {
116: new Printer(runtime.console()).dispatch(node);
117: runtime.console().pln().flush();
118: }
119: }
120:
121: /**
122: * Run the driver with the specified command line arguments.
123: *
124: * @param args The command line arguments.
125: */
126: public static void main(String[] args) {
127: new Overlog().run(args);
128: }
129: }
|