codecin 7.22.10__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1069 @@
|
|
|
1
|
+
package compiler
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"fmt"
|
|
6
|
+
"sort"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
|
|
10
|
+
"codecin-native/engine"
|
|
11
|
+
"codecin-native/ir"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
// 这些测试锁定 docs/SUGGESTIONS.md 批次 B 修复的 Go 侧行为:
|
|
15
|
+
// * switch 负 case 匹配 / 非常量 case 报错 / 标签与分支一一对应;
|
|
16
|
+
// * 语义错误必须报编译错误, 而不是产出静默错误的字节码;
|
|
17
|
+
// * 数值字面量后缀与 64 位范围;
|
|
18
|
+
// * switch 内 continue 不泄漏选择器栈槽。
|
|
19
|
+
|
|
20
|
+
// dumpProgram 把 IR 渲染成稳定文本, 便于比较与断言 (避开对 engine 包的依赖)。
|
|
21
|
+
func dumpProgram(prog *ir.Program) string {
|
|
22
|
+
var sb strings.Builder
|
|
23
|
+
for _, ins := range prog.Instructions {
|
|
24
|
+
sb.WriteString(ins.Op)
|
|
25
|
+
for _, a := range ins.Args {
|
|
26
|
+
fmt.Fprintf(&sb, " %s:%d:%d:%s:%g", a.Kind, a.V, a.Off, a.Name, a.F)
|
|
27
|
+
}
|
|
28
|
+
sb.WriteByte('\n')
|
|
29
|
+
}
|
|
30
|
+
return sb.String()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func compileOK(t *testing.T, src string) string {
|
|
34
|
+
t.Helper()
|
|
35
|
+
prog, err := Compile(src, "t.cin", false)
|
|
36
|
+
if err != nil {
|
|
37
|
+
t.Fatalf("期望编译通过, 实际报错: %v", err)
|
|
38
|
+
}
|
|
39
|
+
return dumpProgram(prog)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func compileErr(t *testing.T, src, wantSubstr string) {
|
|
43
|
+
t.Helper()
|
|
44
|
+
_, err := Compile(src, "t.cin", false)
|
|
45
|
+
if err == nil {
|
|
46
|
+
t.Fatalf("期望编译错误 (%s), 实际通过", wantSubstr)
|
|
47
|
+
}
|
|
48
|
+
if !strings.Contains(err.Error(), wantSubstr) {
|
|
49
|
+
t.Fatalf("错误信息应包含 %q, 实际: %v", wantSubstr, err)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
func TestSwitchNegativeCaseIsComparable(t *testing.T) {
|
|
54
|
+
// 旧实现用 -1 当 default 哨兵并跳过 raw < 0, `case -1` 永远匹配不上。
|
|
55
|
+
// 这里检查分派比较链里确实出现了 0xFFFFFFFFFFFFFFFF。
|
|
56
|
+
src := `
|
|
57
|
+
function main() -> int {
|
|
58
|
+
int x = -1
|
|
59
|
+
switch (x) {
|
|
60
|
+
case -1: return 10
|
|
61
|
+
default: return 30
|
|
62
|
+
}
|
|
63
|
+
return 0
|
|
64
|
+
}`
|
|
65
|
+
prog, err := Compile(src, "t.cin", false)
|
|
66
|
+
if err != nil {
|
|
67
|
+
t.Fatalf("编译失败: %v", err)
|
|
68
|
+
}
|
|
69
|
+
found := false
|
|
70
|
+
for _, ins := range prog.Instructions {
|
|
71
|
+
if ins.Op != "MOV" || len(ins.Args) != 2 {
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
if ins.Args[1].Kind == "imm" && ins.Args[1].V == -1 {
|
|
75
|
+
found = true
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if !found {
|
|
79
|
+
t.Fatal("分派链中未找到 case -1 的比较常量")
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func TestSwitchNonConstantCaseIsCompileError(t *testing.T) {
|
|
84
|
+
compileErr(t, `
|
|
85
|
+
function main() -> int {
|
|
86
|
+
int y = 2
|
|
87
|
+
int x = 1
|
|
88
|
+
switch (x) {
|
|
89
|
+
case y: return 10
|
|
90
|
+
case 1: return 20
|
|
91
|
+
}
|
|
92
|
+
return 0
|
|
93
|
+
}`, "constant")
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func TestSwitchFloatSelectorIsCompileError(t *testing.T) {
|
|
97
|
+
compileErr(t, `
|
|
98
|
+
function main() -> int {
|
|
99
|
+
float f = 1.5
|
|
100
|
+
switch (f) {
|
|
101
|
+
case 1: return 1
|
|
102
|
+
}
|
|
103
|
+
return 0
|
|
104
|
+
}`, "Switch expression must be integer")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
func TestSwitchWithContinueCompiles(t *testing.T) {
|
|
108
|
+
// 只需通过编译与编码: 栈平衡由运行时测试 (tests/test_switch_semantics.py) 覆盖
|
|
109
|
+
compileOK(t, `
|
|
110
|
+
function main() -> int {
|
|
111
|
+
int i = 0
|
|
112
|
+
int n = 0
|
|
113
|
+
while (i < 10) {
|
|
114
|
+
i = i + 1
|
|
115
|
+
switch (i) {
|
|
116
|
+
case 1: continue
|
|
117
|
+
default: n = n + 1
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return n
|
|
121
|
+
}`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
func TestSemanticErrorsAreReported(t *testing.T) {
|
|
125
|
+
cases := []struct{ name, src, want string }{
|
|
126
|
+
{"unknown_function", `function main() -> int { return foo() }`, "Unknown function"},
|
|
127
|
+
{"undefined_var", `function main() -> int { zzz = 1 return 0 }`, "Undefined variable"},
|
|
128
|
+
{"member_on_int", `function main() -> int { int x = 1 return x.foo }`, "non-struct"},
|
|
129
|
+
{"bitnot_float", `function main() -> int { return ~1.5 }`, "Bitwise NOT"},
|
|
130
|
+
{"float_mod", `function main() -> int { return 7.5 % 2.0 }`, "Float modulo"},
|
|
131
|
+
{"sqrt_no_args", `function main() -> int { float f = sqrt() return 0 }`, "at least 1"},
|
|
132
|
+
{"substr_too_few", `function main() -> int { string s = substr("a", 1) return 0 }`, "at least 3"},
|
|
133
|
+
{"break_outside", `function main() -> int { break return 0 }`, "break outside loop"},
|
|
134
|
+
{"continue_outside", `function main() -> int { continue return 0 }`, "continue outside loop"},
|
|
135
|
+
{"int_out_of_range", `function main() -> int { int x = 9223372036854775808 return 0 }`, "Malformed numeric"},
|
|
136
|
+
}
|
|
137
|
+
for _, c := range cases {
|
|
138
|
+
t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, c.want) })
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
func TestValidProgramsStillCompile(t *testing.T) {
|
|
143
|
+
// 防止错误检查做得过严
|
|
144
|
+
srcs := []string{
|
|
145
|
+
`function main() -> int { int x = 0xFFu return x }`,
|
|
146
|
+
`function main() -> int { int x = 0b1010U return x }`,
|
|
147
|
+
`function main() -> int { int x = 42u return x }`,
|
|
148
|
+
`function main() -> int { float f = 1.5f return 0 }`,
|
|
149
|
+
`function main() -> int { return min(3, 4) + max(1, 2) }`,
|
|
150
|
+
`function main() -> int { return strlen("ab", 9) }`,
|
|
151
|
+
`function main() -> int { int a = 6 int b = 3 return (a & b) | (a ^ b) }`,
|
|
152
|
+
}
|
|
153
|
+
for _, src := range srcs {
|
|
154
|
+
if _, err := Compile(src, "t.cin", false); err != nil {
|
|
155
|
+
t.Fatalf("合法程序被拒: %v\n%s", err, src)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func TestCompileIsDeterministic(t *testing.T) {
|
|
161
|
+
src := `function main() -> int { int s = 0 for (int i = 0; i < 5; i++) { s += i } return s }`
|
|
162
|
+
a := compileOK(t, src)
|
|
163
|
+
b := compileOK(t, src)
|
|
164
|
+
if a != b {
|
|
165
|
+
t.Fatal("同一输入的编译产物不稳定")
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// runCompiled 编译 CIN 源码并在 Go VM 中执行, 返回 x0 (main 的返回值)。
|
|
170
|
+
func runCompiled(t *testing.T, src string) uint64 {
|
|
171
|
+
t.Helper()
|
|
172
|
+
prog, err := Compile(src, "t.cin", false)
|
|
173
|
+
if err != nil {
|
|
174
|
+
t.Fatalf("编译失败: %v", err)
|
|
175
|
+
}
|
|
176
|
+
bc, err := engine.EncodeProgram(*prog, 0)
|
|
177
|
+
if err != nil {
|
|
178
|
+
t.Fatalf("编码失败: %v", err)
|
|
179
|
+
}
|
|
180
|
+
mem := make([]byte, 64*1024)
|
|
181
|
+
for _, dw := range prog.DataWrites {
|
|
182
|
+
copy(mem[dw.Addr:], dw.Data)
|
|
183
|
+
}
|
|
184
|
+
res := engine.Run(bc, mem, 0, int64(len(mem)-8), int64(len(mem)/2),
|
|
185
|
+
nil, 100_000_000)
|
|
186
|
+
if res == nil {
|
|
187
|
+
t.Fatal("Run 返回 nil")
|
|
188
|
+
}
|
|
189
|
+
if res.ErrMsg != "" {
|
|
190
|
+
t.Fatalf("运行期错误: %s", res.ErrMsg)
|
|
191
|
+
}
|
|
192
|
+
if res.Status != engine.StatusOK {
|
|
193
|
+
t.Fatalf("非正常结束: status=%d", res.Status)
|
|
194
|
+
}
|
|
195
|
+
return res.Regs[0]
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// TestFloatSubscriptIsTruncatedToInt 锁定子集 issue #1 的 Go 侧修复。
|
|
199
|
+
//
|
|
200
|
+
// `/` 恒为浮点除法, 所以 `a[(lo + hi) / 2]` 的下标是 float。修复前 genIndex
|
|
201
|
+
// 把这个 float 的 IEEE-754 位模式直接当作字节偏移 (于是 249.5 变成
|
|
202
|
+
// 0x0379_8000_0000_0000), 标准 Hoare 快排第一次取枢轴就越界崩溃。
|
|
203
|
+
func TestFloatSubscriptIsTruncatedToInt(t *testing.T) {
|
|
204
|
+
src := `
|
|
205
|
+
int G_C[500]
|
|
206
|
+
|
|
207
|
+
function qrec(int lo, int hi) -> void {
|
|
208
|
+
if (lo >= hi) { return }
|
|
209
|
+
int p = G_C[(lo + hi) / 2]
|
|
210
|
+
int i = lo
|
|
211
|
+
int j = hi
|
|
212
|
+
while (i <= j) {
|
|
213
|
+
while (i <= j && G_C[i] < p) { i = i + 1 }
|
|
214
|
+
while (i <= j && G_C[j] > p) { j = j - 1 }
|
|
215
|
+
if (i <= j) {
|
|
216
|
+
int t = G_C[i]
|
|
217
|
+
G_C[i] = G_C[j]
|
|
218
|
+
G_C[j] = t
|
|
219
|
+
i = i + 1
|
|
220
|
+
j = j - 1
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
qrec(lo, j)
|
|
224
|
+
qrec(i, hi)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function main() -> int {
|
|
228
|
+
for (int i = 0; i < 500; i++) {
|
|
229
|
+
G_C[i] = (i * 7919 + 13) % 10007
|
|
230
|
+
}
|
|
231
|
+
qrec(0, 499)
|
|
232
|
+
for (int i = 0; i < 499; i++) {
|
|
233
|
+
if (G_C[i] > G_C[i + 1]) { return 1000000000 + i }
|
|
234
|
+
}
|
|
235
|
+
return G_C[0] * 10007 + G_C[499]
|
|
236
|
+
}`
|
|
237
|
+
|
|
238
|
+
vals := make([]int, 500)
|
|
239
|
+
for i := range vals {
|
|
240
|
+
vals[i] = (i*7919 + 13) % 10007
|
|
241
|
+
}
|
|
242
|
+
sort.Ints(vals)
|
|
243
|
+
want := uint64(vals[0]*10007 + vals[len(vals)-1])
|
|
244
|
+
if got := runCompiled(t, src); got != want {
|
|
245
|
+
t.Fatalf("快排结果 x0 = %d, 期望 %d (未排序或越界)", got, want)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
func TestFloatSubscriptTruncatesTowardZero(t *testing.T) {
|
|
250
|
+
// float 下标与 `int x = 1.9` 一致: 向零截断; -0.5 -> 0。
|
|
251
|
+
src := `
|
|
252
|
+
function main() -> int {
|
|
253
|
+
int a[4]
|
|
254
|
+
a[0] = 7
|
|
255
|
+
a[1] = 20
|
|
256
|
+
a[2] = 30
|
|
257
|
+
a[3] = 40
|
|
258
|
+
float f = 1.9
|
|
259
|
+
int r = a[f]
|
|
260
|
+
float g = 3.0
|
|
261
|
+
a[g - 0.5] = 99
|
|
262
|
+
float neg = -0.5
|
|
263
|
+
string s = "ABC"
|
|
264
|
+
int ch = s[1.9]
|
|
265
|
+
return r + a[2] + a[neg] + ch
|
|
266
|
+
}`
|
|
267
|
+
// 20 + 99 + 7 + 'B'(66) = 192
|
|
268
|
+
if got := runCompiled(t, src); got != 192 {
|
|
269
|
+
t.Fatalf("x0 = %d, 期望 192", got)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// TestNestedSubscriptUsesCorrectBase 覆盖"临时值必须溢出到栈"这一类缺陷。
|
|
274
|
+
//
|
|
275
|
+
// 编译器没有寄存器分配器, 约定是任何跨另一个子表达式存活的临时值都要存栈
|
|
276
|
+
// (子表达式会随意覆盖 x1..x6)。genIndex 曾把数组/字符串基址暂存在 x3, 于是
|
|
277
|
+
// A[B[i]] 的内层下标把 x3 改成 B 的基址, 外层就用 &B[0] 当 A 的地址 ——
|
|
278
|
+
// 读错值、写坏内存, 且不报任何错。genAssign / genInit2dLiteral 有同一问题。
|
|
279
|
+
func TestNestedSubscriptUsesCorrectBase(t *testing.T) {
|
|
280
|
+
cases := []struct {
|
|
281
|
+
name string
|
|
282
|
+
src string
|
|
283
|
+
want uint64
|
|
284
|
+
}{
|
|
285
|
+
{"nested_read", `
|
|
286
|
+
int A[8]
|
|
287
|
+
int B[8]
|
|
288
|
+
function main() -> int {
|
|
289
|
+
for (int i = 0; i < 8; i++) { B[i] = 7 - i }
|
|
290
|
+
A[0] = 100
|
|
291
|
+
A[7] = 700
|
|
292
|
+
return A[B[7]]
|
|
293
|
+
}`, 100},
|
|
294
|
+
{"nested_write", `
|
|
295
|
+
int A[8]
|
|
296
|
+
int B[8]
|
|
297
|
+
function main() -> int {
|
|
298
|
+
B[0] = 3
|
|
299
|
+
A[3] = 0
|
|
300
|
+
A[B[0]] = 77
|
|
301
|
+
return A[3]
|
|
302
|
+
}`, 77},
|
|
303
|
+
{"call_in_lvalue_index", `
|
|
304
|
+
int A[8]
|
|
305
|
+
function idx(int n) -> int { int t = n * 2
|
|
306
|
+
return t }
|
|
307
|
+
function main() -> int {
|
|
308
|
+
A[6] = 0
|
|
309
|
+
A[idx(3)] = 42
|
|
310
|
+
return A[6]
|
|
311
|
+
}`, 42},
|
|
312
|
+
{"nested_string_index", `
|
|
313
|
+
int B[4]
|
|
314
|
+
function main() -> int {
|
|
315
|
+
string s = "ABC"
|
|
316
|
+
B[0] = 1
|
|
317
|
+
return s[B[0]]
|
|
318
|
+
}`, 66},
|
|
319
|
+
{"three_levels", `
|
|
320
|
+
int A[8]
|
|
321
|
+
int B[8]
|
|
322
|
+
int C[8]
|
|
323
|
+
function main() -> int {
|
|
324
|
+
C[0] = 2
|
|
325
|
+
B[2] = 5
|
|
326
|
+
A[5] = 91
|
|
327
|
+
return A[B[C[0]]]
|
|
328
|
+
}`, 91},
|
|
329
|
+
{"nested_in_2d_row", `
|
|
330
|
+
int B[4]
|
|
331
|
+
function main() -> int {
|
|
332
|
+
int m[3][4]
|
|
333
|
+
B[0] = 2
|
|
334
|
+
m[2][3] = 55
|
|
335
|
+
return m[B[0]][3]
|
|
336
|
+
}`, 55},
|
|
337
|
+
{"compound_through_nested", `
|
|
338
|
+
int A[8]
|
|
339
|
+
int B[8]
|
|
340
|
+
function main() -> int {
|
|
341
|
+
B[0] = 4
|
|
342
|
+
A[4] = 10
|
|
343
|
+
A[B[0]] += 5
|
|
344
|
+
return A[4]
|
|
345
|
+
}`, 15},
|
|
346
|
+
{"float_nested_index", `
|
|
347
|
+
int A[8]
|
|
348
|
+
int B[8]
|
|
349
|
+
function main() -> int {
|
|
350
|
+
B[0] = 6
|
|
351
|
+
A[3] = 44
|
|
352
|
+
return A[B[0] / 2]
|
|
353
|
+
}`, 44},
|
|
354
|
+
{"ptrarray_literal_with_calls", `
|
|
355
|
+
function g(int n) -> int { int t = n * 10
|
|
356
|
+
return t }
|
|
357
|
+
function main() -> int {
|
|
358
|
+
int[][] m = {{g(1), g(2)}, {g(3), g(4)}}
|
|
359
|
+
return m[0][0] * 1000 + m[0][1] * 100 + m[1][0] * 10 + m[1][1]
|
|
360
|
+
}`, 12340},
|
|
361
|
+
}
|
|
362
|
+
for _, c := range cases {
|
|
363
|
+
t.Run(c.name, func(t *testing.T) {
|
|
364
|
+
if got := runCompiled(t, c.src); got != c.want {
|
|
365
|
+
t.Fatalf("x0 = %d, 期望 %d (嵌套下标算错基址)", got, c.want)
|
|
366
|
+
}
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ==================== 枚举 / 范围 for / case 范围 / 多参数 print ====================
|
|
372
|
+
// 这些测试锁定 Python 编译器 (codecin/cin.py) 新增特性在 Go 侧的等价行为。
|
|
373
|
+
|
|
374
|
+
func countOp(prog *ir.Program, op string) int {
|
|
375
|
+
n := 0
|
|
376
|
+
for _, ins := range prog.Instructions {
|
|
377
|
+
if ins.Op == op {
|
|
378
|
+
n++
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return n
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
func countSys(prog *ir.Program, sysID int64) int {
|
|
385
|
+
n := 0
|
|
386
|
+
for _, ins := range prog.Instructions {
|
|
387
|
+
if ins.Op == "SYS" && len(ins.Args) == 1 &&
|
|
388
|
+
ins.Args[0].Kind == "imm" && ins.Args[0].V == sysID {
|
|
389
|
+
n++
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return n
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// hasImm 判定是否存在 op 指令带指定立即数操作数。
|
|
396
|
+
func hasImm(prog *ir.Program, op string, v int64) bool {
|
|
397
|
+
for _, ins := range prog.Instructions {
|
|
398
|
+
if ins.Op != op {
|
|
399
|
+
continue
|
|
400
|
+
}
|
|
401
|
+
for _, a := range ins.Args {
|
|
402
|
+
if a.Kind == "imm" && a.V == v {
|
|
403
|
+
return true
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return false
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
func hasCond(prog *ir.Program, cond string) bool {
|
|
411
|
+
for _, ins := range prog.Instructions {
|
|
412
|
+
if ins.Op != "B" {
|
|
413
|
+
continue
|
|
414
|
+
}
|
|
415
|
+
for _, a := range ins.Args {
|
|
416
|
+
if a.Kind == "cond" && a.Name == cond {
|
|
417
|
+
return true
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return false
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
func hasLabelPrefix(prog *ir.Program, prefix string) bool {
|
|
425
|
+
for name := range prog.Labels {
|
|
426
|
+
if strings.HasPrefix(name, prefix) {
|
|
427
|
+
return true
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return false
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
func hasDataWrite(prog *ir.Program, want []byte) bool {
|
|
434
|
+
for _, dw := range prog.DataWrites {
|
|
435
|
+
if bytes.Equal(dw.Data, want) {
|
|
436
|
+
return true
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return false
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// ---- 枚举 ----
|
|
443
|
+
|
|
444
|
+
func TestEnumConstantsFoldIntoCodegen(t *testing.T) {
|
|
445
|
+
src := `
|
|
446
|
+
enum E { A, B = 5, C }
|
|
447
|
+
function main() -> int {
|
|
448
|
+
return C
|
|
449
|
+
}`
|
|
450
|
+
prog, err := Compile(src, "t.cin", false)
|
|
451
|
+
if err != nil {
|
|
452
|
+
t.Fatalf("编译失败: %v", err)
|
|
453
|
+
}
|
|
454
|
+
// C 自动递增为 6: 值上下文必须折叠为立即数
|
|
455
|
+
if !hasImm(prog, "MOV", 6) {
|
|
456
|
+
t.Fatal("`return C` 未折叠为 MOV imm 6")
|
|
457
|
+
}
|
|
458
|
+
if got := runCompiled(t, src); got != 6 {
|
|
459
|
+
t.Fatalf("x0 = %d, 期望 6", got)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
func TestEnumInGlobalInitializerAndCaseLabel(t *testing.T) {
|
|
464
|
+
src := `
|
|
465
|
+
enum Color { RED = 3, GREEN, BLUE }
|
|
466
|
+
int G = BLUE
|
|
467
|
+
|
|
468
|
+
function main() -> int {
|
|
469
|
+
int x = 5
|
|
470
|
+
int r = 0
|
|
471
|
+
switch (x) {
|
|
472
|
+
case BLUE: r = 20 break
|
|
473
|
+
default: r = 99
|
|
474
|
+
}
|
|
475
|
+
return G * 100 + r
|
|
476
|
+
}`
|
|
477
|
+
// G = BLUE = 5, x = 5 命中 case BLUE -> 520
|
|
478
|
+
if got := runCompiled(t, src); got != 520 {
|
|
479
|
+
t.Fatalf("x0 = %d, 期望 520", got)
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
func TestEnumMemberIsNotAnLvalue(t *testing.T) {
|
|
484
|
+
compileErr(t, `
|
|
485
|
+
enum E { A, B }
|
|
486
|
+
function main() -> int {
|
|
487
|
+
A = 3
|
|
488
|
+
return 0
|
|
489
|
+
}`, "Cannot assign to enum member")
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
func TestEnumErrors(t *testing.T) {
|
|
493
|
+
cases := []struct{ name, src, want string }{
|
|
494
|
+
{"duplicate_member", `
|
|
495
|
+
enum E { A, B = 5, A }
|
|
496
|
+
function main() -> int { return 0 }`, "Duplicate enum member: A"},
|
|
497
|
+
{"duplicate_across_enums", `
|
|
498
|
+
enum E { A }
|
|
499
|
+
enum F { A }
|
|
500
|
+
function main() -> int { return 0 }`, "Duplicate enum member: A"},
|
|
501
|
+
{"empty_enum", `
|
|
502
|
+
enum E { }
|
|
503
|
+
function main() -> int { return 0 }`, "Empty enum E"},
|
|
504
|
+
{"name_is_keyword", `
|
|
505
|
+
enum int { A }
|
|
506
|
+
function main() -> int { return 0 }`, "Invalid enum name: int"},
|
|
507
|
+
{"name_is_taken", `
|
|
508
|
+
enum E { A }
|
|
509
|
+
enum E { B }
|
|
510
|
+
function main() -> int { return 0 }`, "Invalid enum name: E"},
|
|
511
|
+
{"shift_out_of_range", `
|
|
512
|
+
enum E { A = 1 << 63 }
|
|
513
|
+
function main() -> int { return 0 }`, "Enum member A out of 64-bit range"},
|
|
514
|
+
{"increment_out_of_range", `
|
|
515
|
+
enum E { A = 9223372036854775807, B }
|
|
516
|
+
function main() -> int { return 0 }`, "Enum member B out of 64-bit range"},
|
|
517
|
+
{"negative_out_of_range", `
|
|
518
|
+
enum E { A = -9223372036854775807 - 2 }
|
|
519
|
+
function main() -> int { return 0 }`, "Enum member A out of 64-bit range"},
|
|
520
|
+
{"divide_by_zero", `
|
|
521
|
+
enum E { A = 1 / 0, B }
|
|
522
|
+
function main() -> int { return 0 }`, "Enum member A initializer divides by zero"},
|
|
523
|
+
{"modulo_by_zero", `
|
|
524
|
+
enum E { A = 1 % 0, B }
|
|
525
|
+
function main() -> int { return 0 }`, "Enum member A initializer divides by zero"},
|
|
526
|
+
{"unknown_name", `
|
|
527
|
+
enum E { A = NOPE }
|
|
528
|
+
function main() -> int { return 0 }`, "must be an integer constant"},
|
|
529
|
+
{"float_initializer", `
|
|
530
|
+
enum E { A = 1.5 }
|
|
531
|
+
function main() -> int { return 0 }`, "must be an integer constant expression"},
|
|
532
|
+
{"global_name_collides", `
|
|
533
|
+
enum E { A }
|
|
534
|
+
int A = 1
|
|
535
|
+
function main() -> int { return 0 }`, "already used as an enum member or keyword"},
|
|
536
|
+
{"global_name_is_keyword", `
|
|
537
|
+
enum E { A }
|
|
538
|
+
int enum = 1
|
|
539
|
+
function main() -> int { return 0 }`, "already used as an enum member or keyword"},
|
|
540
|
+
{"range_for_var_is_member", `
|
|
541
|
+
enum E { A }
|
|
542
|
+
function main() -> int {
|
|
543
|
+
int a[2]
|
|
544
|
+
for (int A : a) { }
|
|
545
|
+
return 0
|
|
546
|
+
}`, "Invalid range-for variable: A"},
|
|
547
|
+
}
|
|
548
|
+
for _, c := range cases {
|
|
549
|
+
t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, c.want) })
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
func TestEnumInitializerArithmetic(t *testing.T) {
|
|
554
|
+
src := `
|
|
555
|
+
enum E {
|
|
556
|
+
A = 1 + 2 * 3,
|
|
557
|
+
B = 10 / 3,
|
|
558
|
+
C = -10 / 3,
|
|
559
|
+
D = -10 % 3,
|
|
560
|
+
E2 = 1 << 4,
|
|
561
|
+
F = 0xF0 | 0x0F,
|
|
562
|
+
G = ~0,
|
|
563
|
+
H = A + B,
|
|
564
|
+
I = 255 >> 4,
|
|
565
|
+
J = 7 & 3,
|
|
566
|
+
K = 7 ^ 3,
|
|
567
|
+
}
|
|
568
|
+
function main() -> int {
|
|
569
|
+
return A * 1000000 + B * 100000 + C * 10000 + D * 1000 + E2 * 100 + F
|
|
570
|
+
}`
|
|
571
|
+
// A=7 B=3 C=-3 D=-1 E2=16 F=255 -> 7*1e6 + 3*1e5 - 3*1e4 - 1*1e3 + 16*100 + 255
|
|
572
|
+
want := uint64(7*1000000 + 3*100000 + (-3)*10000 + (-1)*1000 + 16*100 + 255)
|
|
573
|
+
if got := runCompiled(t, src); got != want {
|
|
574
|
+
t.Fatalf("x0 = %d, 期望 %d (枚举常量表达式算错)", got, want)
|
|
575
|
+
}
|
|
576
|
+
prog, err := Compile(`
|
|
577
|
+
enum E { G = ~0, H = 255 >> 4 }
|
|
578
|
+
function main() -> int { return G + H }`, "t.cin", false)
|
|
579
|
+
if err != nil {
|
|
580
|
+
t.Fatalf("编译失败: %v", err)
|
|
581
|
+
}
|
|
582
|
+
// ~0 = -1, 255 >> 4 = 15
|
|
583
|
+
if !hasImm(prog, "MOV", -1) || !hasImm(prog, "MOV", 15) {
|
|
584
|
+
t.Fatal("~0 / >> 未折叠为预期立即数")
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// ---- 范围 for ----
|
|
589
|
+
|
|
590
|
+
func TestRangeForCompilesAndWalksPointer(t *testing.T) {
|
|
591
|
+
src := `
|
|
592
|
+
function main() -> int {
|
|
593
|
+
int a[4]
|
|
594
|
+
a[0] = 1
|
|
595
|
+
a[1] = 2
|
|
596
|
+
a[2] = 3
|
|
597
|
+
a[3] = 4
|
|
598
|
+
int s = 0
|
|
599
|
+
for (int v : a) { s = s + v }
|
|
600
|
+
return s
|
|
601
|
+
}`
|
|
602
|
+
prog, err := Compile(src, "t.cin", false)
|
|
603
|
+
if err != nil {
|
|
604
|
+
t.Fatalf("编译失败: %v", err)
|
|
605
|
+
}
|
|
606
|
+
// 指针游走: p += 8, 且 e = p + count*8
|
|
607
|
+
if !hasImm(prog, "ADDI", 8) {
|
|
608
|
+
t.Fatal("范围 for 未生成 ADDI +8 的指针递增")
|
|
609
|
+
}
|
|
610
|
+
if !hasImm(prog, "ADDI", 4*8) {
|
|
611
|
+
t.Fatal("范围 for 未生成 e = p + count*8")
|
|
612
|
+
}
|
|
613
|
+
for _, l := range []string{"_rfloop_", "_rfinc_", "_rfend_"} {
|
|
614
|
+
if !hasLabelPrefix(prog, l) {
|
|
615
|
+
t.Fatalf("缺少标签前缀 %s", l)
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
if got := runCompiled(t, src); got != 10 {
|
|
619
|
+
t.Fatalf("x0 = %d, 期望 10", got)
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
func TestRangeForBreakAndContinue(t *testing.T) {
|
|
624
|
+
src := `
|
|
625
|
+
function main() -> int {
|
|
626
|
+
int a[6]
|
|
627
|
+
for (int i = 0; i < 6; i++) { a[i] = i }
|
|
628
|
+
int s = 0
|
|
629
|
+
for (int v : a) {
|
|
630
|
+
if (v == 1) { continue }
|
|
631
|
+
if (v == 4) { break }
|
|
632
|
+
s = s + v
|
|
633
|
+
}
|
|
634
|
+
return s
|
|
635
|
+
}`
|
|
636
|
+
// 0 + 2 + 3 = 5
|
|
637
|
+
if got := runCompiled(t, src); got != 5 {
|
|
638
|
+
t.Fatalf("x0 = %d, 期望 5 (break/continue 目标错误)", got)
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
func TestRangeForGlobalArrayAndFloatElement(t *testing.T) {
|
|
643
|
+
src := `
|
|
644
|
+
int G[3]
|
|
645
|
+
function main() -> int {
|
|
646
|
+
G[0] = 1
|
|
647
|
+
G[1] = 2
|
|
648
|
+
G[2] = 3
|
|
649
|
+
float s = 0.0
|
|
650
|
+
for (float v : G) { s = s + v }
|
|
651
|
+
return s * 10.0
|
|
652
|
+
}`
|
|
653
|
+
// (1+2+3) * 10 = 60, 元素 int -> float 需插入 ITOF
|
|
654
|
+
prog, err := Compile(src, "t.cin", false)
|
|
655
|
+
if err != nil {
|
|
656
|
+
t.Fatalf("编译失败: %v", err)
|
|
657
|
+
}
|
|
658
|
+
if countSys(prog, SysITOF) == 0 {
|
|
659
|
+
t.Fatal("int 元素转 float 未生成 ITOF")
|
|
660
|
+
}
|
|
661
|
+
if got := runCompiled(t, src); got != 60 {
|
|
662
|
+
t.Fatalf("x0 = %d, 期望 60", got)
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
func TestRangeForErrors(t *testing.T) {
|
|
667
|
+
cases := []struct{ name, src, want string }{
|
|
668
|
+
{"ptrarray_param", `
|
|
669
|
+
function f(int[] a) -> int {
|
|
670
|
+
int s = 0
|
|
671
|
+
for (int v : a) { s = s + v }
|
|
672
|
+
return s
|
|
673
|
+
}
|
|
674
|
+
function main() -> int { return f(0) }`, "range-for requires a fixed-size array"},
|
|
675
|
+
{"ptrarray_local", `
|
|
676
|
+
function main() -> int {
|
|
677
|
+
int[] a
|
|
678
|
+
for (int v : a) { }
|
|
679
|
+
return 0
|
|
680
|
+
}`, "range-for requires a fixed-size array"},
|
|
681
|
+
{"multidimensional", `
|
|
682
|
+
function main() -> int {
|
|
683
|
+
int m[2][3]
|
|
684
|
+
for (int v : m) { }
|
|
685
|
+
return 0
|
|
686
|
+
}`, "range-for over multi-dimensional arrays is not supported"},
|
|
687
|
+
{"array_element_type_is_parse_error_not_range_for", `
|
|
688
|
+
function main() -> int {
|
|
689
|
+
int m[2][3]
|
|
690
|
+
for (int[] r : m) { }
|
|
691
|
+
return 0
|
|
692
|
+
}`, "Expected"},
|
|
693
|
+
}
|
|
694
|
+
for _, c := range cases {
|
|
695
|
+
t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, c.want) })
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// ---- case 范围 / 多值 ----
|
|
700
|
+
|
|
701
|
+
func TestCaseRangeProducesTwoComparisons(t *testing.T) {
|
|
702
|
+
src := `
|
|
703
|
+
function main() -> int {
|
|
704
|
+
int x = 5
|
|
705
|
+
switch (x) {
|
|
706
|
+
case 1..3: return 1
|
|
707
|
+
case 7, 9..11: return 2
|
|
708
|
+
default: return 3
|
|
709
|
+
}
|
|
710
|
+
return 0
|
|
711
|
+
}`
|
|
712
|
+
prog, err := Compile(src, "t.cin", false)
|
|
713
|
+
if err != nil {
|
|
714
|
+
t.Fatalf("编译失败: %v", err)
|
|
715
|
+
}
|
|
716
|
+
if !hasCond(prog, "LT") || !hasCond(prog, "LE") {
|
|
717
|
+
t.Fatal("范围 case 未生成 LT/LE 两段有符号比较")
|
|
718
|
+
}
|
|
719
|
+
if !hasCond(prog, "EQ") {
|
|
720
|
+
t.Fatal("单值 case 未生成 EQ 比较")
|
|
721
|
+
}
|
|
722
|
+
for _, v := range []int64{1, 3, 7, 9, 11} {
|
|
723
|
+
if !hasImm(prog, "MOV", v) {
|
|
724
|
+
t.Fatalf("分派链缺少边界立即数 %d", v)
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if got := runCompiled(t, src); got != 3 {
|
|
728
|
+
t.Fatalf("x0 = %d, 期望 3 (default)", got)
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
func TestCaseRangeRuntime(t *testing.T) {
|
|
733
|
+
src := `
|
|
734
|
+
function classify(int x) -> int {
|
|
735
|
+
switch (x) {
|
|
736
|
+
case 1..3: return 10
|
|
737
|
+
case 7, 9..11: return 20
|
|
738
|
+
default: return 30
|
|
739
|
+
}
|
|
740
|
+
return 0
|
|
741
|
+
}
|
|
742
|
+
function main() -> int {
|
|
743
|
+
int s = 0
|
|
744
|
+
s = s + classify(0)
|
|
745
|
+
s = s + classify(1)
|
|
746
|
+
s = s + classify(3)
|
|
747
|
+
s = s + classify(4)
|
|
748
|
+
s = s + classify(7)
|
|
749
|
+
s = s + classify(9)
|
|
750
|
+
s = s + classify(11)
|
|
751
|
+
s = s + classify(12)
|
|
752
|
+
return s
|
|
753
|
+
}`
|
|
754
|
+
// 30 + 10 + 10 + 30 + 20 + 20 + 20 + 30 = 170
|
|
755
|
+
if got := runCompiled(t, src); got != 170 {
|
|
756
|
+
t.Fatalf("x0 = %d, 期望 170 (case 范围匹配错误)", got)
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
func TestCaseRangeWithNegativeBounds(t *testing.T) {
|
|
761
|
+
src := `
|
|
762
|
+
function main() -> int {
|
|
763
|
+
int x = -2
|
|
764
|
+
int r = 0
|
|
765
|
+
switch (x) {
|
|
766
|
+
case -3..-1: r = 1 break
|
|
767
|
+
default: r = 2
|
|
768
|
+
}
|
|
769
|
+
return r
|
|
770
|
+
}`
|
|
771
|
+
if got := runCompiled(t, src); got != 1 {
|
|
772
|
+
t.Fatalf("x0 = %d, 期望 1", got)
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
func TestEmptyCaseRangeIsCompileError(t *testing.T) {
|
|
777
|
+
compileErr(t, `
|
|
778
|
+
function main() -> int {
|
|
779
|
+
int x = 1
|
|
780
|
+
switch (x) {
|
|
781
|
+
case 5..1: return 1
|
|
782
|
+
}
|
|
783
|
+
return 0
|
|
784
|
+
}`, "Empty case range: 5..1")
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
func TestNonConstantCaseAltsAreCompileError(t *testing.T) {
|
|
788
|
+
cases := []struct{ name, src string }{
|
|
789
|
+
{"single", `
|
|
790
|
+
function main() -> int {
|
|
791
|
+
int y = 2
|
|
792
|
+
int x = 1
|
|
793
|
+
switch (x) {
|
|
794
|
+
case y: return 1
|
|
795
|
+
}
|
|
796
|
+
return 0
|
|
797
|
+
}`},
|
|
798
|
+
{"range_high", `
|
|
799
|
+
function main() -> int {
|
|
800
|
+
int y = 2
|
|
801
|
+
int x = 1
|
|
802
|
+
switch (x) {
|
|
803
|
+
case 0..y: return 1
|
|
804
|
+
}
|
|
805
|
+
return 0
|
|
806
|
+
}`},
|
|
807
|
+
{"range_low", `
|
|
808
|
+
function main() -> int {
|
|
809
|
+
int y = 2
|
|
810
|
+
int x = 1
|
|
811
|
+
switch (x) {
|
|
812
|
+
case y..9: return 1
|
|
813
|
+
}
|
|
814
|
+
return 0
|
|
815
|
+
}`},
|
|
816
|
+
{"second_alt", `
|
|
817
|
+
function main() -> int {
|
|
818
|
+
int y = 2
|
|
819
|
+
int x = 1
|
|
820
|
+
switch (x) {
|
|
821
|
+
case 1, y: return 1
|
|
822
|
+
}
|
|
823
|
+
return 0
|
|
824
|
+
}`},
|
|
825
|
+
}
|
|
826
|
+
for _, c := range cases {
|
|
827
|
+
t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, "constant") })
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
func TestCaseFloatValueStillReportsType(t *testing.T) {
|
|
832
|
+
compileErr(t, `
|
|
833
|
+
function main() -> int {
|
|
834
|
+
int x = 1
|
|
835
|
+
switch (x) {
|
|
836
|
+
case 1.5: return 1
|
|
837
|
+
}
|
|
838
|
+
return 0
|
|
839
|
+
}`, "case value must be an integer constant, got: float")
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// ---- 多参数 print / println ----
|
|
843
|
+
|
|
844
|
+
func TestPrintSingleArgBytecodeUnchanged(t *testing.T) {
|
|
845
|
+
// 单参数 println: 字符串化 + PRINT_STR + OUT 10 (与改动前逐字节一致)
|
|
846
|
+
prog, err := Compile(`function main() -> int { println(7) return 0 }`, "t.cin", false)
|
|
847
|
+
if err != nil {
|
|
848
|
+
t.Fatalf("编译失败: %v", err)
|
|
849
|
+
}
|
|
850
|
+
if n := countSys(prog, SysITOA); n != 1 {
|
|
851
|
+
t.Fatalf("println(7) 的 ITOA 次数 = %d, 期望 1", n)
|
|
852
|
+
}
|
|
853
|
+
if n := countSys(prog, SysPRINT_STR); n != 1 {
|
|
854
|
+
t.Fatalf("println(7) 的 PRINT_STR 次数 = %d, 期望 1", n)
|
|
855
|
+
}
|
|
856
|
+
if n := countOp(prog, "OUT"); n != 1 {
|
|
857
|
+
t.Fatalf("println(7) 的 OUT 次数 = %d, 期望 1", n)
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// 单参数 print: 没有换行
|
|
861
|
+
prog, err = Compile(`function main() -> int { print(7) return 0 }`, "t.cin", false)
|
|
862
|
+
if err != nil {
|
|
863
|
+
t.Fatalf("编译失败: %v", err)
|
|
864
|
+
}
|
|
865
|
+
if n := countSys(prog, SysPRINT_STR); n != 1 {
|
|
866
|
+
t.Fatalf("print(7) 的 PRINT_STR 次数 = %d, 期望 1", n)
|
|
867
|
+
}
|
|
868
|
+
if n := countOp(prog, "OUT"); n != 0 {
|
|
869
|
+
t.Fatalf("print(7) 不应输出换行, OUT 次数 = %d", n)
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
func TestPrintMultipleArgs(t *testing.T) {
|
|
874
|
+
prog, err := Compile(
|
|
875
|
+
`function main() -> int { println(1, "a", 2.5) return 0 }`, "t.cin", false)
|
|
876
|
+
if err != nil {
|
|
877
|
+
t.Fatalf("多参数 println 编译失败: %v", err)
|
|
878
|
+
}
|
|
879
|
+
if n := countSys(prog, SysPRINT_STR); n != 3 {
|
|
880
|
+
t.Fatalf("PRINT_STR 次数 = %d, 期望 3", n)
|
|
881
|
+
}
|
|
882
|
+
if n := countSys(prog, SysITOA); n != 1 {
|
|
883
|
+
t.Fatalf("ITOA 次数 = %d, 期望 1", n)
|
|
884
|
+
}
|
|
885
|
+
if n := countSys(prog, SysFTOA); n != 1 {
|
|
886
|
+
t.Fatalf("FTOA 次数 = %d, 期望 1", n)
|
|
887
|
+
}
|
|
888
|
+
if n := countOp(prog, "OUT"); n != 1 {
|
|
889
|
+
t.Fatalf("OUT 次数 = %d, 期望 1 (只换行一次)", n)
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// print 多参数: 无换行
|
|
893
|
+
prog, err = Compile(`function main() -> int { print(1, 2, 3) return 0 }`, "t.cin", false)
|
|
894
|
+
if err != nil {
|
|
895
|
+
t.Fatalf("多参数 print 编译失败: %v", err)
|
|
896
|
+
}
|
|
897
|
+
if n := countSys(prog, SysPRINT_STR); n != 3 {
|
|
898
|
+
t.Fatalf("print 的 PRINT_STR 次数 = %d, 期望 3", n)
|
|
899
|
+
}
|
|
900
|
+
if n := countOp(prog, "OUT"); n != 0 {
|
|
901
|
+
t.Fatalf("print 多参数不应输出换行, OUT 次数 = %d", n)
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// 无参 println: 仅换行
|
|
905
|
+
prog, err = Compile(`function main() -> int { println() return 0 }`, "t.cin", false)
|
|
906
|
+
if err != nil {
|
|
907
|
+
t.Fatalf("println() 编译失败: %v", err)
|
|
908
|
+
}
|
|
909
|
+
if n := countSys(prog, SysPRINT_STR); n != 0 {
|
|
910
|
+
t.Fatalf("println() 的 PRINT_STR 次数 = %d, 期望 0", n)
|
|
911
|
+
}
|
|
912
|
+
if n := countOp(prog, "OUT"); n != 1 {
|
|
913
|
+
t.Fatalf("println() 的 OUT 次数 = %d, 期望 1", n)
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
// ---- 字符串转义 ----
|
|
918
|
+
|
|
919
|
+
func TestStringEscapeBytes(t *testing.T) {
|
|
920
|
+
cases := []struct {
|
|
921
|
+
name string
|
|
922
|
+
lit string // CIN 源码里的字面量 (含引号)
|
|
923
|
+
want []byte // 期望的数据字节 (含结尾 NUL)
|
|
924
|
+
}{
|
|
925
|
+
{"hex_utf8", `"\xE4\xB8\xAD"`, []byte{0xE4, 0xB8, 0xAD, 0x00}},
|
|
926
|
+
{"unicode_escape", `"\u4e2d"`, []byte{0xE4, 0xB8, 0xAD, 0x00}},
|
|
927
|
+
{"unicode_long", `"\U0001F600"`, []byte{0xF0, 0x9F, 0x98, 0x80, 0x00}},
|
|
928
|
+
{"hex_short", `"\x41"`, []byte{0x41, 0x00}},
|
|
929
|
+
{"hex_single_low", `"\x9"`, []byte{0x09, 0x00}},
|
|
930
|
+
{"control_chars", `"\a\b\f\v"`, []byte{7, 8, 12, 11, 0x00}},
|
|
931
|
+
{"quote_and_backslash", `"\'\\"`, []byte{'\'', '\\', 0x00}},
|
|
932
|
+
{"unknown_escape_verbatim", `"\q"`, []byte{'q', 0x00}},
|
|
933
|
+
{"newline_tab", `"\n\t"`, []byte{'\n', '\t', 0x00}},
|
|
934
|
+
}
|
|
935
|
+
for _, c := range cases {
|
|
936
|
+
t.Run(c.name, func(t *testing.T) {
|
|
937
|
+
src := `function main() -> int { string s = ` + c.lit + ` return strlen(s) }`
|
|
938
|
+
prog, err := Compile(src, "t.cin", false)
|
|
939
|
+
if err != nil {
|
|
940
|
+
t.Fatalf("编译失败: %v", err)
|
|
941
|
+
}
|
|
942
|
+
if !hasDataWrite(prog, c.want) {
|
|
943
|
+
t.Fatalf("未找到期望的数据字节 %v", c.want)
|
|
944
|
+
}
|
|
945
|
+
})
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
func TestHexAndUnicodeEscapesAreIdentical(t *testing.T) {
|
|
950
|
+
hexProg, err := Compile(
|
|
951
|
+
`function main() -> int { string s = "\xE4\xB8\xAD" return strlen(s) }`,
|
|
952
|
+
"t.cin", false)
|
|
953
|
+
if err != nil {
|
|
954
|
+
t.Fatalf("编译失败: %v", err)
|
|
955
|
+
}
|
|
956
|
+
uniProg, err := Compile(
|
|
957
|
+
`function main() -> int { string s = "\u4e2d" return strlen(s) }`,
|
|
958
|
+
"t.cin", false)
|
|
959
|
+
if err != nil {
|
|
960
|
+
t.Fatalf("编译失败: %v", err)
|
|
961
|
+
}
|
|
962
|
+
if dumpProgram(hexProg) != dumpProgram(uniProg) {
|
|
963
|
+
t.Fatal(`"\xE4\xB8\xAD" 与 "\u4e2d" 的编译产物不一致`)
|
|
964
|
+
}
|
|
965
|
+
// 两处都必须是 3 字节 + NUL
|
|
966
|
+
if !hasDataWrite(hexProg, []byte{0xE4, 0xB8, 0xAD, 0x00}) {
|
|
967
|
+
t.Fatal("\\x 转义未产生 3 个原始字节")
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
func TestStringEscapeErrors(t *testing.T) {
|
|
972
|
+
cases := []struct{ name, src, want string }{
|
|
973
|
+
{"hex_empty", `function main() -> int { string s = "\x" return 0 }`,
|
|
974
|
+
"\\x escape needs at least one hex digit"},
|
|
975
|
+
{"hex_no_digits", `function main() -> int { string s = "\xZZ" return 0 }`,
|
|
976
|
+
"\\x escape needs at least one hex digit"},
|
|
977
|
+
{"u_too_short", `function main() -> int { string s = "\u12" return 0 }`,
|
|
978
|
+
"\\u escape needs exactly 4 hex digits"},
|
|
979
|
+
{"u_non_hex", `function main() -> int { string s = "\uZZZZ" return 0 }`,
|
|
980
|
+
"\\u escape needs exactly 4 hex digits"},
|
|
981
|
+
{"U_too_short", `function main() -> int { string s = "\U0001F60" return 0 }`,
|
|
982
|
+
"\\U escape needs exactly 8 hex digits"},
|
|
983
|
+
{"U_non_hex", `function main() -> int { string s = "\U0001F60Z" return 0 }`,
|
|
984
|
+
"\\U escape needs exactly 8 hex digits"},
|
|
985
|
+
{"u_out_of_range", `function main() -> int { string s = "\U00110000" return 0 }`,
|
|
986
|
+
"\\U escape out of Unicode range"},
|
|
987
|
+
}
|
|
988
|
+
for _, c := range cases {
|
|
989
|
+
t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, c.want) })
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// ---- 数字与 '..' 的词法 ----
|
|
994
|
+
|
|
995
|
+
func TestDotDotIsNotPartOfNumber(t *testing.T) {
|
|
996
|
+
// 1..5 必须切成 NUMBER(1) DOTDOT NUMBER(5)
|
|
997
|
+
prog, err := Compile(`
|
|
998
|
+
function main() -> int {
|
|
999
|
+
int x = 4
|
|
1000
|
+
switch (x) {
|
|
1001
|
+
case 1..5: return 7
|
|
1002
|
+
}
|
|
1003
|
+
return 0
|
|
1004
|
+
}`, "t.cin", false)
|
|
1005
|
+
if err != nil {
|
|
1006
|
+
t.Fatalf("编译失败: %v", err)
|
|
1007
|
+
}
|
|
1008
|
+
if !hasImm(prog, "MOV", 1) || !hasImm(prog, "MOV", 5) {
|
|
1009
|
+
t.Fatal("1..5 未切成两个数字 token")
|
|
1010
|
+
}
|
|
1011
|
+
if got := runCompiled(t, `
|
|
1012
|
+
function main() -> int {
|
|
1013
|
+
int x = 4
|
|
1014
|
+
switch (x) {
|
|
1015
|
+
case 1..5: return 7
|
|
1016
|
+
}
|
|
1017
|
+
return 0
|
|
1018
|
+
}`); got != 7 {
|
|
1019
|
+
t.Fatalf("x0 = %d, 期望 7", got)
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
func TestFloatMembersAndSecondDotStillWork(t *testing.T) {
|
|
1024
|
+
// 浮点字面量与 struct 成员访问不受 '..' 支持的影响
|
|
1025
|
+
if _, err := Compile(`
|
|
1026
|
+
struct P { int x }
|
|
1027
|
+
function main() -> int {
|
|
1028
|
+
P p
|
|
1029
|
+
p.x = 3
|
|
1030
|
+
float f = 1.5
|
|
1031
|
+
int z[2]
|
|
1032
|
+
return p.x + f
|
|
1033
|
+
}`, "t.cin", false); err != nil {
|
|
1034
|
+
t.Fatalf("浮点/成员访问被破坏: %v", err)
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
// ---- for (i = 0; ...) 初始化子句 ----
|
|
1039
|
+
|
|
1040
|
+
func TestForInitAssignmentWithoutDeclaration(t *testing.T) {
|
|
1041
|
+
src := `
|
|
1042
|
+
function main() -> int {
|
|
1043
|
+
int i = 0
|
|
1044
|
+
int s = 0
|
|
1045
|
+
for (i = 0; i < 5; i++) { s = s + i }
|
|
1046
|
+
return s * 100 + i
|
|
1047
|
+
}`
|
|
1048
|
+
// s = 0+1+2+3+4 = 10, i = 5 -> 1005
|
|
1049
|
+
if got := runCompiled(t, src); got != 1005 {
|
|
1050
|
+
t.Fatalf("x0 = %d, 期望 1005", got)
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// ---- 枚举类型名当类型用 ----
|
|
1055
|
+
|
|
1056
|
+
func TestEnumTypeNameIsIntAlias(t *testing.T) {
|
|
1057
|
+
src := `
|
|
1058
|
+
enum Color { RED, GREEN, BLUE }
|
|
1059
|
+
function main() -> int {
|
|
1060
|
+
Color c = GREEN
|
|
1061
|
+
unsigned int u = BLUE
|
|
1062
|
+
Color d = c + u
|
|
1063
|
+
return d
|
|
1064
|
+
}`
|
|
1065
|
+
// GREEN=1, BLUE=2 -> 3
|
|
1066
|
+
if got := runCompiled(t, src); got != 3 {
|
|
1067
|
+
t.Fatalf("x0 = %d, 期望 3", got)
|
|
1068
|
+
}
|
|
1069
|
+
}
|