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,13 @@
1
+ remember players as [
2
+ "Haaland",
3
+ "Foden",
4
+ "Rodri"
5
+ ]
6
+
7
+ show players[0]
8
+
9
+ players[1] becomes "Palmer"
10
+
11
+ show players[1]
12
+
13
+ show length(players)
@@ -0,0 +1,29 @@
1
+ // PlainScript v2.1 — Cache, background jobs and cron working together
2
+ //
3
+ // Run with: REDIS_URL=redis://localhost:6379 plainscript run examples/cache-and-jobs.pln
4
+ //
5
+ // Heavy work is pushed off the request path with "run background", shared
6
+ // state lives in Redis, and a cron job refreshes it every minute.
7
+
8
+ cache env("REDIS_URL")
9
+
10
+ make generateReport()
11
+ show "Generating report…"
12
+ give "report-data-" + date()
13
+ done
14
+
15
+ // Refresh the cached report every minute
16
+ schedule "* * * * *"
17
+ run background cacheSet("report", generateReport())
18
+ done
19
+
20
+ remember report as cacheGet("report")
21
+ if report is empty
22
+ show "No report yet — generating one now"
23
+ remember fresh as generateReport()
24
+ cacheSet("report", fresh, 3600)
25
+ otherwise
26
+ show "Cached report: " + report
27
+ done
28
+
29
+ show "Cache demo running"
@@ -0,0 +1,41 @@
1
+ // PlainScript v2.1 — Databases: SQLite, parameters and transactions
2
+ //
3
+ // Run with: plainscript run examples/database.pln
4
+ //
5
+ // SQL placeholders are written {likeThis} and bind to PlainScript variables of
6
+ // the same name. A transaction block runs all enclosed writes atomically.
7
+
8
+ database "plainscript-demo.db"
9
+
10
+ execute
11
+ CREATE TABLE IF NOT EXISTS users (
12
+ name TEXT,
13
+ age NUMBER
14
+ )
15
+ done
16
+
17
+ remember who as "ana"
18
+ insert
19
+ INSERT INTO users (name, age) VALUES ({who}, 30)
20
+ done
21
+
22
+ // Several writes that must succeed or fail together
23
+ transaction
24
+ insert
25
+ INSERT INTO users (name, age) VALUES ('bo', 31)
26
+ done
27
+ insert
28
+ INSERT INTO users (name, age) VALUES ('cy', 40)
29
+ done
30
+ done
31
+
32
+ remember minAge as 30
33
+ remember adults as query
34
+ SELECT name FROM users WHERE age >= {minAge} ORDER BY name
35
+ done
36
+
37
+ for each row in adults
38
+ show row.name
39
+ done
40
+
41
+ show "Database demo complete"
@@ -0,0 +1,7 @@
1
+ remember age as 16
2
+
3
+ if age is greater than 12
4
+ show "Teenager"
5
+ otherwise
6
+ show "Child"
7
+ done
@@ -0,0 +1,11 @@
1
+ make greet()
2
+ show "Hello"
3
+ done
4
+
5
+ greet()
6
+
7
+ make add(a, b)
8
+ give a + b
9
+ done
10
+
11
+ show add(5, 7)
@@ -0,0 +1,12 @@
1
+ // PlainScript v1.0.1 — runtime dependency and deployment example
2
+ //
3
+ // `plainscript build` and `plainscript run` detect express from `web app` and install it
4
+ // when it is missing. `plainscript start` builds src/app.pln by default.
5
+
6
+ web app
7
+
8
+ route "/"
9
+ reply "PlainScript is ready to deploy!"
10
+ done
11
+
12
+ start 3000
@@ -0,0 +1,22 @@
1
+ // PlainScript v2.1 — Sending email
2
+ //
3
+ // Run with: MAIL_PASS=secret plainscript run examples/email-send.pln
4
+ //
5
+ // Configure the outgoing transport once, then send as many mails as you
6
+ // like. Credentials come from the environment — never hardcode them.
7
+
8
+ mail transport
9
+ host is "smtp.gmail.com"
10
+ port is 587
11
+ user is env("EMAIL_USER")
12
+ pass is env("EMAIL_PASS")
13
+ done
14
+
15
+ send mail
16
+ from is "hello@plainscript.dev"
17
+ to is "you@example.com"
18
+ subject is "Hello from PlainScript"
19
+ text is "This email was sent from a PlainScript program."
20
+ done
21
+
22
+ show "Email sent successfully"
@@ -0,0 +1,40 @@
1
+ // PlainScript v1.1 — PlainScript Expressions Examples
2
+
3
+ remember players as ["Haaland", "Foden", "Rodri"]
4
+
5
+ // Items
6
+ show first player from players
7
+ show last player from players
8
+ show player two from players
9
+
10
+ first player from players becomes "Kane"
11
+ show first player from players
12
+
13
+ // Collections
14
+ show players length
15
+
16
+ add("Palmer" to players)
17
+ show players length
18
+
19
+ remove("Rodri" from players)
20
+ show players length
21
+
22
+ if players contains "Foden"
23
+ show "Foden is in the squad"
24
+ done
25
+
26
+ // Properties
27
+ remember user as
28
+ name is "Ayokunle"
29
+ age is 17
30
+ done
31
+
32
+ show name of user
33
+
34
+ name of user becomes "Ayo"
35
+ show name of user
36
+
37
+ // Files
38
+ write("Hello from PlainScript Expressions" to "expressions.txt")
39
+ remember content as read("expressions.txt")
40
+ show content
@@ -0,0 +1,90 @@
1
+ web app
2
+ database ":memory:"
3
+ enable sessions "touchline-session-secret"
4
+ remember coachHash as hashPassword("touchline")
5
+
6
+ execute
7
+ create table teams (
8
+ id integer primary key autoincrement,
9
+ name text not null,
10
+ played integer default 0,
11
+ won integer default 0,
12
+ drawn integer default 0,
13
+ lost integer default 0
14
+ )
15
+ done
16
+
17
+ insert
18
+ insert into teams (name, played, won, drawn, lost) values
19
+ ('Thunder FC', 3, 2, 1, 0),
20
+ ('Harbor City', 3, 1, 1, 1),
21
+ ('Old Dock', 3, 0, 2, 1)
22
+ done
23
+
24
+ route post "/login"
25
+ remember ok as checkPassword(password of body of request, coachHash)
26
+ if ok is true
27
+ user of session of request becomes username of body of request
28
+ reply json
29
+ token is createToken(username of body of request, "touchline-secret")
30
+ user is username of body of request
31
+ done
32
+ otherwise
33
+ status 401
34
+ reply json
35
+ error is "Wrong password"
36
+ done
37
+ done
38
+ done
39
+
40
+ route get "/me"
41
+ reply json
42
+ user is user of session of request
43
+ done
44
+ done
45
+
46
+ require api key from "coach-key-42"
47
+
48
+ route get "/teams"
49
+ remember rows as query
50
+ select id, name, played, won, drawn, lost from teams order by name
51
+ done
52
+ reply json
53
+ teams is rows
54
+ done
55
+ done
56
+
57
+ route post "/teams"
58
+ remember missing as validate(body of request, ["name"])
59
+ remember problems as length(missing)
60
+ if problems is greater than 0
61
+ status 400
62
+ reply missing
63
+ otherwise
64
+ remember teamName as name of body of request
65
+ insert
66
+ insert into teams (name, played, won, drawn, lost) values ({teamName}, 0, 0, 0, 0)
67
+ done
68
+ reply json
69
+ added is teamName
70
+ done
71
+ done
72
+ done
73
+
74
+ route get "/standings"
75
+ remember rows as query
76
+ select name, won, drawn, lost from teams order by won desc, name
77
+ done
78
+ reply json
79
+ standings is rows
80
+ done
81
+ done
82
+
83
+ when nothing matches
84
+ status 404
85
+ reply json
86
+ error is "No such endpoint"
87
+ done
88
+ done
89
+
90
+ start 0
@@ -0,0 +1,3 @@
1
+ remember name as "Ayokunle"
2
+
3
+ show name
@@ -0,0 +1,29 @@
1
+ web app
2
+ accept uploads limit "5 MB" allow ["image/png", "image/jpeg"] folder "uploads"
3
+
4
+ route post "/verify"
5
+ remember found as uploads("id")
6
+ remember count as length(found)
7
+ if count is 0
8
+ status 400
9
+ reply json
10
+ error is "Attach the ID image in the id field"
11
+ done
12
+ otherwise
13
+ remember file as upload("id")
14
+ ocr path of file as scanned
15
+ if scanned contains "ADA LOVELACE"
16
+ reply json
17
+ verified is true
18
+ holder is "Ada Lovelace"
19
+ done
20
+ otherwise
21
+ reply json
22
+ verified is false
23
+ holder is "unknown"
24
+ done
25
+ done
26
+ done
27
+ done
28
+
29
+ start 0
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env node
2
+ // make-sample-id.js — dependency-free synthetic ID card generator.
3
+ //
4
+ // Encodes a name as crisp black-on-white pixel text inside a valid PNG
5
+ // (hand-rolled encoder: zlib deflate + CRC32, no external packages).
6
+ //
7
+ // Usage:
8
+ // node make-sample-id.js writes sample-id.png ("ADA LOVELACE")
9
+ // node make-sample-id.js out.png "IDA CARD"
10
+ //
11
+ // As a module: const { createIdPng } = require('./make-sample-id')
12
+
13
+ 'use strict';
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+ const zlib = require('zlib');
17
+
18
+ // 5x7 pixel font. Each glyph is 7 rows; each row is a 5-bit number
19
+ // (bit 4 = leftmost column). Only the characters this sample needs.
20
+ const FONT = {
21
+ A: [14, 17, 17, 31, 17, 17, 17],
22
+ C: [14, 17, 16, 16, 16, 17, 14],
23
+ D: [30, 17, 17, 17, 17, 17, 30],
24
+ E: [31, 16, 16, 30, 16, 16, 31],
25
+ I: [14, 4, 4, 4, 4, 4, 14],
26
+ L: [16, 16, 16, 16, 16, 16, 31],
27
+ N: [17, 25, 25, 21, 19, 19, 17],
28
+ O: [14, 17, 17, 17, 17, 17, 14],
29
+ R: [30, 17, 17, 30, 20, 18, 17],
30
+ V: [17, 17, 17, 17, 17, 10, 4],
31
+ ' ': [0, 0, 0, 0, 0, 0, 0],
32
+ };
33
+
34
+ const WIDTH = 480;
35
+ const HEIGHT = 160;
36
+ const SCALE = 6; // each font pixel becomes SCALE x SCALE real pixels
37
+ const MARGIN_X = 24;
38
+ const LINE_HEIGHT = 60;
39
+
40
+ function drawText(pixels, text, x0, y0) {
41
+ let x = x0;
42
+ for (const ch of text.toUpperCase()) {
43
+ const glyph = FONT[ch];
44
+ if (!glyph) { x += 6 * SCALE; continue; }
45
+ for (let row = 0; row < 7; row++) {
46
+ for (let col = 0; col < 5; col++) {
47
+ if ((glyph[row] >> (4 - col)) & 1) {
48
+ for (let dy = 0; dy < SCALE; dy++) {
49
+ for (let dx = 0; dx < SCALE; dx++) {
50
+ const px = x + col * SCALE + dx;
51
+ const py = y0 + row * SCALE + dy;
52
+ if (px >= 0 && px < WIDTH && py >= 0 && py < HEIGHT) {
53
+ const i = (py * WIDTH + px) * 3;
54
+ pixels[i] = pixels[i + 1] = pixels[i + 2] = 0;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
61
+ x += 6 * SCALE; // 5 columns + 1 column gap
62
+ }
63
+ }
64
+
65
+ function crc32(buf) {
66
+ let table = crc32.table;
67
+ if (!table) {
68
+ table = crc32.table = new Int32Array(256);
69
+ for (let n = 0; n < 256; n++) {
70
+ let c = n;
71
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
72
+ table[n] = c;
73
+ }
74
+ }
75
+ let crc = -1;
76
+ for (let i = 0; i < buf.length; i++) crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xff];
77
+ return (crc ^ -1) >>> 0;
78
+ }
79
+
80
+ function chunk(type, data) {
81
+ const len = Buffer.alloc(4);
82
+ len.writeUInt32BE(data.length);
83
+ const body = Buffer.concat([Buffer.from(type, 'ascii'), data]);
84
+ const crc = Buffer.alloc(4);
85
+ crc.writeUInt32BE(crc32(body));
86
+ return Buffer.concat([len, body, crc]);
87
+ }
88
+
89
+ function createIdPng(lines) {
90
+ const pixels = Buffer.alloc(WIDTH * HEIGHT * 3, 255); // white background
91
+ lines.forEach((line, index) => {
92
+ drawText(pixels, line, MARGIN_X, 24 + index * LINE_HEIGHT);
93
+ });
94
+
95
+ // PNG scanlines: one filter byte (0 = None) per row.
96
+ const raw = Buffer.alloc(HEIGHT * (WIDTH * 3 + 1));
97
+ for (let y = 0; y < HEIGHT; y++) {
98
+ raw[y * (WIDTH * 3 + 1)] = 0;
99
+ pixels.copy(raw, y * (WIDTH * 3 + 1) + 1, y * WIDTH * 3, (y + 1) * WIDTH * 3);
100
+ }
101
+
102
+ const ihdr = Buffer.alloc(13);
103
+ ihdr.writeUInt32BE(WIDTH, 0);
104
+ ihdr.writeUInt32BE(HEIGHT, 4);
105
+ ihdr[8] = 8; // bit depth
106
+ ihdr[9] = 2; // color type: truecolor RGB
107
+
108
+ return Buffer.concat([
109
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
110
+ chunk('IHDR', ihdr),
111
+ chunk('IDAT', zlib.deflateSync(raw)),
112
+ chunk('IEND', Buffer.alloc(0)),
113
+ ]);
114
+ }
115
+
116
+ module.exports = { createIdPng, FONT, WIDTH, HEIGHT, SCALE, MARGIN_X, LINE_HEIGHT };
117
+
118
+ if (require.main === module) {
119
+ const out = process.argv[2] || path.join(__dirname, 'sample-id.png');
120
+ const lines = process.argv.length > 3 ? process.argv.slice(3) : ['ADA LOVELACE', 'ID CARD'];
121
+ fs.writeFileSync(out, createIdPng(lines));
122
+ console.log(`wrote ${out} (${lines.join(' / ')})`);
123
+ }
@@ -0,0 +1,17 @@
1
+ remember players as [
2
+ "Haaland",
3
+ "Foden",
4
+ "Rodri"
5
+ ]
6
+
7
+ for each player in players
8
+ show player
9
+ done
10
+
11
+ remember count as 0
12
+
13
+ while count is less than 3
14
+ count becomes count + 1
15
+ done
16
+
17
+ show count
@@ -0,0 +1,12 @@
1
+ remember user as
2
+ name is "Ayokunle"
3
+ age is 17
4
+ country is "Nigeria"
5
+ done
6
+
7
+ show user.name
8
+ show user.age
9
+
10
+ user.age becomes 18
11
+
12
+ show user.age
@@ -0,0 +1,94 @@
1
+ // REST API with upstream fetches — multi-source integration example
2
+ //
3
+ // A REST API that calls other APIs with the built-in HTTP client and
4
+ // combines the results. Everything here compiles deterministically:
5
+ // no rules, no AI, no hidden code generation.
6
+
7
+ web app
8
+
9
+ // Health check
10
+ route get "/health"
11
+ reply json
12
+ status is "ok"
13
+ version is "0.1.7"
14
+ done
15
+ done
16
+
17
+ // Fetch a random user from randomuser.me
18
+ route get "/api/random-user"
19
+ remember response as get "https://randomuser.me/api/"
20
+ if ok of response is true
21
+ reply data of response
22
+ otherwise
23
+ status 502
24
+ reply json
25
+ error is "Failed to fetch user"
26
+ done
27
+ done
28
+ done
29
+
30
+ // Fetch multiple APIs and combine the results
31
+ route get "/api/dashboard"
32
+ remember userRes as get "https://randomuser.me/api/"
33
+ remember jokeRes as get "https://official-joke-api.appspot.com/random_joke"
34
+
35
+ remember user as null
36
+ remember joke as null
37
+
38
+ if ok of userRes is true
39
+ user becomes first person from results of data of userRes
40
+ done
41
+
42
+ if ok of jokeRes is true
43
+ joke becomes data of jokeRes
44
+ done
45
+
46
+ reply json
47
+ user is user
48
+ joke is joke
49
+ done
50
+ done
51
+
52
+ // Search with query parameters
53
+ route get "/api/search"
54
+ remember q as query("q")
55
+ remember limit as query("limit")
56
+ remember results as []
57
+ reply json
58
+ q is q
59
+ limit is limit
60
+ results is results
61
+ done
62
+ done
63
+
64
+ // Create an item
65
+ route post "/api/items"
66
+ reply json
67
+ created is true
68
+ item is body of request
69
+ done
70
+ done
71
+
72
+ // Update an item
73
+ route put "/api/items/:id"
74
+ remember id as param("id")
75
+ reply json
76
+ updated is id
77
+ item is body of request
78
+ done
79
+ done
80
+
81
+ // Delete an item
82
+ route delete "/api/items/:id"
83
+ remember id as param("id")
84
+ reply json
85
+ deleted is id
86
+ done
87
+ done
88
+
89
+ when nothing matches
90
+ status 404
91
+ reply "no such road"
92
+ done
93
+
94
+ start 3000
@@ -0,0 +1,68 @@
1
+ // PlainScript v2.1 — A real REST API
2
+ //
3
+ // Run with: plainscript run examples/rest-api.pln
4
+ // Try: curl http://localhost:3000/api/users
5
+ //
6
+ // Demonstrates method routes, route groups, CORS, request validation,
7
+ // path/query/header accessors, status codes and a SQLite database.
8
+
9
+ database "rest-api-demo.db"
10
+
11
+ execute
12
+ CREATE TABLE IF NOT EXISTS users (
13
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
14
+ name TEXT,
15
+ email TEXT
16
+ )
17
+ done
18
+
19
+ web app
20
+ allow cors
21
+
22
+ group "/api"
23
+
24
+ route get "/users"
25
+ remember users as query
26
+ SELECT * FROM users
27
+ done
28
+ reply users
29
+ done
30
+
31
+ route post "/users"
32
+ remember missing as validate(body of request, ["name", "email"])
33
+ remember count as length(missing)
34
+ if count is greater than 0
35
+ status 400
36
+ reply missing
37
+ otherwise
38
+ remember who as body of request.name
39
+ remember mail as body of request.email
40
+ insert
41
+ INSERT INTO users (name, email) VALUES ({who}, {mail})
42
+ done
43
+ status 201
44
+ reply "created"
45
+ done
46
+ done
47
+
48
+ route get "/users/:id"
49
+ remember id as param("id")
50
+ remember found as query
51
+ SELECT * FROM users WHERE id = {id}
52
+ done
53
+ if length(found) is greater than 0
54
+ reply found[0]
55
+ otherwise
56
+ status 404
57
+ reply "not found"
58
+ done
59
+ done
60
+
61
+ done
62
+
63
+ route get "/health"
64
+ remember token as header("x-token")
65
+ reply "ok"
66
+ done
67
+
68
+ start 3000
@@ -0,0 +1,23 @@
1
+ // PlainScript v2.1 — Scheduled and recurring tasks
2
+ //
3
+ // Run with: plainscript run examples/scheduled-tasks.pln
4
+ //
5
+ // "every" covers simple intervals; "schedule" accepts any standard cron
6
+ // expression (powered by croner).
7
+
8
+ // A heartbeat every 30 seconds
9
+ every 30 seconds
10
+ show "Heartbeat: " + now()
11
+ done
12
+
13
+ // Any standard cron expression
14
+ schedule "*/5 * * * *"
15
+ show "Five minutes have passed"
16
+ done
17
+
18
+ // Nightly cleanup at 2 AM
19
+ schedule "0 2 * * *"
20
+ show "Running the nightly cleanup"
21
+ done
22
+
23
+ show "Scheduled tasks registered — press Ctrl+C to stop"
@@ -0,0 +1,25 @@
1
+ use express
2
+
3
+ remember app as express()
4
+
5
+ serve folder "public"
6
+
7
+ when someone visits "/"
8
+ reply "Hello from PlainScript!"
9
+ done
10
+
11
+ when someone visits "/about"
12
+ reply "This server is written in PlainScript."
13
+ done
14
+
15
+ when someone visits "/api/status"
16
+ reply json
17
+ status is "ok"
18
+ language is "PlainScript"
19
+ version is "0.3"
20
+ done
21
+ done
22
+
23
+ listen on 3000
24
+ show "Server running at http://localhost:3000"
25
+ done
@@ -0,0 +1,2 @@
1
+ // PlainScript v1.0.1 — entry file for `plainscript start`
2
+ show "Application started by PlainScript"