plainscript-lang 1.0.1 → 1.0.2-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 (106) hide show
  1. package/README.md +194 -729
  2. package/compiler/bundler.js +2 -2
  3. package/compiler/cli.js +69 -73
  4. package/compiler/dependency-detector.js +5 -0
  5. package/compiler/formatter.js +1 -1
  6. package/compiler/generator.js +931 -89
  7. package/compiler/lexer.js +129 -53
  8. package/compiler/parser.js +995 -152
  9. package/compiler/version.js +1 -1
  10. package/docs/CAPABILITY_GAP_AUDIT.md +98 -435
  11. package/docs/PLAINSCRIPT-SPEC.md +335 -0
  12. package/docs/index.html +101 -608
  13. package/docs/website/script.js +59 -943
  14. package/docs/website/style.css +74 -656
  15. package/examples/ai-providers.pln +7 -0
  16. package/examples/arrays.pln +4 -0
  17. package/examples/async-errors.pln +18 -0
  18. package/examples/auth-api.pln +9 -0
  19. package/examples/auth-sessions.pln +32 -0
  20. package/examples/basics.pln +14 -0
  21. package/examples/bots.pln +13 -0
  22. package/examples/cache-and-jobs.pln +7 -0
  23. package/examples/cache-schedule.pln +22 -0
  24. package/examples/collections.pln +15 -0
  25. package/examples/concurrency.pln +18 -0
  26. package/examples/conditions.pln +22 -0
  27. package/examples/database.pln +8 -0
  28. package/examples/day2.pln +7 -0
  29. package/examples/day3.pln +5 -0
  30. package/examples/deployment.pln +6 -0
  31. package/examples/email-send.pln +3 -0
  32. package/examples/error-handling.pln +7 -0
  33. package/examples/expressions.pln +7 -0
  34. package/examples/file-uploads.pln +10 -0
  35. package/examples/files-and-config.pln +6 -0
  36. package/examples/files.pln +12 -0
  37. package/examples/football-backend/{app.ps → app.pln} +22 -21
  38. package/examples/functions.pln +17 -0
  39. package/examples/hello.pln +2 -0
  40. package/examples/http-client.pln +3 -0
  41. package/examples/http.pln +11 -0
  42. package/examples/id-verification/{app.ps → app.pln} +5 -4
  43. package/examples/id-verification/make-sample-id.js +29 -42
  44. package/examples/json.pln +11 -0
  45. package/examples/loops.pln +18 -0
  46. package/examples/modules/app.pln +3 -0
  47. package/examples/modules/main.pln +4 -0
  48. package/examples/modules/math.pln +9 -0
  49. package/examples/mongo.pln +33 -0
  50. package/examples/objects.pln +4 -0
  51. package/examples/ocr.pln +22 -0
  52. package/examples/packages.pln +6 -0
  53. package/examples/record-kinds.pln +7 -0
  54. package/examples/records.pln +15 -0
  55. package/examples/rest-api-fetch.pln +10 -0
  56. package/examples/rest-api.pln +8 -0
  57. package/examples/scheduled-tasks.pln +4 -0
  58. package/examples/search-and-filter.pln +4 -0
  59. package/examples/server.pln +6 -0
  60. package/examples/sessions-and-cookies.pln +8 -0
  61. package/examples/sqlite.pln +23 -0
  62. package/examples/start.pln +2 -0
  63. package/examples/stdlib.pln +4 -0
  64. package/examples/telegram-ai-bot.pln +19 -0
  65. package/examples/telegram-bot.pln +6 -0
  66. package/examples/testing.pln +13 -0
  67. package/examples/web-api.pln +27 -0
  68. package/examples/web-app.pln +8 -0
  69. package/examples/websocket-server.pln +6 -0
  70. package/examples/websocket.pln +13 -0
  71. package/examples/whatsapp-bot/{pairing.ps → pairing.pln} +4 -5
  72. package/examples/whatsapp-bot/qr.pln +12 -0
  73. package/package.json +3 -2
  74. package/docs/PLINJS_SPEC.md +0 -1151
  75. package/examples/arrays.ps +0 -13
  76. package/examples/auth-api.ps +0 -56
  77. package/examples/cache-and-jobs.ps +0 -29
  78. package/examples/concurrency.ps +0 -49
  79. package/examples/database.ps +0 -41
  80. package/examples/day2.ps +0 -7
  81. package/examples/day3.ps +0 -11
  82. package/examples/deployment.ps +0 -12
  83. package/examples/email-send.ps +0 -22
  84. package/examples/error-handling.ps +0 -54
  85. package/examples/expressions.ps +0 -40
  86. package/examples/file-uploads.ps +0 -39
  87. package/examples/files-and-config.ps +0 -47
  88. package/examples/hello.ps +0 -3
  89. package/examples/http-client.ps +0 -44
  90. package/examples/loops.ps +0 -17
  91. package/examples/modules/app.ps +0 -16
  92. package/examples/modules/math.ps +0 -20
  93. package/examples/objects.ps +0 -12
  94. package/examples/record-kinds.ps +0 -43
  95. package/examples/rest-api-fetch.ps +0 -94
  96. package/examples/rest-api.ps +0 -68
  97. package/examples/scheduled-tasks.ps +0 -23
  98. package/examples/search-and-filter.ps +0 -34
  99. package/examples/server.ps +0 -25
  100. package/examples/sessions-and-cookies.ps +0 -54
  101. package/examples/start.ps +0 -2
  102. package/examples/stdlib.ps +0 -41
  103. package/examples/telegram-bot.ps +0 -42
  104. package/examples/web-app.ps +0 -20
  105. package/examples/websocket-server.ps +0 -27
  106. package/examples/whatsapp-bot/qr.ps +0 -8
