All pastes #753322 Raw Edit

Someone

public text v1 · immutable
#753322 ·published 2007-10-29 03:30 UTC
rendered paste body
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.regex.Pattern


public class Interpreter
{

    public static void main( String args[] ) {
        try {
            Scanner scanner = testArguments( args );
            if( scanner != null ) {
                int index = 1;
                SymbolTable table = new SymbolTable();
                String input = scanner.next();
                while( input != null ) {
                    //String input = scanner.next();
                    if( input.toLowerCase().equals( "var" ) ) {
                        
                    } else if( input.toLowerCase().equals( "int" ) ) {
                        intCall( scanner, table, index );
                    } else if( input.toLowerCase().equals( "string" ) ) {
                        stringCall( scanner, table, index );
                    } else if( input.toLowerCase().equals( "print" ) ) {
                        printCall( scanner, table, index );
                    } else if( input.toLowerCase().equals( "println" ) ) {
                        printlnCall( scanner, table, index );
                    } else if( input.toLowerCase().equals( "set" ) ) {
                        setCall( scanner, table, index );
                    } else if( input.toLowerCase().equals( "begin" ) ) {
                        
                    } else if( input.toLowerCase().equals( "end" ) ) {
                        table.dump();
                    }
                    index++;
                    input = scanner.next();
                }
                scanner.close();
            } else {
                System.err.println( "Usage: java Interpreter " + 
                                    "<Optional_Input_File>" );
            }
        } catch( FileNotFoundException fnfexc ) {
            System.err.println( "Error: Can't open file " + args[ 0 ] );
        }
    }

    private static Scanner testArguments( String test[] ) throws 
    FileNotFoundException {
        if( test.length == 1 ) {
            return new Scanner( new BufferedReader( new FileReader( 
                                test[ 0 ] ) ) );
        } else if( test.length == 0 ) {
            return new Scanner( System.in );
        }
        return null;
    }

    private static void intCall( Scanner scanner, SymbolTable table, 
    int index ) {
        String var = scanner.next();
        try {
            table.put( var, new Node< Integer >( "int", 
                       0, Integer.toString( index ) ), index );
        } catch( Exception exc ) {
            System.err.println( exc.getMessage() );
        }
        while( !var.equals( ";" ) ) {
            var = scanner.next();
        }
    }

    private static void stringCall( Scanner scanner, SymbolTable table, 
    int index ) {
        String var = scanner.next();
        try {
            table.put( var, new Node< String >( "string", 
                       "", Integer.toString( index ) ), index );
        } catch( Exception exc ) {
            System.err.println( exc.getMessage() );
        }
        while( !var.equals( ";" ) ) {
            var = scanner.next();
        }
    }

    private static void printCall( Scanner scanner, SymbolTable table, 
    int index ) {
        String val = scanner.next();
        String temp = val;
        if( val.substring( 0, 1 ).equals( "\"" ) ) {
            val = scanner.next();
            while( !val.equals( ";" ) ) {
                temp += " " + val;
                val = scanner.next();
            }
            //System.out.println( temp.replaceAll( "\"", "" ) );
        } else {
            Node node = table.find( val );
            if( node != null ) {
                node.addLine( index );
            }
            while( !val.equals( ";" ) ) {
                val = scanner.next();
            }
        }
    }

    private static void printlnCall( Scanner scanner, SymbolTable table, 
    int index ) {
        String val = scanner.next();
        String temp = val;
        if( val.substring( 0, 1 ).equals( "\"" ) ) {
            val = scanner.next();
            while( !val.equals( ";" ) ) {
                temp += " " + val;
                val = scanner.next();
            }
            //System.out.println( temp.replaceAll( "\"", "" ) );
        } else {
            Node node = table.find( val );
            if( node != null ) {
                node.addLine( index );
            }
            while( !val.equals( ";" ) ) {
                val = scanner.next();
            }
        }
    }

    private static void setCall( Scanner scanner, SymbolTable table, 
    int index ) {
        String input = scanner.next();
        Node node = table.find( input );
        if( node != null ) {
            node.addLine( index );
        }
        while( !input.equals( ";" ) ) {
            input = scanner.next();
        }
    }

} // Interpreter