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
package/src/ir-gen.ts ADDED
@@ -0,0 +1,459 @@
1
+ // FreeLang v4 — IR Generator
2
+ // AST → IrProgram 변환
3
+
4
+ import { Program, Stmt, Expr, MatchArm } from "./ast";
5
+ import { IrValue, IrInst, IrFunction, IrProgram } from "./ir";
6
+
7
+ export class IRGen {
8
+ private tempCount = 0;
9
+ private labelCount = 0;
10
+ private insts: IrInst[] = []; // 현재 생성 중인 명령어
11
+ private functions: IrFunction[] = [];
12
+
13
+ private newTemp(): string {
14
+ return `t${this.tempCount++}`;
15
+ }
16
+
17
+ private newLabel(): string {
18
+ return `L${this.labelCount++}`;
19
+ }
20
+
21
+ private emit(inst: IrInst): void {
22
+ this.insts.push(inst);
23
+ }
24
+
25
+ // ============================================================
26
+ // 메인 진입점
27
+ // ============================================================
28
+
29
+ generate(prog: Program): IrProgram {
30
+ const savedInsts = this.insts;
31
+ this.insts = [];
32
+
33
+ for (const stmt of prog.stmts) {
34
+ if (stmt.kind === "fn_decl") {
35
+ this.genFnDecl(stmt);
36
+ } else {
37
+ this.genStmt(stmt);
38
+ }
39
+ }
40
+
41
+ const main = this.insts;
42
+ this.insts = savedInsts;
43
+
44
+ return {
45
+ functions: this.functions,
46
+ main,
47
+ };
48
+ }
49
+
50
+ // ============================================================
51
+ // 문 생성
52
+ // ============================================================
53
+
54
+ private genStmt(stmt: Stmt): void {
55
+ switch (stmt.kind) {
56
+ case "var_decl": {
57
+ const val = this.genExpr(stmt.init);
58
+ this.emit({ kind: "assign", dest: stmt.name, src: val });
59
+ break;
60
+ }
61
+
62
+ case "if_stmt": {
63
+ const cond = this.genExpr(stmt.condition);
64
+ const elseLabel = this.newLabel();
65
+ const endLabel = this.newLabel();
66
+
67
+ this.emit({ kind: "jump_if_false", cond, target: elseLabel });
68
+
69
+ for (const s of stmt.then) {
70
+ this.genStmt(s);
71
+ }
72
+ this.emit({ kind: "jump", target: endLabel });
73
+
74
+ this.emit({ kind: "label", name: elseLabel });
75
+ if (stmt.else_) {
76
+ for (const s of stmt.else_) {
77
+ this.genStmt(s);
78
+ }
79
+ }
80
+
81
+ this.emit({ kind: "label", name: endLabel });
82
+ break;
83
+ }
84
+
85
+ case "while_stmt": {
86
+ const loopLabel = this.newLabel();
87
+ const endLabel = this.newLabel();
88
+
89
+ this.emit({ kind: "label", name: loopLabel });
90
+ const cond = this.genExpr(stmt.condition);
91
+ this.emit({ kind: "jump_if_false", cond, target: endLabel });
92
+
93
+ for (const s of stmt.body) {
94
+ this.genStmt(s);
95
+ }
96
+ this.emit({ kind: "jump", target: loopLabel });
97
+
98
+ this.emit({ kind: "label", name: endLabel });
99
+ break;
100
+ }
101
+
102
+ case "for_stmt": {
103
+ // for i = start to end step s → var i; while i < end { body; i = i + s }
104
+ const varName = stmt.variable;
105
+ const iterable = this.genExpr(stmt.iterable); // [start, end, step]
106
+
107
+ // iterable은 보통 i32이므로 0부터 시작
108
+ this.emit({ kind: "assign", dest: varName, src: { kind: "const_i32", val: 0 } });
109
+
110
+ const loopLabel = this.newLabel();
111
+ const endLabel = this.newLabel();
112
+
113
+ this.emit({ kind: "label", name: loopLabel });
114
+ const cond: IrValue = { kind: "local", name: varName };
115
+ this.emit({ kind: "jump_if_false", cond, target: endLabel });
116
+
117
+ for (const s of stmt.body) {
118
+ this.genStmt(s);
119
+ }
120
+
121
+ // i = i + 1
122
+ const nextTemp = this.newTemp();
123
+ this.emit({
124
+ kind: "binop",
125
+ dest: nextTemp,
126
+ op: "+",
127
+ left: cond,
128
+ right: { kind: "const_i32", val: 1 },
129
+ });
130
+ this.emit({ kind: "assign", dest: varName, src: { kind: "local", name: nextTemp } });
131
+
132
+ this.emit({ kind: "jump", target: loopLabel });
133
+ this.emit({ kind: "label", name: endLabel });
134
+ break;
135
+ }
136
+
137
+ case "for_of_stmt": {
138
+ // for x in arr → while iter < length(arr) { x = arr[iter]; body; iter++ }
139
+ const varName = stmt.variable;
140
+ const iterExpr = this.genExpr(stmt.iterable);
141
+ const iterVar = this.newTemp();
142
+
143
+ this.emit({ kind: "assign", dest: iterVar, src: { kind: "const_i32", val: 0 } });
144
+
145
+ const loopLabel = this.newLabel();
146
+ const endLabel = this.newLabel();
147
+
148
+ this.emit({ kind: "label", name: loopLabel });
149
+ const iterVal: IrValue = { kind: "local", name: iterVar };
150
+
151
+ // array_get 명령어 발행 후 결과를 itemTemp에 저장
152
+ const itemTemp = this.newTemp();
153
+ this.emit({
154
+ kind: "array_get",
155
+ dest: itemTemp,
156
+ arr: iterExpr,
157
+ idx: iterVal,
158
+ });
159
+
160
+ // 루프 변수에 할당
161
+ this.emit({ kind: "assign", dest: varName, src: { kind: "local", name: itemTemp } });
162
+
163
+ for (const s of stmt.body) {
164
+ this.genStmt(s);
165
+ }
166
+
167
+ // 반복 변수 증가
168
+ const nextTemp = this.newTemp();
169
+ this.emit({
170
+ kind: "binop",
171
+ dest: nextTemp,
172
+ op: "+",
173
+ left: iterVal,
174
+ right: { kind: "const_i32", val: 1 },
175
+ });
176
+ this.emit({ kind: "assign", dest: iterVar, src: { kind: "local", name: nextTemp } });
177
+
178
+ this.emit({ kind: "jump", target: loopLabel });
179
+ this.emit({ kind: "label", name: endLabel });
180
+ break;
181
+ }
182
+
183
+ case "return_stmt": {
184
+ const val = stmt.value ? this.genExpr(stmt.value) : null;
185
+ this.emit({ kind: "return", value: val });
186
+ break;
187
+ }
188
+
189
+ case "expr_stmt": {
190
+ this.genExpr(stmt.expr);
191
+ break;
192
+ }
193
+
194
+ case "spawn_stmt": {
195
+ // TODO: spawn 처리 (현재 미지원)
196
+ break;
197
+ }
198
+
199
+ // 타입 선언, match, break, continue는 생략
200
+ default:
201
+ break;
202
+ }
203
+ }
204
+
205
+ private genFnDecl(stmt: Stmt & { kind: "fn_decl" }): void {
206
+ const savedInsts = this.insts;
207
+ this.insts = [];
208
+
209
+ const params = stmt.params.map((p) => p.name);
210
+
211
+ for (const s of stmt.body) {
212
+ this.genStmt(s);
213
+ }
214
+
215
+ // 함수의 끝에 return void 추가 (명시적 반환 없으면)
216
+ const lastInst = this.insts[this.insts.length - 1];
217
+ if (!lastInst || lastInst.kind !== "return") {
218
+ this.emit({ kind: "return", value: null });
219
+ }
220
+
221
+ this.functions.push({
222
+ name: stmt.name,
223
+ params,
224
+ insts: this.insts,
225
+ });
226
+
227
+ this.insts = savedInsts;
228
+ }
229
+
230
+ // ============================================================
231
+ // 식 생성
232
+ // ============================================================
233
+
234
+ private genExpr(expr: Expr): IrValue {
235
+ switch (expr.kind) {
236
+ case "int_lit":
237
+ return { kind: "const_i32", val: expr.value };
238
+
239
+ case "float_lit":
240
+ return { kind: "const_f64", val: expr.value };
241
+
242
+ case "string_lit":
243
+ return { kind: "const_str", val: expr.value };
244
+
245
+ case "bool_lit":
246
+ return { kind: "const_bool", val: expr.value };
247
+
248
+ case "ident":
249
+ return { kind: "local", name: expr.name };
250
+
251
+ case "binary": {
252
+ const left = this.genExpr(expr.left);
253
+ const right = this.genExpr(expr.right);
254
+ const dest = this.newTemp();
255
+ this.emit({
256
+ kind: "binop",
257
+ dest,
258
+ op: expr.op,
259
+ left,
260
+ right,
261
+ });
262
+ return { kind: "local", name: dest };
263
+ }
264
+
265
+ case "unary": {
266
+ const src = this.genExpr(expr.operand);
267
+ const dest = this.newTemp();
268
+ this.emit({
269
+ kind: "unop",
270
+ dest,
271
+ op: expr.op,
272
+ src,
273
+ });
274
+ return { kind: "local", name: dest };
275
+ }
276
+
277
+ case "call": {
278
+ const args = expr.args.map((a) => this.genExpr(a));
279
+ const dest = this.newTemp();
280
+
281
+ if (expr.callee.kind === "ident") {
282
+ const name = expr.callee.name;
283
+ const builtins = [
284
+ "println", "print", "read_line", "read_file", "write_file",
285
+ "i32", "i64", "f64", "str",
286
+ "push", "pop", "slice", "clone", "length",
287
+ "char_at", "contains", "split", "trim", "to_upper", "to_lower",
288
+ "abs", "min", "max", "pow", "sqrt",
289
+ "range", "channel", "panic", "typeof", "assert",
290
+ // Phase 7: 20 Core Libraries
291
+ "md5", "sha256", "sha512", "base64_encode", "base64_decode", "hmac",
292
+ "json_parse", "json_stringify", "json_validate", "json_pretty",
293
+ "starts_with", "ends_with", "replace",
294
+ "reverse", "sort", "unique",
295
+ "gcd", "lcm",
296
+ "uuid", "timestamp",
297
+ "send", "recv",
298
+ ];
299
+
300
+ if (builtins.includes(name)) {
301
+ this.emit({
302
+ kind: "call_builtin",
303
+ dest,
304
+ name,
305
+ args,
306
+ });
307
+ } else {
308
+ this.emit({
309
+ kind: "call",
310
+ dest,
311
+ fn: name,
312
+ args,
313
+ });
314
+ }
315
+ } else {
316
+ // 복잡한 callee는 일단 지원 안 함
317
+ }
318
+
319
+ return { kind: "local", name: dest };
320
+ }
321
+
322
+ case "index": {
323
+ const arr = this.genExpr(expr.object);
324
+ const idx = this.genExpr(expr.index);
325
+ const dest = this.newTemp();
326
+ this.emit({
327
+ kind: "array_get",
328
+ dest,
329
+ arr,
330
+ idx,
331
+ });
332
+ return { kind: "local", name: dest };
333
+ }
334
+
335
+ case "field_access": {
336
+ const obj = this.genExpr(expr.object);
337
+ const dest = this.newTemp();
338
+ this.emit({
339
+ kind: "struct_get",
340
+ dest,
341
+ obj,
342
+ field: expr.field,
343
+ });
344
+ return { kind: "local", name: dest };
345
+ }
346
+
347
+ case "assign": {
348
+ const val = this.genExpr(expr.value);
349
+ if (expr.target.kind === "ident") {
350
+ this.emit({
351
+ kind: "assign",
352
+ dest: expr.target.name,
353
+ src: val,
354
+ });
355
+ return val;
356
+ } else if (expr.target.kind === "index") {
357
+ const arr = this.genExpr(expr.target.object);
358
+ const idx = this.genExpr(expr.target.index);
359
+ this.emit({
360
+ kind: "array_set",
361
+ arr,
362
+ idx,
363
+ value: val,
364
+ });
365
+ return val;
366
+ } else if (expr.target.kind === "field_access") {
367
+ const obj = this.genExpr(expr.target.object);
368
+ this.emit({
369
+ kind: "struct_set",
370
+ obj,
371
+ field: expr.target.field,
372
+ value: val,
373
+ });
374
+ return val;
375
+ }
376
+ return { kind: "const_i32", val: 0 };
377
+ }
378
+
379
+ case "array_lit": {
380
+ const elements = expr.elements.map((e) => this.genExpr(e));
381
+ const dest = this.newTemp();
382
+ this.emit({
383
+ kind: "array_new",
384
+ dest,
385
+ elements,
386
+ });
387
+ return { kind: "local", name: dest };
388
+ }
389
+
390
+ case "struct_lit": {
391
+ const fields = expr.fields.map((f) => ({
392
+ name: f.name,
393
+ value: this.genExpr(f.value),
394
+ }));
395
+ const dest = this.newTemp();
396
+ this.emit({
397
+ kind: "struct_new",
398
+ dest,
399
+ sname: expr.structName,
400
+ fields,
401
+ });
402
+ return { kind: "local", name: dest };
403
+ }
404
+
405
+ case "if_expr": {
406
+ const cond = this.genExpr(expr.condition);
407
+ const thenLabel = this.newLabel();
408
+ const elseLabel = this.newLabel();
409
+ const endLabel = this.newLabel();
410
+ const resultDest = this.newTemp();
411
+
412
+ this.emit({ kind: "jump_if_false", cond, target: elseLabel });
413
+
414
+ this.emit({ kind: "label", name: thenLabel });
415
+ let thenVal: IrValue = { kind: "const_i32", val: 0 };
416
+ for (let i = 0; i < expr.then.length; i++) {
417
+ if (i === expr.then.length - 1) {
418
+ // 마지막 식
419
+ thenVal = this.genExpr(expr.then[i] as any);
420
+ } else {
421
+ // 문
422
+ this.genStmt(expr.then[i] as any);
423
+ }
424
+ }
425
+ this.emit({ kind: "assign", dest: resultDest, src: thenVal });
426
+ this.emit({ kind: "jump", target: endLabel });
427
+
428
+ this.emit({ kind: "label", name: elseLabel });
429
+ let elseVal: IrValue = { kind: "const_i32", val: 0 };
430
+ for (let i = 0; i < expr.else_.length; i++) {
431
+ if (i === expr.else_.length - 1) {
432
+ elseVal = this.genExpr(expr.else_[i] as any);
433
+ } else {
434
+ this.genStmt(expr.else_[i] as any);
435
+ }
436
+ }
437
+ this.emit({ kind: "assign", dest: resultDest, src: elseVal });
438
+
439
+ this.emit({ kind: "label", name: endLabel });
440
+ return { kind: "local", name: resultDest };
441
+ }
442
+
443
+ case "block_expr": {
444
+ let result: IrValue = { kind: "const_i32", val: 0 };
445
+ for (const s of expr.stmts) {
446
+ this.genStmt(s);
447
+ }
448
+ if (expr.expr) {
449
+ result = this.genExpr(expr.expr);
450
+ }
451
+ return result;
452
+ }
453
+
454
+ // match_expr, fn_lit, try 등은 일단 미지원
455
+ default:
456
+ return { kind: "const_i32", val: 0 };
457
+ }
458
+ }
459
+ }
package/src/ir.ts ADDED
@@ -0,0 +1,80 @@
1
+ // FreeLang v4 — IR (Intermediate Representation)
2
+ // Three-Address Code 형태의 중간 언어
3
+
4
+ // ============================================================
5
+ // IrValue — 피연산자
6
+ // ============================================================
7
+
8
+ export type IrValue =
9
+ | { kind: "temp"; name: string } // t0, t1, t2... (임시 변수)
10
+ | { kind: "local"; name: string } // 함수 로컬 변수
11
+ | { kind: "global"; name: string } // 글로벌 변수
12
+ | { kind: "const_i32"; val: number } // 정수 상수
13
+ | { kind: "const_f64"; val: number } // 실수 상수
14
+ | { kind: "const_str"; val: string } // 문자열 상수
15
+ | { kind: "const_bool"; val: boolean }; // bool 상수
16
+
17
+ // ============================================================
18
+ // IrInst — 명령어 (Three-Address Code)
19
+ // ============================================================
20
+
21
+ export type IrInst =
22
+ // 할당
23
+ | { kind: "assign"; dest: string; src: IrValue }
24
+
25
+ // 이항 연산
26
+ | { kind: "binop"; dest: string; op: string; left: IrValue; right: IrValue }
27
+
28
+ // 단항 연산
29
+ | { kind: "unop"; dest: string; op: string; src: IrValue }
30
+
31
+ // 제어 흐름
32
+ | { kind: "label"; name: string }
33
+ | { kind: "jump"; target: string }
34
+ | { kind: "jump_if_false"; cond: IrValue; target: string }
35
+
36
+ // 함수 호출
37
+ | { kind: "call"; dest: string | null; fn: string; args: IrValue[] }
38
+ | { kind: "call_builtin"; dest: string | null; name: string; args: IrValue[] }
39
+
40
+ // 반환
41
+ | { kind: "return"; value: IrValue | null }
42
+
43
+ // 배열 연산
44
+ | { kind: "array_new"; dest: string; elements: IrValue[] }
45
+ | { kind: "array_get"; dest: string; arr: IrValue; idx: IrValue }
46
+ | { kind: "array_set"; arr: IrValue; idx: IrValue; value: IrValue }
47
+
48
+ // 구조체 연산
49
+ | {
50
+ kind: "struct_new";
51
+ dest: string;
52
+ sname: string;
53
+ fields: { name: string; value: IrValue }[];
54
+ }
55
+ | { kind: "struct_get"; dest: string; obj: IrValue; field: string }
56
+ | { kind: "struct_set"; obj: IrValue; field: string; value: IrValue }
57
+
58
+ // Option/Result
59
+ | { kind: "wrap_ok"; dest: string; value: IrValue }
60
+ | { kind: "wrap_err"; dest: string; value: IrValue }
61
+ | { kind: "unwrap"; dest: string; value: IrValue };
62
+
63
+ // ============================================================
64
+ // IrFunction — 함수 표현
65
+ // ============================================================
66
+
67
+ export type IrFunction = {
68
+ name: string;
69
+ params: string[]; // 매개변수명들
70
+ insts: IrInst[]; // BasicBlock은 label/jump로 구분
71
+ };
72
+
73
+ // ============================================================
74
+ // IrProgram — 최상위 프로그램
75
+ // ============================================================
76
+
77
+ export type IrProgram = {
78
+ functions: IrFunction[];
79
+ main: IrInst[]; // 최상위 코드 (main 함수 외)
80
+ };