@@ -0,0 +1,7 @@
1
+ // PlainScript 1.0.2-latest: provider-backed AI works in ordinary programs.
2
+
3
+ remember answer as chatWith("groq", "llama-3.3-70b-versatile", "Explain PlainScript in one sentence")
4
+ show answer
5
+
6
+ remember embedding as embedWith("groq", "nomic-embed-text-v1.5", "PlainScript")
7
+ show length(embedding)
@@ -0,0 +1,4 @@
1
+ // PlainScript 1.0.2-latest: array literals and indexing.
2
+ remember values as [1, 2, 3]
3
+ show values[0]
4
+ show length(values)
@@ -0,0 +1,18 @@
1
+ // PlainScript 1.0.2-latest: asynchronous work and error recovery.
2
+
3
+ make loadProfile(id)
4
+ wait for sleep(10)
5
+ give record with id id and ready true done
6
+ done
7
+
8
+ try
9
+ remember profile as wait for loadProfile(7)
10
+ show profile.ready
11
+ recover as err
12
+ show message of err
13
+ finally
14
+ show "request complete"
15
+
16
+ retry 3 times every 2 seconds
17
+ show "retrying"
18
+ done
@@ -0,0 +1,9 @@
1
+ // PlainScript 1.0.2-latest: protected API route.
2
+ web app
3
+ require api key from env("API_KEY")
4
+ route get "/private"
5
+ reply json
6
+ ok is true
7
+ done
8
+ done
9
+ start 3000
@@ -0,0 +1,32 @@
1
+ // PlainScript 1.0.2-latest: API keys, sessions, cookies, and auth helpers.
2
+
3
+ web app
4
+ enable sessions "development-only-session-secret"
5
+ require api key from "demo-key"
6
+ limit requests to 60 per minute
7
+
8
+ route post "/login"
9
+ remember passwordOk as checkPassword(password of body of request, hashPassword("secret"))
10
+ if passwordOk is true
11
+ user of session of request becomes "ada"
12
+ set cookie "theme" to "dark" expires in 7 days
13
+ reply json
14
+ authenticated is true
15
+ token is createToken("ada", "token-secret")
16
+ done
17
+ otherwise
18
+ status 401
19
+ reply json
20
+ authenticated is false
21
+ done
22
+ done
23
+ done
24
+
25
+ route get "/me"
26
+ reply json
27
+ user is user of session of request
28
+ theme is cookie("theme")
29
+ done
30
+ done
31
+
32
+ start 0
@@ -0,0 +1,14 @@
1
+ // PlainScript 1.0.2-latest: variables, values, and output.
2
+
3
+ remember name as "Ada"
4
+ remember visitCount as 3
5
+ let greeting be `Hello, ${name}!`
6
+
7
+ show greeting
8
+ show visitCount + 1
9
+
10
+ if visitCount is at least 3
11
+ show "Returning visitor"
12
+ otherwise
13
+ show "First visit"
14
+ done
@@ -0,0 +1,13 @@
1
+ // PlainScript 1.0.2-latest: Telegram bot handlers.
2
+
3
+ bot env("TELEGRAM_BOT_TOKEN")
4
+
5
+ when someone sends "/start"
6
+ reply "Welcome to PlainScript"
7
+ done
8
+
9
+ when someone sends matching "/echo (.+)"
10
+ reply "echo"
11
+ done
12
+
13
+ start telegram bot
@@ -0,0 +1,7 @@
1
+ // PlainScript 1.0.2-latest: cache and background work.
2
+ cache env("REDIS_URL")
3
+ cacheSet("status", "ready", 60)
4
+ remember status as cacheGet("status")
5
+ every 5 minutes
6
+ show status
7
+ done
@@ -0,0 +1,22 @@
1
+ // PlainScript 1.0.2-latest: cache values and scheduled work.
2
+
3
+ cache "redis://localhost:6379"
4
+
5
+ make refresh()
6
+ remember value as `updated at ${time()}`
7
+ cacheSet("status", value, 60)
8
+ give value
9
+ done
10
+
11
+ every 5 minutes
12
+ run background refresh()
13
+ done
14
+
15
+ schedule "0 * * * *"
16
+ remember status as cacheGet("status")
17
+ if status is empty
18
+ refresh()
19
+ otherwise
20
+ show status
21
+ done
22
+ done
@@ -0,0 +1,15 @@
1
+ // PlainScript 1.0.2-latest: lists, objects, and collection helpers.
2
+
3
+ remember numbers as list with 4, 7, 2, 9
4
+ gather each number in numbers giving number * 2
5
+ filter each number in numbers when number is at least 8
6
+ show numbers
7
+
8
+ remember values as list with 3, 1, 3, 2
9
+ remember uniqueValues as unique(values)
10
+ remember ordered as sort(values)
11
+ show uniqueValues
12
+ show ordered
13
+
14
+ remember profile as {name: "Ada", active: true}
15
+ show profile.name
@@ -0,0 +1,18 @@
1
+ // PlainScript 1.0.2-latest: parallel operations and timeouts.
2
+
3
+ make firstJob()
4
+ wait for sleep(10)
5
+ give "first"
6
+ done
7
+
8
+ make secondJob()
9
+ wait for sleep(20)
10
+ give "second"
11
+ done
12
+
13
+ remember results as allOf(list with firstJob(), secondJob())
14
+ show results
15
+
16
+ remember quick as withTimeout(firstJob(), 1000)
17
+ show quick
18
+ run background secondJob()
@@ -0,0 +1,22 @@
1
+ // PlainScript 1.0.2-latest: conditions and string predicates.
2
+
3
+ remember score as 87
4
+ remember label as "PlainScript"
5
+
6
+ if score is at least 90
7
+ show "excellent"
8
+ otherwise
9
+ if score more than 70
10
+ show "good"
11
+ otherwise
12
+ show "keep practicing"
13
+ done
14
+ done
15
+
16
+ if label contains "Script" and label starts with "Plain"
17
+ show "language found"
18
+ done
19
+
20
+ if label ends as "Script"
21
+ show "suffix matched"
22
+ done
@@ -0,0 +1,8 @@
1
+ // PlainScript 1.0.2-latest: SQLite database.
2
+ database "app.db" using "wasm"
3
+ execute
4
+ CREATE TABLE IF NOT EXISTS notes (body TEXT)
5
+ done
6
+ remember notes as query
7
+ SELECT body FROM notes
8
+ done
@@ -0,0 +1,7 @@
1
+ // PlainScript 1.0.2-latest: conditional blocks.
2
+ remember age as 21
3
+ if age is at least 18
4
+ show "adult"
5
+ otherwise
6
+ show "minor"
7
+ done
@@ -0,0 +1,5 @@
1
+ // PlainScript 1.0.2-latest: functions.
2
+ make greet(name)
3
+ give `Hello, ${name}!`
4
+ done
5
+ show greet("Ada")
@@ -0,0 +1,6 @@
1
+ // PlainScript 1.0.2-latest: production-shaped web entry point.
2
+ web app
3
+ route get "/health"
4
+ reply "ok"
5
+ done
6
+ start 3000
@@ -0,0 +1,3 @@
1
+ // PlainScript 1.0.2-latest: email application placeholder.
2
+ remember recipient as "team@example.com"
3
+ show `mail queued for ${recipient}`
@@ -0,0 +1,7 @@
1
+ // PlainScript 1.0.2-latest: recovery and cleanup.
2
+ try
3
+ show "work"
4
+ recover as error
5
+ show message of error
6
+ finally
7
+ show "finished"
@@ -0,0 +1,7 @@
1
+ // PlainScript 1.0.2-latest: expression operators.
2
+ remember sumValue as (2 + 3) * 4
3
+ if sumValue is 20
4
+ show "exact"
5
+ otherwise
6
+ show "other"
7
+ done
@@ -0,0 +1,10 @@
1
+ // PlainScript 1.0.2-latest: upload route.
2
+ web app
3
+ accept uploads limit "5 MB" allow list with "image/png" folder "uploads"
4
+ route post "/upload"
5
+ remember image as upload("file")
6
+ reply json
7
+ uploaded is image
8
+ done
9
+ done
10
+ start 3000
@@ -0,0 +1,6 @@
1
+ // PlainScript 1.0.2-latest: files and environment configuration.
2
+ remember filename as env("CONFIG_FILE")
3
+ if filename is null
4
+ remember filename as "config.json"
5
+ done
6
+ show fileExists(filename)
@@ -0,0 +1,12 @@
1
+ // PlainScript 1.0.2-latest: file and path operations.
2
+
3
+ remember report as readFile("README.md")
4
+ show length(report)
5
+
6
+ remember outputPath as joinPath("tmp", "report.txt")
7
+ writeFile(outputPath, "PlainScript report")
8
+ appendFile(outputPath, "\nready")
9
+ show fileExists(outputPath)
10
+
11
+ remember parts as split(outputPath, "/")
12
+ show parts
@@ -1,29 +1,31 @@
1
+ // PlainScript 1.0.2-latest: a complete authenticated SQLite API.
2
+
1
3
  web app
2
- database ":memory:"
4
+ database ":memory:" using "wasm"
3
5
  enable sessions "touchline-session-secret"
4
6
  remember coachHash as hashPassword("touchline")
5
7
 
6
8
  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
9
+ CREATE TABLE teams (
10
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
11
+ name TEXT NOT NULL,
12
+ played INTEGER DEFAULT 0,
13
+ won INTEGER DEFAULT 0,
14
+ drawn INTEGER DEFAULT 0,
15
+ lost INTEGER DEFAULT 0
14
16
  )
15
17
  done
16
18
 
17
19
  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)
20
+ INSERT INTO teams (name, played, won, drawn, lost) VALUES
21
+ ("Thunder FC", 3, 2, 1, 0),
22
+ ("Harbor City", 3, 1, 1, 1),
23
+ ("Old Dock", 3, 0, 2, 1)
22
24
  done
23
25
 
24
26
  route post "/login"
25
- remember ok as checkPassword(password of body of request, coachHash)
26
- if ok is true
27
+ remember valid as checkPassword(password of body of request, coachHash)
28
+ if valid is true
27
29
  user of session of request becomes username of body of request
28
30
  reply json
29
31
  token is createToken(username of body of request, "touchline-secret")
@@ -47,7 +49,7 @@ require api key from "coach-key-42"
47
49
 
48
50
  route get "/teams"
49
51
  remember rows as query
50
- select id, name, played, won, drawn, lost from teams order by name
52
+ SELECT id, name, played, won, drawn, lost FROM teams ORDER BY name
51
53
  done
52
54
  reply json
53
55
  teams is rows
@@ -55,15 +57,14 @@ route get "/teams"
55
57
  done
56
58
 
57
59
  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
60
+ remember missing as validate(body of request, list with "name")
61
+ if length(missing) more than 0
61
62
  status 400
62
63
  reply missing
63
64
  otherwise
64
65
  remember teamName as name of body of request
65
66
  insert
66
- insert into teams (name, played, won, drawn, lost) values ({teamName}, 0, 0, 0, 0)
67
+ INSERT INTO teams (name, played, won, drawn, lost) VALUES ({teamName}, 0, 0, 0, 0)
67
68
  done
68
69
  reply json
69
70
  added is teamName
@@ -73,7 +74,7 @@ done
73
74
 
74
75
  route get "/standings"
75
76
  remember rows as query
76
- select name, won, drawn, lost from teams order by won desc, name
77
+ SELECT name, won, drawn, lost FROM teams ORDER BY won DESC, name
77
78
  done
78
79
  reply json
79
80
  standings is rows
@@ -87,4 +88,4 @@ when nothing matches
87
88
  done
