codecin 5.4.2__py3-none-any.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.
Files changed (54) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +229 -0
  3. codecin/assembler.py +548 -0
  4. codecin/cache.py +71 -0
  5. codecin/cin.py +2558 -0
  6. codecin/cli.py +282 -0
  7. codecin/config.py +58 -0
  8. codecin/console.py +183 -0
  9. codecin/cpu.py +1654 -0
  10. codecin/crom.py +222 -0
  11. codecin/debugger.py +659 -0
  12. codecin/disasm.py +86 -0
  13. codecin/errors.py +58 -0
  14. codecin/isa.py +457 -0
  15. codecin/jit.py +270 -0
  16. codecin/libcodecin_native.so +0 -0
  17. codecin/logger.py +145 -0
  18. codecin/memory.py +348 -0
  19. codecin/native/aot/aot.go +70 -0
  20. codecin/native/aot/build.go +219 -0
  21. codecin/native/aot/stub_main.go.txt +22 -0
  22. codecin/native/build.ps1 +33 -0
  23. codecin/native/build.sh +48 -0
  24. codecin/native/cmd/codecin/main.go +287 -0
  25. codecin/native/compiler/codegen.go +1938 -0
  26. codecin/native/compiler/compile_test.go +164 -0
  27. codecin/native/compiler/parser.go +1071 -0
  28. codecin/native/compiler/syscalls.go +86 -0
  29. codecin/native/compiler/tokenizer.go +336 -0
  30. codecin/native/compiler/types.go +213 -0
  31. codecin/native/engine/audio.go +152 -0
  32. codecin/native/engine/audio_other.go +46 -0
  33. codecin/native/engine/audio_windows.go +43 -0
  34. codecin/native/engine/canvas.go +271 -0
  35. codecin/native/engine/crom.go +86 -0
  36. codecin/native/engine/encode.go +93 -0
  37. codecin/native/engine/engine_test.go +198 -0
  38. codecin/native/engine/isa_gen.go +445 -0
  39. codecin/native/engine/system.go +190 -0
  40. codecin/native/engine/termux.go +113 -0
  41. codecin/native/engine/version_gen.go +7 -0
  42. codecin/native/engine/vm.go +1157 -0
  43. codecin/native/go.mod +3 -0
  44. codecin/native/ir/ir.go +48 -0
  45. codecin/native/main.go +225 -0
  46. codecin/native.py +336 -0
  47. codecin/registers.py +130 -0
  48. codecin/stats.py +235 -0
  49. codecin-5.4.2.dist-info/METADATA +985 -0
  50. codecin-5.4.2.dist-info/RECORD +54 -0
  51. codecin-5.4.2.dist-info/WHEEL +5 -0
  52. codecin-5.4.2.dist-info/entry_points.txt +2 -0
  53. codecin-5.4.2.dist-info/licenses/LICENSE +7 -0
  54. codecin-5.4.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,164 @@
