Last active
August 29, 2015 14:03
-
-
Save elvynmejia/43398fb5e36af491b69f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interpreter; | |
import interpreter.bytecodes.ByteCode; | |
import java.io.*; | |
import java.util.*; | |
public class ByteCodeLoader { | |
private BufferedReader byteCodeFile; | |
private String nextLine; | |
public ByteCodeLoader(String programFile) throws IOException { | |
byteCodeFile = new BufferedReader(new FileReader(programFile)); | |
} | |
public Program loadCodes() throws IOException { | |
ArrayList<String> byteCodeArgs = new ArrayList(); | |
Program program = new Program(); | |
int counter = 0; | |
nextLine = byteCodeFile.readLine(); | |
while(nextLine != null) { | |
ByteCode bytecode = null; | |
StringTokenizer byteCodeLine = new StringTokenizer(nextLine); | |
String code = byteCodeLine.nextToken(); | |
try { | |
bytecode = (ByteCode)(Class.forName("interpreter.bytecodes." + getCode(code)).newInstance()); | |
} | |
catch(Exception e) { | |
System.out.println(e); | |
} | |
while(byteCodeLine.hasMoreTokens()) { | |
byteCodeArgs.add(byteCodeLine.nextToken()); | |
} | |
bytecode.init(byteCodeArgs); | |
program.setByteCodeLines(bytecode); | |
byteCodeArgs.clear(); | |
nextLine = byteCodeFile.readLine(); | |
counter++; | |
} | |
program.resolveAddresses(); | |
return program; | |
} | |
public String getCode(String code) { | |
return CodeTable.get(code); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment