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.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,166 @@
1
+ // Code generated by script/gen_native_isa.py from codecin/isa.py; DO NOT EDIT.
2
+ // 单一事实来源: codecin/isa.py Syscall 常量.
3
+
4
+ package compiler
5
+
6
+ // SYS 功能号 (与 isa.py Syscall 一致; 编译器以 SysXXX 引用)。
7
+ const SysSQRT = 0
8
+ const SysPOW = 1
9
+ const SysABS = 2
10
+ const SysSIN = 3
11
+ const SysCOS = 4
12
+ const SysTAN = 5
13
+ const SysFADD = 6
14
+ const SysFSUB = 7
15
+ const SysFMUL = 8
16
+ const SysFDIV = 9
17
+ const SysFCMP = 10
18
+ const SysFTOI = 11
19
+ const SysITOF = 12
20
+ const SysRAND = 13
21
+ const SysSRAND = 14
22
+ const SysTIME = 15
23
+ const SysSTRLEN = 16
24
+ const SysSTRCMP = 17
25
+ const SysSTRCPY = 18
26
+ const SysSTRCAT = 19
27
+ const SysMALLOC = 20
28
+ const SysPRINT_FLOAT = 21
29
+ const SysITOA = 22
30
+ const SysFTOA = 23
31
+ const SysPRINT_STR = 24
32
+ const SysSTR_CONCAT = 25
33
+ const SysBOOL_STR = 26
34
+ const SysABORT = 27
35
+ const SysSUBSTR = 28
36
+ const SysINDEXOF = 29
37
+ const SysTOUPPER = 30
38
+ const SysTOLOWER = 31
39
+ const SysFLOOR = 32
40
+ const SysCEIL = 33
41
+ const SysROUND = 34
42
+ const SysATOI = 35
43
+ const SysTRIM = 36
44
+ const SysLTRIM = 37
45
+ const SysRTRIM = 38
46
+ const SysAUDIOPLAY = 39
47
+ const SysAUDIOSTOP = 40
48
+ const SysAUDIOVOL = 41
49
+ const SysAUDIOWAIT = 42
50
+ const SysCANVASNEW = 43
51
+ const SysCANVASSET = 44
52
+ const SysCANVASRECT = 45
53
+ const SysCANVASCIRC = 46
54
+ const SysCANVASTEXT = 47
55
+ const SysCANVASLINE = 48
56
+ const SysCANVASSAVE = 49
57
+ const SysCANVASSHOW = 50
58
+ const SysFILEREAD = 51
59
+ const SysFILEWRITE = 52
60
+ const SysFILEAPPEND = 53
61
+ const SysFILEEXISTS = 54
62
+ const SysFILEDELETE = 55
63
+ const SysFILESIZE = 56
64
+ const SysMKDIR = 57
65
+ const SysDIRLIST = 58
66
+ const SysEXEC = 59
67
+ const SysEXECOUTPUT = 60
68
+ const SysGETENV = 61
69
+ const SysSETENV = 62
70
+ const SysOSNAME = 63
71
+ const SysHOSTNAME = 64
72
+ const SysUSERNAME = 65
73
+ const SysCWD = 66
74
+ const SysHOMEDIR = 67
75
+ const SysTERMUXAVAIL = 68
76
+ const SysTERMUXNOTIFY = 69
77
+ const SysTERMUXTOAST = 70
78
+ const SysTERMUXCLIPGET = 71
79
+ const SysTERMUXCLIPSET = 72
80
+ const SysTERMUXBATTERY = 73
81
+ const SysTERMUXVIBRATE = 74
82
+ const SysTERMUXTTS = 75
83
+ const SysTERMUXLOCATION = 76
84
+ const SysTERMUXWIFI = 77
85
+ const SysTERMUXDIALOG = 78
86
+ const SysTERMUXSMS = 79
87
+ const SysPATHJOIN = 80
88
+ const SysPATHBASENAME = 81
89
+ const SysPATHDIRNAME = 82
90
+ const SysPATHABS = 83
91
+ const SysFILECOPY = 84
92
+ const SysFILEMOVE = 85
93
+ const SysDIRREMOVE = 86
94
+ const SysISDIR = 87
95
+ const SysFILEMTIME = 88
96
+ const SysTEMPDIR = 89
97
+ const SysCHDIR = 90
98
+ const SysTIMEMS = 91
99
+ const SysSLEEPMS = 92
100
+ const SysCPUCOUNT = 93
101
+ const SysARCHNAME = 94
102
+ const SysMEMINFO = 95
103
+ const SysISANDROID = 96
104
+ const SysHTTPGET = 97
105
+ const SysHTTPPOST = 98
106
+ const SysDOWNLOAD = 99
107
+ const SysSHA256 = 100
108
+ const SysBASE64ENC = 101
109
+ const SysBASE64DEC = 102
110
+ const SysCLIPGET = 103
111
+ const SysCLIPSET = 104
112
+ const SysNOTIFY = 105
113
+ const SysOPENURL = 106
114
+ const SysANDROIDINTENT = 107
115
+ const SysTERMUXCALL = 108
116
+ const SysTERMUXSHARE = 109
117
+ const SysTERMUXTORCH = 110
118
+ const SysTERMUXVOLUME = 111
119
+ const SysTERMUXBRIGHT = 112
120
+ const SysTERMUXCAMERA = 113
121
+ const SysTERMUXFINGER = 114
122
+ const SysTERMUXSENSOR = 115
123
+ const SysKEYHIT = 116
124
+ const SysKEYGET = 117
125
+ const SysKEYFLUSH = 118
126
+ const SysGUINEW = 119
127
+ const SysGUIUPDATE = 120
128
+ const SysGUICLOSE = 121
129
+ const SysGUICLOSED = 122
130
+ const SysMOUSEX = 123
131
+ const SysMOUSEY = 124
132
+ const SysMOUSEBTN = 125
133
+ const SysAUDIOPOS = 126
134
+ const SysAUDIOBEEP = 127
135
+ const SysGUIACTIVE = 128
136
+ const SysARGC = 129
137
+ const SysARGV = 130
138
+ const SysREADLINE = 131
139
+ const SysAUDIODUR = 132
140
+ const SysAUDIOPLAYING = 133
141
+ const SysAUDIOPAUSE = 134
142
+ const SysAUDIORESUME = 135
143
+ const SysAUDIOLEVEL = 136
144
+ const SysALLOCFRAME = 137
145
+ const SysTIMEUS = 138
146
+ const SysTIMENS = 139
147
+ const SysDLOPEN = 140
148
+ const SysDLSYM = 141
149
+ const SysFFICALL = 142
150
+ const SysFFICALLF = 143
151
+ const SysLIBCLOSE = 144
152
+ const SysHTTPREQ = 145
153
+ const SysHTTPCODE = 146
154
+ const SysTCPDIAL = 147
155
+ const SysTCPSEND = 148
156
+ const SysTCPRECV = 149
157
+ const SysTCPCLOSE = 150
158
+ const SysTCPLISTEN = 151
159
+ const SysTCPACCEPT = 152
160
+ const SysUDPOPEN = 153
161
+ const SysUDPSENDTO = 154
162
+ const SysUDPRECVFROM = 155
163
+ const SysUDPCLOSE = 156
164
+ const SysDNSLOOKUP = 157
165
+ const SysVKBSHOW = 158
166
+ const SysVKBHIDE = 159
@@ -0,0 +1,394 @@
1
+ package compiler
2
+
3
+ import (
4
+ "fmt"
5
+ "strconv"
6
+ "strings"
7
+ )
8
+
9
+ // ---------------- 词法分析 ----------------
10
+
11
+ // Token 词法单元。
12
+ type Token struct {
13
+ kind string
14
+ ival int64
15
+ fval float64
16
+ isFloat bool
17
+ sval string
18
+ line int
19
+ filename string
20
+ }
21
+
22
+ var keywords = map[string]bool{
23
+ "struct": true, "function": true, "return": true, "if": true, "else": true,
24
+ "while": true, "for": true, "do": true, "switch": true, "case": true,
25
+ "default": true, "break": true, "continue": true, "true": true, "false": true,
26
+ "int": true, "float": true, "bool": true, "string": true, "void": true,
27
+ "char": true, "short": true, "long": true, "unsigned": true,
28
+ "enum": true,
29
+ "set": true, "add": true, "subtract": true, "multiply": true, "divide": true,
30
+ "increment": true, "decrement": true,
31
+ }
32
+
33
+ var baseTypeWords = map[string]bool{
34
+ "int": true, "float": true, "bool": true, "string": true,
35
+ "char": true, "short": true, "long": true, "unsigned": true,
36
+ }
37
+
38
+ var compoundAssign = map[string]string{
39
+ "PLUSEQ": "+=", "MINUSEQ": "-=", "STAREQ": "*=", "SLASHEQ": "/=",
40
+ "PERCENTEQ": "%=", "ANDEQ": "&=", "OREQ": "|=", "XOREQ": "^=",
41
+ "SHLEQ": "<<=", "SHREQ": ">>=",
42
+ }
43
+
44
+ var singleOps = map[byte]string{
45
+ '{': "LBRACE", '}': "RBRACE", '(': "LPAREN", ')': "RPAREN",
46
+ '[': "LBRACKET", ']': "RBRACKET", ',': "COMMA", ';': "SEMI",
47
+ '+': "PLUS", '-': "MINUS", '*': "STAR", '/': "SLASH", '%': "PERCENT",
48
+ '=': "ASSIGN", '<': "LT", '>': "GT", '!': "BANG", '.': "DOT",
49
+ '?': "QUESTION", ':': "COLON", '&': "AMP", '|': "PIPE", '^': "CARET",
50
+ '~': "TILDE",
51
+ }
52
+
53
+ // strEscapes 单字符转义 (含控制字符), 与 Python 侧 _STR_ESCAPES 一一对应。
54
+ var strEscapes = map[byte]byte{
55
+ 'n': '\n', 't': '\t', 'r': '\r', '0': 0, 'a': 7, 'b': 8,
56
+ 'f': 12, 'v': 11, '"': '"', '\'': '\'', '\\': '\\',
57
+ }
58
+
59
+ // isHexDigit 判定十六进制数字 ([0-9a-fA-F])。
60
+ func isHexDigit(c byte) bool {
61
+ return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
62
+ }
63
+
64
+ // tokenize 把 CIN 源码切分为 token 流 (含续行处理与 BOM 容忍)。
65
+ func tokenize(source, filename string) ([]Token, error) {
66
+ source = strings.TrimPrefix(source, "\ufeff") // 容忍 UTF-8 BOM
67
+ var tokens []Token
68
+ i, line, n := 0, 1, len(source)
69
+
70
+ skipBlock := func() {
71
+ i += 2
72
+ for i < n-1 && !(source[i] == '*' && source[i+1] == '/') {
73
+ if source[i] == '\n' {
74
+ line++
75
+ }
76
+ i++
77
+ }
78
+ i += 2
79
+ }
80
+
81
+ for i < n {
82
+ c := source[i]
83
+ switch {
84
+ case c == '\n':
85
+ tokens = append(tokens, Token{kind: "NL", line: line})
86
+ line++
87
+ i++
88
+ case c == ' ' || c == '\t' || c == '\r':
89
+ i++
90
+ case c == '/' && i+1 < n && source[i+1] == '/':
91
+ for i < n && source[i] != '\n' {
92
+ i++
93
+ }
94
+ case c == '/' && i+1 < n && source[i+1] == '*':
95
+ skipBlock()
96
+ case c == '"':
97
+ startLine := line
98
+ i++
99
+ var buf strings.Builder
100
+ for i < n && source[i] != '"' {
101
+ if source[i] == '\\' && i+1 < n {
102
+ esc := source[i+1]
103
+ i += 2
104
+ if b, ok := strEscapes[esc]; ok {
105
+ buf.WriteByte(b)
106
+ } else if esc == 'x' {
107
+ // \xH 或 \xHH (1-2 位十六进制) —— 追加原始单字节 (C 语义)。
108
+ // Python 侧 >= 0x80 的字节用代理转义码位承载, 由
109
+ // _data_string 原样写回, 因此与这里的逐字节追加一致。
110
+ digits := make([]byte, 0, 2)
111
+ for len(digits) < 2 && i < n && isHexDigit(source[i]) {
112
+ digits = append(digits, source[i])
113
+ i++
114
+ }
115
+ if len(digits) == 0 {
116
+ return nil, fmt.Errorf(
117
+ "\\x escape needs at least one hex digit at line %d", line)
118
+ }
119
+ code, _ := strconv.ParseUint(string(digits), 16, 32)
120
+ buf.WriteByte(byte(code))
121
+ } else if esc == 'u' || esc == 'U' {
122
+ // \uHHHH (4 位) / \UHHHHHHHH (8 位) Unicode 码点 -> UTF-8 字节
123
+ width := 4
124
+ if esc == 'U' {
125
+ width = 8
126
+ }
127
+ if i+width > n {
128
+ return nil, fmt.Errorf(
129
+ "\\%c escape needs exactly %d hex digits at line %d",
130
+ esc, width, line)
131
+ }
132
+ digits := source[i : i+width]
133
+ for k := 0; k < width; k++ {
134
+ if !isHexDigit(digits[k]) {
135
+ return nil, fmt.Errorf(
136
+ "\\%c escape needs exactly %d hex digits at line %d",
137
+ esc, width, line)
138
+ }
139
+ }
140
+ code, _ := strconv.ParseUint(digits, 16, 64)
141
+ if code > 0x10FFFF {
142
+ return nil, fmt.Errorf(
143
+ "\\%c escape out of Unicode range at line %d", esc, line)
144
+ }
145
+ buf.WriteString(string(rune(code)))
146
+ i += width
147
+ } else {
148
+ buf.WriteByte(esc)
149
+ }
150
+ } else {
151
+ if source[i] == '\n' {
152
+ line++
153
+ }
154
+ buf.WriteByte(source[i])
155
+ i++
156
+ }
157
+ }
158
+ i++ // closing quote
159
+ tokens = append(tokens, Token{kind: "STRING", sval: buf.String(), line: startLine})
160
+ case c >= '0' && c <= '9' || (c == '.' && i+1 < n && source[i+1] >= '0' && source[i+1] <= '9'):
161
+ start := i
162
+ isFloat := false
163
+ if c == '0' && i+1 < n && strings.IndexByte("xXbBoO", source[i+1]) >= 0 {
164
+ baseChar := source[i+1]
165
+ var base int
166
+ var allowed string
167
+ switch baseChar {
168
+ case 'x', 'X':
169
+ base, allowed = 16, "0123456789abcdefABCDEF_"
170
+ case 'b', 'B':
171
+ base, allowed = 2, "01_"
172
+ default:
173
+ base, allowed = 8, "01234567_"
174
+ }
175
+ i += 2
176
+ dstart := i
177
+ for i < n && strings.IndexByte(allowed, source[i]) >= 0 {
178
+ i++
179
+ }
180
+ digits := source[dstart:i]
181
+ hasDigit := false
182
+ for _, ch := range digits {
183
+ if ch != '_' {
184
+ hasDigit = true
185
+ break
186
+ }
187
+ }
188
+ if i == dstart || !hasDigit {
189
+ return nil, fmt.Errorf("Malformed numeric literal at line %d", line)
190
+ }
191
+ for i < n && strings.IndexByte("uUlL", source[i]) >= 0 {
192
+ i++
193
+ }
194
+ if i < n && (source[i] == 'f' || source[i] == 'F') {
195
+ isFloat = true
196
+ i++
197
+ }
198
+ clean := strings.ReplaceAll(digits, "_", "")
199
+ v, err := strconv.ParseUint(clean, base, 64)
200
+ if err != nil {
201
+ return nil, fmt.Errorf("Malformed numeric literal at line %d", line)
202
+ }
203
+ _ = start
204
+ if isFloat {
205
+ tokens = append(tokens, Token{kind: "FLOAT", fval: float64(v), isFloat: true, line: line})
206
+ } else {
207
+ tokens = append(tokens, Token{kind: "NUMBER", ival: int64(v), line: line})
208
+ }
209
+ } else {
210
+ for i < n && (source[i] >= '0' && source[i] <= '9' || source[i] == '.' || source[i] == '_') {
211
+ if source[i] == '.' {
212
+ // 小数点最多一个; '..' 是 case 范围运算符, 不属于数字
213
+ if isFloat || (i+1 < n && source[i+1] == '.') {
214
+ break
215
+ }
216
+ isFloat = true
217
+ }
218
+ i++
219
+ }
220
+ if i < n && (source[i] == 'e' || source[i] == 'E') {
221
+ isFloat = true
222
+ i++
223
+ if i < n && (source[i] == '+' || source[i] == '-') {
224
+ i++
225
+ }
226
+ for i < n && (source[i] >= '0' && source[i] <= '9' || source[i] == '_') {
227
+ i++
228
+ }
229
+ }
230
+ digits := source[start:i]
231
+ for i < n && strings.IndexByte("uUlL", source[i]) >= 0 {
232
+ i++
233
+ }
234
+ if i < n && (source[i] == 'f' || source[i] == 'F') {
235
+ isFloat = true
236
+ i++
237
+ }
238
+ text := strings.ReplaceAll(digits, "_", "")
239
+ if isFloat {
240
+ f, err := strconv.ParseFloat(text, 64)
241
+ if err != nil {
242
+ return nil, fmt.Errorf("Malformed numeric literal at line %d", line)
243
+ }
244
+ tokens = append(tokens, Token{kind: "FLOAT", fval: f, isFloat: true, line: line})
245
+ } else {
246
+ iv, err := strconv.ParseInt(text, 10, 64)
247
+ if err != nil {
248
+ return nil, fmt.Errorf("Malformed numeric literal at line %d", line)
249
+ }
250
+ tokens = append(tokens, Token{kind: "NUMBER", ival: iv, line: line})
251
+ }
252
+ }
253
+ case c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_':
254
+ start := i
255
+ for i < n && (isAlnum(source[i]) || source[i] == '_') {
256
+ i++
257
+ }
258
+ tokens = append(tokens, Token{kind: "IDENT", sval: source[start:i], line: line})
259
+ case c == '-' && i+1 < n && source[i+1] == '>':
260
+ tokens = append(tokens, Token{kind: "ARROW", sval: "->", line: line})
261
+ i += 2
262
+ case c == '\'':
263
+ startLine := line
264
+ i++
265
+ if i >= n {
266
+ return nil, fmt.Errorf("Unterminated char literal at line %d", startLine)
267
+ }
268
+ var val int64
269
+ if source[i] == '\\' && i+1 < n {
270
+ esc := source[i+1]
271
+ escMap := map[byte]int64{'n': 10, 't': 9, 'r': 13, '0': 0, 'a': 7, 'b': 8,
272
+ 'f': 12, 'v': 11, '\'': 39, '"': 34, '\\': 92}
273
+ if v, ok := escMap[esc]; ok {
274
+ val = v
275
+ } else {
276
+ val = int64(esc)
277
+ }
278
+ i += 2
279
+ } else {
280
+ val = int64(source[i])
281
+ i++
282
+ }
283
+ if i >= n || source[i] != '\'' {
284
+ return nil, fmt.Errorf("Unterminated char literal at line %d", startLine)
285
+ }
286
+ i++
287
+ tokens = append(tokens, Token{kind: "NUMBER", ival: val, line: line})
288
+ default:
289
+ if strings.IndexByte("+-*/%", c) >= 0 && i+1 < n && source[i+1] == '=' {
290
+ kindMap := map[byte]string{'+': "PLUSEQ", '-': "MINUSEQ", '*': "STAREQ",
291
+ '/': "SLASHEQ", '%': "PERCENTEQ"}
292
+ tokens = append(tokens, Token{kind: kindMap[c], sval: string(c) + "=", line: line})
293
+ i += 2
294
+ } else if strings.IndexByte("&|^", c) >= 0 && i+1 < n && source[i+1] == '=' {
295
+ kindMap := map[byte]string{'&': "ANDEQ", '|': "OREQ", '^': "XOREQ"}
296
+ tokens = append(tokens, Token{kind: kindMap[c], sval: string(c) + "=", line: line})
297
+ i += 2
298
+ } else if (c == '<' || c == '>') && i+2 < n && source[i+1] == c && source[i+2] == '=' {
299
+ k := "SHLEQ"
300
+ if c == '>' {
301
+ k = "SHREQ"
302
+ }
303
+ tokens = append(tokens, Token{kind: k, sval: string(c) + string(c) + "=", line: line})
304
+ i += 3
305
+ } else if (c == '<' || c == '>') && i+1 < n && source[i+1] == c {
306
+ k := "SHL"
307
+ if c == '>' {
308
+ k = "SHR"
309
+ }
310
+ tokens = append(tokens, Token{kind: k, sval: string(c) + string(c), line: line})
311
+ i += 2
312
+ } else if c == '+' && i+1 < n && source[i+1] == '+' {
313
+ tokens = append(tokens, Token{kind: "INC", sval: "++", line: line})
314
+ i += 2
315
+ } else if c == '-' && i+1 < n && source[i+1] == '-' {
316
+ tokens = append(tokens, Token{kind: "DEC", sval: "--", line: line})
317
+ i += 2
318
+ } else if c == '=' && i+1 < n && source[i+1] == '=' {
319
+ tokens = append(tokens, Token{kind: "EQ", sval: "==", line: line})
320
+ i += 2
321
+ } else if c == '!' && i+1 < n && source[i+1] == '=' {
322
+ tokens = append(tokens, Token{kind: "NEQ", sval: "!=", line: line})
323
+ i += 2
324
+ } else if c == '<' && i+1 < n && source[i+1] == '=' {
325
+ tokens = append(tokens, Token{kind: "LE", sval: "<=", line: line})
326
+ i += 2
327
+ } else if c == '>' && i+1 < n && source[i+1] == '=' {
328
+ tokens = append(tokens, Token{kind: "GE", sval: ">=", line: line})
329
+ i += 2
330
+ } else if c == '&' && i+1 < n && source[i+1] == '&' {
331
+ tokens = append(tokens, Token{kind: "AND", sval: "&&", line: line})
332
+ i += 2
333
+ } else if c == '|' && i+1 < n && source[i+1] == '|' {
334
+ tokens = append(tokens, Token{kind: "OR", sval: "||", line: line})
335
+ i += 2
336
+ } else if c == '.' && i+1 < n && source[i+1] == '.' {
337
+ // case 范围: case 1..5: (必须在 singleOps 的 '.' 之前判定)
338
+ tokens = append(tokens, Token{kind: "DOTDOT", sval: "..", line: line})
339
+ i += 2
340
+ } else if k, ok := singleOps[c]; ok {
341
+ tokens = append(tokens, Token{kind: k, sval: string(c), line: line})
342
+ i++
343
+ } else {
344
+ return nil, fmt.Errorf("Unexpected character %q at line %d", c, line)
345
+ }
346
+ }
347
+ }
348
+
349
+ // 续行处理 (圆/方括号内或行尾运算符后的 NL 丢弃)
350
+ var filtered []Token
351
+ depth := 0
352
+ openK := map[string]bool{"LPAREN": true, "LBRACKET": true}
353
+ closeK := map[string]bool{"RPAREN": true, "RBRACKET": true}
354
+ continueOps := map[string]bool{
355
+ "PLUS": true, "MINUS": true, "STAR": true, "SLASH": true, "PERCENT": true,
356
+ "ASSIGN": true, "LT": true, "GT": true, "LE": true, "GE": true, "EQ": true,
357
+ "NEQ": true, "AND": true, "OR": true, "COMMA": true, "ARROW": true, "DOT": true,
358
+ "DOTDOT": true,
359
+ "PLUSEQ": true, "MINUSEQ": true, "STAREQ": true, "SLASHEQ": true,
360
+ "PERCENTEQ": true, "INC": true, "DEC": true, "AMP": true, "PIPE": true,
361
+ "CARET": true, "TILDE": true, "SHL": true, "SHR": true, "ANDEQ": true,
362
+ "OREQ": true, "XOREQ": true, "SHLEQ": true, "SHREQ": true,
363
+ }
364
+ for _, tok := range tokens {
365
+ if openK[tok.kind] {
366
+ depth++
367
+ } else if closeK[tok.kind] {
368
+ if depth > 0 {
369
+ depth--
370
+ }
371
+ }
372
+ if tok.kind == "NL" {
373
+ if depth > 0 {
374
+ continue
375
+ }
376
+ if len(filtered) > 0 {
377
+ prev := filtered[len(filtered)-1]
378
+ if continueOps[prev.kind] {
379
+ continue
380
+ }
381
+ if prev.kind == "NL" {
382
+ continue
383
+ }
384
+ }
385
+ }
386
+ filtered = append(filtered, tok)
387
+ }
388
+ filtered = append(filtered, Token{kind: "EOF", line: line})
389
+ return filtered, nil
390
+ }
391
+
392
+ func isAlnum(c byte) bool {
393
+ return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
394
+ }