88
89
  done
89
90
 
90
- start 0
91
+ start 0
@@ -0,0 +1,17 @@
1
+ // PlainScript 1.0.2-latest: functions, defaults, and returns.
2
+
3
+ make greet(name)
4
+ give `Hello, ${name}!`
5
+ done
6
+
7
+ make add(a, b)
8
+ give a + b
9
+ done
10
+
11
+ make label(name as "guest")
12
+ give `User: ${name}`
13
+ done
14
+
15
+ show greet("Ada")
16
+ show add(2, 3)
17
+ show label()
@@ -0,0 +1,2 @@
1
+ // PlainScript 1.0.2-latest: hello world.
2
+ show "Hello, PlainScript!"
@@ -0,0 +1,3 @@
1
+ // PlainScript 1.0.2-latest: HTTP client request.
2
+ remember response as get "https://example.com" timeout 5000
3
+ show response.status
@@ -0,0 +1,11 @@
1
+ // PlainScript 1.0.2-latest: HTTP client requests.
2
+
3
+ remember response as get "https://example.com/api/items" timeout 5000
4
+ if response.ok is true
5
+ show response.data
6
+ otherwise
7
+ show response.status
8
+ done
9
+
10
+ remember created as post "https://example.com/api/items" with {name: "Ada"} headers {contentType: "application/json"}
11
+ show created.status
@@ -1,10 +1,11 @@
1
+ // PlainScript 1.0.2-latest: upload and OCR verification service.
2
+
1
3
  web app
2
- accept uploads limit "5 MB" allow ["image/png", "image/jpeg"] folder "uploads"
4
+ accept uploads limit "5 MB" allow list with "image/png", "image/jpeg" folder "uploads"
3
5
 
4
6
  route post "/verify"
5
7
  remember found as uploads("id")
6
- remember count as length(found)
7
- if count is 0
8
+ if length(found) is 0
8
9
  status 400
9
10
  reply json
10
11
  error is "Attach the ID image in the id field"
@@ -26,4 +27,4 @@ route post "/verify"
26
27
  done
27
28
  done
28
29
 
29
- start 0
30
+ start 0
@@ -1,22 +1,12 @@
1
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')
2
+ // Dependency-free PNG generator used by the acceptance test.
12
3
 
