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.
- package/README.md +548 -0
- package/dist/ast.d.ts +367 -0
- package/dist/ast.js +4 -0
- package/dist/ast.js.map +1 -0
- package/dist/async-basic.test.d.ts +1 -0
- package/dist/async-basic.test.js +88 -0
- package/dist/async-basic.test.js.map +1 -0
- package/dist/async-jest.test.d.ts +1 -0
- package/dist/async-jest.test.js +99 -0
- package/dist/async-jest.test.js.map +1 -0
- package/dist/channel-jest.test.d.ts +1 -0
- package/dist/channel-jest.test.js +148 -0
- package/dist/channel-jest.test.js.map +1 -0
- package/dist/checker-jest.test.d.ts +1 -0
- package/dist/checker-jest.test.js +160 -0
- package/dist/checker-jest.test.js.map +1 -0
- package/dist/checker.d.ts +149 -0
- package/dist/checker.js +1565 -0
- package/dist/checker.js.map +1 -0
- package/dist/checker.test.d.ts +1 -0
- package/dist/checker.test.js +217 -0
- package/dist/checker.test.js.map +1 -0
- package/dist/compiler-jest.test.d.ts +1 -0
- package/dist/compiler-jest.test.js +233 -0
- package/dist/compiler-jest.test.js.map +1 -0
- package/dist/compiler.d.ts +127 -0
- package/dist/compiler.js +1588 -0
- package/dist/compiler.js.map +1 -0
- package/dist/compiler.test.d.ts +1 -0
- package/dist/compiler.test.js +313 -0
- package/dist/compiler.test.js.map +1 -0
- package/dist/db-100m-full.d.ts +5 -0
- package/dist/db-100m-full.js +78 -0
- package/dist/db-100m-full.js.map +1 -0
- package/dist/db-100m-no-index.d.ts +12 -0
- package/dist/db-100m-no-index.js +119 -0
- package/dist/db-100m-no-index.js.map +1 -0
- package/dist/db-100m-real.d.ts +5 -0
- package/dist/db-100m-real.js +131 -0
- package/dist/db-100m-real.js.map +1 -0
- package/dist/db-100m-streaming.d.ts +15 -0
- package/dist/db-100m-streaming.js +164 -0
- package/dist/db-100m-streaming.js.map +1 -0
- package/dist/db-100m-test.d.ts +5 -0
- package/dist/db-100m-test.js +111 -0
- package/dist/db-100m-test.js.map +1 -0
- package/dist/db-jest.test.d.ts +1 -0
- package/dist/db-jest.test.js +182 -0
- package/dist/db-jest.test.js.map +1 -0
- package/dist/db-runtime.d.ts +24 -0
- package/dist/db-runtime.js +204 -0
- package/dist/db-runtime.js.map +1 -0
- package/dist/db.d.ts +249 -0
- package/dist/db.js +593 -0
- package/dist/db.js.map +1 -0
- package/dist/file-io-jest.test.d.ts +1 -0
- package/dist/file-io-jest.test.js +225 -0
- package/dist/file-io-jest.test.js.map +1 -0
- package/dist/for-of-jest.test.d.ts +1 -0
- package/dist/for-of-jest.test.js +230 -0
- package/dist/for-of-jest.test.js.map +1 -0
- package/dist/for-of.test.d.ts +1 -0
- package/dist/for-of.test.js +305 -0
- package/dist/for-of.test.js.map +1 -0
- package/dist/function-literal-jest.test.d.ts +1 -0
- package/dist/function-literal-jest.test.js +180 -0
- package/dist/function-literal-jest.test.js.map +1 -0
- package/dist/function-literal.test.d.ts +1 -0
- package/dist/function-literal.test.js +245 -0
- package/dist/function-literal.test.js.map +1 -0
- package/dist/generics-jest.test.d.ts +1 -0
- package/dist/generics-jest.test.js +93 -0
- package/dist/generics-jest.test.js.map +1 -0
- package/dist/ir-gen.d.ts +15 -0
- package/dist/ir-gen.js +400 -0
- package/dist/ir-gen.js.map +1 -0
- package/dist/ir.d.ts +114 -0
- package/dist/ir.js +5 -0
- package/dist/ir.js.map +1 -0
- package/dist/lexer.d.ts +110 -0
- package/dist/lexer.js +467 -0
- package/dist/lexer.js.map +1 -0
- package/dist/lexer.test.d.ts +1 -0
- package/dist/lexer.test.js +426 -0
- package/dist/lexer.test.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +241 -0
- package/dist/main.js.map +1 -0
- package/dist/module-jest.test.d.ts +1 -0
- package/dist/module-jest.test.js +123 -0
- package/dist/module-jest.test.js.map +1 -0
- package/dist/parser.d.ts +56 -0
- package/dist/parser.js +1060 -0
- package/dist/parser.js.map +1 -0
- package/dist/parser.test.d.ts +1 -0
- package/dist/parser.test.js +461 -0
- package/dist/parser.test.js.map +1 -0
- package/dist/pattern-matching-jest.test.d.ts +1 -0
- package/dist/pattern-matching-jest.test.js +158 -0
- package/dist/pattern-matching-jest.test.js.map +1 -0
- package/dist/pkg/init.d.ts +1 -0
- package/dist/pkg/init.js +118 -0
- package/dist/pkg/init.js.map +1 -0
- package/dist/pkg/install.d.ts +1 -0
- package/dist/pkg/install.js +77 -0
- package/dist/pkg/install.js.map +1 -0
- package/dist/pkg/registry.d.ts +23 -0
- package/dist/pkg/registry.js +106 -0
- package/dist/pkg/registry.js.map +1 -0
- package/dist/pkg/run.d.ts +1 -0
- package/dist/pkg/run.js +76 -0
- package/dist/pkg/run.js.map +1 -0
- package/dist/pkg/toml.d.ts +5 -0
- package/dist/pkg/toml.js +117 -0
- package/dist/pkg/toml.js.map +1 -0
- package/dist/repl.d.ts +15 -0
- package/dist/repl.js +197 -0
- package/dist/repl.js.map +1 -0
- package/dist/runtime/bytecode.d.ts +92 -0
- package/dist/runtime/bytecode.js +253 -0
- package/dist/runtime/bytecode.js.map +1 -0
- package/dist/runtime/value.d.ts +102 -0
- package/dist/runtime/value.js +302 -0
- package/dist/runtime/value.js.map +1 -0
- package/dist/runtime/vm.d.ts +65 -0
- package/dist/runtime/vm.js +293 -0
- package/dist/runtime/vm.js.map +1 -0
- package/dist/struct-instance-jest.test.d.ts +1 -0
- package/dist/struct-instance-jest.test.js +209 -0
- package/dist/struct-instance-jest.test.js.map +1 -0
- package/dist/struct-instance.test.d.ts +1 -0
- package/dist/struct-instance.test.js +291 -0
- package/dist/struct-instance.test.js.map +1 -0
- package/dist/struct-jest.test.d.ts +1 -0
- package/dist/struct-jest.test.js +176 -0
- package/dist/struct-jest.test.js.map +1 -0
- package/dist/struct.test.d.ts +1 -0
- package/dist/struct.test.js +231 -0
- package/dist/struct.test.js.map +1 -0
- package/dist/trait-jest.test.d.ts +1 -0
- package/dist/trait-jest.test.js +120 -0
- package/dist/trait-jest.test.js.map +1 -0
- package/dist/vm-jest.test.d.ts +1 -0
- package/dist/vm-jest.test.js +569 -0
- package/dist/vm-jest.test.js.map +1 -0
- package/dist/vm.d.ts +81 -0
- package/dist/vm.js +1956 -0
- package/dist/vm.js.map +1 -0
- package/dist/vm.test.d.ts +1 -0
- package/dist/vm.test.js +337 -0
- package/dist/vm.test.js.map +1 -0
- package/dist/web-repl/sandbox.d.ts +11 -0
- package/dist/web-repl/sandbox.js +76 -0
- package/dist/web-repl/sandbox.js.map +1 -0
- package/dist/web-repl/server.d.ts +1 -0
- package/dist/web-repl/server.js +111 -0
- package/dist/web-repl/server.js.map +1 -0
- package/dist/while-loop-jest.test.d.ts +1 -0
- package/dist/while-loop-jest.test.js +201 -0
- package/dist/while-loop-jest.test.js.map +1 -0
- package/dist/while-loop.test.d.ts +1 -0
- package/dist/while-loop.test.js +262 -0
- package/dist/while-loop.test.js.map +1 -0
- package/docs/EXPERIENCE.md +787 -0
- package/docs/README.md +175 -0
- package/docs/V1_V2_V3_ANALYSIS.md +107 -0
- package/docs/_config.yml +36 -0
- package/docs/api-reference.md +459 -0
- package/docs/architecture.md +470 -0
- package/docs/benchmarks.md +295 -0
- package/docs/comparison.md +454 -0
- package/docs/index.md +335 -0
- package/docs/language-completeness.md +228 -0
- package/docs/learning-guide.md +651 -0
- package/package.json +65 -0
- package/src/api/deploy_key.fl +294 -0
- package/src/api/issue.fl +302 -0
- package/src/api/org.fl +356 -0
- package/src/api/repo.fl +394 -0
- package/src/api/team.fl +299 -0
- package/src/api/user.fl +385 -0
- package/src/api/webhook.fl +273 -0
- package/src/ast.ts +158 -0
- package/src/async-basic.test.ts +94 -0
- package/src/async-jest.test.ts +107 -0
- package/src/channel-jest.test.ts +158 -0
- package/src/checker-jest.test.ts +189 -0
- package/src/checker.test.ts +279 -0
- package/src/checker.ts +1861 -0
- package/src/commands/analyze.fl +227 -0
- package/src/commands/auth.fl +315 -0
- package/src/commands/batch.fl +349 -0
- package/src/commands/config.fl +199 -0
- package/src/commands/deploy_key.fl +352 -0
- package/src/commands/issue.fl +275 -0
- package/src/commands/main.fl +492 -0
- package/src/commands/org.fl +425 -0
- package/src/commands/repo.fl +581 -0
- package/src/commands/team.fl +244 -0
- package/src/commands/user.fl +423 -0
- package/src/commands/webhook.fl +400 -0
- package/src/compiler-jest.test.ts +275 -0
- package/src/compiler.test.ts +375 -0
- package/src/compiler.ts +1770 -0
- package/src/config.fl +175 -0
- package/src/core/batch.fl +355 -0
- package/src/core/cache.fl +284 -0
- package/src/core/ensure.fl +324 -0
- package/src/db-100m-full.ts +96 -0
- package/src/db-100m-no-index.ts +133 -0
- package/src/db-100m-real.ts +152 -0
- package/src/db-100m-streaming.ts +154 -0
- package/src/db-100m-test.ts +136 -0
- package/src/db-jest.test.ts +161 -0
- package/src/db-runtime.ts +242 -0
- package/src/db.ts +676 -0
- package/src/errors.fl +134 -0
- package/src/for-of-jest.test.ts +246 -0
- package/src/for-of.test.ts +308 -0
- package/src/function-literal-jest.test.ts +193 -0
- package/src/function-literal.test.ts +248 -0
- package/src/generics-jest.test.ts +104 -0
- package/src/http/client.fl +327 -0
- package/src/ir-gen.ts +459 -0
- package/src/ir.ts +80 -0
- package/src/lexer.test.ts +499 -0
- package/src/lexer.ts +522 -0
- package/src/main.ts +223 -0
- package/src/models.fl +162 -0
- package/src/module-jest.test.ts +145 -0
- package/src/parser.test.ts +542 -0
- package/src/parser.ts +1211 -0
- package/src/pattern-matching-jest.test.ts +170 -0
- package/src/pkg/init.ts +91 -0
- package/src/pkg/install.ts +56 -0
- package/src/pkg/registry.ts +103 -0
- package/src/pkg/run.ts +49 -0
- package/src/pkg/toml.ts +129 -0
- package/src/repl.ts +190 -0
- package/src/runtime/bytecode.ts +291 -0
- package/src/runtime/value.ts +322 -0
- package/src/runtime/vm.ts +354 -0
- package/src/self-host/bootstrap.fl +68 -0
- package/src/self-host/interpreter.fl +361 -0
- package/src/self-host/lexer-simple.fl +22 -0
- package/src/self-host/lexer.fl +305 -0
- package/src/self-host/parser.fl +580 -0
- package/src/struct-instance-jest.test.ts +221 -0
- package/src/struct-instance.test.ts +293 -0
- package/src/struct-jest.test.ts +187 -0
- package/src/struct.test.ts +234 -0
- package/src/trait-jest.test.ts +136 -0
- package/src/vm-jest.test.ts +754 -0
- package/src/vm.ts +1976 -0
- package/src/web-repl/public/index.html +50 -0
- package/src/web-repl/public/main.js +105 -0
- package/src/web-repl/public/style.css +225 -0
- package/src/web-repl/sandbox.ts +88 -0
- package/src/web-repl/server.ts +97 -0
- package/src/while-loop-jest.test.ts +218 -0
- package/src/while-loop.test.ts +267 -0
package/src/repl.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// FreeLang v4.1 — REPL (Read-Eval-Print Loop)
|
|
2
|
+
// 대화형 쉘 구현
|
|
3
|
+
|
|
4
|
+
import * as readline from "readline";
|
|
5
|
+
import { Lexer } from "./lexer";
|
|
6
|
+
import { Parser } from "./parser";
|
|
7
|
+
import { TypeChecker } from "./checker";
|
|
8
|
+
import { Compiler } from "./compiler";
|
|
9
|
+
import { VM } from "./vm";
|
|
10
|
+
|
|
11
|
+
export class REPL {
|
|
12
|
+
private rl: readline.Interface;
|
|
13
|
+
private vm: VM;
|
|
14
|
+
private globals: Map<string, any> = new Map();
|
|
15
|
+
private history: string[] = [];
|
|
16
|
+
private multiLineBuffer: string = "";
|
|
17
|
+
private isMultiLine: boolean = false;
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this.rl = readline.createInterface({
|
|
21
|
+
input: process.stdin,
|
|
22
|
+
output: process.stdout,
|
|
23
|
+
terminal: process.stdout.isTTY !== false,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
this.vm = new VM();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async start(): Promise<void> {
|
|
30
|
+
console.log("🦁 FreeLang v4.1 REPL");
|
|
31
|
+
console.log('입력: "help" (도움말), "exit" (종료), "history" (이력)\n');
|
|
32
|
+
|
|
33
|
+
while (true) {
|
|
34
|
+
try {
|
|
35
|
+
const line = await this.readline(this.isMultiLine ? "... " : "> ");
|
|
36
|
+
|
|
37
|
+
if (!line) continue;
|
|
38
|
+
|
|
39
|
+
// 특수 명령어 처리
|
|
40
|
+
if (this.handleCommand(line)) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 멀티 라인 처리
|
|
45
|
+
if (line.endsWith("\\")) {
|
|
46
|
+
this.multiLineBuffer += line.slice(0, -1) + "\n";
|
|
47
|
+
this.isMultiLine = true;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const fullCode = this.multiLineBuffer + line;
|
|
52
|
+
this.multiLineBuffer = "";
|
|
53
|
+
this.isMultiLine = false;
|
|
54
|
+
|
|
55
|
+
// 실행
|
|
56
|
+
await this.eval(fullCode);
|
|
57
|
+
this.history.push(fullCode);
|
|
58
|
+
} catch (e: any) {
|
|
59
|
+
console.error(`❌ ${e.message}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private handleCommand(input: string): boolean {
|
|
65
|
+
const trimmed = input.trim();
|
|
66
|
+
|
|
67
|
+
switch (trimmed) {
|
|
68
|
+
case "exit":
|
|
69
|
+
case "quit":
|
|
70
|
+
console.log("👋 Bye!");
|
|
71
|
+
process.exit(0);
|
|
72
|
+
|
|
73
|
+
case "help":
|
|
74
|
+
this.printHelp();
|
|
75
|
+
return true;
|
|
76
|
+
|
|
77
|
+
case "history":
|
|
78
|
+
this.printHistory();
|
|
79
|
+
return true;
|
|
80
|
+
|
|
81
|
+
case "clear":
|
|
82
|
+
console.clear();
|
|
83
|
+
return true;
|
|
84
|
+
|
|
85
|
+
default:
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private async eval(code: string): Promise<void> {
|
|
91
|
+
try {
|
|
92
|
+
// 1. Lexer
|
|
93
|
+
const lexer = new Lexer(code);
|
|
94
|
+
const { tokens, errors: lexErrors } = lexer.tokenize();
|
|
95
|
+
|
|
96
|
+
if (lexErrors.length > 0) {
|
|
97
|
+
console.error(`❌ Lex error: ${lexErrors[0].message}`);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 2. Parser
|
|
102
|
+
const parser = new Parser(tokens);
|
|
103
|
+
const { program, errors: parseErrors } = parser.parse();
|
|
104
|
+
|
|
105
|
+
if (parseErrors.length > 0) {
|
|
106
|
+
console.error(`❌ Parse error: ${parseErrors[0].message}`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 3. Type Checker
|
|
111
|
+
const checker = new TypeChecker();
|
|
112
|
+
const checkErrors = checker.check(program);
|
|
113
|
+
|
|
114
|
+
if (checkErrors.length > 0) {
|
|
115
|
+
console.error(`❌ Type error: ${checkErrors[0].message}`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 4. Compiler
|
|
120
|
+
const compiler = new Compiler();
|
|
121
|
+
const chunk = compiler.compile(program);
|
|
122
|
+
|
|
123
|
+
// 5. VM Execution
|
|
124
|
+
const { output, error } = await this.vm.run(chunk);
|
|
125
|
+
|
|
126
|
+
if (error) {
|
|
127
|
+
console.error(`❌ Runtime error: ${error}`);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 출력
|
|
132
|
+
for (const line of output) {
|
|
133
|
+
console.log(line);
|
|
134
|
+
}
|
|
135
|
+
} catch (e: any) {
|
|
136
|
+
console.error(`❌ ${e.message}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private printHelp(): void {
|
|
141
|
+
console.log(`
|
|
142
|
+
📚 FreeLang REPL 명령어:
|
|
143
|
+
|
|
144
|
+
특수 명령:
|
|
145
|
+
exit, quit 프로그램 종료
|
|
146
|
+
help 이 도움말 표시
|
|
147
|
+
history 명령어 이력 표시
|
|
148
|
+
clear 화면 지우기
|
|
149
|
+
|
|
150
|
+
멀티라인 입력:
|
|
151
|
+
마지막에 \\ 추가하면 다음 줄에서 계속 입력 가능
|
|
152
|
+
예: var x = 10 \\
|
|
153
|
+
var y = 20 \\
|
|
154
|
+
println(x + y)
|
|
155
|
+
|
|
156
|
+
예제:
|
|
157
|
+
> var x = 10
|
|
158
|
+
> fn add(a, b) { a + b }
|
|
159
|
+
> add(x, 5)
|
|
160
|
+
15
|
|
161
|
+
`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private printHistory(): void {
|
|
165
|
+
if (this.history.length === 0) {
|
|
166
|
+
console.log("이력이 없습니다");
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
console.log("\n📜 명령어 이력:");
|
|
171
|
+
this.history.forEach((cmd, i) => {
|
|
172
|
+
console.log(`${i + 1}. ${cmd}`);
|
|
173
|
+
});
|
|
174
|
+
console.log();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private readline(prompt: string): Promise<string> {
|
|
178
|
+
return new Promise((resolve) => {
|
|
179
|
+
this.rl.question(prompt, (answer) => {
|
|
180
|
+
resolve(answer);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// REPL 실행
|
|
187
|
+
if (require.main === module) {
|
|
188
|
+
const repl = new REPL();
|
|
189
|
+
repl.start().catch(console.error);
|
|
190
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FreeLang Bytecode Definition
|
|
3
|
+
* 스택 머신의 모든 명령어를 정의
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Value } from './value';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 모든 Bytecode 연산 타입
|
|
10
|
+
*/
|
|
11
|
+
export enum OpcodeType {
|
|
12
|
+
// 스택 연산
|
|
13
|
+
PUSH = 'PUSH',
|
|
14
|
+
POP = 'POP',
|
|
15
|
+
DUP = 'DUP',
|
|
16
|
+
|
|
17
|
+
// 산술 연산
|
|
18
|
+
ADD = 'ADD',
|
|
19
|
+
SUB = 'SUB',
|
|
20
|
+
MUL = 'MUL',
|
|
21
|
+
DIV = 'DIV',
|
|
22
|
+
MOD = 'MOD',
|
|
23
|
+
|
|
24
|
+
// 논리 연산
|
|
25
|
+
AND = 'AND',
|
|
26
|
+
OR = 'OR',
|
|
27
|
+
NOT = 'NOT',
|
|
28
|
+
|
|
29
|
+
// 비교 연산
|
|
30
|
+
EQ = 'EQ',
|
|
31
|
+
NE = 'NE',
|
|
32
|
+
LT = 'LT',
|
|
33
|
+
LTE = 'LTE',
|
|
34
|
+
GT = 'GT',
|
|
35
|
+
GTE = 'GTE',
|
|
36
|
+
|
|
37
|
+
// 제어 흐름
|
|
38
|
+
JUMP = 'JUMP',
|
|
39
|
+
JUMP_IF_TRUE = 'JUMP_IF_TRUE',
|
|
40
|
+
JUMP_IF_FALSE = 'JUMP_IF_FALSE',
|
|
41
|
+
CALL = 'CALL',
|
|
42
|
+
RETURN = 'RETURN',
|
|
43
|
+
|
|
44
|
+
// 변수 접근
|
|
45
|
+
LOAD_LOCAL = 'LOAD_LOCAL',
|
|
46
|
+
STORE_LOCAL = 'STORE_LOCAL',
|
|
47
|
+
LOAD_GLOBAL = 'LOAD_GLOBAL',
|
|
48
|
+
STORE_GLOBAL = 'STORE_GLOBAL',
|
|
49
|
+
|
|
50
|
+
// 배열/객체 연산
|
|
51
|
+
LOAD_ARRAY = 'LOAD_ARRAY',
|
|
52
|
+
STORE_ARRAY = 'STORE_ARRAY',
|
|
53
|
+
LOAD_OBJECT = 'LOAD_OBJECT',
|
|
54
|
+
STORE_OBJECT = 'STORE_OBJECT',
|
|
55
|
+
|
|
56
|
+
// 타입 변환
|
|
57
|
+
TO_INT = 'TO_INT',
|
|
58
|
+
TO_FLOAT = 'TO_FLOAT',
|
|
59
|
+
TO_STRING = 'TO_STRING',
|
|
60
|
+
TO_BOOL = 'TO_BOOL',
|
|
61
|
+
|
|
62
|
+
// 함수 호출
|
|
63
|
+
CALL_BUILTIN = 'CALL_BUILTIN',
|
|
64
|
+
|
|
65
|
+
// 기타
|
|
66
|
+
PRINT = 'PRINT',
|
|
67
|
+
HALT = 'HALT',
|
|
68
|
+
NOP = 'NOP',
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 단일 Bytecode 명령어
|
|
73
|
+
*/
|
|
74
|
+
export interface Instruction {
|
|
75
|
+
op: OpcodeType;
|
|
76
|
+
arg?: any; // 명령어 인자 (값에 따라 다름)
|
|
77
|
+
line: number; // 소스 코드 라인 번호 (디버깅용)
|
|
78
|
+
column: number; // 소스 코드 열 번호 (디버깅용)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Bytecode 빌더 - 명령어를 생성하기 편하게 함
|
|
83
|
+
*/
|
|
84
|
+
export class BytecodeBuilder {
|
|
85
|
+
private instructions: Instruction[] = [];
|
|
86
|
+
private lineNo: number = 1;
|
|
87
|
+
private colNo: number = 1;
|
|
88
|
+
|
|
89
|
+
setLocation(line: number, col: number): void {
|
|
90
|
+
this.lineNo = line;
|
|
91
|
+
this.colNo = col;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
push(value: Value): this {
|
|
95
|
+
this.instructions.push({
|
|
96
|
+
op: OpcodeType.PUSH,
|
|
97
|
+
arg: value,
|
|
98
|
+
line: this.lineNo,
|
|
99
|
+
column: this.colNo,
|
|
100
|
+
});
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
pop(): this {
|
|
105
|
+
this.instructions.push({
|
|
106
|
+
op: OpcodeType.POP,
|
|
107
|
+
line: this.lineNo,
|
|
108
|
+
column: this.colNo,
|
|
109
|
+
});
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
dup(): this {
|
|
114
|
+
this.instructions.push({
|
|
115
|
+
op: OpcodeType.DUP,
|
|
116
|
+
line: this.lineNo,
|
|
117
|
+
column: this.colNo,
|
|
118
|
+
});
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
add(): this {
|
|
123
|
+
this.instructions.push({
|
|
124
|
+
op: OpcodeType.ADD,
|
|
125
|
+
line: this.lineNo,
|
|
126
|
+
column: this.colNo,
|
|
127
|
+
});
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
sub(): this {
|
|
132
|
+
this.instructions.push({
|
|
133
|
+
op: OpcodeType.SUB,
|
|
134
|
+
line: this.lineNo,
|
|
135
|
+
column: this.colNo,
|
|
136
|
+
});
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
mul(): this {
|
|
141
|
+
this.instructions.push({
|
|
142
|
+
op: OpcodeType.MUL,
|
|
143
|
+
line: this.lineNo,
|
|
144
|
+
column: this.colNo,
|
|
145
|
+
});
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
div(): this {
|
|
150
|
+
this.instructions.push({
|
|
151
|
+
op: OpcodeType.DIV,
|
|
152
|
+
line: this.lineNo,
|
|
153
|
+
column: this.colNo,
|
|
154
|
+
});
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
mod(): this {
|
|
159
|
+
this.instructions.push({
|
|
160
|
+
op: OpcodeType.MOD,
|
|
161
|
+
line: this.lineNo,
|
|
162
|
+
column: this.colNo,
|
|
163
|
+
});
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
eq(): this {
|
|
168
|
+
this.instructions.push({
|
|
169
|
+
op: OpcodeType.EQ,
|
|
170
|
+
line: this.lineNo,
|
|
171
|
+
column: this.colNo,
|
|
172
|
+
});
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
ne(): this {
|
|
177
|
+
this.instructions.push({
|
|
178
|
+
op: OpcodeType.NE,
|
|
179
|
+
line: this.lineNo,
|
|
180
|
+
column: this.colNo,
|
|
181
|
+
});
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
lt(): this {
|
|
186
|
+
this.instructions.push({
|
|
187
|
+
op: OpcodeType.LT,
|
|
188
|
+
line: this.lineNo,
|
|
189
|
+
column: this.colNo,
|
|
190
|
+
});
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
jump(target: number): this {
|
|
195
|
+
this.instructions.push({
|
|
196
|
+
op: OpcodeType.JUMP,
|
|
197
|
+
arg: target,
|
|
198
|
+
line: this.lineNo,
|
|
199
|
+
column: this.colNo,
|
|
200
|
+
});
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
jumpIfTrue(target: number): this {
|
|
205
|
+
this.instructions.push({
|
|
206
|
+
op: OpcodeType.JUMP_IF_TRUE,
|
|
207
|
+
arg: target,
|
|
208
|
+
line: this.lineNo,
|
|
209
|
+
column: this.colNo,
|
|
210
|
+
});
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
jumpIfFalse(target: number): this {
|
|
215
|
+
this.instructions.push({
|
|
216
|
+
op: OpcodeType.JUMP_IF_FALSE,
|
|
217
|
+
arg: target,
|
|
218
|
+
line: this.lineNo,
|
|
219
|
+
column: this.colNo,
|
|
220
|
+
});
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
call(name: string, arity: number): this {
|
|
225
|
+
this.instructions.push({
|
|
226
|
+
op: OpcodeType.CALL_BUILTIN,
|
|
227
|
+
arg: { name, arity },
|
|
228
|
+
line: this.lineNo,
|
|
229
|
+
column: this.colNo,
|
|
230
|
+
});
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
loadLocal(index: number): this {
|
|
235
|
+
this.instructions.push({
|
|
236
|
+
op: OpcodeType.LOAD_LOCAL,
|
|
237
|
+
arg: index,
|
|
238
|
+
line: this.lineNo,
|
|
239
|
+
column: this.colNo,
|
|
240
|
+
});
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
storeLocal(index: number): this {
|
|
245
|
+
this.instructions.push({
|
|
246
|
+
op: OpcodeType.STORE_LOCAL,
|
|
247
|
+
arg: index,
|
|
248
|
+
line: this.lineNo,
|
|
249
|
+
column: this.colNo,
|
|
250
|
+
});
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
print(): this {
|
|
255
|
+
this.instructions.push({
|
|
256
|
+
op: OpcodeType.PRINT,
|
|
257
|
+
line: this.lineNo,
|
|
258
|
+
column: this.colNo,
|
|
259
|
+
});
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
halt(): this {
|
|
264
|
+
this.instructions.push({
|
|
265
|
+
op: OpcodeType.HALT,
|
|
266
|
+
line: this.lineNo,
|
|
267
|
+
column: this.colNo,
|
|
268
|
+
});
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
build(): Instruction[] {
|
|
273
|
+
return this.instructions;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Bytecode 프린터 - 명령어를 읽기 좋게 출력
|
|
279
|
+
*/
|
|
280
|
+
export class BytecodePrinter {
|
|
281
|
+
static print(instructions: Instruction[]): string {
|
|
282
|
+
const lines: string[] = [];
|
|
283
|
+
instructions.forEach((instr, idx) => {
|
|
284
|
+
const argStr = instr.arg !== undefined ? ` ${JSON.stringify(instr.arg)}` : '';
|
|
285
|
+
lines.push(
|
|
286
|
+
`${String(idx).padStart(4, '0')}: ${instr.op.padEnd(15)}${argStr}`
|
|
287
|
+
);
|
|
288
|
+
});
|
|
289
|
+
return lines.join('\n');
|
|
290
|
+
}
|
|
291
|
+
}
|