cobolx-2 1.2.3

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 (284) hide show
  1. package/.github/pull_request_template.md +9 -0
  2. package/.github/workflows/ci.yml +18 -0
  3. package/.vscode/launch.json +19 -0
  4. package/.vscode/tasks.json +14 -0
  5. package/CHANGELOG.md +16 -0
  6. package/CONTRIBUTING.md +23 -0
  7. package/CargoX.lock +2 -0
  8. package/LICENSE +21 -0
  9. package/README.md +164 -0
  10. package/api-demo/CargoX.lock +1 -0
  11. package/api-demo/README.md +3 -0
  12. package/api-demo/benchmarks/arith.cbx +6 -0
  13. package/api-demo/cobolx.toml +6 -0
  14. package/api-demo/docs-output/index.html +1 -0
  15. package/api-demo/generated/LEGACYDEMO.cbx +5 -0
  16. package/api-demo/generated/client-types.ts +2 -0
  17. package/api-demo/generated/deploy.json +12 -0
  18. package/api-demo/generated/flowchart.mmd +4 -0
  19. package/api-demo/migrations/20260411215238_init.sql +1 -0
  20. package/api-demo/src/main.cbx +5 -0
  21. package/api-demo/tests/smoke.cbx +6 -0
  22. package/benchmarks/arithmetic.cbx +6 -0
  23. package/cargox/package.json +11 -0
  24. package/cargox/src/index.d.ts +4 -0
  25. package/cargox/src/index.js +5 -0
  26. package/cargox/src/index.js.map +1 -0
  27. package/cargox/src/index.ts +4 -0
  28. package/cargox/src/lockfile/index.d.ts +1 -0
  29. package/cargox/src/lockfile/index.js +9 -0
  30. package/cargox/src/lockfile/index.js.map +1 -0
  31. package/cargox/src/lockfile/index.ts +9 -0
  32. package/cargox/src/manifest.d.ts +10 -0
  33. package/cargox/src/manifest.js +49 -0
  34. package/cargox/src/manifest.js.map +1 -0
  35. package/cargox/src/manifest.ts +58 -0
  36. package/cargox/src/registry/index.d.ts +4 -0
  37. package/cargox/src/registry/index.js +13 -0
  38. package/cargox/src/registry/index.js.map +1 -0
  39. package/cargox/src/registry/index.ts +13 -0
  40. package/cargox/src/resolver/index.d.ts +5 -0
  41. package/cargox/src/resolver/index.js +4 -0
  42. package/cargox/src/resolver/index.js.map +1 -0
  43. package/cargox/src/resolver/index.ts +8 -0
  44. package/cargox/tsconfig.json +7 -0
  45. package/cli/cobolx-cli/package.json +23 -0
  46. package/cli/cobolx-cli/src/commands/add.ts +18 -0
  47. package/cli/cobolx-cli/src/commands/bench.ts +47 -0
  48. package/cli/cobolx-cli/src/commands/build.ts +74 -0
  49. package/cli/cobolx-cli/src/commands/check.ts +24 -0
  50. package/cli/cobolx-cli/src/commands/debug.ts +15 -0
  51. package/cli/cobolx-cli/src/commands/debug_rewind.ts +16 -0
  52. package/cli/cobolx-cli/src/commands/deploy.ts +18 -0
  53. package/cli/cobolx-cli/src/commands/dev.ts +24 -0
  54. package/cli/cobolx-cli/src/commands/doc.ts +19 -0
  55. package/cli/cobolx-cli/src/commands/fmt.ts +14 -0
  56. package/cli/cobolx-cli/src/commands/fuzz.ts +24 -0
  57. package/cli/cobolx-cli/src/commands/generate.ts +38 -0
  58. package/cli/cobolx-cli/src/commands/install.ts +25 -0
  59. package/cli/cobolx-cli/src/commands/legacy.ts +17 -0
  60. package/cli/cobolx-cli/src/commands/lint.ts +17 -0
  61. package/cli/cobolx-cli/src/commands/migrate.ts +27 -0
  62. package/cli/cobolx-cli/src/commands/new.ts +20 -0
  63. package/cli/cobolx-cli/src/commands/profile.ts +8 -0
  64. package/cli/cobolx-cli/src/commands/publish.ts +16 -0
  65. package/cli/cobolx-cli/src/commands/repl.ts +30 -0
  66. package/cli/cobolx-cli/src/commands/run.ts +22 -0
  67. package/cli/cobolx-cli/src/commands/task.ts +27 -0
  68. package/cli/cobolx-cli/src/commands/test.ts +44 -0
  69. package/cli/cobolx-cli/src/commands/update.ts +30 -0
  70. package/cli/cobolx-cli/src/commands/visualize.ts +25 -0
  71. package/cli/cobolx-cli/src/index.ts +101 -0
  72. package/cli/cobolx-cli/src/project.ts +74 -0
  73. package/cli/cobolx-cli/tsconfig.json +7 -0
  74. package/cobolx.toml +7 -0
  75. package/compiler/package.json +14 -0
  76. package/compiler/src/ast/types.d.ts +87 -0
  77. package/compiler/src/ast/types.js +2 -0
  78. package/compiler/src/ast/types.js.map +1 -0
  79. package/compiler/src/ast/types.ts +329 -0
  80. package/compiler/src/backend/custom.d.ts +8 -0
  81. package/compiler/src/backend/custom.js +12 -0
  82. package/compiler/src/backend/custom.js.map +1 -0
  83. package/compiler/src/backend/custom.ts +21 -0
  84. package/compiler/src/borrow_checker/checker.d.ts +3 -0
  85. package/compiler/src/borrow_checker/checker.js +82 -0
  86. package/compiler/src/borrow_checker/checker.js.map +1 -0
  87. package/compiler/src/borrow_checker/checker.ts +100 -0
  88. package/compiler/src/codegen/javascript.d.ts +2 -0
  89. package/compiler/src/codegen/javascript.js +89 -0
  90. package/compiler/src/codegen/javascript.js.map +1 -0
  91. package/compiler/src/codegen/javascript.ts +175 -0
  92. package/compiler/src/const_eval/evaluator.ts +58 -0
  93. package/compiler/src/diagnostics.d.ts +11 -0
  94. package/compiler/src/diagnostics.js +14 -0
  95. package/compiler/src/diagnostics.js.map +1 -0
  96. package/compiler/src/diagnostics.ts +20 -0
  97. package/compiler/src/hir/lower.d.ts +7 -0
  98. package/compiler/src/hir/lower.js +44 -0
  99. package/compiler/src/hir/lower.js.map +1 -0
  100. package/compiler/src/hir/lower.ts +60 -0
  101. package/compiler/src/hir/types.d.ts +21 -0
  102. package/compiler/src/hir/types.js +2 -0
  103. package/compiler/src/hir/types.js.map +1 -0
  104. package/compiler/src/hir/types.ts +26 -0
  105. package/compiler/src/index.d.ts +22 -0
  106. package/compiler/src/index.js +84 -0
  107. package/compiler/src/index.js.map +1 -0
  108. package/compiler/src/index.ts +122 -0
  109. package/compiler/src/lexer/lexer.d.ts +21 -0
  110. package/compiler/src/lexer/lexer.js +207 -0
  111. package/compiler/src/lexer/lexer.js.map +1 -0
  112. package/compiler/src/lexer/lexer.ts +274 -0
  113. package/compiler/src/lexer/tokens.d.ts +8 -0
  114. package/compiler/src/lexer/tokens.js +18 -0
  115. package/compiler/src/lexer/tokens.js.map +1 -0
  116. package/compiler/src/lexer/tokens.ts +126 -0
  117. package/compiler/src/macros/expand.ts +75 -0
  118. package/compiler/src/main.ts +4 -0
  119. package/compiler/src/mir/lower.d.ts +3 -0
  120. package/compiler/src/mir/lower.js +10 -0
  121. package/compiler/src/mir/lower.js.map +1 -0
  122. package/compiler/src/mir/lower.ts +12 -0
  123. package/compiler/src/mir/types.d.ts +13 -0
  124. package/compiler/src/mir/types.js +2 -0
  125. package/compiler/src/mir/types.js.map +1 -0
  126. package/compiler/src/mir/types.ts +16 -0
  127. package/compiler/src/optimizer/constantFold.d.ts +2 -0
  128. package/compiler/src/optimizer/constantFold.js +61 -0
  129. package/compiler/src/optimizer/constantFold.js.map +1 -0
  130. package/compiler/src/optimizer/constantFold.ts +109 -0
  131. package/compiler/src/parser/parser.d.ts +33 -0
  132. package/compiler/src/parser/parser.js +323 -0
  133. package/compiler/src/parser/parser.js.map +1 -0
  134. package/compiler/src/parser/parser.ts +710 -0
  135. package/compiler/src/plugins/api.ts +8 -0
  136. package/compiler/src/plugins/loader.ts +21 -0
  137. package/compiler/src/semantic/analyzer.d.ts +12 -0
  138. package/compiler/src/semantic/analyzer.js +144 -0
  139. package/compiler/src/semantic/analyzer.js.map +1 -0
  140. package/compiler/src/semantic/analyzer.ts +277 -0
  141. package/compiler/src/type_system/checker.d.ts +7 -0
  142. package/compiler/src/type_system/checker.js +84 -0
  143. package/compiler/src/type_system/checker.js.map +1 -0
  144. package/compiler/src/type_system/checker.ts +108 -0
  145. package/compiler/tsconfig.json +7 -0
  146. package/debugger/package.json +11 -0
  147. package/debugger/src/index.d.ts +1 -0
  148. package/debugger/src/index.js +9 -0
  149. package/debugger/src/index.js.map +1 -0
  150. package/debugger/src/index.ts +9 -0
  151. package/debugger/tsconfig.json +7 -0
  152. package/docs/CHANGELOG.md +11 -0
  153. package/docs/CONTRIBUTING.md +15 -0
  154. package/docs/LICENSE +21 -0
  155. package/docs/architecture.md +29 -0
  156. package/docs/cli.md +58 -0
  157. package/docs/language-spec.md +49 -0
  158. package/docs/packages.md +19 -0
  159. package/docs/platform-systems.md +31 -0
  160. package/docs/release-validation.md +22 -0
  161. package/docs/runtime.md +10 -0
  162. package/docs/tooling.md +17 -0
  163. package/docs/vscode-extension.md +40 -0
  164. package/enterprise-demo/CargoX.lock +2 -0
  165. package/enterprise-demo/README.md +3 -0
  166. package/enterprise-demo/benchmarks/arith.cbx +6 -0
  167. package/enterprise-demo/cobolx.toml +7 -0
  168. package/enterprise-demo/src/main.cbx +5 -0
  169. package/enterprise-demo/tests/smoke.cbx +6 -0
  170. package/examples/README.md +20 -0
  171. package/examples/actors-and-flags.md +8 -0
  172. package/examples/api-server/README.md +9 -0
  173. package/examples/api-server/cobolx.toml +6 -0
  174. package/examples/api-server/src/main.cbx +8 -0
  175. package/examples/debug-replay.md +7 -0
  176. package/examples/debugging-demo/README.md +3 -0
  177. package/examples/debugging-demo/cobolx.toml +6 -0
  178. package/examples/debugging-demo/src/main.cbx +7 -0
  179. package/examples/distributed-service.md +12 -0
  180. package/examples/distributed-system/README.md +3 -0
  181. package/examples/distributed-system/cobolx.toml +6 -0
  182. package/examples/distributed-system/generated/deploy.json +12 -0
  183. package/examples/distributed-system/src/main.cbx +6 -0
  184. package/examples/event-driven.md +5 -0
  185. package/examples/event-system/README.md +3 -0
  186. package/examples/event-system/cobolx.toml +6 -0
  187. package/examples/event-system/src/main.cbx +11 -0
  188. package/examples/functions.cbx +10 -0
  189. package/examples/functions.mjs +12 -0
  190. package/examples/hello.cbx +5 -0
  191. package/examples/hello.mjs +7 -0
  192. package/examples/legacy-sample.cob +5 -0
  193. package/examples/parallel-processing/README.md +3 -0
  194. package/examples/parallel-processing/cobolx.toml +6 -0
  195. package/examples/parallel-processing/src/main.cbx +6 -0
  196. package/examples/parallel-processing.md +8 -0
  197. package/examples/platform-features.cbx +32 -0
  198. package/examples/platform-features.mjs +35 -0
  199. package/examples/service.cbx +10 -0
  200. package/examples/workflow-engine/README.md +3 -0
  201. package/examples/workflow-engine/cobolx.toml +6 -0
  202. package/examples/workflow-engine/generated/flowchart.mmd +8 -0
  203. package/examples/workflow-engine/src/main.cbx +13 -0
  204. package/examples/workflow-engine.md +5 -0
  205. package/formatter/package.json +14 -0
  206. package/formatter/src/index.d.ts +1 -0
  207. package/formatter/src/index.js +59 -0
  208. package/formatter/src/index.js.map +1 -0
  209. package/formatter/src/index.ts +103 -0
  210. package/formatter/tsconfig.json +7 -0
  211. package/generated/LEGACYDEMO.cbx +5 -0
  212. package/install.ps1 +4 -0
  213. package/install.sh +5 -0
  214. package/linter/package.json +14 -0
  215. package/linter/src/index.d.ts +2 -0
  216. package/linter/src/index.js +18 -0
  217. package/linter/src/index.js.map +1 -0
  218. package/linter/src/index.ts +19 -0
  219. package/linter/tsconfig.json +7 -0
  220. package/lsp/server/package.json +16 -0
  221. package/lsp/server/src/index.ts +168 -0
  222. package/lsp/server/tsconfig.json +7 -0
  223. package/package.json +30 -0
  224. package/profiler/package.json +11 -0
  225. package/profiler/src/index.d.ts +5 -0
  226. package/profiler/src/index.js +11 -0
  227. package/profiler/src/index.js.map +1 -0
  228. package/profiler/src/index.ts +11 -0
  229. package/profiler/tsconfig.json +7 -0
  230. package/release.json +10 -0
  231. package/runtime/package.json +14 -0
  232. package/runtime/src/actors/index.ts +27 -0
  233. package/runtime/src/async/futures.ts +11 -0
  234. package/runtime/src/code_as_data/index.ts +5 -0
  235. package/runtime/src/config/index.ts +17 -0
  236. package/runtime/src/distributed/index.ts +13 -0
  237. package/runtime/src/events/index.ts +21 -0
  238. package/runtime/src/feature_flags/index.ts +9 -0
  239. package/runtime/src/gc_or_arc/index.ts +1 -0
  240. package/runtime/src/healing/index.ts +26 -0
  241. package/runtime/src/index.ts +20 -0
  242. package/runtime/src/intents/index.ts +9 -0
  243. package/runtime/src/iterators/index.ts +23 -0
  244. package/runtime/src/memory/arc.ts +22 -0
  245. package/runtime/src/observability/index.ts +23 -0
  246. package/runtime/src/repl/context.ts +7 -0
  247. package/runtime/src/scheduler/taskScheduler.ts +11 -0
  248. package/runtime/src/security/capabilities.ts +15 -0
  249. package/runtime/src/security/secrets.ts +13 -0
  250. package/runtime/src/state/versioned.ts +31 -0
  251. package/runtime/src/std_hooks/audit.ts +23 -0
  252. package/runtime/src/time_travel/index.ts +54 -0
  253. package/runtime/src/workflows/index.ts +35 -0
  254. package/runtime/tsconfig.json +7 -0
  255. package/sample-payroll/README.md +3 -0
  256. package/sample-payroll/cobolx.toml +3 -0
  257. package/sample-payroll/src/main.cbx +5 -0
  258. package/stdlib/business/index.js +15 -0
  259. package/stdlib/core/README.md +8 -0
  260. package/stdlib/core/runtime.js +191 -0
  261. package/stdlib/crypto/index.js +5 -0
  262. package/stdlib/datetime/index.js +3 -0
  263. package/stdlib/fs/index.js +9 -0
  264. package/stdlib/http/index.js +7 -0
  265. package/stdlib/io/index.js +1 -0
  266. package/stdlib/json/index.js +7 -0
  267. package/stdlib/net/index.js +4 -0
  268. package/tests/macros.cbx +10 -0
  269. package/tests/pattern_match.cbx +16 -0
  270. package/tests/smoke.cbx +7 -0
  271. package/tests/validate-release.mjs +54 -0
  272. package/tsconfig.base.json +24 -0
  273. package/vscode-extension/LICENSE +21 -0
  274. package/vscode-extension/README.md +47 -0
  275. package/vscode-extension/cobolx-1.2.0.vsix +0 -0
  276. package/vscode-extension/icon.png +0 -0
  277. package/vscode-extension/icon.svg +15 -0
  278. package/vscode-extension/language-configuration.json +17 -0
  279. package/vscode-extension/package.json +148 -0
  280. package/vscode-extension/snippets/cobolx.code-snippets +49 -0
  281. package/vscode-extension/src/extension.ts +283 -0
  282. package/vscode-extension/syntaxes/cobolx.tmLanguage.json +75 -0
  283. package/vscode-extension/tests/test.cbl +3 -0
  284. package/vscode-extension/tsconfig.json +7 -0
