plainscript-lang 0.1.7-latest

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +815 -0
  3. package/compiler/bundler.js +86 -0
  4. package/compiler/cli.js +843 -0
  5. package/compiler/dependency-detector.js +173 -0
  6. package/compiler/formatter.js +170 -0
  7. package/compiler/generator.js +2032 -0
  8. package/compiler/lexer.js +413 -0
  9. package/compiler/parser.js +2077 -0
  10. package/compiler/version.js +6 -0
  11. package/docs/PLINJS_SPEC.md +1051 -0
  12. package/docs/index.html +2287 -0
  13. package/docs/logo.jpg +0 -0
  14. package/docs/website/index.html +1500 -0
  15. package/docs/website/script.js +197 -0
  16. package/docs/website/style.css +1117 -0
  17. package/examples/arrays.pln +13 -0
  18. package/examples/cache-and-jobs.pln +29 -0
  19. package/examples/database.pln +41 -0
  20. package/examples/day2.pln +7 -0
  21. package/examples/day3.pln +11 -0
  22. package/examples/deployment.pln +12 -0
  23. package/examples/email-send.pln +22 -0
  24. package/examples/expressions.pln +40 -0
  25. package/examples/football-backend/app.pln +90 -0
  26. package/examples/hello.pln +3 -0
  27. package/examples/id-verification/app.pln +29 -0
  28. package/examples/id-verification/make-sample-id.js +123 -0
  29. package/examples/loops.pln +17 -0
  30. package/examples/objects.pln +12 -0
  31. package/examples/rest-api-fetch.pln +94 -0
  32. package/examples/rest-api.pln +68 -0
  33. package/examples/scheduled-tasks.pln +23 -0
  34. package/examples/server.pln +25 -0
  35. package/examples/start.pln +2 -0
  36. package/examples/stdlib.pln +41 -0
  37. package/examples/telegram-bot.pln +42 -0
  38. package/examples/web-app.pln +20 -0
  39. package/examples/websocket-server.pln +27 -0
  40. package/examples/whatsapp-bot/pairing.pln +17 -0
  41. package/examples/whatsapp-bot/qr.pln +8 -0
  42. package/package.json +47 -0
@@ -0,0 +1,86 @@
1
+ // Bundler: resolves PlainScript import statements, builds a dependency graph,
2
+ // and returns a single concatenated JavaScript output.
3
+
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const { tokenize } = require('./lexer');
7
+ const { parse } = require('./parser');
8
+ const { generate, createGenerationContext, wrapAsync } = require('./generator');
9
+
10
+ // Returns the import paths declared at the top level of an AST.
11
+ function getImports(ast) {
12
+ return ast.body
13
+ .filter(node => node.type === 'ImportStatement')
14
+ .map(node => node.path);
15
+ }
16
+
17
+ // Resolves all dependencies of entryPath using DFS topological order.
18
+ // Returns an array of { absPath, ast } in compile order (deepest dependencies first).
19
+ // Throws on circular imports or missing files.
20
+ function resolveDependencies(entryPath) {
21
+ const absoluteEntry = path.resolve(entryPath);
22
+ const visited = new Set(); // fully processed files (don't revisit)
23
+ const order = []; // final compile order
24
+
25
+ function visit(absPath, stack) {
26
+ // Already fully processed — skip (handles duplicate imports)
27
+ if (visited.has(absPath)) return;
28
+
29
+ // File is in the current DFS stack → circular dependency
30
+ const cycleIdx = stack.indexOf(absPath);
31
+ if (cycleIdx !== -1) {
32
+ const cycle = [...stack.slice(cycleIdx), absPath]
33
+ .map(p => ` "${path.relative(process.cwd(), p) || path.basename(p)}"`)
34
+ .join(' →\n');
35
+ throw new Error(
36
+ `Circular import detected:\n${cycle}\n\nPlain cannot compile files with circular dependencies.`
37
+ );
38
+ }
39
+
40
+ // File does not exist
41
+ if (!fs.existsSync(absPath)) {
42
+ throw new Error(
43
+ `Cannot find file "${path.relative(process.cwd(), absPath) || absPath}".\n\nMake sure the file exists and the path is correct.`
44
+ );
45
+ }
46
+
47
+ // Parse the file to discover its own imports.
48
+ // Annotate any tokenise/parse error with the filename so callers see
49
+ // "file.pln — Line N, Column N: …" rather than a bare positional message.
50
+ const source = fs.readFileSync(absPath, 'utf8');
51
+ let tokens, ast;
52
+ try {
53
+ tokens = tokenize(source);
54
+ ast = parse(tokens);
55
+ } catch (err) {
56
+ const relPath = path.relative(process.cwd(), absPath) || path.basename(absPath);
57
+ throw new Error(`${relPath} — ${err.message}`);
58
+ }
59
+
60
+ // Recurse into each import before processing this file (DFS)
61
+ const newStack = [...stack, absPath];
62
+ for (const importPath of getImports(ast)) {
63
+ const dir = path.dirname(absPath);
64
+ const resolvedAbs = path.resolve(dir, importPath);
65
+ visit(resolvedAbs, newStack);
66
+ }
67
+
68
+ // All dependencies are done — add this file to the order
69
+ visited.add(absPath);
70
+ order.push({ absPath, ast });
71
+ }
72
+
73
+ visit(absoluteEntry, []);
74
+ return order;
75
+ }
76
+
77
+ // Compile all files in dependency order into one JavaScript string.
78
+ function bundle(entryPath) {
79
+ const files = resolveDependencies(entryPath);
80
+ const context = createGenerationContext();
81
+ const parts = files.map(({ ast }) => generate(ast, context)).filter(js => js.trim() !== '');
82
+ const js = parts.join('\n');
83
+ return context.needsAsync ? wrapAsync(js) : js;
84
+ }
85
+
86
+ module.exports = { bundle, resolveDependencies };