import java.io.*; /** * Manage the phases of a compiler for the FM (flip movie) programming language. */ public class Flip { /** * Manage the phases of a compiler for the FM (flip movie) programming * language. * Command switches must precede the filepath. *
*
-methods, -nomethods
*
enable/disable tracing of method entry/exit
*
-symbols, -nosymbols
*
enable/disable tracing of Symbols as they are defined
*
-echo, -noecho
*
enable/disable echoing of source lines as they are read. default -echo
*
filepath
*
pathname of the file to compile. Must be specified.
*
* @param args the String array of command switches and arguments * @throws IOException thrown if any IO errors occur during processing */ public static void main(String[] args) throws IOException { boolean methods = false; boolean symbols = false; boolean echo = true; String path; int argN = 0; // check for -switches while (argN < args.length) { if (args[argN].charAt(0) != '-') break; String tag = args[argN]; if (tag.equals("-methods")) methods=true; else if (tag.equals("-nomethods")) methods=false; else if (tag.equals("-symbols")) symbols = true; else if (tag.equals("-nosymbols")) symbols = false; else if (tag.equals("-echo")) echo = true; else if (tag.equals("-noecho")) echo = false; else System.out.println("unrecognized switch: "+tag); argN++; } // check for file pathname if (argN == args.length) { System.out.println("Error: Name of file to compile must be provided."); return; } else { path = args[argN]; } CompilerIO io = new CompilerIO(path,"ps"); io.setEchoing(echo); io.setEchoPrefix("% "); SymbolTable sym = new SymbolTable(2); Scanner lex = new Scanner(io,sym); Parser p = new Parser(io,sym,lex); p.setShowMethods(methods); p.setShowSymbols(symbols); p.setShowCode(true); boolean result = p.parse(); io.closeAll(); if (result) { System.out.println("Compiled "+path+" successfully."); } else { System.out.println("Compile errors in "+path); } } }