use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.
the class Assembler method assemble.
/**
* Assembles the given code in to an {@link ExecutableCode}.
*
* @param code T code to be assembled.
*
* @return The executable code.
*
* @throws SyntaxError If the code contains syntax error(s).
* @throws LabelError If an undefined label used or duplicate labels exist.
* @see ExecutableCode
*/
public ExecutableCode assemble(String code) throws SyntaxError, LabelError {
AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AssemblerParser parser = new AssemblerParser(tokens);
parser.prog();
if (parser.getNumberOfSyntaxErrors() > 0) {
throw new SyntaxError("You have error(s) in your code.");
}
ConstantValidator.validate(code);
ExecutableCode executableCode = linker.link(code);
loader.load(executableCode);
return executableCode;
}
use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.
the class ConstantValidator method validate.
public static void validate(String code) {
ConstantValidator constantValidator = new ConstantValidator();
AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AssemblerParser parser = new AssemblerParser(tokens);
AssemblerParser.ProgContext program = parser.prog();
constantValidator.visit(program);
}
use of com.kasirgalabs.thumb2.AssemblerLexer in project ETUmulator by kasirgalabs.
the class Linker method link.
/**
* Returns an ExecutableCode that can then be run by the processor. If the given code contains
* data section, linker will only generate memory addresses. Without loading process, referenced
* data will be unpredictable.
*
* @param code The code that will be linked.
*
* @return The executable code.
*
* @throws LabelError If an undefined label used or duplicate labels exist.
* @see Loader#load(Linker.ExecutableCode)
*/
public ExecutableCode link(String code) throws LabelError {
definedBranches.clear();
definedData.clear();
addressBook.clear();
secondPass = false;
this.code = parseCode(code);
AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AssemblerParser parser = new AssemblerParser(tokens);
AssemblerParser.ProgContext program = parser.prog();
visit(program);
secondPass = true;
visit(program);
List<Data> temp = new ArrayList<>(definedData.size());
definedData.forEach((label, data) -> {
temp.add(data);
});
return new ExecutableCode(this.code, temp);
}
Aggregations