freelang-v4 4.3.0

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 (261) hide show
  1. package/README.md +548 -0
  2. package/dist/ast.d.ts +367 -0
  3. package/dist/ast.js +4 -0
  4. package/dist/ast.js.map +1 -0
  5. package/dist/async-basic.test.d.ts +1 -0
  6. package/dist/async-basic.test.js +88 -0
  7. package/dist/async-basic.test.js.map +1 -0
  8. package/dist/async-jest.test.d.ts +1 -0
  9. package/dist/async-jest.test.js +99 -0
  10. package/dist/async-jest.test.js.map +1 -0
  11. package/dist/channel-jest.test.d.ts +1 -0
  12. package/dist/channel-jest.test.js +148 -0
  13. package/dist/channel-jest.test.js.map +1 -0
  14. package/dist/checker-jest.test.d.ts +1 -0
  15. package/dist/checker-jest.test.js +160 -0
  16. package/dist/checker-jest.test.js.map +1 -0
  17. package/dist/checker.d.ts +149 -0
  18. package/dist/checker.js +1565 -0
  19. package/dist/checker.js.map +1 -0
  20. package/dist/checker.test.d.ts +1 -0
  21. package/dist/checker.test.js +217 -0
  22. package/dist/checker.test.js.map +1 -0
  23. package/dist/compiler-jest.test.d.ts +1 -0
  24. package/dist/compiler-jest.test.js +233 -0
  25. package/dist/compiler-jest.test.js.map +1 -0
  26. package/dist/compiler.d.ts +127 -0
  27. package/dist/compiler.js +1588 -0
  28. package/dist/compiler.js.map +1 -0
  29. package/dist/compiler.test.d.ts +1 -0
  30. package/dist/compiler.test.js +313 -0
  31. package/dist/compiler.test.js.map +1 -0
  32. package/dist/db-100m-full.d.ts +5 -0
  33. package/dist/db-100m-full.js +78 -0
  34. package/dist/db-100m-full.js.map +1 -0
  35. package/dist/db-100m-no-index.d.ts +12 -0
  36. package/dist/db-100m-no-index.js +119 -0
  37. package/dist/db-100m-no-index.js.map +1 -0
  38. package/dist/db-100m-real.d.ts +5 -0
  39. package/dist/db-100m-real.js +131 -0
  40. package/dist/db-100m-real.js.map +1 -0
  41. package/dist/db-100m-streaming.d.ts +15 -0
  42. package/dist/db-100m-streaming.js +164 -0
  43. package/dist/db-100m-streaming.js.map +1 -0
  44. package/dist/db-100m-test.d.ts +5 -0
  45. package/dist/db-100m-test.js +111 -0
  46. package/dist/db-100m-test.js.map +1 -0
  47. package/dist/db-jest.test.d.ts +1 -0
  48. package/dist/db-jest.test.js +182 -0
  49. package/dist/db-jest.test.js.map +1 -0
  50. package/dist/db-runtime.d.ts +24 -0
  51. package/dist/db-runtime.js +204 -0
  52. package/dist/db-runtime.js.map +1 -0
  53. package/dist/db.d.ts +249 -0
  54. package/dist/db.js +593 -0
  55. package/dist/db.js.map +1 -0
  56. package/dist/file-io-jest.test.d.ts +1 -0
  57. package/dist/file-io-jest.test.js +225 -0
  58. package/dist/file-io-jest.test.js.map +1 -0
  59. package/dist/for-of-jest.test.d.ts +1 -0
  60. package/dist/for-of-jest.test.js +230 -0
  61. package/dist/for-of-jest.test.js.map +1 -0
  62. package/dist/for-of.test.d.ts +1 -0
  63. package/dist/for-of.test.js +305 -0
  64. package/dist/for-of.test.js.map +1 -0
  65. package/dist/function-literal-jest.test.d.ts +1 -0
  66. package/dist/function-literal-jest.test.js +180 -0
  67. package/dist/function-literal-jest.test.js.map +1 -0
  68. package/dist/function-literal.test.d.ts +1 -0
  69. package/dist/function-literal.test.js +245 -0
  70. package/dist/function-literal.test.js.map +1 -0
  71. package/dist/generics-jest.test.d.ts +1 -0
  72. package/dist/generics-jest.test.js +93 -0
  73. package/dist/generics-jest.test.js.map +1 -0
  74. package/dist/ir-gen.d.ts +15 -0
  75. package/dist/ir-gen.js +400 -0
  76. package/dist/ir-gen.js.map +1 -0
  77. package/dist/ir.d.ts +114 -0
  78. package/dist/ir.js +5 -0
  79. package/dist/ir.js.map +1 -0
  80. package/dist/lexer.d.ts +110 -0
  81. package/dist/lexer.js +467 -0
  82. package/dist/lexer.js.map +1 -0
  83. package/dist/lexer.test.d.ts +1 -0
  84. package/dist/lexer.test.js +426 -0
  85. package/dist/lexer.test.js.map +1 -0
  86. package/dist/main.d.ts +2 -0
  87. package/dist/main.js +241 -0
  88. package/dist/main.js.map +1 -0
  89. package/dist/module-jest.test.d.ts +1 -0
  90. package/dist/module-jest.test.js +123 -0
  91. package/dist/module-jest.test.js.map +1 -0
  92. package/dist/parser.d.ts +56 -0
  93. package/dist/parser.js +1060 -0
  94. package/dist/parser.js.map +1 -0
  95. package/dist/parser.test.d.ts +1 -0
  96. package/dist/parser.test.js +461 -0
  97. package/dist/parser.test.js.map +1 -0
  98. package/dist/pattern-matching-jest.test.d.ts +1 -0
  99. package/dist/pattern-matching-jest.test.js +158 -0
  100. package/dist/pattern-matching-jest.test.js.map +1 -0
  101. package/dist/pkg/init.d.ts +1 -0
  102. package/dist/pkg/init.js +118 -0
  103. package/dist/pkg/init.js.map +1 -0
  104. package/dist/pkg/install.d.ts +1 -0
  105. package/dist/pkg/install.js +77 -0
  106. package/dist/pkg/install.js.map +1 -0
  107. package/dist/pkg/registry.d.ts +23 -0
  108. package/dist/pkg/registry.js +106 -0
  109. package/dist/pkg/registry.js.map +1 -0
  110. package/dist/pkg/run.d.ts +1 -0
  111. package/dist/pkg/run.js +76 -0
  112. package/dist/pkg/run.js.map +1 -0
  113. package/dist/pkg/toml.d.ts +5 -0
  114. package/dist/pkg/toml.js +117 -0
  115. package/dist/pkg/toml.js.map +1 -0
  116. package/dist/repl.d.ts +15 -0
  117. package/dist/repl.js +197 -0
  118. package/dist/repl.js.map +1 -0
  119. package/dist/runtime/bytecode.d.ts +92 -0
  120. package/dist/runtime/bytecode.js +253 -0
  121. package/dist/runtime/bytecode.js.map +1 -0
  122. package/dist/runtime/value.d.ts +102 -0
  123. package/dist/runtime/value.js +302 -0
  124. package/dist/runtime/value.js.map +1 -0
  125. package/dist/runtime/vm.d.ts +65 -0
  126. package/dist/runtime/vm.js +293 -0
  127. package/dist/runtime/vm.js.map +1 -0
  128. package/dist/struct-instance-jest.test.d.ts +1 -0
  129. package/dist/struct-instance-jest.test.js +209 -0
  130. package/dist/struct-instance-jest.test.js.map +1 -0
  131. package/dist/struct-instance.test.d.ts +1 -0
  132. package/dist/struct-instance.test.js +291 -0
  133. package/dist/struct-instance.test.js.map +1 -0
  134. package/dist/struct-jest.test.d.ts +1 -0
  135. package/dist/struct-jest.test.js +176 -0
  136. package/dist/struct-jest.test.js.map +1 -0
  137. package/dist/struct.test.d.ts +1 -0
  138. package/dist/struct.test.js +231 -0
  139. package/dist/struct.test.js.map +1 -0
  140. package/dist/trait-jest.test.d.ts +1 -0
  141. package/dist/trait-jest.test.js +120 -0
  142. package/dist/trait-jest.test.js.map +1 -0
  143. package/dist/vm-jest.test.d.ts +1 -0
  144. package/dist/vm-jest.test.js +569 -0
  145. package/dist/vm-jest.test.js.map +1 -0
  146. package/dist/vm.d.ts +81 -0
  147. package/dist/vm.js +1956 -0
  148. package/dist/vm.js.map +1 -0
  149. package/dist/vm.test.d.ts +1 -0
  150. package/dist/vm.test.js +337 -0
  151. package/dist/vm.test.js.map +1 -0
  152. package/dist/web-repl/sandbox.d.ts +11 -0
  153. package/dist/web-repl/sandbox.js +76 -0
  154. package/dist/web-repl/sandbox.js.map +1 -0
  155. package/dist/web-repl/server.d.ts +1 -0
  156. package/dist/web-repl/server.js +111 -0
  157. package/dist/web-repl/server.js.map +1 -0
  158. package/dist/while-loop-jest.test.d.ts +1 -0
  159. package/dist/while-loop-jest.test.js +201 -0
  160. package/dist/while-loop-jest.test.js.map +1 -0
  161. package/dist/while-loop.test.d.ts +1 -0
  162. package/dist/while-loop.test.js +262 -0
  163. package/dist/while-loop.test.js.map +1 -0
  164. package/docs/EXPERIENCE.md +787 -0
  165. package/docs/README.md +175 -0
  166. package/docs/V1_V2_V3_ANALYSIS.md +107 -0
  167. package/docs/_config.yml +36 -0
  168. package/docs/api-reference.md +459 -0
  169. package/docs/architecture.md +470 -0
  170. package/docs/benchmarks.md +295 -0
  171. package/docs/comparison.md +454 -0
  172. package/docs/index.md +335 -0
  173. package/docs/language-completeness.md +228 -0
  174. package/docs/learning-guide.md +651 -0
  175. package/package.json +65 -0
  176. package/src/api/deploy_key.fl +294 -0
  177. package/src/api/issue.fl +302 -0
  178. package/src/api/org.fl +356 -0
  179. package/src/api/repo.fl +394 -0
  180. package/src/api/team.fl +299 -0
  181. package/src/api/user.fl +385 -0
  182. package/src/api/webhook.fl +273 -0
  183. package/src/ast.ts +158 -0
  184. package/src/async-basic.test.ts +94 -0
  185. package/src/async-jest.test.ts +107 -0
  186. package/src/channel-jest.test.ts +158 -0
  187. package/src/checker-jest.test.ts +189 -0
  188. package/src/checker.test.ts +279 -0
  189. package/src/checker.ts +1861 -0
  190. package/src/commands/analyze.fl +227 -0
  191. package/src/commands/auth.fl +315 -0
  192. package/src/commands/batch.fl +349 -0
  193. package/src/commands/config.fl +199 -0
  194. package/src/commands/deploy_key.fl +352 -0
  195. package/src/commands/issue.fl +275 -0
  196. package/src/commands/main.fl +492 -0
  197. package/src/commands/org.fl +425 -0
  198. package/src/commands/repo.fl +581 -0
  199. package/src/commands/team.fl +244 -0
  200. package/src/commands/user.fl +423 -0
  201. package/src/commands/webhook.fl +400 -0
  202. package/src/compiler-jest.test.ts +275 -0
  203. package/src/compiler.test.ts +375 -0
  204. package/src/compiler.ts +1770 -0
  205. package/src/config.fl +175 -0
  206. package/src/core/batch.fl +355 -0
  207. package/src/core/cache.fl +284 -0
  208. package/src/core/ensure.fl +324 -0
  209. package/src/db-100m-full.ts +96 -0
  210. package/src/db-100m-no-index.ts +133 -0
  211. package/src/db-100m-real.ts +152 -0
  212. package/src/db-100m-streaming.ts +154 -0
  213. package/src/db-100m-test.ts +136 -0
  214. package/src/db-jest.test.ts +161 -0
  215. package/src/db-runtime.ts +242 -0
  216. package/src/db.ts +676 -0
  217. package/src/errors.fl +134 -0
  218. package/src/for-of-jest.test.ts +246 -0
  219. package/src/for-of.test.ts +308 -0
  220. package/src/function-literal-jest.test.ts +193 -0
  221. package/src/function-literal.test.ts +248 -0
  222. package/src/generics-jest.test.ts +104 -0
  223. package/src/http/client.fl +327 -0
  224. package/src/ir-gen.ts +459 -0
  225. package/src/ir.ts +80 -0
  226. package/src/lexer.test.ts +499 -0
  227. package/src/lexer.ts +522 -0
  228. package/src/main.ts +223 -0
  229. package/src/models.fl +162 -0
  230. package/src/module-jest.test.ts +145 -0
  231. package/src/parser.test.ts +542 -0
  232. package/src/parser.ts +1211 -0
  233. package/src/pattern-matching-jest.test.ts +170 -0
  234. package/src/pkg/init.ts +91 -0
  235. package/src/pkg/install.ts +56 -0
  236. package/src/pkg/registry.ts +103 -0
  237. package/src/pkg/run.ts +49 -0
  238. package/src/pkg/toml.ts +129 -0
  239. package/src/repl.ts +190 -0
  240. package/src/runtime/bytecode.ts +291 -0
  241. package/src/runtime/value.ts +322 -0
  242. package/src/runtime/vm.ts +354 -0
  243. package/src/self-host/bootstrap.fl +68 -0
  244. package/src/self-host/interpreter.fl +361 -0
  245. package/src/self-host/lexer-simple.fl +22 -0
  246. package/src/self-host/lexer.fl +305 -0
  247. package/src/self-host/parser.fl +580 -0
  248. package/src/struct-instance-jest.test.ts +221 -0
  249. package/src/struct-instance.test.ts +293 -0
  250. package/src/struct-jest.test.ts +187 -0
  251. package/src/struct.test.ts +234 -0
  252. package/src/trait-jest.test.ts +136 -0
  253. package/src/vm-jest.test.ts +754 -0
  254. package/src/vm.ts +1976 -0
  255. package/src/web-repl/public/index.html +50 -0
  256. package/src/web-repl/public/main.js +105 -0
  257. package/src/web-repl/public/style.css +225 -0
  258. package/src/web-repl/sandbox.ts +88 -0
  259. package/src/web-repl/server.ts +97 -0
  260. package/src/while-loop-jest.test.ts +218 -0
  261. package/src/while-loop.test.ts +267 -0
