@xnoxs/flux-lang 3.1.1

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 (56) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/README.md +1089 -0
  3. package/bin/flux.js +1397 -0
  4. package/dist/flux.cjs.js +6664 -0
  5. package/dist/flux.esm.js +6674 -0
  6. package/dist/flux.min.js +263 -0
  7. package/index.d.ts +202 -0
  8. package/index.js +26 -0
  9. package/package.json +77 -0
  10. package/scripts/build.js +76 -0
  11. package/src/bundler.js +216 -0
  12. package/src/checker.js +322 -0
  13. package/src/codegen.js +785 -0
  14. package/src/css-preprocessor.js +399 -0
  15. package/src/formatter.js +140 -0
  16. package/src/jsx.js +480 -0
  17. package/src/lexer.js +518 -0
  18. package/src/linter.js +758 -0
  19. package/src/mangler.js +280 -0
  20. package/src/parser.js +1671 -0
  21. package/src/self/bundler.flux +167 -0
  22. package/src/self/bundler.js +187 -0
  23. package/src/self/checker.flux +249 -0
  24. package/src/self/checker.js +338 -0
  25. package/src/self/codegen.flux +555 -0
  26. package/src/self/codegen.js +784 -0
  27. package/src/self/css-preprocessor.flux +373 -0
  28. package/src/self/css-preprocessor.js +387 -0
  29. package/src/self/formatter.flux +93 -0
  30. package/src/self/formatter.js +114 -0
  31. package/src/self/jsx.flux +430 -0
  32. package/src/self/jsx.js +396 -0
  33. package/src/self/lexer.flux +529 -0
  34. package/src/self/lexer.js +709 -0
  35. package/src/self/lexer.stage2.js +700 -0
  36. package/src/self/linter.flux +515 -0
  37. package/src/self/linter.js +804 -0
  38. package/src/self/mangler.flux +253 -0
  39. package/src/self/mangler.js +348 -0
  40. package/src/self/parser.flux +1146 -0
  41. package/src/self/parser.js +1571 -0
  42. package/src/self/sourcemap.flux +66 -0
  43. package/src/self/sourcemap.js +72 -0
  44. package/src/self/stdlib.flux +356 -0
  45. package/src/self/stdlib.js +396 -0
  46. package/src/self/test-runner.flux +201 -0
  47. package/src/self/test-runner.js +132 -0
  48. package/src/self/transpiler.flux +123 -0
  49. package/src/self/transpiler.js +83 -0
  50. package/src/self/type-checker.flux +821 -0
  51. package/src/self/type-checker.js +1106 -0
  52. package/src/sourcemap.js +82 -0
  53. package/src/stdlib.js +436 -0
  54. package/src/test-runner.js +239 -0
  55. package/src/transpiler.js +172 -0
  56. package/src/type-checker.js +1206 -0