13
4
  'use strict';
5
+
14
6
  const fs = require('fs');
15
7
  const path = require('path');
16
8
  const zlib = require('zlib');
17
9
 
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
10
  const FONT = {
21
11
  A: [14, 17, 17, 31, 17, 17, 17],
22
12
  C: [14, 17, 16, 16, 16, 17, 14],
@@ -33,7 +23,7 @@ const FONT = {
33
23
 
34
24
  const WIDTH = 480;
35
25
  const HEIGHT = 160;
36
- const SCALE = 6; // each font pixel becomes SCALE x SCALE real pixels
26
+ const SCALE = 6;
37
27
  const MARGIN_X = 24;
38
28
  const LINE_HEIGHT = 60;
39
29
 
@@ -41,7 +31,10 @@ function drawText(pixels, text, x0, y0) {
41
31
  let x = x0;
42
32
  for (const ch of text.toUpperCase()) {
43
33
  const glyph = FONT[ch];
44
- if (!glyph) { x += 6 * SCALE; continue; }
34
+ if (!glyph) {
35
+ x += 6 * SCALE;
36
+ continue;
37
+ }
45
38
  for (let row = 0; row < 7; row++) {
46
39
  for (let col = 0; col < 5; col++) {
47
40
  if ((glyph[row] >> (4 - col)) & 1) {
@@ -50,19 +43,19 @@ function drawText(pixels, text, x0, y0) {
50
43
  const px = x + col * SCALE + dx;
51
44
  const py = y0 + row * SCALE + dy;
52
45
  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;
46
+ const index = (py * WIDTH + px) * 3;
47
+ pixels[index] = pixels[index + 1] = pixels[index + 2] = 0;
55
48
  }
56
49
  }
57
50
  }
58
51
  }
59
52
  }
60
53
  }
61
- x += 6 * SCALE; // 5 columns + 1 column gap
54
+ x += 6 * SCALE;
62
55
  }
63
56
  }
64
57
 
65
- function crc32(buf) {
58
+ function crc32(buffer) {
66
59
  let table = crc32.table;
67
60
  if (!table) {
68
61
  table = crc32.table = new Int32Array(256);
@@ -73,41 +66,35 @@ function crc32(buf) {
73
66
  }
74
67
  }
75
68
  let crc = -1;
76
- for (let i = 0; i < buf.length; i++) crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xff];
69
+ for (const byte of buffer) crc = (crc >>> 8) ^ table[(crc ^ byte) & 0xff];
77
70
  return (crc ^ -1) >>> 0;
78
71
  }
79
72
 
80
73
  function chunk(type, data) {
81
- const len = Buffer.alloc(4);
82
- len.writeUInt32BE(data.length);
74
+ const length = Buffer.alloc(4);
75
+ length.writeUInt32BE(data.length);
83
76
  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]);
77
+ const checksum = Buffer.alloc(4);
78
+ checksum.writeUInt32BE(crc32(body));
79
+ return Buffer.concat([length, body, checksum]);
87
80
  }
88
81
 
89
82
  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.
83
+ const pixels = Buffer.alloc(WIDTH * HEIGHT * 3, 255);
84
+ lines.forEach((line, index) => drawText(pixels, line, MARGIN_X, 24 + index * LINE_HEIGHT));
96
85
  const raw = Buffer.alloc(HEIGHT * (WIDTH * 3 + 1));
97
86
  for (let y = 0; y < HEIGHT; y++) {
98
87
  raw[y * (WIDTH * 3 + 1)] = 0;
99
88
  pixels.copy(raw, y * (WIDTH * 3 + 1) + 1, y * WIDTH * 3, (y + 1) * WIDTH * 3);
100
89
  }
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
-
90
+ const header = Buffer.alloc(13);
91
+ header.writeUInt32BE(WIDTH, 0);
92
+ header.writeUInt32BE(HEIGHT, 4);
93
+ header[8] = 8;
94
+ header[9] = 2;
108
95
  return Buffer.concat([
109
96
  Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
110
- chunk('IHDR', ihdr),
97
+ chunk('IHDR', header),
111
98
  chunk('IDAT', zlib.deflateSync(raw)),
112
99
  chunk('IEND', Buffer.alloc(0)),
113
100
  ]);