@@ -0,0 +1,274 @@
1
+ import type { SourceLocation, SourceRange } from "../ast/types.js";
2
+ import { CobolxError } from "../diagnostics.js";
3
+ import type { Token, TokenType } from "./tokens.js";
4
+ import { KEYWORDS } from "./tokens.js";
5
+
6
+ function cloneLocation(location: SourceLocation): SourceLocation {
7
+ return { ...location };
8
+ }
9
+
10
+ export class Lexer {
11
+ private index = 0;
12
+ private location: SourceLocation = { line: 1, column: 1, offset: 0 };
13
+
14
+ constructor(private readonly source: string) {}
15
+
16
+ tokenize(): Token[] {
17
+ const tokens: Token[] = [];
18
+
19
+ while (!this.isAtEnd()) {
20
+ const char = this.peek();
21
+
22
+ if (char === " " || char === "\t" || char === "\r") {
23
+ this.advance();
24
+ continue;
25
+ }
26
+
27
+ if (char === "\n") {
28
+ tokens.push(this.simpleToken("NEWLINE", this.advance()));
29
+ continue;
30
+ }
31
+
32
+ if (char === "/" && this.peekNext() === "/" && this.peekThird() === "/") {
33
+ tokens.push(this.docComment());
34
+ continue;
35
+ }
36
+
37
+ if (char === "*" && this.peekNext() === ">") {
38
+ this.consumeLineComment();
39
+ continue;
40
+ }
41
+
42
+ if (this.isAlpha(char)) {
43
+ tokens.push(this.identifier());
44
+ continue;
45
+ }
46
+
47
+ if (this.isDigit(char)) {
48
+ tokens.push(this.number());
49
+ continue;
50
+ }
51
+
52
+ if (char === "\"") {
53
+ tokens.push(this.string());
54
+ continue;
55
+ }
56
+
57
+ tokens.push(this.symbol());
58
+ }
59
+
60
+ const eofRange: SourceRange = {
61
+ start: cloneLocation(this.location),
62
+ end: cloneLocation(this.location)
63
+ };
64
+ tokens.push({ type: "EOF", lexeme: "", range: eofRange });
65
+ return tokens;
66
+ }
67
+
68
+ private docComment(): Token {
69
+ const start = cloneLocation(this.location);
70
+ this.advance();
71
+ this.advance();
72
+ this.advance();
73
+ let value = "";
74
+ while (!this.isAtEnd() && this.peek() !== "\n") {
75
+ value += this.advance();
76
+ }
77
+ return {
78
+ type: "DOC_COMMENT",
79
+ lexeme: value.trim(),
80
+ range: { start, end: cloneLocation(this.location) }
81
+ };
82
+ }
83
+
84
+ private consumeLineComment(): void {
85
+ while (!this.isAtEnd() && this.peek() !== "\n") {
86
+ this.advance();
87
+ }
88
+ }
89
+
90
+ private identifier(): Token {
91
+ const start = cloneLocation(this.location);
92
+ let value = "";
93
+ while (!this.isAtEnd() && (this.isAlphaNumeric(this.peek()) || this.peek() === "-" || this.peek() === "_")) {
94
+ value += this.advance();
95
+ }
96
+ const upper = value.toUpperCase();
97
+ const type = KEYWORDS[upper] ?? "IDENTIFIER";
98
+ return {
99
+ type,
100
+ lexeme: type === "IDENTIFIER" ? value : upper,
101
+ range: { start, end: cloneLocation(this.location) }
102
+ };
103
+ }
104
+
105
+ private number(): Token {
106
+ const start = cloneLocation(this.location);
107
+ let value = "";
108
+ while (!this.isAtEnd() && this.isDigit(this.peek())) {
109
+ value += this.advance();
110
+ }
111
+ if (!this.isAtEnd() && this.peek() === "." && this.isDigit(this.peekNext())) {
112
+ value += this.advance();
113
+ while (!this.isAtEnd() && this.isDigit(this.peek())) {
114
+ value += this.advance();
115
+ }
116
+ }
117
+ return {
118
+ type: "NUMBER",
119
+ lexeme: value,
120
+ range: { start, end: cloneLocation(this.location) }
121
+ };
122
+ }
123
+
124
+ private string(): Token {
125
+ const start = cloneLocation(this.location);
126
+ this.advance();
127
+ let value = "";
128
+ while (!this.isAtEnd() && this.peek() !== "\"") {
129
+ if (this.peek() === "\n") {
130
+ throw new CobolxError({
131
+ message: "Unterminated string literal",
132
+ range: { start, end: cloneLocation(this.location) },
133
+ severity: "error"
134
+ });
135
+ }
136
+ value += this.advance();
137
+ }
138
+ if (this.isAtEnd()) {
139
+ throw new CobolxError({
140
+ message: "Unterminated string literal",
141
+ range: { start, end: cloneLocation(this.location) },
142
+ severity: "error"
143
+ });
144
+ }
145
+ this.advance();
146
+ return {
147
+ type: "STRING",
148
+ lexeme: value,
149
+ range: { start, end: cloneLocation(this.location) }
150
+ };
151
+ }
152
+
153
+ private symbol(): Token {
154
+ const start = cloneLocation(this.location);
155
+ const char = this.advance();
156
+ const emit = (type: TokenType, lexeme: string): Token => ({
157
+ type,
158
+ lexeme,
159
+ range: { start, end: cloneLocation(this.location) }
160
+ });
161
+
162
+ switch (char) {
163
+ case "+":
164
+ return emit("PLUS", char);
165
+ case "-":
166
+ if (this.peek() === ">") {
167
+ this.advance();
168
+ return emit("ARROW", "->");
169
+ }
170
+ return emit("MINUS", char);
171
+ case "*":
172
+ return emit("STAR", char);
173
+ case "/":
174
+ return emit("SLASH", char);
175
+ case "%":
176
+ return emit("PERCENT", char);
177
+ case "!":
178
+ if (this.peek() === "=") {
179
+ this.advance();
180
+ return emit("NOT_EQUAL", "!=");
181
+ }
182
+ return emit("BANG", char);
183
+ case "?":
184
+ return emit("QUESTION", char);
185
+ case "(":
186
+ return emit("LPAREN", char);
187
+ case ")":
188
+ return emit("RPAREN", char);
189
+ case "[":
190
+ return emit("LBRACKET", char);
191
+ case "]":
192
+ return emit("RBRACKET", char);
193
+ case ",":
194
+ return emit("COMMA", char);
195
+ case ":":
196
+ return emit("COLON", char);
197
+ case ".":
198
+ return emit("DOT", char);
199
+ case "=":
200
+ return emit("ASSIGN", char);
201
+ case "<":
202
+ if (this.peek() === "=") {
203
+ this.advance();
204
+ return emit("LESS_EQUAL", "<=");
205
+ }
206
+ return emit("LESS", char);
207
+ case ">":
208
+ if (this.peek() === "=") {
209
+ this.advance();
210
+ return emit("GREATER_EQUAL", ">=");
211
+ }
212
+ return emit("GREATER", char);
213
+ case "&":
214
+ return emit("AMPERSAND", char);
215
+ }
216
+
217
+ throw new CobolxError({
218
+ message: `Unexpected character '${char}'`,
219
+ range: { start, end: cloneLocation(this.location) },
220
+ severity: "error"
221
+ });
222
+ }
223
+
224
+ private simpleToken(type: TokenType, lexeme: string): Token {
225
+ const end = cloneLocation(this.location);
226
+ const start = {
227
+ line: end.line,
228
+ column: end.column - lexeme.length,
229
+ offset: end.offset - lexeme.length
230
+ };
231
+ return { type, lexeme, range: { start, end } };
232
+ }
233
+
234
+ private advance(): string {
235
+ const char = this.source[this.index] ?? "\0";
236
+ this.index += 1;
237
+ this.location.offset += 1;
238
+ if (char === "\n") {
239
+ this.location.line += 1;
240
+ this.location.column = 1;
241
+ } else {
242
+ this.location.column += 1;
243
+ }
244
+ return char;
245
+ }
246
+
247
+ private peek(): string {
248
+ return this.source[this.index] ?? "\0";
249
+ }
250
+
251
+ private peekNext(): string {
252
+ return this.source[this.index + 1] ?? "\0";
253
+ }
254
+
255
+ private peekThird(): string {
256
+ return this.source[this.index + 2] ?? "\0";
257
+ }
258
+
259
+ private isAtEnd(): boolean {
260
+ return this.index >= this.source.length;
261
+ }
262
+
263
+ private isAlpha(char: string): boolean {
264
+ return /[A-Za-z_]/.test(char);
265
+ }
266
+
267
+ private isDigit(char: string): boolean {
268
+ return /[0-9]/.test(char);
269
+ }
270
+
271
+ private isAlphaNumeric(char: string): boolean {
272
+ return this.isAlpha(char) || this.isDigit(char);
273
+ }
274
+ }
@@ -0,0 +1,8 @@
1
+ import type { SourceRange } from "../ast/types.js";
2
+ export type TokenType = "PROGRAM" | "BEGIN" | "END" | "SET" | "DISPLAY" | "INPUT" | "IF" | "THEN" | "ELSE" | "END-IF" | "FUNCTION" | "RETURN" | "END-FUNCTION" | "TRUE" | "FALSE" | "IDENTIFIER" | "NUMBER" | "STRING" | "PLUS" | "MINUS" | "STAR" | "SLASH" | "EQUAL" | "NOT_EQUAL" | "LESS" | "LESS_EQUAL" | "GREATER" | "GREATER_EQUAL" | "ASSIGN" | "LPAREN" | "RPAREN" | "COMMA" | "NEWLINE" | "EOF";
3
+ export interface Token {
4
+ type: TokenType;
5
+ lexeme: string;
6
+ range: SourceRange;
7
+ }
8
+ export declare const KEYWORDS: Record<string, TokenType>;
@@ -0,0 +1,18 @@
1
+ export const KEYWORDS = {
2
+ PROGRAM: "PROGRAM",
3
+ BEGIN: "BEGIN",
4
+ END: "END",
5
+ SET: "SET",
6
+ DISPLAY: "DISPLAY",
7
+ INPUT: "INPUT",
8
+ IF: "IF",
9
+ THEN: "THEN",
10
+ ELSE: "ELSE",
11
+ "END-IF": "END-IF",
12
+ FUNCTION: "FUNCTION",
13
+ RETURN: "RETURN",
14
+ "END-FUNCTION": "END-FUNCTION",
15
+ TRUE: "TRUE",
16
+ FALSE: "FALSE"
17
+ };
18
+ //# sourceMappingURL=tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokens.js","sourceRoot":"","sources":["tokens.ts"],"names":[],"mappings":"AA4CA,MAAM,CAAC,MAAM,QAAQ,GAA8B;IACjD,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,EAAE,EAAE,IAAI;IACR,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,cAAc,EAAE,cAAc;IAC9B,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;CACf,CAAC"}
@@ -0,0 +1,126 @@
1
+ import type { SourceRange } from "../ast/types.js";
2
+
3
+ export type TokenType =
4
+ | "PROGRAM"
5
+ | "BEGIN"
6
+ | "END"
7
+ | "SET"
8
+ | "LET"
9
+ | "MUT"
10
+ | "DISPLAY"
11
+ | "INPUT"
12
+ | "IF"
13
+ | "THEN"
14
+ | "ELSE"
15
+ | "END-IF"
16
+ | "MATCH"
17
+ | "END-MATCH"
18
+ | "FUNCTION"
19
+ | "ASYNC"
20
+ | "CONST"
21
+ | "RETURN"
22
+ | "END-FUNCTION"
23
+ | "TRAIT"
24
+ | "END-TRAIT"
25
+ | "IMPL"
26
+ | "FOR"
27
+ | "END-IMPL"
28
+ | "ENUM"
29
+ | "MODULE"
30
+ | "END-MODULE"
31
+ | "IMPORT"
32
+ | "EXPORT"
33
+ | "FROM"
34
+ | "AS"
35
+ | "PLUGIN"
36
+ | "MACRO"
37
+ | "END-MACRO"
38
+ | "TEST"
39
+ | "END-TEST"
40
+ | "ASSERT"
41
+ | "UNSAFE"
42
+ | "END-UNSAFE"
43
+ | "SPAWN"
44
+ | "PUBLIC"
45
+ | "TRUE"
46
+ | "FALSE"
47
+ | "IDENTIFIER"
48
+ | "NUMBER"
49
+ | "STRING"
50
+ | "DOC_COMMENT"
51
+ | "PLUS"
52
+ | "MINUS"
53
+ | "STAR"
54
+ | "SLASH"
55
+ | "PERCENT"
56
+ | "BANG"
57
+ | "QUESTION"
58
+ | "LESS"
59
+ | "LESS_EQUAL"
60
+ | "GREATER"
61
+ | "GREATER_EQUAL"
62
+ | "ASSIGN"
63
+ | "NOT_EQUAL"
64
+ | "LPAREN"
65
+ | "RPAREN"
66
+ | "LBRACKET"
67
+ | "RBRACKET"
68
+ | "COMMA"
69
+ | "COLON"
70
+ | "DOT"
71
+ | "ARROW"
72
+ | "AMPERSAND"
73
+ | "NEWLINE"
74
+ | "EOF";
75
+
76
+ export interface Token {
77
+ type: TokenType;
78
+ lexeme: string;
79
+ range: SourceRange;
80
+ }
81
+
82
+ export const KEYWORDS: Record<string, TokenType> = {
83
+ PROGRAM: "PROGRAM",
84
+ BEGIN: "BEGIN",
85
+ END: "END",
86
+ SET: "SET",
87
+ LET: "LET",
88
+ MUT: "MUT",
89
+ DISPLAY: "DISPLAY",
90
+ INPUT: "INPUT",
91
+ IF: "IF",
92
+ THEN: "THEN",
93
+ ELSE: "ELSE",
94
+ "END-IF": "END-IF",
95
+ MATCH: "MATCH",
96
+ "END-MATCH": "END-MATCH",
97
+ FUNCTION: "FUNCTION",
98
+ ASYNC: "ASYNC",
99
+ CONST: "CONST",
100
+ RETURN: "RETURN",
101
+ "END-FUNCTION": "END-FUNCTION",
102
+ TRAIT: "TRAIT",
103
+ "END-TRAIT": "END-TRAIT",
104
+ IMPL: "IMPL",
105
+ FOR: "FOR",
106
+ "END-IMPL": "END-IMPL",
107
+ ENUM: "ENUM",
108
+ MODULE: "MODULE",
109
+ "END-MODULE": "END-MODULE",
110
+ IMPORT: "IMPORT",
111
+ EXPORT: "EXPORT",
112
+ FROM: "FROM",
113
+ AS: "AS",
114
+ PLUGIN: "PLUGIN",
115
+ MACRO: "MACRO",
116
+ "END-MACRO": "END-MACRO",
117
+ TEST: "TEST",
118
+ "END-TEST": "END-TEST",
119
+ ASSERT: "ASSERT",
120
+ UNSAFE: "UNSAFE",
121
+ "END-UNSAFE": "END-UNSAFE",
122
+ SPAWN: "SPAWN",
123
+ PUBLIC: "PUBLIC",
124
+ TRUE: "TRUE",
125
+ FALSE: "FALSE"
126
+ };
@@ -0,0 +1,75 @@
1
+ import type {
2
+ CallExpressionNode,
3
+ ExpressionNode,
4
+ MacroInvocationNode,
5
+ ProgramNode,
6
+ StatementNode
7
+ } from "../ast/types.js";
8
+
9
+ function rewriteExpression(expression: ExpressionNode): ExpressionNode {
10
+ switch (expression.kind) {
11
+ case "BinaryExpression":
12
+ return { ...expression, left: rewriteExpression(expression.left), right: rewriteExpression(expression.right) };
13
+ case "UnaryExpression":
14
+ return { ...expression, operand: rewriteExpression(expression.operand) };
15
+ case "TryExpression":
16
+ return { ...expression, expression: rewriteExpression(expression.expression) };
17
+ case "MemberExpression":
18
+ return { ...expression, object: rewriteExpression(expression.object) };
19
+ case "CallExpression":
20
+ return { ...expression, callee: rewriteExpression(expression.callee), args: expression.args.map(rewriteExpression) };
21
+ case "MacroInvocation":
22
+ return macroToCall(expression);
23
+ case "EnumConstructorExpression":
24
+ return { ...expression, fields: expression.fields.map(rewriteExpression) };
25
+ case "ArrayLiteral":
26
+ return { ...expression, items: expression.items.map(rewriteExpression) };
27
+ default:
28
+ return expression;
29
+ }
30
+ }
31
+
32
+ function rewriteStatement(statement: StatementNode): StatementNode {
33
+ switch (statement.kind) {
34
+ case "LetStatement":
35
+ return { ...statement, expression: rewriteExpression(statement.expression) };
36
+ case "SetStatement":
37
+ case "DisplayStatement":
38
+ case "ReturnStatement":
39
+ case "ExpressionStatement":
40
+ case "AssertStatement":
41
+ case "SpawnStatement":
42
+ return { ...statement, expression: rewriteExpression(statement.expression) };
43
+ case "InputStatement":
44
+ return { ...statement, prompt: statement.prompt ? rewriteExpression(statement.prompt) : undefined };
45
+ case "IfStatement":
46
+ return { ...statement, condition: rewriteExpression(statement.condition), thenBranch: statement.thenBranch.map(rewriteStatement), elseBranch: statement.elseBranch.map(rewriteStatement) };
47
+ case "MatchStatement":
48
+ return { ...statement, expression: rewriteExpression(statement.expression), arms: statement.arms.map((arm) => ({ ...arm, body: arm.body.map(rewriteStatement) })) };
49
+ case "UnsafeBlock":
50
+ case "BlockStatement":
51
+ return { ...statement, body: statement.body.map(rewriteStatement) };
52
+ }
53
+ }
54
+
55
+ function macroToCall(expression: MacroInvocationNode): CallExpressionNode {
56
+ return {
57
+ kind: "CallExpression",
58
+ callee: { kind: "Identifier", name: `__macro_${expression.name}`, range: expression.range },
59
+ args: expression.args.map(rewriteExpression),
60
+ range: expression.range
61
+ };
62
+ }
63
+
64
+ export function expandMacros(program: ProgramNode): ProgramNode {
65
+ return {
66
+ ...program,
67
+ functions: program.functions.map((fn) => ({ ...fn, body: fn.body.map(rewriteStatement) })),
68
+ macros: program.macros.map((macro) => ({
69
+ ...macro,
70
+ name: `__macro_${macro.name}`,
71
+ body: macro.body.map(rewriteStatement)
72
+ })),
73
+ body: program.body.map(rewriteStatement)
74
+ };
75
+ }
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { runCompilerCli } from "./index.js";
3
+
4
+ process.exit(runCompilerCli(process.argv.slice(2)));
@@ -0,0 +1,3 @@
1
+ import type { HIRProgram } from "../hir/types.js";
2
+ import type { MIRProgram } from "./types.js";
3
+ export declare function lowerToMIR(hir: HIRProgram): MIRProgram;
@@ -0,0 +1,10 @@
1
+ export function lowerToMIR(hir) {
2
+ return {
3
+ functions: hir.functions.map((fn) => ({
4
+ name: fn.name,
5
+ instructions: fn.body.map(({ statement }) => ({ op: "statement", statement }))
6
+ })),
7
+ body: hir.body.map(({ statement }) => ({ op: "statement", statement }))
8
+ };
9
+ }
10
+ //# sourceMappingURL=lower.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lower.js","sourceRoot":"","sources":["lower.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,UAAU,CAAC,GAAe;IACxC,OAAO;QACL,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YACpC,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,YAAY,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;SAC/E,CAAC,CAAC;QACH,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { HIRProgram } from "../hir/types.js";
2
+ import type { MIRProgram } from "./types.js";
3
+
4
+ export function lowerToMIR(hir: HIRProgram): MIRProgram {
5
+ return {
6
+ functions: hir.functions.map((fn) => ({
7
+ name: fn.name,
8
+ instructions: fn.body.map(({ statement }) => ({ op: "statement", statement }))
9
+ })),
10
+ body: hir.body.map(({ statement }) => ({ op: "statement", statement }))
11
+ };
12
+ }
@@ -0,0 +1,13 @@
1
+ import type { StatementNode } from "../ast/types.js";
2
+ export interface MIRInstruction {
3
+ op: "statement";
4
+ statement: StatementNode;
5
+ }
6
+ export interface MIRFunction {
7
+ name: string;
8
+ instructions: MIRInstruction[];
9
+ }
10
+ export interface MIRProgram {
11
+ functions: MIRFunction[];
12
+ body: MIRInstruction[];
13
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import type { StatementNode } from "../ast/types.js";
2
+
3
+ export interface MIRInstruction {
4
+ op: "statement";
5
+ statement: StatementNode;
6
+ }
7
+
8
+ export interface MIRFunction {
9
+ name: string;
10
+ instructions: MIRInstruction[];
11
+ }
12
+
13
+ export interface MIRProgram {
14
+ functions: MIRFunction[];
15
+ body: MIRInstruction[];
16
+ }
@@ -0,0 +1,2 @@
1
+ import type { MIRProgram } from "../mir/types.js";
2
+ export declare function optimizeMIR(mir: MIRProgram): MIRProgram;
@@ -0,0 +1,61 @@
1
+ function foldExpression(expression) {
2
+ if (expression.kind === "BinaryExpression") {
3
+ const left = foldExpression(expression.left);
4
+ const right = foldExpression(expression.right);
5
+ if (left.kind === "NumberLiteral" && right.kind === "NumberLiteral") {
6
+ switch (expression.operator) {
7
+ case "+":
8
+ return { ...expression, kind: "NumberLiteral", value: left.value + right.value };
9
+ case "-":
10
+ return { ...expression, kind: "NumberLiteral", value: left.value - right.value };
11
+ case "*":
12
+ return { ...expression, kind: "NumberLiteral", value: left.value * right.value };
13
+ case "/":
14
+ return { ...expression, kind: "NumberLiteral", value: left.value / right.value };
15
+ }
16
+ }
17
+ return { ...expression, left, right };
18
+ }
19
+ if (expression.kind === "UnaryExpression") {
20
+ return { ...expression, operand: foldExpression(expression.operand) };
21
+ }
22
+ if (expression.kind === "CallExpression") {
23
+ return { ...expression, args: expression.args.map(foldExpression) };
24
+ }
25
+ return expression;
26
+ }
27
+ function foldStatement(statement) {
28
+ switch (statement.kind) {
29
+ case "SetStatement":
30
+ return { ...statement, expression: foldExpression(statement.expression) };
31
+ case "DisplayStatement":
32
+ case "ReturnStatement":
33
+ case "ExpressionStatement":
34
+ return { ...statement, expression: foldExpression(statement.expression) };
35
+ case "InputStatement":
36
+ return { ...statement, prompt: statement.prompt ? foldExpression(statement.prompt) : undefined };
37
+ case "IfStatement":
38
+ return {
39
+ ...statement,
40
+ condition: foldExpression(statement.condition),
41
+ thenBranch: statement.thenBranch.map(foldStatement),
42
+ elseBranch: statement.elseBranch.map(foldStatement)
43
+ };
44
+ }
45
+ }
46
+ function foldInstruction(instruction) {
47
+ return {
48
+ ...instruction,
49
+ statement: foldStatement(instruction.statement)
50
+ };
51
+ }
52
+ export function optimizeMIR(mir) {
53
+ return {
54
+ functions: mir.functions.map((fn) => ({
55
+ ...fn,
56
+ instructions: fn.instructions.map(foldInstruction)
57
+ })),
58
+ body: mir.body.map(foldInstruction)
59
+ };
60
+ }
61
+ //# sourceMappingURL=constantFold.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constantFold.js","sourceRoot":"","sources":["constantFold.ts"],"names":[],"mappings":"AAGA,SAAS,cAAc,CAAC,UAA0B;IAChD,IAAI,UAAU,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;QAC3C,MAAM,IAAI,GAAG,cAAc,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC7C,MAAM,KAAK,GAAG,cAAc,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAC/C,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,IAAI,KAAK,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;YACpE,QAAQ,UAAU,CAAC,QAAQ,EAAE,CAAC;gBAC5B,KAAK,GAAG;oBACN,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;gBACnF,KAAK,GAAG;oBACN,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;gBACnF,KAAK,GAAG;oBACN,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;gBACnF,KAAK,GAAG;oBACN,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;YACrF,CAAC;QACH,CAAC;QACD,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACxC,CAAC;IACD,IAAI,UAAU,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;QAC1C,OAAO,EAAE,GAAG,UAAU,EAAE,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;IACxE,CAAC;IACD,IAAI,UAAU,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;QACzC,OAAO,EAAE,GAAG,UAAU,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;IACtE,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,aAAa,CAAC,SAAwB;IAC7C,QAAQ,SAAS,CAAC,IAAI,EAAE,CAAC;QACvB,KAAK,cAAc;YACjB,OAAO,EAAE,GAAG,SAAS,EAAE,UAAU,EAAE,cAAc,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5E,KAAK,kBAAkB,CAAC;QACxB,KAAK,iBAAiB,CAAC;QACvB,KAAK,qBAAqB;YACxB,OAAO,EAAE,GAAG,SAAS,EAAE,UAAU,EAAE,cAAc,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5E,KAAK,gBAAgB;YACnB,OAAO,EAAE,GAAG,SAAS,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;QACnG,KAAK,aAAa;YAChB,OAAO;gBACL,GAAG,SAAS;gBACZ,SAAS,EAAE,cAAc,CAAC,SAAS,CAAC,SAAS,CAAC;gBAC9C,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,aAAa,CAAC;gBACnD,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,aAAa,CAAC;aACpD,CAAC;IACN,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,WAA2B;IAClD,OAAO;QACL,GAAG,WAAW;QACd,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC,SAAS,CAAC;KAChD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,GAAe;IACzC,OAAO;QACL,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YACpC,GAAG,EAAE;YACL,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,eAAe,CAAC;SACnD,CAAC,CAAC;QACH,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC;KACpC,CAAC;AACJ,CAAC"}