# You may have to edit this file to delete header lines produced by # mailers or news systems from this file (all lines before these shell # comments you are currently reading). # Shell archive made by dwjones on Sun Nov 15 17:09:48 CST 2015 # To install this software on a UNIX system: # 1) create a directory (e.g. with the shell command mkdir mp5) # 1a) get all the source files for mp4 into this file. # 2) change to that directory (e.g. with the command cd mp5), # 3) direct the remainder of this text to sh (e.g. sh < thistext). # This will make update directory mp5 to set you up; it will do # nothing else (if you're paranoid, you should scan the following text # to verify this before you follow these directions). cat > Input.java <<\xxxxxxxxxx // Input.java /** The entire contents of this file are junk, the minimal framework needed to * fool the Java compiler into thinking that there is something here. */ import java.util.Scanner; class Input extends Gate { public static Gate scan( Scanner sc ) { // needs to have useful body return null; } public String toString() { // needs to have a useful body return ""; } public void inputChange( float t, int i, boolean v ) { // needs to have a useful body } } xxxxxxxxxx cat > LogicCircuit.java <<\xxxxxxxxxx // LogicCircuit.java /** * Top level code to build and simulate one logic circuit * composed of gates connected by wires. * @author Douglas Jones * @version MP3 * * This code depends on: * @see Simulator * @see Errors * @see Wire * @see Gate * @see AndGate * @see OrGate * @see NotGate * @see Input * @see Output */ import java.util.LinkedList; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class LogicCircuit { /** Top level description of a logic circuit made of * some gates connected by some wires. * @see Gate * @see Wire */ private static LinkedList gates = new LinkedList (); private static LinkedList wires = new LinkedList (); public static Gate findGate( String s ) { /** Given s the name of a particular gate * returns null if that gate does not exist, * returns that gate if it exists. * @see Intersection */ // Bug: Reengineering this to use a hash should be possible for (Gate i: gates) { if (i.name.equals( s )) { return i; } } return null; } private static void readCircuit( Scanner sc ) { /** Read a logic circuit, scanning its description from sc. */ while (sc.hasNext() && !sc.hasNextFloat()) { // until the input file is finished // or the next line begins with a float String command = sc.next(); if ("gate".equals( command )) { String kind = sc.next(); Gate g = null; if ("and".equals( kind )) { g = AndGate.scan( sc ); } else if ("or".equals( kind )) { g = OrGate.scan( sc ); } else if ("not".equals( kind )) { g = NotGate.scan( sc ); } else if ("output".equals( kind )) { g = Output.scan( sc ); } else if ("input".equals( kind )) { g = Input.scan( sc ); } else { Errors.warn( "gate '" + kind + "' type not supported" ); sc.nextLine(); } if (g != null) gates.add( g ); } else if ("wire".equals( command )) { Wire w = Wire.scan( sc ); if (w != null) wires.add( w ); } else { Errors.warn( "'" + command + "' not a wire or gate" ); sc.nextLine(); } } } private static void checkCircuit() { /** Check the completeness of the logic circuit description. */ for (Gate g: gates) { g.checkInputs(); } } private static void writeCircuit() { /** Write out a textual description of the entire logic circuit. * This routine is scaffolding used during development. */ for (Gate g: gates) { System.out.println( g.toString() ); } for (Wire w: wires) { System.out.println( w.toString() ); } } public static void main(String[] args) { /** Create a logic circuit. * The command line argument names the input file. * For now, the output is just a reconstruction of the input. */ if (args.length < 1) { Errors.fatal( "Missing filename argument" ); } if (args.length > 1) { Errors.fatal( "Extra command-line arguments" ); } try { readCircuit( new Scanner( new File( args[0] ))); checkCircuit(); writeCircuit(); Simulator.run(); } catch (FileNotFoundException e) { Errors.fatal( "Can't open file '" + args[0] + "'" ); } } } xxxxxxxxxx