plainscript-lang 1.0.0-beta → 1.0.0-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.
@@ -0,0 +1,39 @@
1
+ // PlainScript v1.0.0-latest — File uploads
2
+ //
3
+ // Run with: plainscript run examples/file-uploads.ps
4
+ // Then try:
5
+ // curl -F photo=@some-image.png http://localhost:3000/upload
6
+ // curl -F doc=@notes.txt http://localhost:3000/upload-many
7
+ //
8
+ // Uploaded files are kept in memory (no folder) so nothing touches the disk.
9
+ // A file over 5 MB is rejected with HTTP 413 and a non-image with HTTP 415.
10
+
11
+ web app
12
+
13
+ accept uploads limit "5 MB" allow ["image/png", "image/jpeg", "text/plain"]
14
+
15
+ route post "/upload"
16
+ remember file as upload("file")
17
+ if file is not empty
18
+ reply json
19
+ name is name of file
20
+ type is type of file
21
+ size is size of file
22
+ done
23
+ otherwise
24
+ reply json
25
+ error is "no file under the 'file' field"
26
+ done
27
+ done
28
+ done
29
+
30
+ route post "/upload-many"
31
+ remember files as uploads("docs")
32
+ gather each file in files giving name of file
33
+ reply json
34
+ received is length(files)
35
+ names is files
36
+ done
37
+ done
38
+
39
+ start 3000
@@ -0,0 +1,47 @@
1
+ // PlainScript v1.0.0-latest — File I/O, YAML and environment configuration
2
+ //
3
+ // Run with: plainscript run examples/files-and-config.ps
4
+ //
5
+ // Writes and reads files, walks a folder, round-trips YAML and applies a
6
+ // .env file. Everything is deterministic and dependency-free.
7
+
8
+ makeFolder("example-output")
9
+ writeFile("example-output/greeting.txt", "Hello, files!")
10
+ writeLine("example-output/log.txt", "first line")
11
+ appendLine("example-output/log.txt", "second line")
12
+
13
+ show readFile("example-output/greeting.txt")
14
+ show readFile("example-output/log.txt")
15
+ show fileExists("example-output/greeting.txt")
16
+ show fileSize("example-output/greeting.txt")
17
+ show fileType("example-output") // directory
18
+ show lastModified("example-output/greeting.txt")
19
+
20
+ show listFolder("example-output")
21
+ show walkFolder("example-output")
22
+
23
+ remember settings as
24
+ app is "plainscript"
25
+ limits is { requests: 100, window: "hour" }
26
+ done
27
+
28
+ remember encoded as yamlEncode(settings)
29
+ show encoded
30
+
31
+ remember parsed as yamlDecode(encoded)
32
+ show app of parsed
33
+ remember limits as limits of parsed
34
+ show requests of limits
35
+
36
+ load env file ".env"
37
+ show env("APP_ENV")
38
+
39
+ // JSON round-trip stays convenient too
40
+ remember backup as jsonEncode(settings)
41
+ show jsonDecode(backup)
42
+
43
+ // Clean up after ourselves
44
+ deleteFile("example-output/greeting.txt")
45
+ deleteFile("example-output/log.txt")
46
+ deleteFolder("example-output")
47
+ show "cleanup done"
@@ -0,0 +1,44 @@
1
+ // PlainScript v1.0.0-latest — HTTP client: every method, headers and timeouts
2
+ //
3
+ // Run with: plainscript run examples/http-client.ps
4
+ //
5
+ // Uses the public JSONPlaceholder API. Every call returns a record with
6
+ // ok / status / headers / data; JSON bodies are parsed automatically.
7
+
8
+ // GET with an explicit timeout
9
+ remember users as get "https://jsonplaceholder.typicode.com/users" timeout 8000
10
+ show "GET users -> " + text(status of users) + ", count " + text(length(data of users))
11
+
12
+ // POST an inline object, sent as JSON
13
+ remember created as post "https://jsonplaceholder.typicode.com/posts" with {
14
+ title: "Hello from PlainScript",
15
+ userId: 1
16
+ }
17
+ show "POST -> status " + text(status of created)
18
+ if ok of created is true
19
+ show "new post id " + text(id of data of created)
20
+ done
21
+
22
+ // PUT replaces the whole resource
23
+ remember replaced as put "https://jsonplaceholder.typicode.com/posts/1" with {
24
+ title: "Updated",
25
+ body: "new body",
26
+ userId: 1
27
+ }
28
+ show "PUT -> status " + text(status of replaced)
29
+
30
+ // PATCH merges into the resource
31
+ remember patched as patch "https://jsonplaceholder.typicode.com/posts/1" with {
32
+ title: "Patched title"
33
+ }
34
+ show "PATCH -> status " + text(status of patched)
35
+
36
+ // DELETE
37
+ remember removed as delete "https://jsonplaceholder.typicode.com/posts/1"
38
+ show "DELETE -> ok " + text(ok of removed)
39
+
40
+ // Custom headers and error surfacing
41
+ remember guarded as get "https://api.example.com/private" headers {
42
+ Authorization: "Bearer test"
43
+ } timeout 3000
44
+ show "guard called? " + text(ok of guarded) + " (status " + text(status of guarded) + ")"
@@ -0,0 +1,16 @@
1
+ // PlainScript v1.0.0-latest — Importing your own modules
2
+ //
3
+ // Run with: plainscript run examples/modules/app.ps
4
+ //
5
+ // Files are compiled in dependency order (deepest first), so the helpers in
6
+ // math.ps are visible here before app.ps runs.
7
+
8
+ import "./math.ps"
9
+
10
+ show PI // 3.14
11
+ show double(21) // 42
12
+ show square(9) // 81
13
+ show circumference(10) // 62.8
14
+
15
+ remember radius as 2.5
16
+ show "circle area: " + text(square(radius) * PI)
@@ -0,0 +1,20 @@
1
+ // PlainScript v1.0.0-latest — reusable module, imported by app.ps
2
+
3
+ remember PI as 3.14
4
+
5
+ make double(n)
6
+ give n * 2
7
+ done
8
+
9
+ make square(n)
10
+ give n * n
11
+ done
12
+
13
+ make circumference(radius)
14
+ give 2 * PI * radius
15
+ done
16
+
17
+ export PI
18
+ export double
19
+ export square
20
+ export circumference
@@ -0,0 +1,43 @@
1
+ // PlainScript v1.0.0-latest — Record kinds, generators and the test DSL
2
+ //
3
+ // Run with: plainscript run examples/record-kinds.ps
4
+ //
5
+ // Defines a schema, builds instances, composes records, streams values from
6
+ // generators, and verifies behaviour with the built-in test runner.
7
+
8
+ define a kind called "Book" with
9
+ title is ""
10
+ pages is 0
11
+ rating is 0
12
+ done
13
+
14
+ remember fav as create a Book with title "IOPL in Action" and pages 240
15
+ fav.rating becomes 4.5
16
+
17
+ show title of fav
18
+ show pages of fav
19
+ show rating of fav
20
+
21
+ remember extended as merge(fav, { author: "Ada" })
22
+ show author of extended
23
+ show fieldsOf(extended)
24
+
25
+ test "book schema"
26
+ check title of fav equals "IOPL in Action"
27
+ check rating of fav equals 4.5
28
+ check hasField(fav, "author") equals false
29
+ done
30
+
31
+ test "generator"
32
+ remember ids as spread of range(1, 4)
33
+ check length(ids) equals 3
34
+ check sum(ids) equals 6
35
+ done
36
+
37
+ make makeTitles(catalog)
38
+ for each book in catalog
39
+ yield title of book
40
+ done
41
+ done
42
+
43
+ show spread of makeTitles([fav, extended])
@@ -0,0 +1,34 @@
1
+ // PlainScript v1.0.0-latest — Functional transformations
2
+ //
3
+ // Run with: plainscript run examples/search-and-filter.ps
4
+ //
5
+ // gather / filter / total map, filter and reduce in place; match routes a
6
+ // value to behaviour.
7
+
8
+ remember scores as [70, 45, 92, 38, 60]
9
+
10
+ gather each score in scores giving score + 5
11
+ show scores // [ 75, 50, 97, 43, 65 ]
12
+
13
+ filter each score in scores when score is at least 60
14
+ show scores // [ 75, 97, 65 ]
15
+
16
+ remember prices as [19.99, 4.99, 29.99]
17
+ total each price in prices giving price
18
+ show prices // 54.97
19
+
20
+ // Pattern matching
21
+ remember day as "wed"
22
+
23
+ match day against
24
+ "mon" -> show "Start of the week"
25
+ "wed" -> show "Middle of the week"
26
+ "fri" -> show "Almost there"
27
+ otherwise -> show "Another day"
28
+ done
29
+
30
+ // Regex matching extracts a group into a variable
31
+ match pattern "^([0-9]+)$" in "2026" as digits
32
+ if digits is not empty
33
+ show "matched digits: " + first of digits
34
+ done
@@ -0,0 +1,54 @@
1
+ // PlainScript v1.0.0-latest — Sessions, cookies and rate limiting
2
+ //
3
+ // Run with: plainscript run examples/sessions-and-cookies.ps
4
+ // Then try:
5
+ // curl -c cookies.txt -b cookies.txt http://localhost:3000/login
6
+ // curl -c cookies.txt -b cookies.txt http://localhost:3000/profile
7
+ // curl -c cookies.txt -b cookies.txt http://localhost:3000/logout
8
+ //
9
+ // The app shows login/logout flow backed by signed sessions, preference
10
+ // cookies, and a per-IP rate limit.
11
+
12
+ web app
13
+
14
+ limit requests to 100 per minute
15
+ enable sessions "a-long-random-secret-minimum"
16
+
17
+ allow cors
18
+
19
+ route get "/login"
20
+ user of session of request becomes "ada"
21
+ set cookie "theme" to "dark" expires in 7 days
22
+ reply json
23
+ welcome is true
24
+ who is user of session of request
25
+ done
26
+ done
27
+
28
+ route get "/profile"
29
+ remember who as user of session of request
30
+ if who is empty
31
+ status 401
32
+ reply "Please log in first via /login"
33
+ otherwise
34
+ reply json
35
+ user is who
36
+ theme is cookie("theme")
37
+ done
38
+ done
39
+ done
40
+
41
+ route post "/logout"
42
+ destroy session
43
+ clear cookie "theme"
44
+ reply "Logged out. Goodbye!"
45
+ done
46
+
47
+ when nothing matches
48
+ status 404
49
+ reply json
50
+ error is "no such road"
51
+ done
52
+ done
53
+
54
+ start 3000
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "plainscript-lang",
3
- "version": "1.0.0-beta",
3
+ "version": "1.0.0-latest",
4
4
  "description": "PlainScript — an Intent-Oriented Programming Language. When even a simple sentence can be code.",
5
5
  "main": "compiler/cli.js",
6
6
  "bin": {
7
7
  "plainscript": "./compiler/cli.js"
8
8
  },
9
9
  "scripts": {
10
- "test": "node tests/compiler.test.js && node tests/build.test.js && node tests/telegram.test.js && node tests/ocr.test.js && node tests/backend.test.js && node tests/runtime.test.js && node tests/acceptance.test.js && node tests/whatsapp.test.js && node tests/audit.test.js"
10
+ "test": "node tests/compiler.test.js && node tests/build.test.js && node tests/telegram.test.js && node tests/ocr.test.js && node tests/backend.test.js && node tests/runtime.test.js && node tests/acceptance.test.js && node tests/whatsapp.test.js && node tests/audit.test.js && node tests/compat/language.test.js && node tests/compat/node.test.js && node tests/compat/binary.test.js && node tests/compat/errors.test.js && node tests/compat/async.test.js && node tests/compat/modules.test.js && node tests/compat/npm.test.js && node tests/compat/web.test.js && node tests/compat/real-world.test.js"
11
11
  },
12
12
  "files": [
13
13
  "compiler",