001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2007 Robert Grimm
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;
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:
031: import xtc.util.SymbolTable;
032: import xtc.util.Tool;
033:
034: /**
035: * An analyzer for the simply typed lambda calculus.
036: *
037: * @author Robert Grimm
038: * @version $Revision: 1.11 $
039: */
040: public class TypedLambda extends Tool {
041:
042: /** Create a new driver for the simply typed lambda calculus. */
043: public TypedLambda() {
044: /* Nothing to do. */
045: }
046:
047: public String getName() {
048: return "xtc Simply Typed Lambda Calculus Driver";
049: }
050:
051: public String getCopy() {
052: return Constants.COPY;
053: }
054:
055: public void init() {
056: super .init();
057: runtime.bool("printSymbolTable", "printSymbolTable", false,
058: "Print the symbol table.").bool("printAST", "printAST",
059: false, "Print the program's AST in generic form.")
060: .bool("printSource", "printSource", false,
061: "Print the program's AST in source form.");
062: }
063:
064: public File locate(String name) throws IOException {
065: final File file = super .locate(name);
066: if (Integer.MAX_VALUE < file.length()) {
067: throw new IllegalArgumentException(file
068: + ": file too large");
069: }
070: return file;
071: }
072:
073: public Node parse(Reader in, File file) throws IOException,
074: ParseException {
075: final TypedLambdaParser parser = new TypedLambdaParser(in, file
076: .toString(), (int) file.length());
077: return (Node) parser.value(parser.pExpression(0));
078: }
079:
080: public void process(Node node) {
081: final SymbolTable table = new TypedLambdaAnalyzer(runtime)
082: .run(node);
083:
084: // Print symbol table.
085: if (runtime.test("printSymbolTable")) {
086: table.root().dump(runtime.console());
087: runtime.console().flush();
088: }
089:
090: // Print AST.
091: if (runtime.test("printAST")) {
092: runtime.console().format(node).pln().flush();
093: }
094:
095: // Print source.
096: if (runtime.test("printSource")) {
097: new TypedLambdaPrinter(runtime.console()).dispatch(node);
098: runtime.console().pln().flush();
099: }
100: }
101:
102: /**
103: * Run the driver with the specified command line arguments.
104: *
105: * @param args The command line arguments.
106: */
107: public static void main(String[] args) {
108: new TypedLambda().run(args);
109: }
110:
111: }
|