@@ -0,0 +1,499 @@
1
+ // FreeLang v4 — Lexer 테스트
2
+
3
+ import { Lexer, TokenType, Token, LexError } from "./lexer";
4
+
5
+ function lex(source: string): { tokens: Token[]; errors: LexError[] } {
6
+ return new Lexer(source).tokenize();
7
+ }
8
+
9
+ function types(source: string): TokenType[] {
10
+ return lex(source).tokens.map((t) => t.type);
11
+ }
12
+
13
+ function lexemes(source: string): string[] {
14
+ return lex(source).tokens.map((t) => t.lexeme);
15
+ }
16
+
17
+ // ============================================================
18
+ // 1. 키워드 (13개)
19
+ // ============================================================
20
+
21
+ console.log("=== 키워드 테스트 ===");
22
+
23
+ {
24
+ const r = lex("var let const fn if else match for in return spawn true false");
25
+ const kw = r.tokens.slice(0, -1); // EOF 제외
26
+ console.assert(kw.length === 13, `키워드 13개 예상, 실제 ${kw.length}`);
27
+ console.assert(kw[0].type === TokenType.VAR, "var");
28
+ console.assert(kw[1].type === TokenType.LET, "let");
29
+ console.assert(kw[2].type === TokenType.CONST, "const");
30
+ console.assert(kw[3].type === TokenType.FN, "fn");
31
+ console.assert(kw[4].type === TokenType.IF, "if");
32
+ console.assert(kw[5].type === TokenType.ELSE, "else");
33
+ console.assert(kw[6].type === TokenType.MATCH, "match");
34
+ console.assert(kw[7].type === TokenType.FOR, "for");
35
+ console.assert(kw[8].type === TokenType.IN, "in");
36
+ console.assert(kw[9].type === TokenType.RETURN, "return");
37
+ console.assert(kw[10].type === TokenType.SPAWN, "spawn");
38
+ console.assert(kw[11].type === TokenType.TRUE, "true");
39
+ console.assert(kw[12].type === TokenType.FALSE, "false");
40
+ console.log(" ✅ 키워드 13개 통과");
41
+ }
42
+
43
+ // ============================================================
44
+ // 2. 타입 이름 (7개)
45
+ // ============================================================
46
+
47
+ console.log("=== 타입 이름 테스트 ===");
48
+
49
+ {
50
+ const r = lex("i32 i64 f64 bool string void channel");
51
+ const t = r.tokens.slice(0, -1);
52
+ console.assert(t.length === 7, `타입 7개 예상, 실제 ${t.length}`);
53
+ console.assert(t[0].type === TokenType.TYPE_I32, "i32");
54
+ console.assert(t[1].type === TokenType.TYPE_I64, "i64");
55
+ console.assert(t[2].type === TokenType.TYPE_F64, "f64");
56
+ console.assert(t[3].type === TokenType.TYPE_BOOL, "bool");
57
+ console.assert(t[4].type === TokenType.TYPE_STRING, "string");
58
+ console.assert(t[5].type === TokenType.TYPE_VOID, "void");
59
+ console.assert(t[6].type === TokenType.TYPE_CHANNEL, "channel");
60
+ console.log(" ✅ 타입 이름 7개 통과");
61
+ }
62
+
63
+ // ============================================================
64
+ // 3. Option/Result/Ok/Err/Some/None은 IDENT (SPEC_04 Q4)
65
+ // ============================================================
66
+
67
+ console.log("=== Option/Result은 IDENT 테스트 ===");
68
+
69
+ {
70
+ const r = lex("Option Result Ok Err Some None");
71
+ const t = r.tokens.slice(0, -1);
72
+ for (const tok of t) {
73
+ console.assert(tok.type === TokenType.IDENT, `${tok.lexeme}은 IDENT여야 함, 실제 ${tok.type}`);
74
+ }
75
+ console.log(" ✅ Option/Result/Ok/Err/Some/None = IDENT 통과");
76
+ }
77
+
78
+ // ============================================================
79
+ // 4. 식별자
80
+ // ============================================================
81
+
82
+ console.log("=== 식별자 테스트 ===");
83
+
84
+ {
85
+ const r = lex("x my_func _tmp abc123 _");
86
+ const t = r.tokens.slice(0, -1);
87
+ console.assert(t.length === 5, `식별자 5개 예상`);
88
+ for (const tok of t) {
89
+ console.assert(tok.type === TokenType.IDENT, `${tok.lexeme}은 IDENT여야 함`);
90
+ }
91
+ console.assert(t[0].lexeme === "x");
92
+ console.assert(t[1].lexeme === "my_func");
93
+ console.assert(t[2].lexeme === "_tmp");
94
+ console.assert(t[3].lexeme === "abc123");
95
+ console.assert(t[4].lexeme === "_");
96
+ console.log(" ✅ 식별자 통과");
97
+ }
98
+
99
+ // ============================================================
100
+ // 5. 정수 리터럴 (SPEC_04 Q5)
101
+ // ============================================================
102
+
103
+ console.log("=== 정수 리터럴 테스트 ===");
104
+
105
+ {
106
+ const r = lex("0 42 1000 1_000 1_000_000");
107
+ const t = r.tokens.slice(0, -1);
108
+ console.assert(t.length === 5, `정수 5개 예상`);
109
+ for (const tok of t) {
110
+ console.assert(tok.type === TokenType.INT_LIT, `${tok.lexeme}은 INT_LIT여야 함`);
111
+ }
112
+ console.assert(t[0].lexeme === "0");
113
+ console.assert(t[1].lexeme === "42");
114
+ console.assert(t[2].lexeme === "1000");
115
+ console.assert(t[3].lexeme === "1_000");
116
+ console.assert(t[4].lexeme === "1_000_000");
117
+ console.log(" ✅ 정수 리터럴 통과");
118
+ }
119
+
120
+ // 끝 밑줄 에러
121
+ {
122
+ const r = lex("42_");
123
+ console.assert(r.errors.length > 0, "끝 밑줄 에러 예상");
124
+ console.assert(r.errors[0].message === "trailing underscore in number");
125
+ console.log(" ✅ 끝 밑줄 에러 통과");
126
+ }
127
+
128
+ // 연속 밑줄 에러
129
+ {
130
+ const r = lex("4__2");
131
+ console.assert(r.errors.length > 0, "연속 밑줄 에러 예상");
132
+ console.assert(r.errors[0].message === "consecutive underscores in number");
133
+ console.log(" ✅ 연속 밑줄 에러 통과");
134
+ }
135
+
136
+ // ============================================================
137
+ // 6. 부동소수점 리터럴 (SPEC_04 Q5)
138
+ // ============================================================
139
+
140
+ console.log("=== 부동소수점 리터럴 테스트 ===");
141
+
142
+ {
143
+ const r = lex("0.0 3.14 1_000.5 0.001");
144
+ const t = r.tokens.slice(0, -1);
145
+ console.assert(t.length === 4, `부동소수점 4개 예상`);
146
+ for (const tok of t) {
147
+ console.assert(tok.type === TokenType.FLOAT_LIT, `${tok.lexeme}은 FLOAT_LIT여야 함`);
148
+ }
149
+ console.log(" ✅ 부동소수점 리터럴 통과");
150
+ }
151
+
152
+ // ".5" → DOT + INT_LIT (선행 숫자 없음)
153
+ {
154
+ const t = types(".5");
155
+ console.assert(t[0] === TokenType.DOT, ".5의 첫 토큰은 DOT");
156
+ console.assert(t[1] === TokenType.INT_LIT, ".5의 두 번째 토큰은 INT_LIT");
157
+ console.log(" ✅ .5 → DOT + INT_LIT 통과");
158
+ }
159
+
160
+ // "5." → INT_LIT + DOT (후행 숫자 없음)
161
+ {
162
+ const t = types("5.");
163
+ console.assert(t[0] === TokenType.INT_LIT, "5.의 첫 토큰은 INT_LIT");
164
+ console.assert(t[1] === TokenType.DOT, "5.의 두 번째 토큰은 DOT");
165
+ console.log(" ✅ 5. → INT_LIT + DOT 통과");
166
+ }
167
+
168
+ // ============================================================
169
+ // 7. 문자열 리터럴 (SPEC_04 Q6)
170
+ // ============================================================
171
+
172
+ console.log("=== 문자열 리터럴 테스트 ===");
173
+
174
+ // 기본 문자열
175
+ {
176
+ const r = lex('"hello"');
177
+ const t = r.tokens[0];
178
+ console.assert(t.type === TokenType.STRING_LIT);
179
+ console.assert(t.lexeme === "hello");
180
+ console.log(" ✅ 기본 문자열 통과");
181
+ }
182
+
183
+ // 빈 문자열
184
+ {
185
+ const r = lex('""');
186
+ console.assert(r.tokens[0].type === TokenType.STRING_LIT);
187
+ console.assert(r.tokens[0].lexeme === "");
188
+ console.log(" ✅ 빈 문자열 통과");
189
+ }
190
+
191
+ // 이스케이프 시퀀스
192
+ {
193
+ const r = lex('"a\\nb"');
194
+ console.assert(r.tokens[0].lexeme === "a\nb", "\\n 이스케이프");
195
+ console.log(" ✅ \\n 이스케이프 통과");
196
+ }
197
+
198
+ {
199
+ const r = lex('"a\\tb"');
200
+ console.assert(r.tokens[0].lexeme === "a\tb", "\\t 이스케이프");
201
+ console.log(" ✅ \\t 이스케이프 통과");
202
+ }
203
+
204
+ {
205
+ const r = lex('"say \\"hi\\""');
206
+ console.assert(r.tokens[0].lexeme === 'say "hi"', '\\" 이스케이프');
207
+ console.log(' ✅ \\" 이스케이프 통과');
208
+ }
209
+
210
+ {
211
+ const r = lex('"path\\\\file"');
212
+ console.assert(r.tokens[0].lexeme === "path\\file", "\\\\ 이스케이프");
213
+ console.log(" ✅ \\\\ 이스케이프 통과");
214
+ }
215
+
216
+ // 미종료 문자열
217
+ {
218
+ const r = lex('"unterminated');
219
+ console.assert(r.errors.length > 0);
220
+ console.assert(r.errors[0].message === "unterminated string literal");
221
+ console.log(" ✅ 미종료 문자열 에러 통과");
222
+ }
223
+
224
+ // 문자열 내 줄바꿈
225
+ {
226
+ const r = lex('"hello\nworld"');
227
+ console.assert(r.errors.length > 0);
228
+ console.assert(r.errors[0].message === "newline in string literal, use \\n");
229
+ console.log(" ✅ 문자열 내 줄바꿈 에러 통과");
230
+ }
231
+
232
+ // 알 수 없는 이스케이프
233
+ {
234
+ const r = lex('"\\x"');
235
+ console.assert(r.errors.length > 0);
236
+ console.assert(r.errors[0].message === "unknown escape sequence: \\x");
237
+ console.log(" ✅ 알 수 없는 이스케이프 에러 통과");
238
+ }
239
+
240
+ // ============================================================
241
+ // 8. 연산자 — 최장 일치 (SPEC_04 Q3)
242
+ // ============================================================
243
+
244
+ console.log("=== 연산자 최장 일치 테스트 ===");
245
+
246
+ {
247
+ const t = types("==");
248
+ console.assert(t[0] === TokenType.EQEQ, "== → EQEQ");
249
+ console.log(" ✅ == → EQEQ 통과");
250
+ }
251
+
252
+ {
253
+ const t = types("=>");
254
+ console.assert(t[0] === TokenType.ARROW, "=> → ARROW");
255
+ console.log(" ✅ => → ARROW 통과");
256
+ }
257
+
258
+ {
259
+ const t = types("!=");
260
+ console.assert(t[0] === TokenType.NEQ, "!= → NEQ");
261
+ console.log(" ✅ != → NEQ 통과");
262
+ }
263
+
264
+ {
265
+ const t = types("<=");
266
+ console.assert(t[0] === TokenType.LTEQ, "<= → LTEQ");
267
+ console.log(" ✅ <= → LTEQ 통과");
268
+ }
269
+
270
+ {
271
+ const t = types(">=");
272
+ console.assert(t[0] === TokenType.GTEQ, ">= → GTEQ");
273
+ console.log(" ✅ >= → GTEQ 통과");
274
+ }
275
+
276
+ {
277
+ const t = types("&&");
278
+ console.assert(t[0] === TokenType.AND, "&& → AND");
279
+ console.log(" ✅ && → AND 통과");
280
+ }
281
+
282
+ {
283
+ const t = types("||");
284
+ console.assert(t[0] === TokenType.OR, "|| → OR");
285
+ console.log(" ✅ || → OR 통과");
286
+ }
287
+
288
+ // 공백 분리
289
+ {
290
+ const t = types("= =");
291
+ console.assert(t[0] === TokenType.EQ, "= = → EQ, EQ");
292
+ console.assert(t[1] === TokenType.EQ);
293
+ console.log(" ✅ = = → EQ, EQ 통과");
294
+ }
295
+
296
+ {
297
+ const t = types("= >");
298
+ console.assert(t[0] === TokenType.EQ);
299
+ console.assert(t[1] === TokenType.GT);
300
+ console.log(" ✅ = > → EQ, GT 통과");
301
+ }
302
+
303
+ {
304
+ const t = types("! =");
305
+ console.assert(t[0] === TokenType.NOT);
306
+ console.assert(t[1] === TokenType.EQ);
307
+ console.log(" ✅ ! = → NOT, EQ 통과");
308
+ }
309
+
310
+ // ============================================================
311
+ // 9. 1글자 연산자/구두점
312
+ // ============================================================
313
+
314
+ console.log("=== 1글자 연산자 테스트 ===");
315
+
316
+ {
317
+ const t = types("+ - * / % = < > ! ? : , . ( ) [ ] { }");
318
+ const expected = [
319
+ TokenType.PLUS, TokenType.MINUS, TokenType.STAR, TokenType.SLASH,
320
+ TokenType.PERCENT, TokenType.EQ, TokenType.LT, TokenType.GT,
321
+ TokenType.NOT, TokenType.QUESTION, TokenType.COLON, TokenType.COMMA,
322
+ TokenType.DOT, TokenType.LPAREN, TokenType.RPAREN, TokenType.LBRACKET,
323
+ TokenType.RBRACKET, TokenType.LBRACE, TokenType.RBRACE, TokenType.EOF,
324
+ ];
325
+ for (let i = 0; i < expected.length; i++) {
326
+ console.assert(t[i] === expected[i], `인덱스 ${i}: ${expected[i]} 예상, 실제 ${t[i]}`);
327
+ }
328
+ console.log(" ✅ 1글자 연산자 19종 통과");
329
+ }
330
+
331
+ // ============================================================
332
+ // 10. 주석 (SPEC_04 Q8)
333
+ // ============================================================
334
+
335
+ console.log("=== 주석 테스트 ===");
336
+
337
+ {
338
+ const t = types("var x = 42 // comment\nvar y = 10");
339
+ // var x = 42 var y = 10 EOF
340
+ console.assert(t[0] === TokenType.VAR);
341
+ console.assert(t[1] === TokenType.IDENT);
342
+ console.assert(t[2] === TokenType.EQ);
343
+ console.assert(t[3] === TokenType.INT_LIT);
344
+ console.assert(t[4] === TokenType.VAR);
345
+ console.assert(t[5] === TokenType.IDENT);
346
+ console.assert(t[6] === TokenType.EQ);
347
+ console.assert(t[7] === TokenType.INT_LIT);
348
+ console.log(" ✅ 한 줄 주석 건너뜀 통과");
349
+ }
350
+
351
+ // 블록 주석 에러
352
+ {
353
+ const r = lex("/* block */");
354
+ console.assert(r.errors.length > 0);
355
+ console.assert(r.errors[0].message === "block comments not supported, use //");
356
+ console.log(" ✅ 블록 주석 에러 통과");
357
+ }
358
+
359
+ // ============================================================
360
+ // 11. NEWLINE — 토큰 생성 안 함 (SPEC_04 Q7)
361
+ // ============================================================
362
+
363
+ console.log("=== NEWLINE 테스트 ===");
364
+
365
+ {
366
+ const t = types("var x = 42\nvar y = 10");
367
+ // NEWLINE 토큰이 없어야 함
368
+ for (const tt of t) {
369
+ console.assert(tt !== "NEWLINE" as any, "NEWLINE 토큰이 있으면 안 됨");
370
+ }
371
+ console.log(" ✅ NEWLINE 토큰 없음 통과");
372
+ }
373
+
374
+ // ============================================================
375
+ // 12. 위치 추적 (줄:열)
376
+ // ============================================================
377
+
378
+ console.log("=== 위치 추적 테스트 ===");
379
+
380
+ {
381
+ const r = lex("var x = 42\nvar y = 10");
382
+ const t = r.tokens;
383
+ // 1번째 줄: var(1:1) x(1:5) =(1:7) 42(1:9)
384
+ console.assert(t[0].line === 1 && t[0].col === 1, "var at 1:1");
385
+ console.assert(t[1].line === 1 && t[1].col === 5, "x at 1:5");
386
+ console.assert(t[2].line === 1 && t[2].col === 7, "= at 1:7");
387
+ console.assert(t[3].line === 1 && t[3].col === 9, "42 at 1:9");
388
+ // 2번째 줄: var(2:1) y(2:5) =(2:7) 10(2:9)
389
+ console.assert(t[4].line === 2 && t[4].col === 1, "var at 2:1");
390
+ console.assert(t[5].line === 2 && t[5].col === 5, "y at 2:5");
391
+ console.log(" ✅ 위치 추적 통과");
392
+ }
393
+
394
+ // ============================================================
395
+ // 13. 에러 복구 (SPEC_04 Q9)
396
+ // ============================================================
397
+
398
+ console.log("=== 에러 복구 테스트 ===");
399
+
400
+ {
401
+ const r = lex("var x = @\nvar y = 42");
402
+ // 에러 1개 (@ 문자)
403
+ console.assert(r.errors.length >= 1, "에러 1개 이상");
404
+ // 에러 복구 후 나머지 토큰 정상 스캔
405
+ const hasY = r.tokens.some((t) => t.lexeme === "y");
406
+ console.assert(hasY, "에러 복구 후 y 토큰 존재");
407
+ console.log(" ✅ 에러 복구 통과");
408
+ }
409
+
410
+ // ============================================================
411
+ // 14. 통합 테스트 — 실제 FreeLang v4 코드
412
+ // ============================================================
413
+
414
+ console.log("=== 통합 테스트 ===");
415
+
416
+ {
417
+ const source = `fn add(a: i32, b: i32): i32 {
418
+ return a + b
419
+ }`;
420
+ const r = lex(source);
421
+ console.assert(r.errors.length === 0, `에러 없어야 함, 실제 ${r.errors.length}`);
422
+ const t = r.tokens.slice(0, -1); // EOF 제외
423
+ // fn add ( a : i32 , b : i32 ) : i32 { return a + b }
424
+ console.assert(t[0].type === TokenType.FN);
425
+ console.assert(t[1].type === TokenType.IDENT && t[1].lexeme === "add");
426
+ console.assert(t[2].type === TokenType.LPAREN);
427
+ console.assert(t[3].type === TokenType.IDENT && t[3].lexeme === "a");
428
+ console.assert(t[4].type === TokenType.COLON);
429
+ console.assert(t[5].type === TokenType.TYPE_I32);
430
+ console.assert(t[6].type === TokenType.COMMA);
431
+ console.assert(t[7].type === TokenType.IDENT && t[7].lexeme === "b");
432
+ console.assert(t[8].type === TokenType.COLON);
433
+ console.assert(t[9].type === TokenType.TYPE_I32);
434
+ console.assert(t[10].type === TokenType.RPAREN);
435
+ console.assert(t[11].type === TokenType.COLON);
436
+ console.assert(t[12].type === TokenType.TYPE_I32);
437
+ console.assert(t[13].type === TokenType.LBRACE);
438
+ console.assert(t[14].type === TokenType.RETURN);
439
+ console.assert(t[15].type === TokenType.IDENT && t[15].lexeme === "a");
440
+ console.assert(t[16].type === TokenType.PLUS);
441
+ console.assert(t[17].type === TokenType.IDENT && t[17].lexeme === "b");
442
+ console.assert(t[18].type === TokenType.RBRACE);
443
+ console.log(" ✅ fn add(a: i32, b: i32): i32 통합 통과");
444
+ }
445
+
446
+ {
447
+ const source = `var x: i32 = 42
448
+ var name: string = "hello"
449
+ if x == 42 {
450
+ println(name)
451
+ }`;
452
+ const r = lex(source);
453
+ console.assert(r.errors.length === 0, `에러 없어야 함`);
454
+ console.log(" ✅ var + if + println 통합 통과");
455
+ }
456
+
457
+ {
458
+ const source = `for item in items {
459
+ match item {
460
+ 0 => println("zero")
461
+ _ => println("other")
462
+ }
463
+ }`;
464
+ const r = lex(source);
465
+ console.assert(r.errors.length === 0, `에러 없어야 함`);
466
+ // for, item, in, items, {, match, item, {, 0, =>, ...
467
+ const t = r.tokens;
468
+ console.assert(t[0].type === TokenType.FOR);
469
+ console.assert(t[2].type === TokenType.IN);
470
+ console.assert(t[5].type === TokenType.MATCH);
471
+ console.log(" ✅ for + match 통합 통과");
472
+ }
473
+
474
+ // ============================================================
475
+ // 15. EOF
476
+ // ============================================================
477
+
478
+ console.log("=== EOF 테스트 ===");
479
+
480
+ {
481
+ const r = lex("");
482
+ console.assert(r.tokens.length === 1);
483
+ console.assert(r.tokens[0].type === TokenType.EOF);
484
+ console.log(" ✅ 빈 소스 → EOF 통과");
485
+ }
486
+
487
+ {
488
+ const r = lex(" \n\n\t ");
489
+ console.assert(r.tokens.length === 1);
490
+ console.assert(r.tokens[0].type === TokenType.EOF);
491
+ console.log(" ✅ 공백만 있는 소스 → EOF 통과");
492
+ }
493
+
494
+ // ============================================================
495
+ // 결과
496
+ // ============================================================
497
+
498
+ console.log("\n=== 전체 결과 ===");
499
+ console.log(" Lexer 테스트 완료");