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.
Files changed (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
@@ -0,0 +1,360 @@
1
+ // Mezzanines: the part of lib written in Trebol itself. Evaluated into lib
2
+ // at startup by `makeRuntime`. Anything that can live here instead of in a
3
+ // TS native should, since it carries over unchanged to another core.
4
+ export const MEZZANINES = String.raw `
5
+ func: make function! [[
6
+ "Makes a function from a spec and a body."
7
+ spec [block!] body [block!]
8
+ ][
9
+ make function! reduce [spec body]
10
+ ]]
11
+
12
+ does: func ["A function with no arguments." body [block!]] [
13
+ make function! reduce [[] body]
14
+ ]
15
+
16
+ has: func ["A function with only local words." locals [block!] body [block!]] [
17
+ make function! reduce [append copy [/local] locals body]
18
+ ]
19
+
20
+ ; Each call already gets its own frame, so closures and functions are the same.
21
+ closure: :func
22
+
23
+ ; Rebol 3 calls context? bound?.
24
+ bound?: :context?
25
+
26
+ context: func ["Makes an object." spec [block!]] [make object! spec]
27
+
28
+ function: func [
29
+ "A function whose set-words in the body are local."
30
+ spec [block!] body [block!] /local words
31
+ ][
32
+ words: collect-words/deep/set/ignore body spec
33
+ spec: copy spec
34
+ unless any [empty? words find spec /local] [append spec /local]
35
+ make function! reduce [append spec words body]
36
+ ]
37
+
38
+ ; Type predicates: integer? string? series? ... for every datatype and typeset.
39
+ foreach type [
40
+ unset! none! logic! integer! decimal! percent! money! char! pair! tuple! date! time!
41
+ string! file! email! url! tag! binary! bitset! word! set-word! get-word! lit-word!
42
+ refinement! issue! block! paren! path! set-path! get-path! lit-path! map! object!
43
+ error! function! native! op! datatype! typeset! handle!
44
+ any-type! any-value! number! scalar! any-string! series! any-word! any-block!
45
+ any-path! any-object! any-function!
46
+ ] [
47
+ set bind/new to word! append copy/part form type (length? form type) - 1 "?" 'type? func compose [(append copy "True if the value is " form type) value [any-type!]] compose [
48
+ find (make typeset! reduce [get type]) type? :value
49
+ ]
50
+ ]
51
+
52
+ ; Converters: to-integer to-string ... for every datatype that to makes.
53
+ foreach type [
54
+ logic! integer! decimal! percent! money! char! pair! tuple! date! time!
55
+ string! file! email! url! tag! binary! bitset! word! set-word! get-word! lit-word!
56
+ refinement! issue! block! paren! path! set-path! get-path! lit-path! map!
57
+ ] [
58
+ set bind/new to word! append copy "to-" copy/part form type (length? form type) - 1 'type? func compose [(append copy "Converts a value to " form type) value] compose [
59
+ to (get type) :value
60
+ ]
61
+ ]
62
+
63
+ ; Characters with a name, as in Rebol 3.
64
+ space: sp: #" "
65
+ tab: #"^-"
66
+ newline: lf: #"^/"
67
+ cr: #"^M"
68
+ crlf: "^M^/"
69
+ escape: #"^["
70
+ comma: #","
71
+ dot: #"."
72
+ slash: #"/"
73
+ backslash: #"\"
74
+ dbl-quote: #"^""
75
+
76
+ first: func ["The first value of a series." series] [pick series 1]
77
+ second: func ["The second value of a series." series] [pick series 2]
78
+ third: func ["The third value of a series." series] [pick series 3]
79
+ fourth: func ["The fourth value of a series." series] [pick series 4]
80
+ fifth: func ["The fifth value of a series." series] [pick series 5]
81
+ last: func ["The last value of a series." series [series!]] [pick back tail series 1]
82
+
83
+ empty?: func ["True if the series has nothing from its position on, or the map nothing at all." series [series! map!]] [zero? length? series]
84
+
85
+ rejoin: func [
86
+ "Reduces a block and joins the values into one series, of the type of the first."
87
+ block [block!] /local result
88
+ ][
89
+ block: reduce block
90
+ if empty? block [return copy ""]
91
+ result: either series? first block [copy first block] [form first block]
92
+ foreach value next block [append result :value]
93
+ result
94
+ ]
95
+
96
+ join: func [
97
+ "Joins a value with another value, or with a reduced block."
98
+ value rest
99
+ ][
100
+ value: either series? :value [copy value] [form :value]
101
+ append value either block? :rest [reduce rest] [:rest]
102
+ ]
103
+
104
+ collect: func [
105
+ "Evaluates a block where keep adds values to a new block, and returns it."
106
+ body [block!] /local out run
107
+ ][
108
+ out: make block! 0
109
+ run: func [keep] body
110
+ run func [value [any-value!] /only] [
111
+ either only [append/only out :value] [append out :value]
112
+ :value
113
+ ]
114
+ out
115
+ ]
116
+
117
+ replace: function [
118
+ "Replaces the first (or with /all every) occurrence of a pattern in a series."
119
+ target [series!] pattern value /all
120
+ ] [
121
+ len: either series? :pattern [length? pattern] [1]
122
+ pos: target
123
+ while [pos: find pos :pattern] [
124
+ pos: change/part pos :value len
125
+ unless all [break]
126
+ ]
127
+ target
128
+ ]
129
+
130
+ reword: function [
131
+ "Makes a copy of a template with each keyword, marked by an escape ($ by default), replaced by its value."
132
+ source [any-string!]
133
+ values [block! map! object!] "Keywords and their values; a function value is called with the keyword"
134
+ /case "Keywords are case-sensitive"
135
+ /only "Uses the values of a block as they are, without reducing it"
136
+ /escape "Marks keywords another way"
137
+ char [char! any-string! block! none!] "A prefix, a [prefix suffix] block, or none for no mark"
138
+ ] [
139
+ prefix: "$"
140
+ suffix: none
141
+ if escape [either block? char [set [prefix suffix] char] [prefix: char]]
142
+ if char? prefix [prefix: to string! prefix]
143
+ if char? suffix [suffix: to string! suffix]
144
+ keys: make block! 0
145
+ either all [block? values not only] [
146
+ while [not tail? values] [
147
+ key: form first values
148
+ value: do/next next values 'values
149
+ append/only keys reduce [key :value]
150
+ ]
151
+ ] [
152
+ foreach [key value] to block! values [append/only keys reduce [form key :value]]
153
+ ]
154
+ out: copy/part source 0
155
+ pos: source
156
+ while [not tail? pos] [
157
+ mark: either prefix [find pos prefix] [pos]
158
+ unless mark [append out pos break]
159
+ append out copy/part pos mark
160
+ after: either prefix [skip mark length? prefix] [mark]
161
+ hit: none
162
+ foreach entry keys [
163
+ rest: either case [find/match/case after entry/1] [find/match after entry/1]
164
+ if all [rest suffix] [
165
+ rest: either case [find/match/case rest suffix] [find/match rest suffix]
166
+ ]
167
+ if all [rest any [none? hit (length? entry/1) > length? hit/1]] [hit: entry stop: rest]
168
+ ]
169
+ either hit [
170
+ value: second hit
171
+ append out either any-function? :value [value first hit] [:value]
172
+ pos: stop
173
+ ] [
174
+ append out first mark
175
+ pos: next mark
176
+ ]
177
+ ]
178
+ out
179
+ ]
180
+
181
+ pad: func [
182
+ "Pads a string with spaces on the right (with /left, on the left) up to a length. Modifies it."
183
+ str [string!] n [integer!]
184
+ /left "Pads on the left, which aligns to the right"
185
+ /with "Pads with another character" c [char!]
186
+ ] [
187
+ c: any [c #" "]
188
+ loop n - length? str [either left [insert str c] [append str c]]
189
+ str
190
+ ]
191
+
192
+ take: function [
193
+ "Removes the value at the head of a series and returns it; none if there is none."
194
+ series [series! none!]
195
+ /part "Takes that many values, as a series of the same type" length [integer!]
196
+ /last "Takes from the tail instead"
197
+ /deep "Copies the series inside what is taken"
198
+ ] [
199
+ if none? series [return none]
200
+ n: either part [max 0 length] [1]
201
+ if last [series: skip tail series negate n]
202
+ either part [
203
+ out: copy/part series n
204
+ remove/part series n
205
+ either deep [copy/deep out] [out]
206
+ ] [
207
+ if tail? series [return none]
208
+ v: first series
209
+ remove series
210
+ either all [deep series? :v] [copy/deep v] [:v]
211
+ ]
212
+ ]
213
+
214
+ also: func ["Returns the first value, after evaluating the second." a [any-type!] b [any-type!]] [:a]
215
+
216
+ pi: 3.141592653589793
217
+
218
+ as-pair: func ["Makes a pair of two numbers: as-pair 3 4 is 3x4." x [number!] y [number!]] [make pair! reduce [x y]]
219
+
220
+ ajoin: func ["Reduces a block and joins the values into a string, with no spaces." block [block!]] [
221
+ to string! reduce block
222
+ ]
223
+
224
+ reform: func ["Reduces a block and forms it: the values with spaces between. Any other value is formed." value] [
225
+ form reduce :value
226
+ ]
227
+
228
+ comment: func ["Ignores its argument." value [any-type!]] []
229
+
230
+ forever: func ["Evaluates a block again and again, until break or return." body [block!]] [
231
+ while [true] body
232
+ ]
233
+
234
+ default: func [
235
+ "Sets a word to a value if it has none yet; gives the word's value."
236
+ 'word [word! set-word!] value
237
+ ][
238
+ unless all [value? word not none? get word] [set word :value]
239
+ get word
240
+ ]
241
+
242
+ last?: func ["True if the series has one value left." series [series!]] [1 = length? series]
243
+ single?: :last?
244
+
245
+ extract: function [
246
+ "Takes every so many values of a series: the first of each record."
247
+ series [series!] width [integer!] "Size of each record"
248
+ /index "Take this position of each record instead" pos [integer!]
249
+ ][
250
+ out: copy/part series 0
251
+ i: any [pos 1]
252
+ while [i <= length? series] [
253
+ append/only out pick series i
254
+ i: i + width
255
+ ]
256
+ out
257
+ ]
258
+
259
+ alter: func [
260
+ "Removes the value if the series has it, adds it if not; true if it was added."
261
+ series [series!] value
262
+ ][
263
+ either find/only series :value [remove find/only series :value false] [append/only series :value true]
264
+ ]
265
+
266
+ swap: func [
267
+ "Swaps the first values of two series; gives the first series."
268
+ a [series!] b [series!] /local x
269
+ ][
270
+ unless any [tail? a tail? b] [
271
+ x: first a
272
+ poke a 1 first b
273
+ poke b 1 :x
274
+ ]
275
+ a
276
+ ]
277
+
278
+ move: func [
279
+ "Moves a value, or a span of values, within a series; gives the series after it."
280
+ source [series!] "Changed"
281
+ offset [integer!] "How far to move it, or with /to where to"
282
+ /part "Move this many values" length [integer!]
283
+ /skip "The series is records of this size" size [integer!]
284
+ /to "Offset is a position from the head"
285
+ /local taken
286
+ ][
287
+ unless length [length: 1]
288
+ if skip [
289
+ if size < 1 [cause-error 'script 'out-of-range ["size must be at least 1"]]
290
+ offset: either to [offset - 1 * size + 1] [offset * size]
291
+ length: length * size
292
+ ]
293
+ taken: take/part source length
294
+ insert either to [at head source offset] [at source either offset < 0 [offset] [offset + 1]] taken
295
+ ]
296
+
297
+ array: function [
298
+ "Makes a block of the size given, or of blocks for a block of sizes."
299
+ size [integer! block!]
300
+ /initial "Fill it with this; a series is copied, a function called" value
301
+ ][
302
+ rest: none
303
+ if block? size [
304
+ rest: next size
305
+ if tail? rest [rest: none]
306
+ size: first size
307
+ ]
308
+ out: copy []
309
+ loop size [
310
+ append/only out case [
311
+ rest [either initial [array/initial rest :value] [array rest]]
312
+ series? :value [copy/deep value]
313
+ any-function? :value [value]
314
+ true [:value]
315
+ ]
316
+ ]
317
+ out
318
+ ]
319
+
320
+ mod: func [
321
+ "The remainder of a divided by b, with the sign of b."
322
+ a [number! money! time!] b [number! money! time!] "Must not be zero"
323
+ /local r
324
+ ][
325
+ r: a // b
326
+ either all [not zero? r (negative? r) <> negative? b] [r + b] [r]
327
+ ]
328
+
329
+ modulo: func [
330
+ "Like mod, but a remainder too small to tell from b or zero is zero."
331
+ a [number! money! time!] b [number! money! time!] "Must not be zero"
332
+ /local r
333
+ ][
334
+ r: mod a b
335
+ either any [
336
+ all [decimal? r (abs r) < (1e-13 * abs b)]
337
+ all [decimal? r (abs r - b) < (1e-13 * abs b)]
338
+ ] [0.0] [r]
339
+ ]
340
+
341
+ suffix?: func [
342
+ "The suffix of a file name or URL, as %.txt; none when it has none."
343
+ path [file! url! string!]
344
+ /local end
345
+ ][
346
+ either all [url? path end: any [find path #"?" find path #"#"]] [
347
+ all [
348
+ path: find/reverse end #"."
349
+ not find/part path #"/" end
350
+ to file! copy/part path end
351
+ ]
352
+ ] [
353
+ all [
354
+ path: find/last path #"."
355
+ not find path #"/"
356
+ to file! path
357
+ ]
358
+ ]
359
+ ]
360
+ `;
@@ -0,0 +1,20 @@
1
+ import { type Cell, type Series, type DateVal } from './values.ts';
2
+ export interface MoldOptions {
3
+ /** Use construction syntax so that load(mold(x)) is exactly x. */
4
+ all?: boolean;
5
+ /** For a block: mold its contents without the outer brackets. */
6
+ only?: boolean;
7
+ }
8
+ export declare function mold(c: Cell, opts?: MoldOptions): string;
9
+ export declare function form(c: Cell): string;
10
+ /**
11
+ * A decimal as Trebol writes it. As in R3, mold shows 15 significant digits,
12
+ * so 0.1 + 0.2 reads 0.3; `exact` (mold/all) gives the shortest text that
13
+ * loads back to the very same number.
14
+ */
15
+ export declare function formatDecimal(n: number, exact?: boolean): string;
16
+ /** h:mm[:ss[.fraction]], seconds only when not zero. Sign handled by caller. */
17
+ export declare function formatTime(ns: number): string;
18
+ export declare function formatDate(v: DateVal): string;
19
+ /** "file:line:col" for an element of a loaded block, for error messages. */
20
+ export declare function sourceOf(s: Series<Cell>, index: number): string | null;