use of com.kasirgalabs.etumulator.lang.Linker.ExecutableCode 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.etumulator.lang.Linker.ExecutableCode in project ETUmulator by kasirgalabs.
the class LinkerTest method testLink.
/**
* Test of link method, of class Linker.
*/
@Test
public void testLink() {
Linker linker = new Linker();
ExecutableCode executableCode;
String expData;
String code = "ldr r0, =label\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, there is an undefined label.");
} catch (LabelError ex) {
}
code = "ldr r0, =label\n" + "label: .asciz \"data\"\n" + "label:\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, duplicate labels.");
} catch (LabelError ex) {
}
code = "ldr r0, =label\n" + "label: .asciz \"data\"\n" + "label: .asciz \"duplicate\"\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, duplicate labels.");
} catch (LabelError ex) {
}
code = "b label\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, there is an undefined label.");
} catch (LabelError ex) {
}
// No branch target given.
// The given label is defines a data not branch target.
code = "b label\n" + "label: .asciz \"DATA LABEL\"\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, there is an undefined label.");
} catch (LabelError ex) {
}
code = "b label\n" + "label: .asciz \"DATA LABEL\"\n" + "label:\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, duplicate labels.");
} catch (LabelError ex) {
}
code = "b label\n" + "label: .asciz \"DATA LABEL\"\n" + "label:\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, duplicate labels.");
} catch (LabelError ex) {
}
code = "ldr r0, =label\n" + "label: .asciz \"DATA\"\n";
executableCode = linker.link(code);
expData = "DATA\0";
assertEquals("Linker did not resolve data properly.", expData, executableCode.getData().get(0).getValue());
code = "ldr r0, =label\n" + "label: .asciz \"DATA\"\n" + "label: .asciz \"DATA\"\n" + "label: .asciz \"DATA\"\n";
try {
linker.link(code);
fail("Linker did not throw LabelError, duplicate labels.");
} catch (LabelError ex) {
}
code = "ldr r0, =label0\n" + "label0: .asciz \"DATA0\"\n" + "label1: .asciz \"DATA1\"\n" + "label2: .asciz \"DATA2\"\n";
executableCode = linker.link(code);
List<Data> dataResult = executableCode.getData();
expData = "DATA0\0";
assertEquals("Linker did not resolve data properly.", expData, dataResult.get(0).getValue());
expData = "DATA1\0";
assertEquals("Linker did not resolve data properly.", expData, dataResult.get(1).getValue());
expData = "DATA2\0";
assertEquals("Linker did not resolve data properly.", expData, dataResult.get(2).getValue());
}
use of com.kasirgalabs.etumulator.lang.Linker.ExecutableCode in project ETUmulator by kasirgalabs.
the class ETUmulatorController method runButtonOnAction.
@FXML
private void runButtonOnAction(ActionEvent event) {
processor.stop();
processorUnits.reset();
Assembler assembler = new Assembler(memory);
ExecutableCode executableCode;
try {
executableCode = assembler.assemble(document.getText() + "\n");
} catch (SyntaxError | LabelError | NumberFormatException ex) {
System.err.println(ex.getMessage());
return;
}
processor.run(executableCode);
}
Aggregations