/
utils.go
60 lines (52 loc) · 1.01 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package quasigo
import (
"encoding/binary"
"go/ast"
"go/types"
)
func pickOp(cond bool, ifTrue, otherwise opcode) opcode {
if cond {
return ifTrue
}
return otherwise
}
func put16(code []byte, pos, value int) {
binary.LittleEndian.PutUint16(code[pos:], uint16(value))
}
func decode16(code []byte, pos int) int {
return int(int16(binary.LittleEndian.Uint16(code[pos:])))
}
func typeIsInt(typ types.Type) bool {
basic, ok := typ.Underlying().(*types.Basic)
if !ok {
return false
}
switch basic.Kind() {
case types.Int, types.UntypedInt:
return true
default:
return false
}
}
func typeIsString(typ types.Type) bool {
basic, ok := typ.Underlying().(*types.Basic)
if !ok {
return false
}
return basic.Info()&types.IsString != 0
}
func walkBytecode(code []byte, fn func(pc int, op opcode)) {
pc := 0
for pc < len(code) {
op := opcode(code[pc])
fn(pc, op)
pc += opcodeInfoTable[op].width
}
}
func identName(n ast.Expr) string {
id, ok := n.(*ast.Ident)
if ok {
return id.Name
}
return ""
}