Typed operands
This commit is contained in:
@@ -61,12 +61,13 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
|
||||
instr.OpCode = opCode
|
||||
|
||||
operands := in[1:]
|
||||
if len(operands) != operandsByOpCode[instr.OpCode] {
|
||||
return nil, fmt.Errorf("Incorrect number of operands: expected %d, found %d\n", operandsByOpCode[instr.OpCode], len(operands))
|
||||
pattern := 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))
|
||||
}
|
||||
|
||||
if len(operands) >= 1 {
|
||||
op1, err := assembleOperand(operands[0])
|
||||
op1, err := assembleOperand(operands[0], pattern[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "In first operand")
|
||||
}
|
||||
@@ -75,7 +76,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
|
||||
}
|
||||
|
||||
if len(operands) >= 2 {
|
||||
op2, err := assembleOperand(operands[1])
|
||||
op2, err := assembleOperand(operands[1], pattern[1])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "In second operand")
|
||||
}
|
||||
@@ -86,7 +87,7 @@ func assembleInstruction(in instruction) (*vm.Instruction, error) {
|
||||
return instr, nil
|
||||
}
|
||||
|
||||
func assembleOperand(op string) (vm.Operand, error) {
|
||||
func assembleOperand(op string, t operandType) (vm.Operand, error) {
|
||||
op = strings.ToLower(op)
|
||||
ret := vm.Operand{}
|
||||
numStr := ""
|
||||
@@ -100,25 +101,31 @@ func assembleOperand(op string) (vm.Operand, error) {
|
||||
ret.Type = vm.GlobalMemoryIndex
|
||||
numStr = strings.TrimPrefix(op, "g")
|
||||
|
||||
default:
|
||||
case t == s || t == u || t == us:
|
||||
ret.Type = vm.Literal
|
||||
numStr = op
|
||||
|
||||
default:
|
||||
return ret, fmt.Errorf("Invalid operand value type: %s", op)
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"):
|
||||
case t == s || (t == us && (strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"))):
|
||||
num, err := strconv.ParseInt(numStr, 10, 64)
|
||||
if err != nil {
|
||||
return ret, errors.Wrapf(err, "While parsing operand value %s", numStr)
|
||||
}
|
||||
ret.Value = uint64(num)
|
||||
|
||||
default:
|
||||
case t == u || t == us || t == r:
|
||||
num, err := strconv.ParseUint(numStr, 10, 64)
|
||||
if err != nil {
|
||||
return ret, errors.Wrapf(err, "While parsing operand value %s", numStr)
|
||||
}
|
||||
ret.Value = num
|
||||
|
||||
default:
|
||||
return ret, fmt.Errorf("Invalid operand value: %s", numStr)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
Reference in New Issue
Block a user