Commit efc5e2b6 authored by atzedijkstra@gmail.com's avatar atzedijkstra@gmail.com
Browse files

initial

parents
/**
* Simple Stack Machine
*
* Written by Atze Dijkstra, atze@cs.uu.nl,
* Copyright Utrecht University.
*
*/
package nl.uu.cs.ssm ;
import java.util.* ;
public interface MachineStateListener extends EventListener
{
public void stateChanged( MachineStateEvent e ) ;
}
/**
* Simple Stack Machine
*
* Written by Atze Dijkstra, atze@cs.uu.nl,
* Copyright Utrecht University.
*
*/
package nl.uu.cs.ssm ;
public class Main {
public static void main(String args[]) {
System.out.println( "Hello World!" );
}
}
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public class Memory extends AbstractMemoryCellModel implements MemoryCellModel{ private int cells[] ; private Hashtable annotations ; private int nUsedForCode ; private Messenger messenger ; protected Memory( int initCapacity, Messenger m ) { messenger = m ; cells = new int[ initCapacity ] ; for ( int i = 0 ; i < cells.length ; i++ ) cells[ i ] = 0 ; reset() ; } public void reset() { nUsedForCode = 0 ; annotations = new Hashtable() ; for ( int i = 0 ; i < cells.length ; i++ ) setAt( i, 0 ) ; } public int getCapacity() { return cells.length ; } public int getUsedForCode() { return nUsedForCode ; } private boolean checkWithinMemory( int addr ) { boolean res ; if ( res = ( addr < 0 || addr >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access location {0} outside {1}" , new Object[] { Utils.asHex(addr), ""+this } ) ) ; } return ! res ; } class UndoMemoryModification implements Modification { private int offset, value ; UndoMemoryModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setAt( offset, value ) ; } } public void setAt( int addr, int v ) { if ( checkWithinMemory( addr ) ) { int oldv = cells[ addr ] ; cells[ addr ] = v ; fireCellChange( this, addr, oldv, new UndoMemoryModification( addr, oldv ) ) ; } } public void setAt( int addr, String v ) { setAt( addr, Utils.fromHex( v ) ) ; } public int getAt( int addr ) { int res = 0 ; if ( checkWithinMemory( addr ) ) { res = cells[ addr ] ; } return res ; } public int[] getAt( int addr, int len ) { int res[] = new int[ len ] ; for ( int i = 0 ; i < len && checkWithinMemory( addr + i ) ; i++ ) { res[ i ] = cells[ addr + i ] ; } return res ; } public String getAsHexAt( int addr ) { return Utils.asHex( getAt( addr ) ) ; } class UndoAnnotationModification implements Modification { private int offset ; private MemoryAnnotation value ; UndoAnnotationModification( int o, MemoryAnnotation v ) { offset = o ; value = v ; } public void modify() { setAnnotationAt( offset, value ) ; } } public void setAnnotationAt( int addr, MemoryAnnotation v ) { if ( checkWithinMemory( addr ) ) { //System.out.println( "mem set annote at " + addr + "=" + v ) ; Integer a = new Integer( addr ) ; Object oldv ; if ( v != null ) oldv = annotations.put( a, v ) ; else oldv = annotations.remove( a ) ; if ( oldv != v ) fireCellChange( this, addr, (MemoryAnnotation)oldv, new UndoAnnotationModification( addr, (MemoryAnnotation)oldv ) ) ; } } public MemoryAnnotation getAnnotationAt( int addr ) { MemoryAnnotation res ; res = (MemoryAnnotation)annotations.get( new Integer(addr) ) ; //System.out.println( "mem got annote at " + addr + "=" + res ) ; return res ; } public void ensureCapacity( int nCells ) { if ( nCells > (cells.length - nUsedForCode) ) { int newCells[] = new int[ 2 * (nCells + 10) + cells.length ] ; System.arraycopy( cells, 0, newCells, 0, cells.length ) ; cells = newCells ; } } public void deleteAt( int pos, int n ) { for( int i = pos + n ; i < nUsedForCode ; i++ ) cells[ i-n ] = cells[ i ] ; nUsedForCode -= n ; } public void reserveAt( int pos, int n ) { ensureCapacity( n ) ; for( int i = nUsedForCode - 1 ; i >= pos ; i-- ) { cells[ i+n ] = cells[ i ] ; } for( int i = 0 ; i < n ; i++ ) cells[ i+pos ] = 0 ; nUsedForCode += n ; } public void shiftAt( int pos, int n ) { if ( n > 0 ) reserveAt( pos, n ) ; else if ( n < 0 ) deleteAt( pos, -n ) ; } public void copyAt( int pos, int[] vals ) { for( int i = 0 ; i < vals.length ; i++ ) cells[ i+pos ] = vals[ i ] ; } public void insertAt( int pos, int[] vals ) { reserveAt( pos, vals.length ) ; copyAt( pos, vals ) ; } public String toString() { return "memory [0(" + Utils.asHex(nUsedForCode,false) + ").." + Utils.asHex(getCapacity()-1,false) + "]" ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ;import java.awt.* ; public class MemoryAnnotation extends ColoredText{ public MemoryAnnotation( String ann, Color col ) { super( ann, col ) ; } public String getAnnotation() { return getText() ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; class MemoryCell{ private int value = 0 ; private String label = null ; protected MemoryCell( int v ) { setValue( v ) ; } protected MemoryCell( ) { } public int getValue() { return value ; } public int setValue( int v ) { return value = v ; } public int swapValue( int v ) { int oldv = value ; value = v ; return oldv ; } public String getLabel() { return label ; } public String setLabel( String v ) { return label = v ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public class MemoryCellEvent extends UndoableEvent{ public static final int CELL = 0 ; public static final int ANNOTATION = 1 ; public int cellIndex ; public Object oldCellValue ; public int event ; protected MemoryCellEvent( Object src, int which, int previous, Modification mdf ) { super( src, mdf ) ; event = CELL ; cellIndex = which ; oldCellValue = new Integer( previous ) ; } protected MemoryCellEvent( Object src, int which, MemoryAnnotation previous, Modification mdf ) { super( src, mdf ) ; event = ANNOTATION ; cellIndex = which ; oldCellValue = previous ; } public int getCellIndex() { return cellIndex ; } public int getOldCellValue() { return ((Integer)oldCellValue).intValue() ; } public MemoryAnnotation getOldAnnotation() { return (MemoryAnnotation)oldCellValue ; } public String toString() { return "MemCellEvent src=" + getSource() + " inx=" + cellIndex + " prev=" + oldCellValue ; }}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public interface MemoryCellListener extends EventListener{ public void cellChanged( MemoryCellEvent e ) ;}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; interface MemoryCellModel{ public void addMemoryCellListener( MemoryCellListener l ) ; public void removeMemoryCellListener( MemoryCellListener l ) ; }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; public interface MemoryUser{ public int firstLocAfterCode() ;}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; public interface Messenger{ public void println( String s ) ; }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public abstract class MetaInstrInstantiator{ protected MetaInstrInstantiator() { } public abstract MetaInstruction instantiate( Instruction instr, Vector args ) ; public static MetaInstrInstantiator newAnnoteInstantiator() { return new MetaInstrInstantiator() { public MetaInstruction instantiate( Instruction instr, Vector args ) { return MetaInstruction.newAnnote( instr, args ) ; } } ; }}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ;import java.awt.* ; public abstract class MetaInstruction{ private static Hashtable colorMap ; static { colorMap = new Hashtable() ; colorMap.put( "black" , Color.black ) ; colorMap.put( "blue" , Color.blue ) ; colorMap.put( "cyan" , Color.cyan ) ; colorMap.put( "darkGray" , Color.darkGray ) ; colorMap.put( "gray" , Color.gray ) ; colorMap.put( "green" , Color.green ) ; colorMap.put( "lightGray" , Color.lightGray ) ; colorMap.put( "magenta" , Color.magenta ) ; colorMap.put( "orange" , Color.orange ) ; colorMap.put( "pink" , Color.pink ) ; colorMap.put( "red" , Color.red ) ; colorMap.put( "yellow" , Color.yellow ) ; } protected Instruction instruction ; private MetaInstruction( Instruction instr ) { instruction = instr ; } public abstract void exec( MachineState machineState ) ; private static class Annote extends MetaInstruction { int register ; int loDispl, hiDispl ; Color color ; String text ; Annote( Instruction i, int r, int l, int h, Color c, String t ) { super( i ) ; register = r ; loDispl = l ; hiDispl = h ; color = c ; text = t ; } public void exec( MachineState machineState ) { Memory mem = machineState.getMemory() ; Registers regs = machineState.getRegisters() ; for ( int i = loDispl ; i <= hiDispl ; i++ ) { mem.setAnnotationAt( regs.getRegDispl( register, i ), new MemoryAnnotation( text, color ) ) ; } } public String toString() { return "Annote reg=" + register + " displ=" + loDispl + "/" + hiDispl + " col=" + color + " text=" + text ; } } public static MetaInstruction newAnnote( Instruction instr, Vector args ) { int r = Registers.findRegOfName( (String)args.elementAt(0) ) ; int l = Utils.fromHex( (String)args.elementAt(1) ) ; int h = Utils.fromHex( (String)args.elementAt(2) ) ; Color c = (Color)colorMap.get( (String)args.elementAt(3) ) ; String t = (String)args.elementAt(4) ; if ( r >= 0 && l <= h && c != null && t != null ) return new Annote( instr, r, l, h, c, t ) ; else return null ; } }
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public class Model{ private Vector listeners = new Vector() ; protected void addListener( EventListener l ) { listeners.addElement( l ) ; } protected void removeListener( EventListener l ) { listeners.removeElement( l ) ; } protected Enumeration getListeners() { return listeners.elements() ; } }
\ No newline at end of file
/**
* Simple Stack Machine
*
* Written by Atze Dijkstra, atze@cs.uu.nl,
* Copyright Utrecht University.
*
*/
package nl.uu.cs.ssm ;
public interface Modification
{
public void modify() ;
}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ; public class Registers extends AbstractMemoryCellModel implements MemoryCellModel{ public static final int PC = 0 ; public static final int SP = 1 ; public static final int MP = 2 ; //public static final int RR = 3 ; private static final int nrRegs = 8 ; private static final String[] regNames = { "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7" } ; private static final String[] regNamesAlias = { "PC", "SP", "MP", "RR" } ; //private MemoryCell cells[] ; private int cells[] ; private Memory memory ; private Messenger messenger ; protected Registers( Memory m, Messenger msgr ) { messenger = msgr ; cells = new int[ nrRegs ] ; for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ] = new MemoryCell() ; cells[ i ] = 0 ; memory = m ; reset() ; } public void reset() { for ( int i = 0 ; i < cells.length ; i++ ) //cells[ i ].setValue( 0 ) ; cells[ i ] = 0 ; } public static int getNrRegs( ) { return nrRegs ; } /* private static String[] getRegNames( ) { return regNames ; } */ public static String getRegName( int r ) { return (r < regNames.length && r >= 0) ? regNames[ r ] : ("R"+r) ; } public static String getRegOrAliasName( int r ) { return r < regNamesAlias.length ? regNamesAlias[r] : getRegName( r ) ; } public static String getRegNAliasName( int r ) { return getRegName(r) + ( r < regNamesAlias.length ? ("(" + regNamesAlias[r] + ")") : "" ) ; } public static Enumeration getRegNAliasNames( ) { Vector v = new Vector() ; Utils.addAllTo( v, Utils.asVector( regNames ) ) ; Utils.addAllTo( v, Utils.asVector( regNamesAlias ) ) ; return v.elements() ; } public static int findRegOfName( String nm ) // should be in some kinda environment { for ( int i = 0 ; i < regNames.length ; i++ ) { if ( regNames[i].equals( nm ) ) return i ; } for ( int i = 0 ; i < regNamesAlias.length ; i++ ) { if ( regNamesAlias[i].equals( nm ) ) return i ; } return -1 ; } private boolean checkWithinMemory( int r ) { boolean res ; if ( res = ( r < 0 || r >= cells.length ) ) { messenger.println ( java.text.MessageFormat.format ( "attempt to access nonexisting register {0,number,integer}" , new Object[] { new Integer(r) } ) ) ; } return ! res ; } public int getReg( int r ) { int res = 0 ; if ( checkWithinMemory( r ) ) { res = cells[ r ] ; } return res ; } public int getRegDispl( int r, int d ) { return getReg( r ) + d ; } public int getRegDisplInd( int r, int d ) { return memory.getAt( getRegDispl( r, d ) ) ; } public int getRegInd( int r ) { return getRegDisplInd( r, 0 ) ; } class UndoRegistersModification implements Modification { private int offset, value ; UndoRegistersModification( int o, int v ) { offset = o ; value = v ; } public void modify() { setReg( offset, value ) ; } } public void setReg( int r, int v ) { if ( checkWithinMemory( r ) ) { int oldv = cells[ r ] ; cells[ r ] = v ; fireCellChange( this, r, oldv, new UndoRegistersModification( r, oldv ) ) ; } } public void setRegDisplInd( int r, int d, int v ) { memory.setAt( getRegDispl( r, d ), v ) ; } public void setRegInd( int r, int v ) { setRegDisplInd( r, 0, v ) ; } public int swapReg( int r, int v ) { int oldVal = getReg( r ) ; setReg( r, v ) ; return oldVal ; } public void adjustReg( int r, int v ) { setReg( r, getReg( r ) + v ) ; } public void setPC( int v ) { setReg( PC, v ) ; } public int getPC( ) { return getReg( PC ) ; } public void adjustPC( int v ) { setPC( getPC() + v ) ; } public void setSP( int v ) { setReg( SP, v ) ; } public int getSP( ) { return getReg( SP ) ; } public void adjustSP( int v ) { setSP( getSP() + v ) ; } public void setMP( int v ) { setReg( MP, v ) ; } public int getMP( ) { return getReg( MP ) ; } public void adjustMP( int v ) { setMP( getMP() + v ) ; } public int getTOS() { return getRegInd( SP ) ; } public String toString() { return "Register PC=" + getReg( PC ) + " SP=" + getReg( SP ) + " MP=" + getReg( MP ) ; } }
\ No newline at end of file
/**
* Simple Stack Machine
*
* Written by Atze Dijkstra, atze@cs.uu.nl,
* Copyright Utrecht University.
*
*/
package nl.uu.cs.ssm ;
import java.util.* ;
public class UndoableEvent extends EventObject
{
private Modification undoModification ;
protected UndoableEvent( Object src, Modification mdf )
{
super( src ) ;
undoModification = mdf ;
}
public Modification undoModification()
{
return undoModification ;
}
}
\ No newline at end of file
/** * Simple Stack Machine * * Written by Atze Dijkstra, atze@cs.uu.nl, * Copyright Utrecht University. * */ package nl.uu.cs.ssm ; import java.util.* ;import java.io.* ;import java.awt.* ;import javax.swing.* ; public class Utils{ private static final String zeroChars = "00000000" ; private static final String hexReprPrefix = "0x" ; public static String asHex( int i, boolean hexIsDefault, boolean signed, boolean padZero ) { String sign = "" ; if ( signed && i < 0 ) { sign = "-" ; i = -i ; } String s = Integer.toHexString( i ) ; return sign + ( hexIsDefault ? ( padZero ? zeroChars.substring( s.length() ) : "" ) : ( i < 10 ? "" : "0x" ) ) + s ; } public static String asHex( int i, boolean hexIsDefault ) { return asHex( i, hexIsDefault, false, true ) ; } public static String asHex( int i ) { return asHex( i, true, false, true ) ; } public static void addAllTo( Vector v, Enumeration es ) { while( es.hasMoreElements() ) v.addElement( es.nextElement() ) ; } public static void addAllTo( Vector v, Vector es ) { addAllTo( v, es.elements() ) ; } public static Vector asVector( Object[] os ) { Vector v = new Vector() ; for ( int i = 0 ; i < os.length ; i++ ) v.addElement( os[i] ) ; return v ; } public static Vector splitAt( String s, char sep ) { Vector v = new Vector() ; while( true ) { int i = s.indexOf( sep ) ; if ( i >= 0 ) { v.addElement( s.substring( 0, i ) ) ; s = s.substring( i+1 ) ; } else { if ( s.length() > 0 ) v.addElement( s ) ; break ; } } return v ; } private static int toDigit( char c ) { if (c >= '0' && c <= '9') return c - '0' ; if (c >= 'a' && c <= 'f') return c - 'a' + 10 ; if (c >= 'A' && c <= 'F') return c - 'A' + 10 ; return -1 ; } public static boolean isNumberRepr( String s, boolean hexIsDefault ) { int radix = hexIsDefault ? 16 : 10 ; int pos = 0 ; if ( s.charAt( 0 ) == '-' ) pos += 1 ; if ( ( ! hexIsDefault ) && s.regionMatches( pos, hexReprPrefix, 0, 2 ) ) pos += 2 ; int max = s.length() ; for( ; pos < max ; pos++ ) { int d = toDigit( s.charAt( pos ) ) ; if ( d < 0 || d >= radix ) return false ; } return true ; } public static int fromHex( String s, boolean hexIsDefault ) { int radix = hexIsDefault ? 16 : 10 ; int res = 0 ; int pos = 0 ; int sign = 1 ; if ( s.charAt( 0 ) == '-' ) { pos += 1 ; sign = -1 ; } if ( ( ! hexIsDefault ) && s.regionMatches( pos, hexReprPrefix, 0, 2 ) ) { pos += 2 ; radix = 16 ; } int max = s.length() ; for( ; pos < max ; pos++ ) { int d = toDigit( s.charAt( pos ) ) ; //System.out.println( "from hex " + s + " dig " + s.charAt( pos ) + "=" + d ) ; if ( ( d < 0 ) || ( radix <= d ) ) return res ; res = res * radix + d ; } return res * sign ; } public static int fromHex( String s ) { return fromHex( s, true ) ; } public static String repeat( String s, int l ) { StringBuffer b = new StringBuffer() ; for ( int i = 0 ; i < l ; i++ ) b.append( s ) ; return b.toString() ; } public static class ExtensionFileFilter extends javax.swing.filechooser.FileFilter { private Vector extensions = new Vector() ; public ExtensionFileFilter( Enumeration exts ) { while ( exts.hasMoreElements() ) addExtension( (String)exts.nextElement() ) ; } public ExtensionFileFilter( String exts ) { this( new StringTokenizer( exts ) ) ; } public void addExtension( String ext ) { extensions.addElement( ext ) ; } public boolean accept( File f ) { boolean res = true ; if ( ! f.isDirectory() ) { String extension = Utils.getExtension( f ) ; res = (extension != null) && (extensions.contains( extension )) ; } return res ; } public String getDescription() { return ppList( extensions.elements(), "", ", ", "" ) + " files" ; } } /* * Get the extension of a file. */ public static String getExtension( File f ) { String ext = null ; String s = f.getName() ; int i = s.lastIndexOf( '.' ) ; if (i > 0 && i < s.length() - 1) { ext = s.substring( i+1 ).toLowerCase(); } return ext; } public static String ppList( Object l[] ) { return ppList( Arrays.asList( l ) ) ; } public static String ppList( java.util.List l ) { return ppList( Collections.enumeration( l ), "[", ",", "]" ) ; } public static String ppList( Enumeration e, String o, String s, String c ) { StringBuffer b = new StringBuffer() ; b.append( o ) ; if ( e.hasMoreElements() ) { b.append( e.nextElement().toString() ) ; while ( e.hasMoreElements() ) { b.append( s ) ; b.append( e.nextElement().toString() ) ; } } b.append( c ) ; return b.toString() ; } public static int indexOf( int a[], int v ) { for ( int i = 0 ; i < a.length ; i++ ) if ( a[i] == v ) return v ; return -1 ; } public static boolean contains( int a[], int v ) { return indexOf( a, v ) >= 0 ; } /** * Scroll a component contained in a JScrollPane to its end */ public static void scrollComponentTo( JComponent c, Rectangle r ) { int extraHeight = 40 ; // 2*r.height ; Rectangle rr = new Rectangle( r.x, r.y-extraHeight, r.width, 2*extraHeight+r.height ) ; Rectangle rrr = rr.intersection( c.getBounds() ) ; //System.out.println( "rr=" + rr + ", rrr=" + rrr + ", bounds=" + c.getBounds() ) ; if ( rrr.height > 0 ) c.scrollRectToVisible( rrr ) ; } /** * Scroll a component contained in a JScrollPane to its end */ public static void scrollComponentToEnd( JComponent c ) { Rectangle r = c.getBounds() ; scrollComponentTo( c, new Rectangle( 0, r.height-2, r.width, 2 ) ) ; } }
\ No newline at end of file
/* Runner.java Title: Simple Stack Machine Runner Author: atze Description: */ package nl.uu.cs.ssmui; import javax.swing.*; import java.util.* ; import nl.uu.cs.ssm.* ; class AssemblyParseResult { protected int lineNr ; protected String message ; protected Vector definedLabels ; protected Vector instrNArgs ; protected AssemblyParseResult( int l, String m, String lb, Vector ia ) { definedLabels = new Vector() ; addInfo( l, m, lb, ia ) ; } protected AssemblyParseResult addInfo( int l, String m, String lb, Vector ia ) { lineNr = l ; message = m ; if ( lb != null ) definedLabels.addElement( lb ) ; instrNArgs = ia ; return this ; } public String toString() { return "line " + lineNr + "(" + message + "), " + definedLabels + ": " + instrNArgs ; } public void addLabels( Vector ls ) { Utils.addAllTo( definedLabels, ls.elements() ) ; } }
\ No newline at end of file
/* Runner.java Title: Simple Stack Machine Runner Author: atze Description: */ package nl.uu.cs.ssmui; import javax.swing.*; import java.io.* ;import java.util.* ; public class AssemblyParser { private StreamTokenizer tokens ; private int tok ; protected AssemblyParser( Reader input ) { tokens = new StreamTokenizer( input ) ; tokens.resetSyntax() ; tokens.wordChars( '0', '9' ) ; tokens.wordChars( 'a', 'z' ) ; tokens.wordChars( 'A', 'Z' ) ; tokens.wordChars( 0xA0, 0xF0 ) ; tokens.wordChars( '_', '_' ) ; tokens.wordChars( '-', '-' ) ; tokens.whitespaceChars( 0x00, 0x20 ) ; tokens.commentChar( ';' ) ; tokens.quoteChar( '"' ) ; tokens.eolIsSignificant( true ) ; tokens.slashSlashComments( true ) ; next() ; } private void next() { try { if ( ! isAtEOF() ) tok = tokens.nextToken() ; } catch ( IOException ex ) { tok = tokens.TT_EOF ; } } public boolean isAtEOF() { return tok == tokens.TT_EOF ; } private boolean isAtEOL() { return tok == tokens.TT_EOL || isAtEOF() ; } protected AssemblyParseResult parse1Line( AssemblyParseResult prevAPR ) { int lineNr = tokens.lineno() ; String label = null ; String msg = null ; Vector instrNArgs = new Vector() ; if ( ! isAtEOL() ) { if ( tok == tokens.TT_WORD ) { String s = tokens.sval ; next() ; if ( tok == ':' ) { label = s ; next() ; } else { instrNArgs.addElement( s ) ; } while( ! isAtEOL() ) { if ( tok == tokens.TT_WORD || tok == '"' ) { instrNArgs.addElement( tokens.sval ) ; } else if ( tok == tokens.TT_NUMBER ) { msg = "did not expect number " + tokens.nval ; } else { msg = "did not expect `" + (char)tok + "'" ; } next() ; } } else { msg = "expected label or instruction" ; } } next() ; if ( prevAPR == null ) return new AssemblyParseResult( lineNr, msg, label, instrNArgs ) ; else return prevAPR.addInfo( lineNr, msg, label, instrNArgs ) ; } }
\ No newline at end of file
/* Title: Simple Stack Machine Runner Author: atze Description: */ package nl.uu.cs.ssmui; interface CodeRowSupplier{ public int memLocOfRow( int row ) ; public void setInstrArgAt( int row, int argOffset, int val ) ; }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment