package info.s5d.assembly.compiler;
import info.s5d.assembly.AssemblyScript;
import info.s5d.assembly.compiler.token.BasicTokenHandler;
import info.s5d.assembly.compiler.token.TokenHandler;
import info.s5d.assembly.instruction.Instruction;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
/**
* Class to read a script from a file/string into an AssemblyScript.
*
* @author Chris
*/
public class AssemblyScriptCompiler
{
public static AssemblyScript compile(Reader reader) throws IOException
{
return compile(reader, new BasicTokenHandler());
}
public static AssemblyScript compile(Reader reader, TokenHandler tokenhandler) throws IOException
{
AssemblyScript script = new AssemblyScript();
//Read each line of the script
BufferedReader bufferedreader = new BufferedReader(reader);
String line;
while ((line = bufferedreader.readLine()) != null) {
String[] tokens = line.split("\\s"); //split by whitespace
Instruction instruction = tokenhandler.getInstructionByToken(tokens[0]);
instruction.readParametersFromTokens(tokens);
script.push(instruction);
}
return script;
}
}