compiler: refactor convertToken func

Move `getEqualityOpcode` into `convertToken`.
`convertToken` does not need codegen.
This commit is contained in:
Evgenii Stratonikov 2020-08-23 12:39:58 +03:00
parent ae88c77a8a
commit fd7af77895
3 changed files with 79 additions and 69 deletions

View file

@ -2,9 +2,9 @@ package compiler
import (
"go/token"
"go/types"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/assert"
)
@ -14,59 +14,74 @@ func TestConvertToken(t *testing.T) {
name string
token token.Token
opcode opcode.Opcode
typ types.Type
}
testCases := []testCase{
{"ADD",
{"ADD (string)",
token.ADD,
opcode.ADD,
types.Typ[types.Int],
},
{"ADD (number)",
token.ADD,
opcode.CAT,
types.Typ[types.String],
},
{"SUB",
token.SUB,
opcode.SUB,
nil,
},
{"MUL",
token.MUL,
opcode.MUL,
nil,
},
{"QUO",
token.QUO,
opcode.DIV,
nil,
},
{"REM",
token.REM,
opcode.MOD,
nil,
},
{"ADD_ASSIGN",
token.ADD_ASSIGN,
opcode.ADD,
nil,
},
{"SUB_ASSIGN",
token.SUB_ASSIGN,
opcode.SUB,
nil,
},
{"MUL_ASSIGN",
token.MUL_ASSIGN,
opcode.MUL,
nil,
},
{"QUO_ASSIGN",
token.QUO_ASSIGN,
opcode.DIV,
nil,
},
{"REM_ASSIGN",
token.REM_ASSIGN,
opcode.MOD,
nil,
},
}
for _, tcase := range testCases {
t.Run(tcase.name, func(t *testing.T) { eval(t, tcase.token, tcase.opcode) })
t.Run(tcase.name, func(t *testing.T) { eval(t, tcase.token, tcase.opcode, tcase.typ) })
}
}
func eval(t *testing.T, token token.Token, opcode opcode.Opcode) {
codegen := &codegen{prog: io.NewBufBinWriter()}
codegen.convertToken(token)
readOpcode := codegen.prog.Bytes()
assert.Equal(t, []byte{byte(opcode)}, readOpcode)
func eval(t *testing.T, token token.Token, opcode opcode.Opcode, typ types.Type) {
op, err := convertToken(token, typ)
assert.NoError(t, err)
assert.Equal(t, opcode, op)
}