public void parseToken(String value) throws MemoryFullException, MemoryEmptyException, InvalidOperationException, InvalidTokenException { Token token = TokenFactory.buildToken(value); if (token instanceof ValueToken) { ValueToken valueToken = (ValueToken) token; registers.push(valueToken.getValue()); } else if (token instanceof UnaryOperatorToken) { UnaryOperatorToken unaryToken = (UnaryOperatorToken) token; if (registers.size() < 1) { throw new InvalidOperationException("Not enough operands."); } double op1 = registers.pop(); double res = unaryToken.getUnaryOperation().doOperation(op1); registers.push(res); } else if (token instanceof BinaryOperatorToken) { BinaryOperatorToken binaryToken = (BinaryOperatorToken) token; if (registers.size() < 2) { throw new InvalidOperationException("Not enough operands."); } double op1 = registers.pop(); double op2 = registers.pop(); double res = binaryToken.getBinaryOperation().doOperation(op2, op1); registers.push(res); } }
/* * Function : parseInstruction * Description : Parses the IType instructions without displacements. */ public static ITypeImmediate parseInstruction(String instruction, int address) { ITypeImmediate instruct = new ITypeImmediate(); // set the icode instruct.icode = IMap.get(Instruction.getIcodeName(instruction)); // grab the string from the registers.java class that we will use to match registers Pattern registerPattern = Pattern.compile(Registers.registerString()); Matcher registerMatcher = registerPattern.matcher(instruction); // match the first register if there is one if (registerMatcher.find()) { instruct.rd = Registers.registerValue(registerMatcher.group(0)); // I clear off the bits we've already read instruction = instruction.substring(registerMatcher.end(0)); } // match the second register if we can if (registerMatcher.find()) { instruct.r1 = Registers.registerValue(registerMatcher.group(0)); } // This regex attempts to match a label name. The [^\\s\\d] means not a space or digit character // This ensures that the label is infact a valid label and does not start with a number. Pattern symbolPattern = Pattern.compile("\\b([^\\d\\s][^\\s]*)\\b$"); Matcher symbolMatcher = symbolPattern.matcher(instruction); Pattern literalPattern = Pattern.compile("\\b(0x)?\\d+\\b"); Matcher literalMatcher = literalPattern.matcher(instruction); if (symbolMatcher.find()) { SymbolTable symbolTable = SymbolTable.getInstance(); instruct.immediateValue = symbolTable.getSymbol(symbolMatcher.group(0).replace(" ", "")); if (instruct.icode == BEQZ || instruct.icode == BNEZ) { instruct.r1 = instruct.rd; instruct.rd = 0; instruct.immediateValue = instruct.immediateValue - (address + 4); } } else if (literalMatcher.find()) { instruct.immediateValue = Tools.processNumber(literalMatcher.group(0).replace(" ", "")); } else { instruct.r1 = instruct.rd; instruct.rd = 0; } return instruct; }