@@ -0,0 +1,338 @@
1
+ // Generated by Flux Transpiler v3.1.0
2
+ "use strict";
3
+
4
+ class CheckError {
5
+ constructor(message, loc, hint, name, line, col) {
6
+ this.message = message;
7
+ this.loc = loc;
8
+ this.hint = hint;
9
+ this.name = name;
10
+ this.line = line;
11
+ this.col = col;
12
+ }
13
+
14
+ }
15
+
16
+ module.exports.CheckError = CheckError;
17
+ class Scope {
18
+ constructor(parent, vars) {
19
+ this.parent = parent;
20
+ this.vars = vars;
21
+ }
22
+
23
+ define(name, kind, loc) {
24
+ this.vars.set(name, { kind, loc });
25
+ }
26
+
27
+ lookup(name) {
28
+ let scope = this;
29
+ while (scope) {
30
+ if (scope.vars.has(name)) {
31
+ return scope.vars.get(name);
32
+ }
33
+ scope = scope.parent;
34
+ }
35
+ return null;
36
+ }
37
+
38
+ isVal(name) {
39
+ const entry = this.lookup(name);
40
+ return (entry && (entry.kind == "val"));
41
+ }
42
+
43
+ }
44
+
45
+ module.exports.Scope = Scope;
46
+ class Checker {
47
+ constructor(errors, warnings) {
48
+ this.errors = errors;
49
+ this.warnings = warnings;
50
+ }
51
+
52
+ error(msg, loc, hint) {
53
+ const err = new CheckError();
54
+ err.message = msg;
55
+ err.loc = loc;
56
+ err.hint = (hint ?? null);
57
+ err.name = "CheckError";
58
+ if (loc) {
59
+ err.line = loc.line;
60
+ err.col = loc.col;
61
+ }
62
+ this.errors.push(err);
63
+ }
64
+
65
+ warn(msg, loc, hint) {
66
+ const w = new CheckError();
67
+ w.message = msg;
68
+ w.loc = loc;
69
+ w.hint = (hint ?? null);
70
+ w.name = "CheckError";
71
+ if (loc) {
72
+ w.line = loc.line;
73
+ w.col = loc.col;
74
+ }
75
+ this.warnings.push(w);
76
+ }
77
+
78
+ check(ast) {
79
+ this.errors = [];
80
+ this.warnings = [];
81
+ const scope = new Scope(null, new Map());
82
+ this.walkStmts(ast.body, scope);
83
+ return { errors: this.errors, warnings: this.warnings };
84
+ }
85
+
86
+ walkStmts(stmts, scope) {
87
+ for (const node of stmts) {
88
+ this.walkStmt(node, scope);
89
+ }
90
+ }
91
+
92
+ walkStmt(node, scope) {
93
+ if (!node) {
94
+ return;
95
+ }
96
+ if ((node.type == "VarDecl")) {
97
+ if (node.init) {
98
+ this.walkExpr(node.init, scope);
99
+ }
100
+ scope.define(node.name, node.kind, node.loc);
101
+ }
102
+ else if ((node.type == "DestructureDecl")) {
103
+ if (node.init) {
104
+ this.walkExpr(node.init, scope);
105
+ }
106
+ if ((node.patternType == "object")) {
107
+ for (const p of node.pattern) {
108
+ scope.define(p.alias, node.kind, node.loc);
109
+ }
110
+ }
111
+ else {
112
+ for (const p of node.pattern) {
113
+ if (p) {
114
+ scope.define(p.name, node.kind, node.loc);
115
+ }
116
+ }
117
+ }
118
+ }
119
+ else if ((node.type == "FnDecl")) {
120
+ const inner = new Scope(scope, new Map());
121
+ for (const p of node.params) {
122
+ inner.define(p.name, "val", node.loc);
123
+ }
124
+ if (node.name) {
125
+ scope.define(node.name, "val", node.loc);
126
+ }
127
+ if (node.inline) {
128
+ this.walkExpr(node.body, inner);
129
+ }
130
+ else {
131
+ this.walkStmts(node.body, inner);
132
+ }
133
+ }
134
+ else if ((node.type == "ClassDecl")) {
135
+ scope.define(node.name, "val", node.loc);
136
+ const cls = new Scope(scope, new Map());
137
+ for (const f of node.fields) {
138
+ cls.define(f.name, "var", node.loc);
139
+ }
140
+ for (const m of node.methods) {
141
+ this.walkStmt(m, cls);
142
+ }
143
+ }
144
+ else if ((node.type == "TypeDecl")) {
145
+ for (const v of node.variants) {
146
+ scope.define(v.name, "val", node.loc);
147
+ }
148
+ }
149
+ else if ((node.type == "InterfaceDecl")) {
150
+ scope.define(node.name, "val", node.loc);
151
+ }
152
+ else if ((node.type == "EnumDecl")) {
153
+ scope.define(node.name, "val", node.loc);
154
+ }
155
+ else if ((node.type == "IfStmt")) {
156
+ this.walkExpr(node.cond, scope);
157
+ this.walkStmts(node.then, new Scope(scope, new Map()));
158
+ for (const ei of node.elseifs) {
159
+ this.walkExpr(ei.cond, scope);
160
+ this.walkStmts(ei.body, new Scope(scope, new Map()));
161
+ }
162
+ if (node.else_) {
163
+ this.walkStmts(node.else_, new Scope(scope, new Map()));
164
+ }
165
+ }
166
+ else if ((node.type == "ForInStmt")) {
167
+ this.walkExpr(node.iter, scope);
168
+ const inner = new Scope(scope, new Map());
169
+ inner.define(node.var, "val", node.loc);
170
+ this.walkStmts(node.body, inner);
171
+ }
172
+ else if ((node.type == "WhileStmt")) {
173
+ this.walkExpr(node.cond, scope);
174
+ this.walkStmts(node.body, new Scope(scope, new Map()));
175
+ }
176
+ else if ((node.type == "DoWhileStmt")) {
177
+ this.walkStmts(node.body, new Scope(scope, new Map()));
178
+ this.walkExpr(node.cond, scope);
179
+ }
180
+ else if ((node.type == "MatchStmt")) {
181
+ this.walkExpr(node.subject, scope);
182
+ for (const arm of node.arms) {
183
+ const inner = new Scope(scope, new Map());
184
+ if ((arm.pattern.type == "VariantPat")) {
185
+ for (const b of arm.pattern.bindings) {
186
+ inner.define(b, "val", node.loc);
187
+ }
188
+ }
189
+ if (arm.guard) {
190
+ this.walkExpr(arm.guard, inner);
191
+ }
192
+ this.walkStmts(arm.body, inner);
193
+ }
194
+ }
195
+ else if ((node.type == "ReturnStmt")) {
196
+ if (node.value) {
197
+ this.walkExpr(node.value, scope);
198
+ }
199
+ }
200
+ else if ((node.type == "ThrowStmt")) {
201
+ this.walkExpr(node.value, scope);
202
+ }
203
+ else if ((node.type == "TryCatchStmt")) {
204
+ this.walkStmts(node.tryBody, new Scope(scope, new Map()));
205
+ if (node.catchBody) {
206
+ const inner = new Scope(scope, new Map());
207
+ if (node.catchParam) {
208
+ inner.define(node.catchParam, "val", node.loc);
209
+ }
210
+ this.walkStmts(node.catchBody, inner);
211
+ }
212
+ if (node.finallyBody) {
213
+ this.walkStmts(node.finallyBody, new Scope(scope, new Map()));
214
+ }
215
+ }
216
+ else if ((node.type == "ImportDecl")) {
217
+ if (node.defaultName) {
218
+ scope.define(node.defaultName, "val", node.loc);
219
+ }
220
+ if (node.namespaceName) {
221
+ scope.define(node.namespaceName, "val", node.loc);
222
+ }
223
+ for (const n of node.names) {
224
+ const nm = ((typeof n == "string") ? n : n.alias);
225
+ scope.define(nm, "val", node.loc);
226
+ }
227
+ }
228
+ else if ((node.type == "ExportDecl")) {
229
+ if (node.isDefault) {
230
+ this.walkExpr(node.decl, scope);
231
+ }
232
+ else {
233
+ this.walkStmt(node.decl, scope);
234
+ }
235
+ }
236
+ else if ((node.type == "ExprStmt")) {
237
+ this.walkExpr(node.expr, scope);
238
+ }
239
+ }
240
+
241
+ walkExpr(node, scope) {
242
+ if (!node) {
243
+ return;
244
+ }
245
+ if ((node.type == "AssignExpr")) {
246
+ const target = node.target;
247
+ if ((target.type == "Identifier")) {
248
+ if (scope.isVal(target.name)) {
249
+ this.error((("Cannot reassign 'val " + target.name) + "' — val is immutable"), target.loc, (("Use 'var " + target.name) + "' if you need a mutable variable"));
250
+ }
251
+ }
252
+ this.walkExpr(node.target, scope);
253
+ this.walkExpr(node.value, scope);
254
+ }
255
+ else if ((node.type == "UpdateExpr")) {
256
+ if (((node.operand.type == "Identifier") && scope.isVal(node.operand.name))) {
257
+ this.error((((("Cannot apply '" + node.op) + "' to 'val ") + node.operand.name) + "' — val is immutable"), node.operand.loc, (("Use 'var " + node.operand.name) + "' if you need a mutable variable"));
258
+ }
259
+ this.walkExpr(node.operand, scope);
260
+ }
261
+ else if (((node.type == "BinaryExpr") || (node.type == "PipeExpr"))) {
262
+ this.walkExpr(node.left, scope);
263
+ this.walkExpr(node.right, scope);
264
+ }
265
+ else if ((((node.type == "UnaryExpr") || (node.type == "AwaitExpr")) || (node.type == "TypeofExpr"))) {
266
+ this.walkExpr(node.operand, scope);
267
+ }
268
+ else if ((node.type == "TernaryExpr")) {
269
+ this.walkExpr(node.cond, scope);
270
+ this.walkExpr(node.then, scope);
271
+ this.walkExpr(node.else_, scope);
272
+ }
273
+ else if (((node.type == "CallExpr") || (node.type == "OptCallExpr"))) {
274
+ this.walkExpr(node.callee, scope);
275
+ for (const a of node.args) {
276
+ this.walkExpr(a, scope);
277
+ }
278
+ }
279
+ else if (((node.type == "MemberExpr") || (node.type == "OptMemberExpr"))) {
280
+ this.walkExpr(node.obj, scope);
281
+ }
282
+ else if (((node.type == "IndexExpr") || (node.type == "OptIndexExpr"))) {
283
+ this.walkExpr(node.obj, scope);
284
+ this.walkExpr(node.idx, scope);
285
+ }
286
+ else if ((node.type == "NewExpr")) {
287
+ for (const a of node.args) {
288
+ this.walkExpr(a, scope);
289
+ }
290
+ }
291
+ else if ((node.type == "ArrayExpr")) {
292
+ for (const item of node.items) {
293
+ if (item) {
294
+ this.walkExpr(item, scope);
295
+ }
296
+ }
297
+ }
298
+ else if ((node.type == "ObjectExpr")) {
299
+ for (const pair of node.pairs) {
300
+ if (pair.value) {
301
+ this.walkExpr(pair.value, scope);
302
+ }
303
+ }
304
+ }
305
+ else if ((node.type == "SpreadExpr")) {
306
+ this.walkExpr(node.expr, scope);
307
+ }
308
+ else if ((node.type == "LambdaExpr")) {
309
+ const inner = new Scope(scope, new Map());
310
+ for (const p of node.params) {
311
+ inner.define(p.name, "val", null);
312
+ }
313
+ this.walkExpr(node.body, inner);
314
+ }
315
+ else if ((node.type == "FnDecl")) {
316
+ const inner = new Scope(scope, new Map());
317
+ for (const p of node.params) {
318
+ inner.define(p.name, "val", null);
319
+ }
320
+ if (node.inline) {
321
+ this.walkExpr(node.body, inner);
322
+ }
323
+ else {
324
+ this.walkStmts(node.body, inner);
325
+ }
326
+ }
327
+ else if ((((((node.type == "CastExpr") || (node.type == "AsConstExpr")) || (node.type == "SatisfiesExpr")) || (node.type == "IsExpr")) || (node.type == "NonNullExpr"))) {
328
+ this.walkExpr(node.expr, scope);
329
+ }
330
+ else if ((node.type == "RangeExpr")) {
331
+ this.walkExpr(node.start, scope);
332
+ this.walkExpr(node.end, scope);
333
+ }
334
+ }
335
+
336
+ }
337
+
338
+ module.exports.Checker = Checker;