Move operand type lists into the vm

This commit is contained in:
Ian Gulliver
2021-11-20 17:26:58 -10:00
parent 30ff003494
commit ae771823b0
4 changed files with 60 additions and 60 deletions

View File

@@ -65,7 +65,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
instr.OpCode = opCode
operands := in[1:]
pattern := operandsByOpCode[instr.OpCode]
pattern := vm.OperandsByOpCode[instr.OpCode]
if len(operands) != len(pattern) {
return nil, fmt.Errorf("Incorrect number of operands: expected %d, found %d\n", len(pattern), len(operands))
}
@@ -82,7 +82,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
return instr, nil
}
func assembleOperand(op string, t operandType) (*vm.Operand, error) {
func assembleOperand(op string, t vm.OperandNumericType) (*vm.Operand, error) {
op = strings.ToLower(op)
ret := &vm.Operand{}
numStr := ""
@@ -96,7 +96,7 @@ func assembleOperand(op string, t operandType) (*vm.Operand, error) {
ret.Type = vm.GlobalMemoryIndex
numStr = strings.TrimPrefix(op, "g")
case t == s || t == u || t == us:
case t == vm.OperandSigned || t == vm.OperandUnsigned || t == vm.OperandSignedOrUnsigned:
ret.Type = vm.Literal
numStr = op
@@ -105,14 +105,14 @@ func assembleOperand(op string, t operandType) (*vm.Operand, error) {
}
switch {
case t == s || (t == us && (strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"))):
case t == vm.OperandSigned || (t == vm.OperandSignedOrUnsigned && (strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"))):
num, err := strconv.ParseInt(numStr, 0, 64)
if err != nil {
return ret, errors.Wrapf(err, "While parsing operand value %s", numStr)
}
ret.Value = uint64(num)
case t == u || t == us || t == r:
case t == vm.OperandUnsigned || t == vm.OperandSignedOrUnsigned || t == vm.OperandReference:
num, err := strconv.ParseUint(numStr, 0, 64)
if err != nil {
return ret, errors.Wrapf(err, "While parsing operand value %s", numStr)