Search in sources :

Example 1 with UnknownTokenException

use of com.rox.emu.UnknownTokenException in project emuRox by rossdrew.

the class Mos6502CompilerTest method firstInvalidOpcode.

@Test
public void firstInvalidOpcode() {
    final Mos6502Compiler compiler = new Mos6502Compiler("ROX");
    try {
        compiler.compileProgram();
        fail("Exception expected, 'ROX' is an invalid OpCode");
    } catch (UnknownTokenException e) {
        assertTrue(e.getMessage().contains("ROX"));
        assertNotNull(e);
    }
}
Also used : UnknownTokenException(com.rox.emu.UnknownTokenException) Mos6502Compiler(com.rox.emu.processor.mos6502.util.Mos6502Compiler) Test(org.junit.Test)

Example 2 with UnknownTokenException

use of com.rox.emu.UnknownTokenException in project emuRox by rossdrew.

the class Mos6502CompilerTest method opCode2JaCoCoCoverage.

@Test
public void opCode2JaCoCoCoverage() {
    final Mos6502Compiler compiler = new Mos6502Compiler("\0BRK");
    try {
        compiler.compileProgram();
        fail("Exception expected.  This should not pass a String switch statement");
    } catch (UnknownTokenException e) {
        assertTrue(e.getMessage().contains("\0BRK"));
        assertNotNull(e);
    }
}
Also used : UnknownTokenException(com.rox.emu.UnknownTokenException) Mos6502Compiler(com.rox.emu.processor.mos6502.util.Mos6502Compiler) Test(org.junit.Test)

Example 3 with UnknownTokenException

use of com.rox.emu.UnknownTokenException in project emuRox by rossdrew.

the class Mos6502CompilerTest method opCode1JaCoCoCoverage.

@Test
public void opCode1JaCoCoCoverage() {
    final Mos6502Compiler compiler = new Mos6502Compiler("\0ADC $10");
    try {
        compiler.compileProgram();
        fail("Exception expected.  This should not pass a String switch statement");
    } catch (UnknownTokenException e) {
        assertTrue(e.getMessage().contains("\0ADC"));
        assertNotNull(e);
    }
}
Also used : UnknownTokenException(com.rox.emu.UnknownTokenException) Mos6502Compiler(com.rox.emu.processor.mos6502.util.Mos6502Compiler) Test(org.junit.Test)

Example 4 with UnknownTokenException

use of com.rox.emu.UnknownTokenException in project emuRox by rossdrew.

the class Mos6502Compiler method compileProgram.

/**
 * Extract a compiled {@link Program} from this compiler.
 * This will use the program text contained in this object and attempt to compile it
 *
 * @return A compiled {@link Program} object
 * @throws UnknownOpCodeException if during the course of compilation, we encounter an unknown where we expect an op-code to be
 */
public Program compileProgram() {
    Program workingProgram = new Program();
    final StringTokenizer tokenizer = new StringTokenizer(sanitize(programText));
    while (tokenizer.hasMoreTokens()) {
        final String opCodeToken = tokenizer.nextToken();
        if (LABEL_REGEX.matcher(opCodeToken).matches()) {
            workingProgram = workingProgram.with(opCodeToken.substring(0, opCodeToken.length() - 1));
            continue;
        }
        switch(opCodeToken) {
            // XXX Do these need to work with hard coded values as well
            case "BPL":
            case "BMI":
            case "BVC":
            case "BVS":
            case "BCC":
            case "BCS":
            case "BNE":
            case "BEQ":
            case "JSR":
                workingProgram = workingProgram.with(OpCode.from(opCodeToken).getByteValue());
                final String argToken = tokenizer.nextToken().trim();
                if (Character.isAlphabetic(argToken.charAt(0))) {
                    workingProgram = workingProgram.with(workingProgram.referenceBuilder(argToken));
                } else {
                    throw new UnknownTokenException("Invalid label ('" + argToken + "') specified for " + opCodeToken, opCodeToken);
                }
                break;
            // Opcodes with no arguments
            case "TAX":
            case "TAY":
            case "TYA":
            case "TXA":
            case "TXS":
            case "TXY":
            case "TSX":
            case "PHA":
            case "PLA":
            case "PHP":
            case "PLP":
            case "INY":
            case "DEY":
            case "INX":
            case "DEX":
            case "RTS":
            case "RTI":
            case "SEC":
            case "CLC":
            case "SEI":
            case "SED":
            case "CLD":
            case "CLI":
            case "CLV":
            case "BRK":
            case "NOP":
                workingProgram = workingProgram.with(OpCode.from(opCodeToken).getByteValue());
                break;
            // Opcodes with non label arguments
            case "ADC":
            case "SBC":
            case "LDA":
            case "LDY":
            case "LDX":
            case "AND":
            case "ORA":
            case "EOR":
            case "ASL":
            case "ROL":
            case "LSR":
            case "STY":
            case "STX":
            case "STA":
            case "CMP":
            case "CPX":
            case "CPY":
            case "INC":
            case "DEC":
            case "BIT":
            // Absolute only
            case "JMP":
            case // Accumulator only
            "ROR":
                final String valueToken = tokenizer.nextToken().trim();
                final String prefix = extractFirstOccurrence(ARG_PREFIX_REGEX, valueToken).trim();
                final String value = extractFirstOccurrence(ARG_VALUE_REGEX, valueToken).trim();
                final String postfix = extractFirstOccurrence(ARG_POSTFIX_REGEX, valueToken).trim();
                final AddressingMode addressingMode = getAddressingModeFrom(prefix, value, postfix);
                workingProgram = workingProgram.with(OpCode.from(opCodeToken, addressingMode).getByteValue());
                workingProgram = extractArgumentValue(workingProgram, value);
                break;
            default:
                throw new UnknownTokenException("Unknown token (\"" + opCodeToken + "\") while parsing program", opCodeToken);
        }
    }
    return workingProgram;
}
Also used : StringTokenizer(java.util.StringTokenizer) UnknownTokenException(com.rox.emu.UnknownTokenException) AddressingMode(com.rox.emu.processor.mos6502.op.AddressingMode)

Aggregations

UnknownTokenException (com.rox.emu.UnknownTokenException)4 Mos6502Compiler (com.rox.emu.processor.mos6502.util.Mos6502Compiler)3 Test (org.junit.Test)3 AddressingMode (com.rox.emu.processor.mos6502.op.AddressingMode)1 StringTokenizer (java.util.StringTokenizer)1