Skip to content

Commit 10694ba

Browse files
committed
Add func names in disassemble
1 parent ba04206 commit 10694ba

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

compiler/compiler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
5454
Bytecode: c.bytecode,
5555
Arguments: c.arguments,
5656
Functions: c.functions,
57+
FuncNames: c.functionNames,
5758
}
5859
return
5960
}
@@ -64,6 +65,7 @@ type compiler struct {
6465
constants []interface{}
6566
constantsIndex map[interface{}]int
6667
functions []Function
68+
functionNames []string
6769
functionsIndex map[string]int
6870
mapEnv bool
6971
cast reflect.Kind
@@ -154,6 +156,7 @@ func (c *compiler) addFunction(fn *builtin.Function) int {
154156
}
155157
p := len(c.functions)
156158
c.functions = append(c.functions, fn.Func)
159+
c.functionNames = append(c.functionNames, fn.Name)
157160
c.functionsIndex[fn.Name] = p
158161
return p
159162
}

vm/program.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Program struct {
2323
Bytecode []Opcode
2424
Arguments []int
2525
Functions []Function
26+
FuncNames []string
2627
}
2728

2829
func (program *Program) Disassemble() string {
@@ -74,6 +75,9 @@ func (program *Program) Opcodes(w io.Writer) {
7475
builtin := func(label string) {
7576
_, _ = fmt.Fprintf(w, "%v\t%v\t<%v>\t%v\n", pp, label, arg, builtin.Functions[arg].Name)
7677
}
78+
funcName := func(label string) {
79+
_, _ = fmt.Fprintf(w, "%v\t%v\t<%v>\t%v()\n", pp, label, arg, program.FuncNames[arg])
80+
}
7781

7882
switch op {
7983
case OpInvalid:
@@ -218,16 +222,16 @@ func (program *Program) Opcodes(w io.Writer) {
218222
argument("OpCall")
219223

220224
case OpCall0:
221-
argument("OpCall0")
225+
funcName("OpCall0")
222226

223227
case OpCall1:
224-
argument("OpCall1")
228+
funcName("OpCall1")
225229

226230
case OpCall2:
227-
argument("OpCall2")
231+
funcName("OpCall2")
228232

229233
case OpCall3:
230-
argument("OpCall3")
234+
funcName("OpCall3")
231235

232236
case OpCallN:
233237
argument("OpCallN")

vm/program_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestProgram_Disassemble(t *testing.T) {
1313
Constants: []interface{}{1, 2},
1414
Bytecode: []vm.Opcode{op},
1515
Arguments: []int{1},
16+
FuncNames: []string{"foo", "bar"},
1617
}
1718
d := program.Disassemble()
1819
if strings.Contains(d, "(unknown)") {

0 commit comments

Comments
 (0)