@@ -116,8 +103,8 @@ function createIdPng(lines) {
116
103
  module.exports = { createIdPng, FONT, WIDTH, HEIGHT, SCALE, MARGIN_X, LINE_HEIGHT };
117
104
 
118
105
  if (require.main === module) {
119
- const out = process.argv[2] || path.join(__dirname, 'sample-id.png');
106
+ const output = process.argv[2] || path.join(__dirname, 'sample-id.png');
120
107
  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
- }
108
+ fs.writeFileSync(output, createIdPng(lines));
109
+ console.log(`wrote ${output}`);
110
+ }
@@ -0,0 +1,11 @@
1
+ // PlainScript 1.0.2-latest: JSON values and transformations.
2
+
3
+ remember person as record with name "Ada" and role "engineer" done
4
+
5
+ remember encoded as jsonEncode(person)
6
+ remember decoded as jsonDecode(encoded)
7
+ show decoded.name
8
+ show decoded.role
9
+
10
+ remember payload as {ok: true, count: 2}
11
+ show jsonEncode(payload)
@@ -0,0 +1,18 @@
1
+ // PlainScript 1.0.2-latest: collection and numeric loops.
2
+
3
+ remember colors as list with "red", "green", "blue"
4
+
5
+ for each color in colors
6
+ show color
7
+ done
8
+
9
+ for index position from 0 to 2
10
+ show position
11
+ show colors at position 1
12
+ done
13
+
14
+ remember count as 0
15
+ while count is less than 3
16
+ show count
17
+ count becomes count + 1
18
+ done
@@ -0,0 +1,3 @@
1
+ // PlainScript 1.0.2-latest: module entry point.
2
+ import { circleArea } from "./math.pln"
3
+ show circleArea(2)
@@ -0,0 +1,4 @@
1
+ // PlainScript 1.0.2-latest: named imports.
2
+
3
+ import { circleArea } from "./math.pln"
4
+ show circleArea(2)
@@ -0,0 +1,9 @@
1
+ // PlainScript 1.0.2-latest: an imported module.
2
+
3
+ remember PI as 3.14159
4
+
5
+ make circleArea(radius)
6
+ give PI * radius * radius
7
+ done
8
+
9
+ export circleArea
@@ -0,0 +1,33 @@
1
+ // PlainScript 1.0.2-latest: MongoDB.
2
+ //
3
+ // The `mongo` statement binds a connection to a MongoDB instance using the
4
+ // official `mongodb` driver. Give it a connection string and an optional
5
+ // database name:
6
+ //
7
+ // mongo "<connection-string>" [db "<name>"]
8
+ //
9
+ // After connecting, the `remember ... as query` and `insert`/`update`/`delete`
10
+ // blocks run against the bound database. MongoDB understands a light SQL-like
11
+ // subset here:
12
+ //
13
+ // - SELECT <fields|*> FROM <collection> (query)
14
+ // - INSERT INTO <collection> (a, b) VALUES (?, ?) (insert)
15
+ //
16
+ // Swap the connection string below for your own server to run this file.
17
+
18
+ mongo "mongodb://localhost:27017" db "testdb"
19
+
20
+ // Fetch every user document. The result of the query block is stored in the
21
+ // `users` variable so it can be inspected or returned later.
22
+ remember users as query
23
+ SELECT * FROM users
24
+ done
25
+ show users
26
+
27
+ // Insert a new user document from a PlainScript variable. Field names and the
28
+ // order they appear in VALUES must match the column list.
29
+ remember name as "Ada"
30
+ remember email as "ada@example.com"
31
+ insert
32
+ INSERT INTO users (name, email) VALUES ({name}, {email})
33
+ done