Compiler/virtual machine interpreter: Difference between revisions

Content added Content deleted
(New post.)
m (Minor improvement to coding.)
Line 8,426: Line 8,426:


public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException {
Path path = Path.of("Compiler Test Cases/AsciiMandlebrot.txt");
Path filePath = Path.of("Compiler Test Cases/AsciiMandlebrot.txt");
VirtualMachineInfo info = loadCode(path);
VirtualMachineInfo info = loadCode(filePath);
runVirtualMachine(info.dataSize, info.vmStrings, info.codes());
runVirtualMachine(info.dataSize, info.vmStrings, info.codes());
}
}
Line 8,439: Line 8,439:
int index = 0;
int index = 0;
OP_CODE opCode = null;
OpCode opCode = null;
while ( opCode != OP_CODE.HALT ) {
while ( opCode != OpCode.HALT ) {
opCode = OP_CODE.havingCode(codes.get(index));
opCode = OpCode.havingCode(codes.get(index));
index += 1;
index += 1;
Line 8,458: Line 8,458:
case NE -> stack.set(stack.size() - 2, ( stack.get(stack.size() - 2) != stack.pop() ) ? 1 : 0);
case NE -> stack.set(stack.size() - 2, ( stack.get(stack.size() - 2) != stack.pop() ) ? 1 : 0);
case AND -> { final int value = ( stack.get(stack.size() - 2) != 0 && stack.pop() != 0 ) ? 1 : 0;
case AND -> { final int value = ( stack.get(stack.size() - 2) != 0 && stack.pop() != 0 ) ? 1 : 0;
stack.set(stack.size() - 1, value);
stack.set(stack.size() - 1, value);
}
}
case OR -> { final int value = ( stack.get(stack.size() - 2) != 0 || stack.pop() != 0 ) ? 1 : 0;
case OR -> { final int value = ( stack.get(stack.size() - 2) != 0 || stack.pop() != 0 ) ? 1 : 0;
Line 8,478: Line 8,478:
}
}
private static VirtualMachineInfo loadCode(Path path) throws IOException {
private static VirtualMachineInfo loadCode(Path filePath) throws IOException {
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
String line = lines.getFirst();
String line = lines.getFirst();
Line 8,502: Line 8,502:
sections = lines.get(i).trim().split("\\s+");
sections = lines.get(i).trim().split("\\s+");
offset = Integer.parseInt(sections[0]);
offset = Integer.parseInt(sections[0]);
OP_CODE opCode = OP_CODE.valueOf(sections[1].toUpperCase());
OpCode opCode = OpCode.valueOf(sections[1].toUpperCase());
codes.addLast(opCode.byteCode());
codes.addLast(opCode.byteCode());
Line 8,518: Line 8,518:
private static int operand(int index, List<Byte> codes) {
private static int operand(int index, List<Byte> codes) {
buffer.clear();
byteBuffer.clear();
for ( int i = index; i < index + 4; i++ ) {
for ( int i = index; i < index + 4; i++ ) {
buffer.put((byte) codes.get(i));
byteBuffer.put(codes.get(i));
}
}
buffer.flip();
byteBuffer.flip();
return buffer.getInt();
return byteBuffer.getInt();
}
}
private static void addToCodes(int number, List<Byte> codes) {
private static void addToCodes(int number, List<Byte> codes) {
buffer.clear();
byteBuffer.clear();
buffer.putInt(number);
byteBuffer.putInt(number);
buffer.flip();
byteBuffer.flip();
for ( byte bb : buffer.array() ) {
for ( byte bb : byteBuffer.array() ) {
codes.addLast(bb);
codes.addLast(bb);
}
}
Line 8,557: Line 8,557:
}
}
private static ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
private static ByteBuffer byteBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
private static enum OP_CODE {
private static enum OpCode {
HALT(0), ADD(1), SUB(2), MUL(3), DIV(4), MOD(5), LT(6), GT(7), LE(8), GE(9), EQ(10), NE(11),
HALT(0), ADD(1), SUB(2), MUL(3), DIV(4), MOD(5), LT(6), GT(7), LE(8), GE(9), EQ(10), NE(11),
Line 8,569: Line 8,569:
}
}
public static OP_CODE havingCode(Byte byteCode) {
public static OpCode havingCode(Byte byteCode) {
return op_codes[(int) byteCode];
return op_codes[(int) byteCode];
}
}
private OP_CODE(int aByteCode) {
private OpCode(int aByteCode) {
byteCode = aByteCode;
byteCode = aByteCode;
}
}
Line 8,579: Line 8,579:
private int byteCode;
private int byteCode;
private static OP_CODE[] op_codes = values();
private static OpCode[] op_codes = values();
}
}