trebol 0.1.0 → 0.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.
- package/README.md +8 -2
- package/dist/cli.js +55 -20
- package/dist/lib/core/access.d.ts +10 -0
- package/dist/lib/core/access.js +210 -0
- package/dist/lib/core/base.d.ts +4 -0
- package/dist/lib/core/base.js +50 -0
- package/dist/lib/core/bitset.d.ts +9 -0
- package/dist/lib/core/bitset.js +75 -0
- package/dist/lib/core/boot.d.ts +18 -0
- package/dist/lib/core/boot.js +72 -0
- package/dist/lib/core/compare.d.ts +9 -0
- package/dist/lib/core/compare.js +156 -0
- package/dist/lib/core/ctx.d.ts +36 -0
- package/dist/lib/core/ctx.js +251 -0
- package/dist/lib/core/dec.d.ts +28 -0
- package/dist/lib/core/dec.js +158 -0
- package/dist/lib/core/errors.d.ts +15 -0
- package/dist/lib/core/errors.js +44 -0
- package/dist/lib/core/escapes.d.ts +14 -0
- package/dist/lib/core/escapes.js +80 -0
- package/dist/lib/core/eval.d.ts +220 -0
- package/dist/lib/core/eval.js +666 -0
- package/dist/lib/core/func.d.ts +64 -0
- package/dist/lib/core/func.js +129 -0
- package/dist/lib/core/hash.d.ts +5 -0
- package/dist/lib/core/hash.js +155 -0
- package/dist/lib/core/index.d.ts +18 -0
- package/dist/lib/core/index.js +20 -0
- package/dist/lib/core/int64.d.ts +22 -0
- package/dist/lib/core/int64.js +113 -0
- package/dist/lib/core/kinds.d.ts +67 -0
- package/dist/lib/core/kinds.js +134 -0
- package/dist/lib/core/lexer.d.ts +13 -0
- package/dist/lib/core/lexer.js +702 -0
- package/dist/lib/core/mezz.d.ts +1 -0
- package/dist/lib/core/mezz.js +360 -0
- package/dist/lib/core/mold.d.ts +20 -0
- package/dist/lib/core/mold.js +434 -0
- package/dist/lib/core/natives/control.d.ts +1 -0
- package/dist/lib/core/natives/control.js +374 -0
- package/dist/lib/core/natives/csv.d.ts +15 -0
- package/dist/lib/core/natives/csv.js +315 -0
- package/dist/lib/core/natives/data.d.ts +11 -0
- package/dist/lib/core/natives/data.js +367 -0
- package/dist/lib/core/natives/define.d.ts +17 -0
- package/dist/lib/core/natives/define.js +47 -0
- package/dist/lib/core/natives/encode.d.ts +3 -0
- package/dist/lib/core/natives/encode.js +187 -0
- package/dist/lib/core/natives/json.d.ts +5 -0
- package/dist/lib/core/natives/json.js +273 -0
- package/dist/lib/core/natives/math.d.ts +3 -0
- package/dist/lib/core/natives/math.js +500 -0
- package/dist/lib/core/natives/parse.d.ts +1 -0
- package/dist/lib/core/natives/parse.js +600 -0
- package/dist/lib/core/natives/series.d.ts +4 -0
- package/dist/lib/core/natives/series.js +509 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +484 -0
- package/dist/lib/core/natives/xml.d.ts +9 -0
- package/dist/lib/core/natives/xml.js +242 -0
- package/dist/lib/core/series.d.ts +26 -0
- package/dist/lib/core/series.js +73 -0
- package/dist/lib/core/symbols.d.ts +9 -0
- package/dist/lib/core/symbols.js +44 -0
- package/dist/lib/core/values.d.ts +206 -0
- package/dist/lib/core/values.js +223 -0
- package/dist/lib/host/browser.d.ts +4 -0
- package/dist/lib/host/browser.js +4081 -0
- package/dist/lib/host/chart.d.ts +26 -0
- package/dist/lib/host/chart.js +158 -0
- package/dist/lib/host/compress.d.ts +1 -0
- package/dist/lib/host/compress.js +37 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/icons.d.ts +3 -0
- package/dist/lib/host/icons.js +97 -0
- package/dist/lib/host/index.d.ts +38 -0
- package/dist/lib/host/index.js +190 -0
- package/dist/lib/host/locale.d.ts +40 -0
- package/dist/lib/host/locale.js +241 -0
- package/dist/lib/host/markdown.d.ts +28 -0
- package/dist/lib/host/markdown.js +151 -0
- package/dist/lib/host/modules.d.ts +10 -0
- package/dist/lib/host/modules.js +262 -0
- package/dist/lib/host/natives.d.ts +6 -0
- package/dist/lib/host/natives.js +98 -0
- package/dist/lib/host/net.d.ts +12 -0
- package/dist/lib/host/net.js +541 -0
- package/dist/lib/host/page.d.ts +9 -0
- package/dist/lib/host/page.js +55 -0
- package/dist/lib/host/rows.d.ts +15 -0
- package/dist/lib/host/rows.js +80 -0
- package/dist/lib/host/scheduler.d.ts +45 -0
- package/dist/lib/host/scheduler.js +173 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1462 -0
- package/dist/lib/ui/i18n.trb +111 -0
- package/dist/lib/ui/ui.trb +1237 -0
- package/dist/trebol.js +123 -56
- package/package.json +17 -5
- package/src/host/browser.ts +274 -33
- package/src/host/index.ts +2 -2
- package/src/host/modules.ts +8 -1
- package/src/host/theme.ts +63 -4
- package/src/ui/i18n.trb +4 -0
- package/src/ui/ui.trb +48 -15
package/README.md
CHANGED
|
@@ -31,8 +31,8 @@ like any other value.
|
|
|
31
31
|
- **Tasks that never block**: `fetch`, `wait` and the rest pause only the task that asked, anywhere, even in
|
|
32
32
|
the middle of a `parse`. HTTP, WebSocket and server-sent events are built in.
|
|
33
33
|
- **The UI dialect**: forms with validation, tables, lists, dialogs, toasts, tabs, menus, trees, screens with
|
|
34
|
-
their own links, languages, drawings with Red's draw dialect,
|
|
35
|
-
from a theme of a few parameters. No CSS.
|
|
34
|
+
their own links, languages, drawings with Red's draw dialect, charts, images behind the screen, photos,
|
|
35
|
+
video and sound, in nine looks and five skins, all from a theme of a few parameters. No CSS.
|
|
36
36
|
- **A console** like Rebol's, and a development server that reloads on save.
|
|
37
37
|
|
|
38
38
|
## Getting started
|
|
@@ -53,6 +53,9 @@ npx trebol # the console: >> 1 + 2 * 3 == 9
|
|
|
53
53
|
npx trebol dev my-app # serves the folder my-app, with its app.trb, at http://localhost:5173/
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
A JavaScript program can run Trebol too, through `trebol/host` and `trebol/core`; see
|
|
57
|
+
[using Trebol from JavaScript](https://acasadom.github.io/trebol/guides/architecture.html#using-trebol-from-javascript).
|
|
58
|
+
|
|
56
59
|
## Working on Trebol
|
|
57
60
|
|
|
58
61
|
There is no build step while working on Trebol: Node runs the TypeScript directly.
|
|
@@ -65,6 +68,9 @@ npm run dev # the examples, at http://localhost:5173/examples/signup/
|
|
|
65
68
|
npm run build # dist/trebol.js and dist/cli.js, what the npm package runs
|
|
66
69
|
```
|
|
67
70
|
|
|
71
|
+
A release is a tag: `npm version minor` (or `patch`) and `git push --follow-tags`. The `release` workflow checks
|
|
72
|
+
the tagged commit and stages it on npm, where a maintainer approves it with `npm stage approve`.
|
|
73
|
+
|
|
68
74
|
## Documentation
|
|
69
75
|
|
|
70
76
|
The documentation is at <https://acasadom.github.io/trebol/>. It has two tutorials,
|
package/dist/cli.js
CHANGED
|
@@ -3893,8 +3893,8 @@ import { stripTypeScriptTypes } from "node:module";
|
|
|
3893
3893
|
function importMap() {
|
|
3894
3894
|
const pkg = JSON.parse(readFileSync(join(TREBOL2, "package.json"), "utf8"));
|
|
3895
3895
|
const imports = {};
|
|
3896
|
-
for (const [name,
|
|
3897
|
-
imports[`trebol${name.slice(1)}`] = PREFIX2 +
|
|
3896
|
+
for (const [name, paths] of Object.entries(pkg.exports)) {
|
|
3897
|
+
imports[`trebol${name.slice(1)}`] = PREFIX2 + paths.source.slice(2);
|
|
3898
3898
|
}
|
|
3899
3899
|
return `<script type="importmap">${JSON.stringify({ imports })}</script>`;
|
|
3900
3900
|
}
|
|
@@ -10808,6 +10808,8 @@ function defaultLoader() {
|
|
|
10808
10808
|
return {
|
|
10809
10809
|
base: g.location?.href ?? "file:///",
|
|
10810
10810
|
read: async (href) => {
|
|
10811
|
+
const fs = href.startsWith("file:") ? g.process?.getBuiltinModule?.("node:fs/promises") : void 0;
|
|
10812
|
+
if (fs !== void 0) return fs.readFile(new URL(href), "utf8");
|
|
10811
10813
|
const res = await fetch(href);
|
|
10812
10814
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
10813
10815
|
return res.text();
|
|
@@ -11800,14 +11802,14 @@ fetch: func [
|
|
|
11800
11802
|
]
|
|
11801
11803
|
|
|
11802
11804
|
show-progress: func [
|
|
11803
|
-
"Shows how far a request went on a face: a progress bar's value, or the bar of an upload."
|
|
11805
|
+
"Shows how far a request went on a face: a progress bar's value, or the bar of an upload or a photo."
|
|
11804
11806
|
face [object!]
|
|
11805
11807
|
done [integer! none!] "Bytes done; none once the request is over"
|
|
11806
11808
|
total [integer! none!] "Bytes in all, when known"
|
|
11807
11809
|
/local part
|
|
11808
11810
|
][
|
|
11809
11811
|
part: either all [done total total > 0] [done / to decimal! total] [none]
|
|
11810
|
-
either
|
|
11812
|
+
either find [upload photo] select face 'style [face/sent: part] [if done [face/value: part]]
|
|
11811
11813
|
]
|
|
11812
11814
|
|
|
11813
11815
|
save: func [
|
|
@@ -11985,6 +11987,12 @@ face: make object! [
|
|
|
11985
11987
|
readonly: false ; shown and sent, but the user cannot change it, and no rule applies
|
|
11986
11988
|
inert: false ; shown as it is, but neither it nor its children can be used
|
|
11987
11989
|
linked: false ; a tile that stands for something kept elsewhere: marked with an arrow
|
|
11990
|
+
round: false ; an image: cut to a circle, as for a person's photo
|
|
11991
|
+
poster: none ; a video: the image shown before it plays
|
|
11992
|
+
captions: none ; a video: its captions, a .vtt file, or a block of one per language [en %a.vtt es %b.vtt]
|
|
11993
|
+
looping: false ; a video or a sound starts again when it ends (loop in the dialect)
|
|
11994
|
+
silent: false ; a video or a sound plays without its sound (muted in the dialect)
|
|
11995
|
+
autoplay: false ; a video or a sound plays on its own, always silent, as browsers allow
|
|
11988
11996
|
drag: false ; can be dragged: carries its value, or the row in a table, list or tree
|
|
11989
11997
|
drop: false ; accepts drops (on-drop)
|
|
11990
11998
|
reorder: false ; its children or rows can be moved around (on-reorder)
|
|
@@ -11992,7 +12000,7 @@ face: make object! [
|
|
|
11992
12000
|
error: none ; current message, shown once the face is touched
|
|
11993
12001
|
touched: false
|
|
11994
12002
|
pending: false ; an asynchronous check is running
|
|
11995
|
-
sent: none ; an upload: how far its files went up, 0-1; none hides the bar
|
|
12003
|
+
sent: none ; an upload or photo: how far its files went up, 0-1; none hides the bar
|
|
11996
12004
|
async-error: none ; result of the last asynchronous check
|
|
11997
12005
|
action: none ; the block after the face: click, change
|
|
11998
12006
|
events: none ; [on-change [...] on-select [...]]
|
|
@@ -12008,11 +12016,11 @@ face: make object! [
|
|
|
12008
12016
|
built: none ; rows: what its rows were last made from
|
|
12009
12017
|
owner: none ; a row of rows: the rows face it belongs to
|
|
12010
12018
|
typed: none ; what the user typed in a number, money or date field when it is not one;
|
|
12011
|
-
; in an upload, the names of the files it refused
|
|
12019
|
+
; in an upload or photo, the names of the files it refused
|
|
12012
12020
|
multiple: false ; an upload or a lookup takes more than one
|
|
12013
|
-
accept: none ; the files an upload takes: ["pdf" "image/*"]
|
|
12021
|
+
accept: none ; the files an upload takes: ["pdf" "image/*"]; a photo, images unless told
|
|
12014
12022
|
pane: none
|
|
12015
|
-
columns: none ; table, list and tree: [field "Label" ...]
|
|
12023
|
+
columns: none ; table, list and tree: [field "Label" ...]; a column of images, [field ["Label" image round]]
|
|
12016
12024
|
data: none ; table, list and tree: a block of map! or object! rows (a tree's
|
|
12017
12025
|
; have their own in \`children\`); breadcrumb: its items
|
|
12018
12026
|
key: 'id ; the row field that identifies a row
|
|
@@ -12033,14 +12041,15 @@ face: make object! [
|
|
|
12033
12041
|
styles: make map! []
|
|
12034
12042
|
|
|
12035
12043
|
container-styles: [view column row form panel grid dialog scroll split toolbar desktop]
|
|
12036
|
-
input-styles: [field password number money date area rich check toggle choice radio upload lookup rows]
|
|
12044
|
+
input-styles: [field password number money date area rich check toggle choice radio upload photo lookup rows]
|
|
12037
12045
|
data-styles: [table list tree chart]
|
|
12038
12046
|
|
|
12039
12047
|
foreach [name base] [
|
|
12040
12048
|
view column column column row row form form panel panel grid grid scroll scroll desktop desktop
|
|
12041
12049
|
title title heading heading text text divider divider
|
|
12042
12050
|
field field password password number number money money date date area area rich rich check check choice choice
|
|
12043
|
-
toggle toggle radio radio upload upload lookup lookup progress progress image image icon icon
|
|
12051
|
+
toggle toggle radio radio upload upload photo photo lookup lookup progress progress image image backdrop backdrop icon icon
|
|
12052
|
+
video video audio audio
|
|
12044
12053
|
button button link link tile tile table table list list tabs tabs tab tab
|
|
12045
12054
|
toolbar toolbar breadcrumb breadcrumb tree tree screens screens screen screen rows rows entry entry
|
|
12046
12055
|
canvas canvas chart chart
|
|
@@ -12075,11 +12084,13 @@ roles: [primary accent danger success warning muted]
|
|
|
12075
12084
|
sizes: [small medium large]
|
|
12076
12085
|
aligns: [left center right spread]
|
|
12077
12086
|
valigns: [top middle bottom]
|
|
12078
|
-
flags: [fill fit hidden disabled readonly inert narrow-only wide-only linked drag drop reorder multiple sortable stacked]
|
|
12087
|
+
flags: [fill fit hidden disabled readonly inert narrow-only wide-only linked round drag drop reorder multiple sortable stacked]
|
|
12079
12088
|
chart-kinds: [bar line area pie]
|
|
12080
12089
|
simple-rules: [required email url integer]
|
|
12081
12090
|
letter: charset [#"a" - #"z" #"A" - #"Z"]
|
|
12082
|
-
rule-words: [min max length pattern match check check-async calc width pad gap menu accept known links sorted]
|
|
12091
|
+
rule-words: [min max length pattern match check check-async calc width pad gap menu accept known links sorted poster captions]
|
|
12092
|
+
media-styles: [video audio]
|
|
12093
|
+
media-flags: [loop muted autoplay] ; of a video or a sound
|
|
12083
12094
|
|
|
12084
12095
|
; Words that never name a field. email, url and integer are rules too,
|
|
12085
12096
|
; but also common field names, so the first word after an input style names it.
|
|
@@ -12201,6 +12212,12 @@ build-faces: function [
|
|
|
12201
12212
|
| 'length set a integer! set n integer! (add-rule f reduce ['length a n])
|
|
12202
12213
|
| 'pattern set b block! (add-rule f reduce ['pattern b])
|
|
12203
12214
|
| 'accept set b block! (f/accept: b)
|
|
12215
|
+
| 'poster set v [file! | url!] (f/poster: v)
|
|
12216
|
+
| 'captions set v [file! | url! | block!] (f/captions: v)
|
|
12217
|
+
; muted names a role elsewhere, and loop a function, so a video keeps them in fields of its own
|
|
12218
|
+
| set w word! if (all [find media-flags w find media-styles f/style]) (
|
|
12219
|
+
poke f select [muted silent loop looping autoplay autoplay] w true
|
|
12220
|
+
)
|
|
12204
12221
|
| 'known set v map! (f/known: v)
|
|
12205
12222
|
| 'links set v map! (f/links: v)
|
|
12206
12223
|
| 'match set w word! (add-rule f reduce ['match w])
|
|
@@ -12254,10 +12271,19 @@ spacing: func [n [integer!]] [
|
|
|
12254
12271
|
]
|
|
12255
12272
|
|
|
12256
12273
|
; [name "Name" email] -> [name "Name" email "email"]
|
|
12257
|
-
|
|
12274
|
+
; A column of images keeps what it is with its label: [photo "" image round] -> [photo ["" image round]]
|
|
12275
|
+
columns-of: function [spec [block!] /local w label kind] [
|
|
12258
12276
|
out: copy []
|
|
12259
|
-
unless parse spec [any [
|
|
12260
|
-
|
|
12277
|
+
unless parse spec [any [
|
|
12278
|
+
set w word! (label: form w kind: none) opt [set label text-rule]
|
|
12279
|
+
opt ['image (kind: copy [image]) opt ['round (append kind 'round)]]
|
|
12280
|
+
(
|
|
12281
|
+
label: text-of label
|
|
12282
|
+
append out w
|
|
12283
|
+
append/only out either kind [head insert/only kind label] [label]
|
|
12284
|
+
)
|
|
12285
|
+
]] [
|
|
12286
|
+
do make error! "layout: table columns are words, each with an optional label, then image or image round"
|
|
12261
12287
|
]
|
|
12262
12288
|
out
|
|
12263
12289
|
]
|
|
@@ -12826,6 +12852,7 @@ validate-face: function [
|
|
|
12826
12852
|
]
|
|
12827
12853
|
if f/typed [
|
|
12828
12854
|
if f/style = 'upload [return message 'accept reduce [list-of f/accept]]
|
|
12855
|
+
if f/style = 'photo [return either f/accept [message 'accept reduce [list-of f/accept]] [message 'images []]]
|
|
12829
12856
|
return message select [number number money amount date date] f/style []
|
|
12830
12857
|
]
|
|
12831
12858
|
if blank? :v [
|
|
@@ -12967,6 +12994,7 @@ changed: function [f] [
|
|
|
12967
12994
|
|
|
12968
12995
|
; An upload's value is none or a block of objects [name size type file];
|
|
12969
12996
|
; file is a handle the host keeps, read with read-file or sent with multipart.
|
|
12997
|
+
; A photo's is none, the object of the image chosen, or the address of one kept elsewhere.
|
|
12970
12998
|
ui-files: function [
|
|
12971
12999
|
"Called by the host when the user chooses or drops files: name size type file for each, and the names it refused."
|
|
12972
13000
|
f files [block!] refused [block!]
|
|
@@ -12975,7 +13003,11 @@ ui-files: function [
|
|
|
12975
13003
|
foreach [n s t h] files [keep make object! [name: n size: s type: t file: h]]
|
|
12976
13004
|
]
|
|
12977
13005
|
unless empty? chosen [
|
|
12978
|
-
f/value:
|
|
13006
|
+
f/value: case [
|
|
13007
|
+
f/style = 'photo [first chosen]
|
|
13008
|
+
f/multiple [append copy any [f/value []] chosen]
|
|
13009
|
+
true [copy/part chosen 1]
|
|
13010
|
+
]
|
|
12979
13011
|
]
|
|
12980
13012
|
f/typed: either empty? refused [none] [refused]
|
|
12981
13013
|
f/touched: true
|
|
@@ -12983,12 +13015,15 @@ ui-files: function [
|
|
|
12983
13015
|
]
|
|
12984
13016
|
|
|
12985
13017
|
ui-remove-file: function [
|
|
12986
|
-
"Called by the host when the user removes the file at a position of an upload."
|
|
13018
|
+
"Called by the host when the user removes the file at a position of an upload, or a photo's."
|
|
12987
13019
|
f index [integer!]
|
|
12988
13020
|
] [
|
|
12989
|
-
|
|
12990
|
-
|
|
13021
|
+
f/value: either f/style = 'photo [none] [
|
|
13022
|
+
files: remove at copy f/value index
|
|
13023
|
+
either empty? head files [none] [head files]
|
|
13024
|
+
]
|
|
12991
13025
|
f/typed: none
|
|
13026
|
+
f/touched: true
|
|
12992
13027
|
changed f
|
|
12993
13028
|
]
|
|
12994
13029
|
|
|
@@ -13152,7 +13187,7 @@ move-item: function [items [block!] from [integer!] to [integer!]] [
|
|
|
13152
13187
|
items
|
|
13153
13188
|
]
|
|
13154
13189
|
`);
|
|
13155
|
-
addBuiltIn(I18N_SOURCE, 'Trebol [\n title: "Languages: the active language, texts per language and a dictionary"\n exports: [i18n tr translated localize]\n]\n\n; A text per language is a map: #(es: "Guardar" en: "Save"). `tr` picks the\n; active language, then the main one, then any. Settings live in the `i18n`\n; object, so code changes them with `i18n/language: \'en`.\n\ni18n: make object! [\n language: \'en ; the active language\n main: \'en ; what shows when a text is missing in the active one\n languages: [en es]\n texts: make map! [] ; the application\'s dictionary: word -> text per language\n ui: make map! [ ; Trebol\'s own texts; an app may change them or add a language\n ; validation messages (#1 and #2 take the rule\'s values)\n required #(en: "This field is required" es: "Este campo es obligatorio")\n email #(en: "Enter a valid email address" es: "Introduce un email v\xE1lido")\n url #(en: "Enter a valid web address" es: "Introduce una direcci\xF3n web v\xE1lida")\n integer #(en: "Enter a whole number" es: "Introduce un n\xFAmero entero")\n number #(en: "Enter a number" es: "Introduce un n\xFAmero")\n amount #(en: "Enter an amount" es: "Introduce un importe")\n date #(en: "Enter a valid date" es: "Introduce una fecha v\xE1lida")\n accept #(en: "Only #1 files" es: "Solo archivos #1")\n min #(en: "Must be at least #1" es: "Debe ser como m\xEDnimo #1")\n max #(en: "Must be at most #1" es: "Debe ser como m\xE1ximo #1")\n length #(en: "Must be between #1 and #2 characters" es: "Debe tener entre #1 y #2 caracteres")\n length-exact #(en: "Must be exactly #1 characters" es: "Debe tener exactamente #1 caracteres")\n pattern #(en: "This value is not valid" es: "Este valor no es v\xE1lido")\n match #(en: "Must match #1" es: "Debe coincidir con #1")\n rows-min #(en: "Must have at least #1 rows" es: "Debe tener como m\xEDnimo #1 filas")\n rows-max #(en: "Must have at most #1 rows" es: "Debe tener como m\xE1ximo #1 filas")\n check #(en: "This value is not valid" es: "Este valor no es v\xE1lido")\n ; what the components show by themselves\n empty #(en: "Nothing to show yet" es: "A\xFAn no hay nada que mostrar")\n choose #(en: "Choose\u2026" es: "Elegir\u2026")\n yes #(en: "Yes" es: "S\xED")\n no #(en: "No" es: "No")\n back #(en: "Back" es: "Atr\xE1s")\n close #(en: "Close" es: "Cerrar")\n move-to #(en: "Move to" es: "Mover a")\n resize-panes #(en: "Resize panes" es: "Cambiar el tama\xF1o de los paneles")\n breadcrumb #(en: "Breadcrumb" es: "Ruta de navegaci\xF3n")\n more-levels #(en: "Show more levels" es: "Mostrar m\xE1s niveles")\n choose-date #(en: "Choose date" es: "Elegir fecha")\n choose-date-of #(en: "#1: choose date" es: "#1: elegir fecha")\n previous-month #(en: "Previous month" es: "Mes anterior")\n next-month #(en: "Next month" es: "Mes siguiente")\n no-matches #(en: "Nothing found" es: "No se ha encontrado nada")\n remove-value #(en: "Remove #1" es: "Quitar #1")\n choose-file #(en: "Choose file" es: "Elegir archivo")\n choose-files #(en: "Choose files" es: "Elegir archivos")\n drop-file #(en: "or drop it here" es: "o su\xE9ltalo aqu\xED")\n drop-files #(en: "or drop them here" es: "o su\xE9ltalos aqu\xED")\n remove-file #(en: "Remove #1" es: "Quitar #1")\n add-row #(en: "Add row" es: "A\xF1adir fila")\n remove-row #(en: "Remove row" es: "Quitar fila")\n bold #(en: "Bold" es: "Negrita")\n italic #(en: "Italic" es: "Cursiva")\n heading #(en: "Heading" es: "T\xEDtulo")\n bullet-list #(en: "Bulleted list" es: "Lista con vi\xF1etas")\n number-list #(en: "Numbered list" es: "Lista numerada")\n link #(en: "Link" es: "Enlace")\n link-url #(en: "Web address" es: "Direcci\xF3n web")\n link-add #(en: "Add link" es: "A\xF1adir enlace")\n ]\n]\n\ntranslated: function [\n "A text per language (a map) from a string in the main language or a block [es ... en ...]."\n text [string! block! map!]\n] [\n if map? text [return text]\n m: make map! []\n either string? text [poke m i18n/main text] [\n foreach [lang t] text [poke m to word! lang t]\n ]\n m\n]\n\ntr: function [\n "A text in the active language: from a string, a text per language, or a word of the dictionary."\n text [any-type!]\n] [\n case [\n none? :text [copy ""]\n string? :text [text]\n word? :text [either t: select i18n/texts text [tr t] [form text]]\n block? :text [tr translated text]\n map? :text [\n any [\n select text i18n/language\n select text i18n/main\n foreach [lang t] to block! text [if string? :t [return t]]\n copy ""\n ]\n ]\n true [form :text]\n ]\n]\n\nlocalize: func [\n "A value as a string in the format of the active language: dates, times, numbers, percents and money."\n value [any-type!]\n] [\n format-local :value i18n/language\n]\n');
|
|
13190
|
+
addBuiltIn(I18N_SOURCE, 'Trebol [\n title: "Languages: the active language, texts per language and a dictionary"\n exports: [i18n tr translated localize]\n]\n\n; A text per language is a map: #(es: "Guardar" en: "Save"). `tr` picks the\n; active language, then the main one, then any. Settings live in the `i18n`\n; object, so code changes them with `i18n/language: \'en`.\n\ni18n: make object! [\n language: \'en ; the active language\n main: \'en ; what shows when a text is missing in the active one\n languages: [en es]\n texts: make map! [] ; the application\'s dictionary: word -> text per language\n ui: make map! [ ; Trebol\'s own texts; an app may change them or add a language\n ; validation messages (#1 and #2 take the rule\'s values)\n required #(en: "This field is required" es: "Este campo es obligatorio")\n email #(en: "Enter a valid email address" es: "Introduce un email v\xE1lido")\n url #(en: "Enter a valid web address" es: "Introduce una direcci\xF3n web v\xE1lida")\n integer #(en: "Enter a whole number" es: "Introduce un n\xFAmero entero")\n number #(en: "Enter a number" es: "Introduce un n\xFAmero")\n amount #(en: "Enter an amount" es: "Introduce un importe")\n date #(en: "Enter a valid date" es: "Introduce una fecha v\xE1lida")\n accept #(en: "Only #1 files" es: "Solo archivos #1")\n images #(en: "Only images" es: "Solo im\xE1genes")\n min #(en: "Must be at least #1" es: "Debe ser como m\xEDnimo #1")\n max #(en: "Must be at most #1" es: "Debe ser como m\xE1ximo #1")\n length #(en: "Must be between #1 and #2 characters" es: "Debe tener entre #1 y #2 caracteres")\n length-exact #(en: "Must be exactly #1 characters" es: "Debe tener exactamente #1 caracteres")\n pattern #(en: "This value is not valid" es: "Este valor no es v\xE1lido")\n match #(en: "Must match #1" es: "Debe coincidir con #1")\n rows-min #(en: "Must have at least #1 rows" es: "Debe tener como m\xEDnimo #1 filas")\n rows-max #(en: "Must have at most #1 rows" es: "Debe tener como m\xE1ximo #1 filas")\n check #(en: "This value is not valid" es: "Este valor no es v\xE1lido")\n ; what the components show by themselves\n empty #(en: "Nothing to show yet" es: "A\xFAn no hay nada que mostrar")\n choose #(en: "Choose\u2026" es: "Elegir\u2026")\n yes #(en: "Yes" es: "S\xED")\n no #(en: "No" es: "No")\n back #(en: "Back" es: "Atr\xE1s")\n close #(en: "Close" es: "Cerrar")\n move-to #(en: "Move to" es: "Mover a")\n resize-panes #(en: "Resize panes" es: "Cambiar el tama\xF1o de los paneles")\n breadcrumb #(en: "Breadcrumb" es: "Ruta de navegaci\xF3n")\n more-levels #(en: "Show more levels" es: "Mostrar m\xE1s niveles")\n choose-date #(en: "Choose date" es: "Elegir fecha")\n choose-date-of #(en: "#1: choose date" es: "#1: elegir fecha")\n previous-month #(en: "Previous month" es: "Mes anterior")\n next-month #(en: "Next month" es: "Mes siguiente")\n no-matches #(en: "Nothing found" es: "No se ha encontrado nada")\n remove-value #(en: "Remove #1" es: "Quitar #1")\n choose-file #(en: "Choose file" es: "Elegir archivo")\n choose-files #(en: "Choose files" es: "Elegir archivos")\n drop-file #(en: "or drop it here" es: "o su\xE9ltalo aqu\xED")\n drop-files #(en: "or drop them here" es: "o su\xE9ltalos aqu\xED")\n remove-file #(en: "Remove #1" es: "Quitar #1")\n choose-photo #(en: "Choose a photo" es: "Elegir una foto")\n change-photo #(en: "Change" es: "Cambiar")\n remove-photo #(en: "Remove" es: "Quitar")\n add-row #(en: "Add row" es: "A\xF1adir fila")\n remove-row #(en: "Remove row" es: "Quitar fila")\n bold #(en: "Bold" es: "Negrita")\n italic #(en: "Italic" es: "Cursiva")\n heading #(en: "Heading" es: "T\xEDtulo")\n bullet-list #(en: "Bulleted list" es: "Lista con vi\xF1etas")\n number-list #(en: "Numbered list" es: "Lista numerada")\n link #(en: "Link" es: "Enlace")\n link-url #(en: "Web address" es: "Direcci\xF3n web")\n link-add #(en: "Add link" es: "A\xF1adir enlace")\n ]\n]\n\ntranslated: function [\n "A text per language (a map) from a string in the main language or a block [es ... en ...]."\n text [string! block! map!]\n] [\n if map? text [return text]\n m: make map! []\n either string? text [poke m i18n/main text] [\n foreach [lang t] text [poke m to word! lang t]\n ]\n m\n]\n\ntr: function [\n "A text in the active language: from a string, a text per language, or a word of the dictionary."\n text [any-type!]\n] [\n case [\n none? :text [copy ""]\n string? :text [text]\n word? :text [either t: select i18n/texts text [tr t] [form text]]\n block? :text [tr translated text]\n map? :text [\n any [\n select text i18n/language\n select text i18n/main\n foreach [lang t] to block! text [if string? :t [return t]]\n copy ""\n ]\n ]\n true [form :text]\n ]\n]\n\nlocalize: func [\n "A value as a string in the format of the active language: dates, times, numbers, percents and money."\n value [any-type!]\n] [\n format-local :value i18n/language\n]\n');
|
|
13156
13191
|
|
|
13157
13192
|
// tools/cli.ts
|
|
13158
13193
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Cell, type Series } from './values.ts';
|
|
2
|
+
/** Keys of maps and selects: words by symbol (any word type), the rest strictly. */
|
|
3
|
+
export declare function sameKey(a: Cell, b: Cell): boolean;
|
|
4
|
+
/** Index of the value slot for `key` in a map series, or -1. */
|
|
5
|
+
export declare function mapFind(s: Series<Cell>, key: Cell): number;
|
|
6
|
+
/** Position of the first element equal to `key` (as a key) from `from`, or -1. */
|
|
7
|
+
export declare function blockFind(s: Series<Cell>, from: number, key: Cell): number;
|
|
8
|
+
export declare function pick(cur: Cell, key: Cell): Cell | string;
|
|
9
|
+
/** Sets `key` in `cur` to `v`. Returns null on success, or an error message. */
|
|
10
|
+
export declare function poke(cur: Cell, key: Cell, v: Cell): string | null;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Picking and poking by key: what paths do at each segment, and what the
|
|
2
|
+
// `pick`, `select` and `poke` natives build on.
|
|
3
|
+
//
|
|
4
|
+
// Both return an error message string instead of throwing; the caller turns
|
|
5
|
+
// it into a Trebol error.
|
|
6
|
+
import { K, isAnyBlock, isAnyString, isAnyWord, aKindName } from "./kinds.js";
|
|
7
|
+
import { canon, spell } from "./symbols.js";
|
|
8
|
+
import * as I64 from "./int64.js";
|
|
9
|
+
import { NONE, integer, char, decimal, time, date, strictEqual, codePoints, fromCodePoints, } from "./values.js";
|
|
10
|
+
import { ctxFind, ctxSet } from "./ctx.js";
|
|
11
|
+
import { mold } from "./mold.js";
|
|
12
|
+
/** Keys of maps and selects: words by symbol (any word type), the rest strictly. */
|
|
13
|
+
export function sameKey(a, b) {
|
|
14
|
+
if (isAnyWord(a.t) && isAnyWord(b.t))
|
|
15
|
+
return canon(a.sym) === canon(b.sym);
|
|
16
|
+
return strictEqual(a, b);
|
|
17
|
+
}
|
|
18
|
+
/** Index of the value slot for `key` in a map series, or -1. */
|
|
19
|
+
export function mapFind(s, key) {
|
|
20
|
+
for (let j = 0; j < s.data.length; j += 2)
|
|
21
|
+
if (sameKey(s.data[j], key))
|
|
22
|
+
return j + 1;
|
|
23
|
+
return -1;
|
|
24
|
+
}
|
|
25
|
+
/** Position of the first element equal to `key` (as a key) from `from`, or -1. */
|
|
26
|
+
export function blockFind(s, from, key) {
|
|
27
|
+
for (let j = from; j < s.data.length; j++)
|
|
28
|
+
if (sameKey(s.data[j], key))
|
|
29
|
+
return j;
|
|
30
|
+
return -1;
|
|
31
|
+
}
|
|
32
|
+
/** 0-based offset for a 1-based integer index, or null if it is not one. */
|
|
33
|
+
function offset(key) {
|
|
34
|
+
if (key.t !== K.Integer)
|
|
35
|
+
return null;
|
|
36
|
+
return I64.toNumber(key.v) - 1;
|
|
37
|
+
}
|
|
38
|
+
const DATE_PARTS = ['year', 'month', 'day', 'time', 'zone', 'date', 'weekday', 'yearday', 'hour', 'minute', 'second'];
|
|
39
|
+
const TIME_PARTS = ['hour', 'minute', 'second'];
|
|
40
|
+
const NS_HOUR = 3600e9;
|
|
41
|
+
const NS_MIN = 60e9;
|
|
42
|
+
const DAY_MS = 86400000;
|
|
43
|
+
function partName(key) {
|
|
44
|
+
return isAnyWord(key.t) ? spell(canon(key.sym)) : null;
|
|
45
|
+
}
|
|
46
|
+
export function pick(cur, key) {
|
|
47
|
+
switch (cur.t) {
|
|
48
|
+
case K.Url:
|
|
49
|
+
case K.File:
|
|
50
|
+
// url/segment and file/segment build a longer path
|
|
51
|
+
if (isAnyWord(key.t) || isAnyString(key.t) || key.t === K.Integer) {
|
|
52
|
+
const base = cur.s.data.slice(cur.i);
|
|
53
|
+
const seg = codePoints(isAnyWord(key.t) ? spell(key.sym) : isAnyString(key.t) ? fromCodePoints(key.s.data, key.i) : mold(key));
|
|
54
|
+
const slash = base[base.length - 1] === 47 ? [] : [47];
|
|
55
|
+
const tail = seg[0] === 47 ? seg.slice(1) : seg;
|
|
56
|
+
return { t: cur.t, s: { data: [...base, ...slash, ...tail] }, i: 0 };
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case K.Email: {
|
|
60
|
+
// email/user and email/host, as in R3
|
|
61
|
+
const p = partName(key);
|
|
62
|
+
if (p === 'user' || p === 'host') {
|
|
63
|
+
const cps = cur.s.data.slice(cur.i);
|
|
64
|
+
const at = cps.indexOf(64);
|
|
65
|
+
const part = p === 'user' ? (at < 0 ? cps : cps.slice(0, at)) : at < 0 ? [] : cps.slice(at + 1);
|
|
66
|
+
return { t: K.String, s: { data: part }, i: 0 };
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case K.Object:
|
|
71
|
+
case K.Error: {
|
|
72
|
+
if (!isAnyWord(key.t))
|
|
73
|
+
return `cannot pick ${mold(key)} in an object`;
|
|
74
|
+
const i = ctxFind(cur.ctx, key.sym);
|
|
75
|
+
if (i < 0)
|
|
76
|
+
return `${mold(key)} is not a field of the object`;
|
|
77
|
+
return cur.ctx.vals[i];
|
|
78
|
+
}
|
|
79
|
+
case K.Map: {
|
|
80
|
+
const i = mapFind(cur.s, key);
|
|
81
|
+
return i < 0 ? NONE : cur.s.data[i];
|
|
82
|
+
}
|
|
83
|
+
case K.Pair: {
|
|
84
|
+
const p = partName(key);
|
|
85
|
+
const n = offset(key);
|
|
86
|
+
if (p === 'x' || n === 0)
|
|
87
|
+
return decimalOrInt(cur.x);
|
|
88
|
+
if (p === 'y' || n === 1)
|
|
89
|
+
return decimalOrInt(cur.y);
|
|
90
|
+
return `cannot pick ${mold(key)} in a pair`;
|
|
91
|
+
}
|
|
92
|
+
case K.Tuple: {
|
|
93
|
+
const n = offset(key);
|
|
94
|
+
if (n === null)
|
|
95
|
+
return `cannot pick ${mold(key)} in a tuple`;
|
|
96
|
+
const x = cur.v[n];
|
|
97
|
+
return x === undefined ? NONE : integer(x);
|
|
98
|
+
}
|
|
99
|
+
case K.Date: {
|
|
100
|
+
const p = partName(key) ?? DATE_PARTS[offset(key) ?? -1] ?? null;
|
|
101
|
+
const d = cur.v;
|
|
102
|
+
switch (p) {
|
|
103
|
+
case 'year': return integer(d.y);
|
|
104
|
+
case 'month': return integer(d.m);
|
|
105
|
+
case 'day': return integer(d.d);
|
|
106
|
+
case 'time': return d.time === null ? NONE : time(d.time);
|
|
107
|
+
case 'zone': return d.zone === null ? NONE : time(d.zone * NS_MIN);
|
|
108
|
+
case 'date': return date({ y: d.y, m: d.m, d: d.d, time: null, zone: null });
|
|
109
|
+
// ISO weekday: Monday is 1, Sunday 7; 1970-01-01 was a Thursday
|
|
110
|
+
case 'weekday': return integer(((Date.UTC(d.y, d.m - 1, d.d) / DAY_MS) % 7 + 10) % 7 + 1);
|
|
111
|
+
case 'yearday': return integer((Date.UTC(d.y, d.m - 1, d.d) - Date.UTC(d.y, 0, 1)) / DAY_MS + 1);
|
|
112
|
+
case 'hour':
|
|
113
|
+
case 'minute':
|
|
114
|
+
case 'second':
|
|
115
|
+
return d.time === null ? NONE : pick(time(d.time), key.t === K.Integer ? integer(TIME_PARTS.indexOf(p) + 1) : key);
|
|
116
|
+
}
|
|
117
|
+
return `cannot pick ${mold(key)} in a date`;
|
|
118
|
+
}
|
|
119
|
+
case K.Time: {
|
|
120
|
+
const p = partName(key) ?? TIME_PARTS[offset(key) ?? -1] ?? null;
|
|
121
|
+
const ns = Math.abs(cur.v);
|
|
122
|
+
const sign = cur.v < 0 ? -1 : 1;
|
|
123
|
+
switch (p) {
|
|
124
|
+
case 'hour': return integer(sign * Math.floor(ns / NS_HOUR));
|
|
125
|
+
case 'minute': return integer(sign * Math.floor((ns % NS_HOUR) / NS_MIN));
|
|
126
|
+
case 'second': {
|
|
127
|
+
const s = (ns % NS_MIN) / 1e9;
|
|
128
|
+
return Number.isInteger(s) ? integer(sign * s) : decimal(sign * s);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return `cannot pick ${mold(key)} in a time`;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (isAnyBlock(cur.t)) {
|
|
135
|
+
const b = cur;
|
|
136
|
+
const n = offset(key);
|
|
137
|
+
if (n !== null) {
|
|
138
|
+
const v = n >= 0 ? b.s.data[b.i + n] : undefined;
|
|
139
|
+
return v ?? NONE;
|
|
140
|
+
}
|
|
141
|
+
const at = blockFind(b.s, b.i, key);
|
|
142
|
+
return at < 0 ? NONE : b.s.data[at + 1] ?? NONE;
|
|
143
|
+
}
|
|
144
|
+
if (isAnyString(cur.t) || cur.t === K.Binary) {
|
|
145
|
+
const s = cur;
|
|
146
|
+
const n = offset(key);
|
|
147
|
+
if (n === null)
|
|
148
|
+
return `cannot pick ${mold(key)} in ${aKindName(cur.t)}`;
|
|
149
|
+
const cp = n >= 0 ? s.s.data[s.i + n] : undefined;
|
|
150
|
+
if (cp === undefined)
|
|
151
|
+
return NONE;
|
|
152
|
+
return cur.t === K.Binary ? integer(cp) : char(cp);
|
|
153
|
+
}
|
|
154
|
+
return `cannot pick ${mold(key)} in ${aKindName(cur.t)}`;
|
|
155
|
+
}
|
|
156
|
+
function decimalOrInt(n) {
|
|
157
|
+
return Number.isInteger(n) ? integer(n) : decimal(n);
|
|
158
|
+
}
|
|
159
|
+
/** Sets `key` in `cur` to `v`. Returns null on success, or an error message. */
|
|
160
|
+
export function poke(cur, key, v) {
|
|
161
|
+
switch (cur.t) {
|
|
162
|
+
case K.Object:
|
|
163
|
+
case K.Error: {
|
|
164
|
+
if (!isAnyWord(key.t))
|
|
165
|
+
return `cannot set ${mold(key)} in an object`;
|
|
166
|
+
const i = ctxFind(cur.ctx, key.sym);
|
|
167
|
+
if (i < 0)
|
|
168
|
+
return `${mold(key)} is not a field of the object`;
|
|
169
|
+
ctxSet(cur.ctx, i, v);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
case K.Map: {
|
|
173
|
+
const i = mapFind(cur.s, key);
|
|
174
|
+
if (i >= 0) {
|
|
175
|
+
cur.s.data[i] = v;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
const k = isAnyWord(key.t) ? { t: K.Word, sym: key.sym, bind: null } : key;
|
|
179
|
+
cur.s.data.push(k, v);
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (isAnyBlock(cur.t)) {
|
|
185
|
+
const b = cur;
|
|
186
|
+
const n = offset(key);
|
|
187
|
+
if (n !== null) {
|
|
188
|
+
if (n < 0 || b.i + n >= b.s.data.length)
|
|
189
|
+
return `index ${n + 1} is out of range`;
|
|
190
|
+
b.s.data[b.i + n] = v;
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const at = blockFind(b.s, b.i, key);
|
|
194
|
+
if (at < 0 || at + 1 >= b.s.data.length)
|
|
195
|
+
return `${mold(key)} not found in the block`;
|
|
196
|
+
b.s.data[at + 1] = v;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (cur.t === K.String) {
|
|
200
|
+
const s = cur;
|
|
201
|
+
const n = offset(key);
|
|
202
|
+
if (n === null || v.t !== K.Char)
|
|
203
|
+
return `cannot set ${mold(key)} in a string`;
|
|
204
|
+
if (n < 0 || s.i + n >= s.s.data.length)
|
|
205
|
+
return `index ${n + 1} is out of range`;
|
|
206
|
+
s.s.data[s.i + n] = v.v;
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
return `cannot set ${mold(key)} in ${aKindName(cur.t)}`;
|
|
210
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The bytes the text holds in that base (white space ignored), or null if it is not valid. */
|
|
2
|
+
export declare function decodeBase(text: string, base: number): number[] | null;
|
|
3
|
+
/** The bytes written in base 2, 16 (capitals) or 64 (padded with =). */
|
|
4
|
+
export declare function encodeBase(bytes: readonly number[], base: number): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Bytes as text in base 2, 16 and 64, shared by the lexer (2#{...} 16#{...}
|
|
2
|
+
// 64#{...}) and by enbase and debase.
|
|
3
|
+
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
4
|
+
/** The bytes the text holds in that base (white space ignored), or null if it is not valid. */
|
|
5
|
+
export function decodeBase(text, base) {
|
|
6
|
+
const clean = text.replace(/\s+/g, '');
|
|
7
|
+
const bytes = [];
|
|
8
|
+
if (base === 16) {
|
|
9
|
+
if (!/^(?:[0-9A-Fa-f]{2})*$/.test(clean))
|
|
10
|
+
return null;
|
|
11
|
+
for (let j = 0; j < clean.length; j += 2)
|
|
12
|
+
bytes.push(parseInt(clean.slice(j, j + 2), 16));
|
|
13
|
+
return bytes;
|
|
14
|
+
}
|
|
15
|
+
if (base === 2) {
|
|
16
|
+
if (!/^(?:[01]{8})*$/.test(clean))
|
|
17
|
+
return null;
|
|
18
|
+
for (let j = 0; j < clean.length; j += 8)
|
|
19
|
+
bytes.push(parseInt(clean.slice(j, j + 8), 2));
|
|
20
|
+
return bytes;
|
|
21
|
+
}
|
|
22
|
+
if (base !== 64 || !/^[A-Za-z0-9+/]*={0,2}$/.test(clean) || clean.length % 4 === 1)
|
|
23
|
+
return null;
|
|
24
|
+
let buf = 0;
|
|
25
|
+
let bits = 0;
|
|
26
|
+
for (const ch of clean.replace(/=+$/, '')) {
|
|
27
|
+
buf = (buf << 6) | B64.indexOf(ch);
|
|
28
|
+
bits += 6;
|
|
29
|
+
if (bits >= 8) {
|
|
30
|
+
bits -= 8;
|
|
31
|
+
bytes.push((buf >> bits) & 0xff);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return bytes;
|
|
35
|
+
}
|
|
36
|
+
/** The bytes written in base 2, 16 (capitals) or 64 (padded with =). */
|
|
37
|
+
export function encodeBase(bytes, base) {
|
|
38
|
+
if (base === 16)
|
|
39
|
+
return bytes.map(b => b.toString(16).toUpperCase().padStart(2, '0')).join('');
|
|
40
|
+
if (base === 2)
|
|
41
|
+
return bytes.map(b => b.toString(2).padStart(8, '0')).join('');
|
|
42
|
+
let out = '';
|
|
43
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
44
|
+
const n = (bytes[i] << 16) | ((bytes[i + 1] ?? 0) << 8) | (bytes[i + 2] ?? 0);
|
|
45
|
+
out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63];
|
|
46
|
+
out += i + 1 < bytes.length ? B64[(n >> 6) & 63] : '=';
|
|
47
|
+
out += i + 2 < bytes.length ? B64[n & 63] : '=';
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Cell, BitsetCell } from './values.ts';
|
|
2
|
+
export declare function bitHas(b: BitsetCell, n: number): boolean;
|
|
3
|
+
export declare function bitSet(bytes: number[], n: number): void;
|
|
4
|
+
/**
|
|
5
|
+
* Adds the members described by a spec: a char, an integer, a string (its
|
|
6
|
+
* chars), a binary (raw bytes, as the bitset data itself), or a block of
|
|
7
|
+
* those where `a - b` is a range. Returns an error message on a bad spec.
|
|
8
|
+
*/
|
|
9
|
+
export declare function bitsFrom(spec: Cell, bytes: number[]): string | null;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Bitsets, as in Rebol: bit n is bit (0x80 >> (n & 7)) of byte (n >> 3),
|
|
2
|
+
// so `#{80}` holds code point 0. A negated bitset holds everything its bytes
|
|
3
|
+
// do not, which keeps `complement charset "abc"` small.
|
|
4
|
+
import { K } from "./kinds.js";
|
|
5
|
+
import * as I64 from "./int64.js";
|
|
6
|
+
import { spell } from "./symbols.js";
|
|
7
|
+
export function bitHas(b, n) {
|
|
8
|
+
const byte = b.s.data[n >> 3];
|
|
9
|
+
const on = byte !== undefined && (byte & (0x80 >> (n & 7))) !== 0;
|
|
10
|
+
return on !== b.neg;
|
|
11
|
+
}
|
|
12
|
+
export function bitSet(bytes, n) {
|
|
13
|
+
const i = n >> 3;
|
|
14
|
+
while (bytes.length <= i)
|
|
15
|
+
bytes.push(0);
|
|
16
|
+
bytes[i] |= 0x80 >> (n & 7);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Adds the members described by a spec: a char, an integer, a string (its
|
|
20
|
+
* chars), a binary (raw bytes, as the bitset data itself), or a block of
|
|
21
|
+
* those where `a - b` is a range. Returns an error message on a bad spec.
|
|
22
|
+
*/
|
|
23
|
+
export function bitsFrom(spec, bytes) {
|
|
24
|
+
switch (spec.t) {
|
|
25
|
+
case K.Char:
|
|
26
|
+
bitSet(bytes, spec.v);
|
|
27
|
+
return null;
|
|
28
|
+
case K.Integer:
|
|
29
|
+
bitSet(bytes, I64.toNumber(spec.v));
|
|
30
|
+
return null;
|
|
31
|
+
case K.String:
|
|
32
|
+
case K.File:
|
|
33
|
+
case K.Email:
|
|
34
|
+
case K.Url:
|
|
35
|
+
case K.Tag:
|
|
36
|
+
for (const cp of spec.s.data.slice(spec.i))
|
|
37
|
+
bitSet(bytes, cp);
|
|
38
|
+
return null;
|
|
39
|
+
case K.Binary:
|
|
40
|
+
spec.s.data.slice(spec.i).forEach((x, i) => { bytes[i] = (bytes[i] ?? 0) | x; });
|
|
41
|
+
return null;
|
|
42
|
+
case K.Block: {
|
|
43
|
+
const d = spec.s.data.slice(spec.i);
|
|
44
|
+
for (let i = 0; i < d.length; i++) {
|
|
45
|
+
const c = d[i];
|
|
46
|
+
const dash = d[i + 1];
|
|
47
|
+
if (dash !== undefined && dash.t === K.Word && d[i + 2] !== undefined && isDash(dash)) {
|
|
48
|
+
const lo = codeOf(c);
|
|
49
|
+
const hi = codeOf(d[i + 2]);
|
|
50
|
+
if (lo === null || hi === null || hi < lo)
|
|
51
|
+
return 'bad range in bitset spec';
|
|
52
|
+
for (let n = lo; n <= hi; n++)
|
|
53
|
+
bitSet(bytes, n);
|
|
54
|
+
i += 2;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const err = bitsFrom(c, bytes);
|
|
58
|
+
if (err !== null)
|
|
59
|
+
return err;
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return 'bad bitset spec';
|
|
65
|
+
}
|
|
66
|
+
function isDash(w) {
|
|
67
|
+
return w.t === K.Word && spell(w.sym) === '-';
|
|
68
|
+
}
|
|
69
|
+
function codeOf(c) {
|
|
70
|
+
if (c.t === K.Char)
|
|
71
|
+
return c.v;
|
|
72
|
+
if (c.t === K.Integer)
|
|
73
|
+
return I64.toNumber(c.v);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type BlockCell } from './values.ts';
|
|
2
|
+
import { type Runtime, type RunResult } from './eval.ts';
|
|
3
|
+
import './natives/control.ts';
|
|
4
|
+
import { scriptStart } from './natives/data.ts';
|
|
5
|
+
export { scriptStart };
|
|
6
|
+
import './natives/math.ts';
|
|
7
|
+
import './natives/series.ts';
|
|
8
|
+
import './natives/text.ts';
|
|
9
|
+
import './natives/encode.ts';
|
|
10
|
+
import './natives/json.ts';
|
|
11
|
+
import './natives/csv.ts';
|
|
12
|
+
import './natives/xml.ts';
|
|
13
|
+
import './natives/parse.ts';
|
|
14
|
+
export declare function makeRuntime(): Runtime;
|
|
15
|
+
/** Runs a block to completion; an `await` becomes an error. */
|
|
16
|
+
export declare function runSync(rt: Runtime, code: BlockCell): RunResult;
|
|
17
|
+
/** Loads source into the user context and runs it to completion. */
|
|
18
|
+
export declare function doSource(rt: Runtime, src: string, file?: string): RunResult;
|