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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package text_test
import (
"jinx/pkg/lang/vm/text"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestDecompileSimple(t *testing.T) {
src := `
push_int 1
push_int 2
add
`
expected := `
push_int 1
push_int 2
add
`
test(t, src, expected)
}
func TestDecompileValues(t *testing.T) {
src := `
push_int 1
push_string "foo"
push_float 3.14
push_function @foo
halt
@foo:
push_int 1
ret
`
// No label names yet.
expected := `
push_int 1
push_string "foo"
push_float 3.14
push_function @33
halt
push_int 1
ret
`
test(t, src, expected)
}
func test(t *testing.T, code string, expected string) {
expectedLines := strings.Split(expected, "\n")
trimmedExpectedLines := make([]string, 0, len(expectedLines))
for _, line := range expectedLines {
trimmedLine := strings.TrimSpace(line)
if trimmedLine == "" {
continue
}
trimmedExpectedLines = append(trimmedExpectedLines, trimmedLine)
}
trimmedExpected := strings.Join(trimmedExpectedLines, "\n")
comp := text.NewCompiler(strings.NewReader(code))
resCompiled, err := comp.Compile()
require.NoError(t, err)
decomp := text.NewDecompiler(resCompiled)
resDecompiled := decomp.Decompile()
require.Equal(t, trimmedExpected, resDecompiled)
}
|