1
+ package compiler
2
+
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ "testing"
7
+
8
+ "codecin-native/ir"
9
+ )
10
+
11
+ // 这些测试锁定 docs/SUGGESTIONS.md 批次 B 修复的 Go 侧行为:
12
+ // * switch 负 case 匹配 / 非常量 case 报错 / 标签与分支一一对应;
13
+ // * 语义错误必须报编译错误, 而不是产出静默错误的字节码;
14
+ // * 数值字面量后缀与 64 位范围;
15
+ // * switch 内 continue 不泄漏选择器栈槽。
16
+
17
+ // dumpProgram 把 IR 渲染成稳定文本, 便于比较与断言 (避开对 engine 包的依赖)。
18
+ func dumpProgram(prog *ir.Program) string {
19
+ var sb strings.Builder
20
+ for _, ins := range prog.Instructions {
21
+ sb.WriteString(ins.Op)
22
+ for _, a := range ins.Args {
23
+ fmt.Fprintf(&sb, " %s:%d:%d:%s:%g", a.Kind, a.V, a.Off, a.Name, a.F)
24
+ }
25
+ sb.WriteByte('\n')
26
+ }
27
+ return sb.String()
28
+ }
29
+
30
+ func compileOK(t *testing.T, src string) string {
31
+ t.Helper()
32
+ prog, err := Compile(src, "t.cin", false)
33
+ if err != nil {
34
+ t.Fatalf("期望编译通过, 实际报错: %v", err)
35
+ }
36
+ return dumpProgram(prog)
37
+ }
38
+
39
+ func compileErr(t *testing.T, src, wantSubstr string) {
40
+ t.Helper()
41
+ _, err := Compile(src, "t.cin", false)
42
+ if err == nil {
43
+ t.Fatalf("期望编译错误 (%s), 实际通过", wantSubstr)
44
+ }
45
+ if !strings.Contains(err.Error(), wantSubstr) {
46
+ t.Fatalf("错误信息应包含 %q, 实际: %v", wantSubstr, err)
47
+ }
48
+ }
49
+
50
+ func TestSwitchNegativeCaseIsComparable(t *testing.T) {
51
+ // 旧实现用 -1 当 default 哨兵并跳过 raw < 0, `case -1` 永远匹配不上。
52
+ // 这里检查分派比较链里确实出现了 0xFFFFFFFFFFFFFFFF。
53
+ src := `
54
+ function main() -> int {
55
+ int x = -1
56
+ switch (x) {
57
+ case -1: return 10
58
+ default: return 30
59
+ }
60
+ return 0
61
+ }`
62
+ prog, err := Compile(src, "t.cin", false)
63
+ if err != nil {
64
+ t.Fatalf("编译失败: %v", err)
65
+ }
66
+ found := false
67
+ for _, ins := range prog.Instructions {
68
+ if ins.Op != "MOV" || len(ins.Args) != 2 {
69
+ continue
70
+ }
71
+ if ins.Args[1].Kind == "imm" && ins.Args[1].V == -1 {
72
+ found = true
73
+ }
74
+ }
75
+ if !found {
76
+ t.Fatal("分派链中未找到 case -1 的比较常量")
77
+ }
78
+ }
79
+
80
+ func TestSwitchNonConstantCaseIsCompileError(t *testing.T) {
81
+ compileErr(t, `
82
+ function main() -> int {
83
+ int y = 2
84
+ int x = 1
85
+ switch (x) {
86
+ case y: return 10
87
+ case 1: return 20
88
+ }
89
+ return 0
90
+ }`, "constant")
91
+ }
92
+
93
+ func TestSwitchFloatSelectorIsCompileError(t *testing.T) {
94
+ compileErr(t, `
95
+ function main() -> int {
96
+ float f = 1.5
97
+ switch (f) {
98
+ case 1: return 1
99
+ }
100
+ return 0
101
+ }`, "Switch expression must be integer")
102
+ }
103
+
104
+ func TestSwitchWithContinueCompiles(t *testing.T) {
105
+ // 只需通过编译与编码: 栈平衡由运行时测试 (tests/test_switch_semantics.py) 覆盖
106
+ compileOK(t, `
107
+ function main() -> int {
108
+ int i = 0
109
+ int n = 0
110
+ while (i < 10) {
111
+ i = i + 1
112
+ switch (i) {
113
+ case 1: continue
114
+ default: n = n + 1
115
+ }
116
+ }
117
+ return n
118
+ }`)
119
+ }
120
+
121
+ func TestSemanticErrorsAreReported(t *testing.T) {
122
+ cases := []struct{ name, src, want string }{
123
+ {"unknown_function", `function main() -> int { return foo() }`, "Unknown function"},
124
+ {"undefined_var", `function main() -> int { zzz = 1 return 0 }`, "Undefined variable"},
125
+ {"member_on_int", `function main() -> int { int x = 1 return x.foo }`, "non-struct"},
126
+ {"bitnot_float", `function main() -> int { return ~1.5 }`, "Bitwise NOT"},
127
+ {"float_mod", `function main() -> int { return 7.5 % 2.0 }`, "Float modulo"},
128
+ {"sqrt_no_args", `function main() -> int { float f = sqrt() return 0 }`, "at least 1"},
129
+ {"substr_too_few", `function main() -> int { string s = substr("a", 1) return 0 }`, "at least 3"},
130
+ {"break_outside", `function main() -> int { break return 0 }`, "break outside loop"},
131
+ {"continue_outside", `function main() -> int { continue return 0 }`, "continue outside loop"},
132
+ {"int_out_of_range", `function main() -> int { int x = 9223372036854775808 return 0 }`, "Malformed numeric"},
133
+ }
134
+ for _, c := range cases {
135
+ t.Run(c.name, func(t *testing.T) { compileErr(t, c.src, c.want) })
136
+ }
137
+ }
138
+
139
+ func TestValidProgramsStillCompile(t *testing.T) {
140
+ // 防止错误检查做得过严
141
+ srcs := []string{
142
+ `function main() -> int { int x = 0xFFu return x }`,
143
+ `function main() -> int { int x = 0b1010U return x }`,
144
+ `function main() -> int { int x = 42u return x }`,
145
+ `function main() -> int { float f = 1.5f return 0 }`,
146
+ `function main() -> int { return min(3, 4) + max(1, 2) }`,
147
+ `function main() -> int { return strlen("ab", 9) }`,
148
+ `function main() -> int { int a = 6 int b = 3 return (a & b) | (a ^ b) }`,
149
+ }
150
+ for _, src := range srcs {
151
+ if _, err := Compile(src, "t.cin", false); err != nil {
152
+ t.Fatalf("合法程序被拒: %v\n%s", err, src)
153
+ }
154
+ }
155
+ }
156
+
157
+ func TestCompileIsDeterministic(t *testing.T) {
158
+ src := `function main() -> int { int s = 0 for (int i = 0; i < 5; i++) { s += i } return s }`
159
+ a := compileOK(t, src)
160
+ b := compileOK(t, src)
161
+ if a != b {
162
+ t.Fatal("同一输入的编译产物不稳定")
163
+ }
164
+ }