Hex and binary literal support in hex

This commit is contained in:
Ian Gulliver
2021-11-20 10:19:08 -10:00
parent c43836a9f0
commit c67028520c
2 changed files with 32 additions and 2 deletions

View File

@@ -106,14 +106,14 @@ func assembleOperand(op string, t operandType) (*vm.Operand, error) {
switch {
case t == s || (t == us && (strings.HasPrefix(numStr, "+") || strings.HasPrefix(numStr, "-"))):
num, err := strconv.ParseInt(numStr, 10, 64)
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:
num, err := strconv.ParseUint(numStr, 10, 64)
num, err := strconv.ParseUint(numStr, 0, 64)
if err != nil {
return ret, errors.Wrapf(err, "While parsing operand value %s", numStr)
}

View File

@@ -33,3 +33,33 @@ functions:
t.Fatal("Lack of error when passing signed value to ltu")
}
}
func TestHex(t *testing.T) {
prog, err := asm.AssembleString(`
functions:
- - [mov, g0, 0xfeedc0de]
`)
if err != nil {
t.Fatal(err)
}
val := prog.Functions[0].Instructions[0].Operands[1].Value
if val != 4276994270 {
t.Fatalf("Mismatch: expected=4276994270, actual=%d", val)
}
}
func TestBinary(t *testing.T) {
prog, err := asm.AssembleString(`
functions:
- - [mov, g0, 0b100101]
`)
if err != nil {
t.Fatal(err)
}
val := prog.Functions[0].Instructions[0].Operands[1].Value
if val != 37 {
t.Fatalf("Mismatch: expected=37, actual=%d", val)
}
}