@mylasertools/kerfscript 0.3.0 → 0.5.0
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/CHANGELOG.md +114 -0
- package/LANGUAGE.md +43 -23
- package/README.md +65 -2
- package/dist/assembly.d.cts +20 -0
- package/dist/assembly.d.ts +20 -0
- package/dist/ast.d.cts +13 -1
- package/dist/ast.d.ts +13 -1
- package/dist/cli.js +5037 -1359
- package/dist/diagnostics.d.cts +6 -0
- package/dist/diagnostics.d.ts +6 -0
- package/dist/evaluator.d.cts +15 -0
- package/dist/evaluator.d.ts +15 -0
- package/dist/format.d.cts +4 -0
- package/dist/format.d.ts +4 -0
- package/dist/geometry.d.cts +2 -0
- package/dist/geometry.d.ts +2 -0
- package/dist/highlight.cjs +151 -0
- package/dist/highlight.d.cts +13 -0
- package/dist/highlight.d.ts +13 -0
- package/dist/highlight.js +131 -0
- package/dist/index.cjs +369 -20
- package/dist/index.d.cts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +369 -20
- package/dist/language.cjs +417 -0
- package/dist/language.d.cts +143 -0
- package/dist/language.d.ts +143 -0
- package/dist/language.js +417 -0
- package/dist/lexer.d.cts +17 -0
- package/dist/lexer.d.ts +17 -0
- package/dist/libraries.cjs +380 -0
- package/dist/libraries.d.cts +112 -0
- package/dist/libraries.d.ts +112 -0
- package/dist/libraries.js +380 -0
- package/dist/lsp.d.cts +12 -0
- package/dist/lsp.d.ts +12 -0
- package/dist/render.d.cts +2 -0
- package/dist/render.d.ts +2 -0
- package/dist/signatures.cjs +675 -0
- package/dist/signatures.d.cts +20 -0
- package/dist/signatures.d.ts +20 -0
- package/dist/signatures.js +655 -0
- package/docs/cli.md +45 -2
- package/docs/desktop.md +5 -0
- package/docs/editors.md +121 -0
- package/docs/javascript.md +157 -20
- package/docs/kerf.md +10 -0
- package/docs/library/box.svg +11 -0
- package/docs/library/calibration.svg +11 -0
- package/docs/library/dividers.svg +11 -0
- package/docs/library/enclosure.svg +11 -0
- package/docs/library/hanging.svg +11 -0
- package/docs/library/hinge.svg +11 -0
- package/docs/library/tabs.svg +11 -0
- package/docs/library.md +339 -0
- package/docs/openscad.md +1 -1
- package/docs/parts-and-sheets.md +32 -0
- package/docs/shared-libraries.md +229 -0
- package/docs/specification.md +529 -0
- package/docs/versions.md +2 -2
- package/docs/webassembly.md +3 -1
- package/package.json +51 -4
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
# Specification
|
|
2
|
+
|
|
3
|
+
Language version 0.2. Copyright (c) 2026 MyLaserTools. This document is
|
|
4
|
+
licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
|
|
5
|
+
|
|
6
|
+
This document defines KerfScript programs precisely: what text is a
|
|
7
|
+
program, what it evaluates to, and when it is an error. The
|
|
8
|
+
[language reference](../LANGUAGE.md) explains the same language for people
|
|
9
|
+
writing designs and lists the built-in modules and functions; where the two
|
|
10
|
+
differ, this document is the one to fix.
|
|
11
|
+
|
|
12
|
+
The [conformance suite](../conformance/README.md) turns these rules into
|
|
13
|
+
small programs with expected results. Each section below names the cases
|
|
14
|
+
that test it. An implementation conforms to language version 0.2 when it
|
|
15
|
+
passes every case.
|
|
16
|
+
|
|
17
|
+
## Notation
|
|
18
|
+
|
|
19
|
+
Grammar rules use EBNF: `{ x }` repeats `x` zero or more times, `[ x ]`
|
|
20
|
+
makes it optional, `|` separates choices and quoted text is literal. Rules
|
|
21
|
+
in `lower_case` are syntax, built from the tokens in
|
|
22
|
+
[Lexical structure](#lexical-structure); rules in `UPPER_CASE` are tokens.
|
|
23
|
+
|
|
24
|
+
## Source text
|
|
25
|
+
|
|
26
|
+
A program is a string of Unicode text. Positions in messages are a line
|
|
27
|
+
and a column, both counted from 1. Lines end at `\n`; a `\r` before it is
|
|
28
|
+
whitespace. Columns count UTF-16 code units, as JavaScript strings and the
|
|
29
|
+
Language Server Protocol do, so a character outside the Basic Multilingual
|
|
30
|
+
Plane such as an emoji takes two columns.
|
|
31
|
+
|
|
32
|
+
## Lexical structure
|
|
33
|
+
|
|
34
|
+
Cases: `lexical/*`.
|
|
35
|
+
|
|
36
|
+
The text is read from start to end as a sequence of tokens. Whitespace
|
|
37
|
+
(space, tab, `\r`, `\n`) and comments separate tokens and are otherwise
|
|
38
|
+
ignored.
|
|
39
|
+
|
|
40
|
+
```ebnf
|
|
41
|
+
LINE_COMMENT = "//" { any character except "\n" } ;
|
|
42
|
+
BLOCK_COMMENT = "/*" { any character } "*/" ; (* ends at the first "*/" *)
|
|
43
|
+
|
|
44
|
+
NUMBER = DIGITS [ "." { DIGIT } ] [ EXPONENT ]
|
|
45
|
+
| "." DIGITS [ EXPONENT ] ;
|
|
46
|
+
EXPONENT = ( "e" | "E" ) [ "+" | "-" ] DIGITS ;
|
|
47
|
+
DIGITS = DIGIT { DIGIT } ;
|
|
48
|
+
|
|
49
|
+
STRING = '"' { CHARACTER | ESCAPE } '"' ; (* CHARACTER is anything but '"', "\" and "\n" *)
|
|
50
|
+
ESCAPE = "\" ( "n" | "t" | "u" HEX HEX HEX HEX | any other character ) ;
|
|
51
|
+
|
|
52
|
+
NAME = ( LETTER | "_" | "$" ) { LETTER | DIGIT | "_" } ; (* ASCII letters and digits *)
|
|
53
|
+
|
|
54
|
+
PATH = "<" PATH_CHARACTER { PATH_CHARACTER } ">" ;
|
|
55
|
+
PATH_CHARACTER = LETTER | DIGIT | "_" | "." | "/" | "-" ;
|
|
56
|
+
|
|
57
|
+
PUNCTUATOR = "<=" | ">=" | "==" | "!=" | "&&" | "||"
|
|
58
|
+
| "+" | "-" | "*" | "/" | "%" | "^" | "<" | ">" | "!" | "?" | ":"
|
|
59
|
+
| "=" | "." | "," | ";" | "(" | ")" | "[" | "]" | "{" | "}" | "#" ;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- Where two punctuators could start at the same place, the longer one is
|
|
63
|
+
taken: `<=` is one token, not `<` and `=`.
|
|
64
|
+
- A number token's value is the nearest IEEE 754 double. `5.` is the number 5. A number has no sign; `-1` is unary minus applied to `1`.
|
|
65
|
+
- In a string, `\n` is a line break, `\t` a tab and `\uXXXX` the UTF-16
|
|
66
|
+
code unit with that hex value. A backslash before any other character
|
|
67
|
+
gives that character, so `\"` is `"`, `\\` is `\` and `\q` is `q`. A
|
|
68
|
+
`\u` not followed by four hex digits is an error.
|
|
69
|
+
- A `PATH` token is only read directly after the name `use` or `include`
|
|
70
|
+
(whitespace and comments may come between). Anywhere else `<` is a
|
|
71
|
+
punctuator.
|
|
72
|
+
- These names are keywords and cannot name a variable, function, module or
|
|
73
|
+
parameter: `module function if else for let each use include true false undef`.
|
|
74
|
+
- A name starting with `$` is a special variable (see
|
|
75
|
+
[Special variables](#special-variables)).
|
|
76
|
+
|
|
77
|
+
Errors, each at the position given:
|
|
78
|
+
|
|
79
|
+
| Text | Message | Position |
|
|
80
|
+
| ---------------------------------------- | -------------------------- | --------------- |
|
|
81
|
+
| a `/*` with no `*/` after it | `Unclosed block comment` | the `/*` |
|
|
82
|
+
| a string with no closing `"` on its line | `Unclosed string` | the opening `"` |
|
|
83
|
+
| `\u` without four hex digits | `Bad \u escape` | the `\` |
|
|
84
|
+
| a character that starts no token | `Unexpected character "c"` | the character |
|
|
85
|
+
|
|
86
|
+
## Syntax
|
|
87
|
+
|
|
88
|
+
Cases: `errors/missing-*`, `lexical/keyword-as-name`.
|
|
89
|
+
|
|
90
|
+
```ebnf
|
|
91
|
+
program = { statement } ;
|
|
92
|
+
|
|
93
|
+
statement = ";"
|
|
94
|
+
| "{" { statement } "}"
|
|
95
|
+
| "module" NAME params body
|
|
96
|
+
| "function" NAME params "=" expr ";"
|
|
97
|
+
| ( "use" | "include" ) PATH [ ";" ]
|
|
98
|
+
| "for" bindings body
|
|
99
|
+
| "if" "(" expr ")" body [ "else" body ]
|
|
100
|
+
| "let" bindings body
|
|
101
|
+
| NAME "=" expr ";"
|
|
102
|
+
| [ modifier ] NAME arguments body ;
|
|
103
|
+
|
|
104
|
+
body = ";" | "{" { statement } "}" | statement ;
|
|
105
|
+
modifier = "*" | "!" | "#" | "%" ;
|
|
106
|
+
|
|
107
|
+
params = "(" [ param { "," param } [ "," ] ] ")" ;
|
|
108
|
+
param = NAME [ "=" expr ] ;
|
|
109
|
+
arguments = "(" [ argument { "," argument } [ "," ] ] ")" ;
|
|
110
|
+
argument = [ NAME "=" ] expr ;
|
|
111
|
+
bindings = "(" [ binding { "," binding } [ "," ] ] ")" ;
|
|
112
|
+
binding = NAME "=" expr ;
|
|
113
|
+
|
|
114
|
+
expr = "function" params expr
|
|
115
|
+
| "let" bindings expr
|
|
116
|
+
| or [ "?" expr ":" expr ] ;
|
|
117
|
+
or = and { "||" and } ;
|
|
118
|
+
and = equality { "&&" equality } ;
|
|
119
|
+
equality = comparison { ( "==" | "!=" ) comparison } ;
|
|
120
|
+
comparison = sum { ( "<" | "<=" | ">" | ">=" ) sum } ;
|
|
121
|
+
sum = product { ( "+" | "-" ) product } ;
|
|
122
|
+
product = unary { ( "*" | "/" | "%" ) unary } ;
|
|
123
|
+
unary = ( "-" | "+" | "!" ) unary | power ;
|
|
124
|
+
power = postfix [ "^" unary ] ;
|
|
125
|
+
postfix = primary { arguments | "[" expr "]" | "." NAME } ;
|
|
126
|
+
primary = NUMBER | STRING | "true" | "false" | "undef" | NAME
|
|
127
|
+
| "(" expr ")" | list ;
|
|
128
|
+
|
|
129
|
+
list = "[" "]"
|
|
130
|
+
| "[" expr ":" expr [ ":" expr ] "]"
|
|
131
|
+
| "[" item { "," item } [ "," ] "]" ;
|
|
132
|
+
item = "for" bindings item
|
|
133
|
+
| "if" "(" expr ")" item [ "else" item ]
|
|
134
|
+
| "let" bindings item
|
|
135
|
+
| "each" expr
|
|
136
|
+
| "(" item ")" (* only when the item inside starts with for or if *)
|
|
137
|
+
| expr ;
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The grammar is read with one rule at a time from left to right, choosing
|
|
141
|
+
the first choice that fits. Where that needs more than the next token:
|
|
142
|
+
|
|
143
|
+
- A statement that starts with a name followed by `=` is an assignment.
|
|
144
|
+
- `function` followed by a name is a function definition; followed by `(`
|
|
145
|
+
it is a function value.
|
|
146
|
+
- `let` followed by `(` is a `let` statement or expression.
|
|
147
|
+
- In a list, a first item followed by `:` makes a range: `[0:5]`,
|
|
148
|
+
`[0:2:10]`. With three parts the middle one is the step.
|
|
149
|
+
- A `use` or `include` statement may end with `;` or not.
|
|
150
|
+
- In arguments and bindings, the name before `=` can be any name, including
|
|
151
|
+
a keyword; it only matters when it matches a parameter.
|
|
152
|
+
- Binary operators on the same level group to the left: `a - b - c` is
|
|
153
|
+
`(a - b) - c`. `^` groups to the right and binds tighter than unary minus
|
|
154
|
+
on its left: `2 ^ 3 ^ 2` is 512 and `-2 ^ 2` is -4. `?:` nests to the
|
|
155
|
+
right.
|
|
156
|
+
- An `else` belongs to the nearest `if`.
|
|
157
|
+
|
|
158
|
+
A token that fits no rule is an error: `Expected <what>, found "<token>"`,
|
|
159
|
+
or `found end of file`, at that token.
|
|
160
|
+
|
|
161
|
+
## Values
|
|
162
|
+
|
|
163
|
+
Cases: `values/*`.
|
|
164
|
+
|
|
165
|
+
| Type | Values |
|
|
166
|
+
| -------- | -------------------------------------------------------------------------- |
|
|
167
|
+
| number | IEEE 754 doubles, including infinity and NaN |
|
|
168
|
+
| string | sequences of Unicode characters |
|
|
169
|
+
| boolean | `true` and `false` |
|
|
170
|
+
| undef | the one value `undef` |
|
|
171
|
+
| list | ordered sequences of values |
|
|
172
|
+
| range | a start, step and end number; `[a:b]` has step 1 |
|
|
173
|
+
| function | a parameter list, a body expression and the scope the function was made in |
|
|
174
|
+
|
|
175
|
+
`type_of(v)` gives the type's name as above. Values cannot be changed
|
|
176
|
+
after they are made; every operation makes a new value.
|
|
177
|
+
|
|
178
|
+
Strings count, index and iterate by Unicode character (code point):
|
|
179
|
+
`len("😀")` is 1.
|
|
180
|
+
|
|
181
|
+
### Truth
|
|
182
|
+
|
|
183
|
+
In a condition (`if`, `?:`, `&&`, `||`, `!`, a comprehension's `if`,
|
|
184
|
+
`assert`), `false`, `0`, NaN, `""`, `[]` and `undef` are false. Every other
|
|
185
|
+
value is true, including `[0]`, `"0"`, ranges and functions.
|
|
186
|
+
|
|
187
|
+
### Equality
|
|
188
|
+
|
|
189
|
+
`a == b` is true when:
|
|
190
|
+
|
|
191
|
+
- both are numbers, strings or booleans of the same type and value (so
|
|
192
|
+
`1 == "1"` and `0 == false` are false, and NaN is not equal to itself),
|
|
193
|
+
- both are `undef`,
|
|
194
|
+
- both are lists of the same length whose items are equal in order,
|
|
195
|
+
- both are ranges with the same start, step and end (`[0:2] == [0:1:2]`),
|
|
196
|
+
- or both are the same function value.
|
|
197
|
+
|
|
198
|
+
`a != b` is `!(a == b)`.
|
|
199
|
+
|
|
200
|
+
### Writing values
|
|
201
|
+
|
|
202
|
+
`echo()` and `str()` write values as text. A number is written as an
|
|
203
|
+
integer when it is one, and otherwise with up to 10 significant digits
|
|
204
|
+
(`1 / 3` is `0.3333333333`, `0.1 + 0.2` is `0.3`). Very large and small
|
|
205
|
+
numbers use exponent form (`1e+21`); infinities are `Infinity` and
|
|
206
|
+
`-Infinity`, NaN is `NaN`, and -0 is `0`. Lists are written as
|
|
207
|
+
`[a, b]`, ranges as `[start:step:end]`, functions as `function(a, b)` and
|
|
208
|
+
`undef` as `undef`. Strings are written as they are at the top level and in
|
|
209
|
+
double quotes, with escapes, inside a list or after a name. `echo()` writes
|
|
210
|
+
its arguments in the order they are given, a named one as `name = value`.
|
|
211
|
+
|
|
212
|
+
## Expressions
|
|
213
|
+
|
|
214
|
+
Cases: `operators/*`, `lists/*`, `functions/*`.
|
|
215
|
+
|
|
216
|
+
### Arithmetic
|
|
217
|
+
|
|
218
|
+
| Operation | Operands | Result |
|
|
219
|
+
| ---------------- | --------------------------------- | ------------------------------------------------------------- |
|
|
220
|
+
| `a + b`, `a - b` | numbers | sum, difference |
|
|
221
|
+
| | two strings (`+` only) | the strings joined |
|
|
222
|
+
| | two lists of the same length | item by item |
|
|
223
|
+
| `a * b` | numbers | product |
|
|
224
|
+
| | list and number, either order | each item times the number |
|
|
225
|
+
| | two lists of numbers, same length | dot product |
|
|
226
|
+
| | matrix (list of rows) and list | each row times the list |
|
|
227
|
+
| | list and matrix | the list times each column; the matrix needs one row per item |
|
|
228
|
+
| `a / b` | numbers; list and number | quotient; each item divided by the number |
|
|
229
|
+
| `a % b` | numbers | remainder, with the sign of `a` (`-7 % 3` is -1) |
|
|
230
|
+
| `a ^ b` | numbers | `a` to the power `b` |
|
|
231
|
+
| `-a` | number or list | `a * -1` |
|
|
232
|
+
| `+a` | anything | `a` unchanged |
|
|
233
|
+
|
|
234
|
+
Arithmetic follows IEEE 754: `1 / 0` is infinity and `0 / 0` is NaN. Any
|
|
235
|
+
other combination of operands, including `undef`, booleans and lists of
|
|
236
|
+
different lengths, is an error at the operator: `Can't add boolean and
|
|
237
|
+
number`, `Can't add lists of length 2 and 3`.
|
|
238
|
+
|
|
239
|
+
### Comparison and logic
|
|
240
|
+
|
|
241
|
+
`<`, `<=`, `>` and `>=` compare two numbers, or two strings by UTF-16 code
|
|
242
|
+
unit (so `"B" < "a"`). Any other pair is an error: `Can't compare number <
|
|
243
|
+
string`. Comparisons give `true` or `false`; `1 < 2 < 3` compares the
|
|
244
|
+
boolean `true` with 3 and is an error.
|
|
245
|
+
|
|
246
|
+
`!a` is `true` when `a` is false and `false` otherwise. `a && b` and
|
|
247
|
+
`a || b` give `true` or `false`, never one of their operands, and skip `b`
|
|
248
|
+
when `a` decides the result, so `false && missing` is `false` without an
|
|
249
|
+
error.
|
|
250
|
+
|
|
251
|
+
`c ? a : b` evaluates only the chosen side.
|
|
252
|
+
|
|
253
|
+
### Names
|
|
254
|
+
|
|
255
|
+
A name in an expression is looked up in this order:
|
|
256
|
+
|
|
257
|
+
1. a variable of that name in the current scope or a scope around it
|
|
258
|
+
(special variables follow the caller chain, see
|
|
259
|
+
[Special variables](#special-variables));
|
|
260
|
+
2. a function definition of that name, which gives a function value;
|
|
261
|
+
3. for a name starting with `$`, `undef`;
|
|
262
|
+
4. otherwise the error `Unknown variable "name"`.
|
|
263
|
+
|
|
264
|
+
### Indexing and members
|
|
265
|
+
|
|
266
|
+
`a[i]` needs a number `i` (otherwise `List index must be a number, got
|
|
267
|
+
<type>`), rounded down. On a list it gives the item at that position from
|
|
268
|
+
0; on a string, the character; on a range, `[start, step, end][i]`. An
|
|
269
|
+
index outside the list, a negative index and an index into any other value
|
|
270
|
+
give `undef`.
|
|
271
|
+
|
|
272
|
+
`a.x`, `a.y` and `a.z` are `a[0]`, `a[1]` and `a[2]` on a list.
|
|
273
|
+
`r.begin`, `r.step` and `r.end` read a range. Any other member gives
|
|
274
|
+
`undef`.
|
|
275
|
+
|
|
276
|
+
### Calls
|
|
277
|
+
|
|
278
|
+
In `f(args)`, when `f` is a name:
|
|
279
|
+
|
|
280
|
+
1. If a variable named `f` is in scope, its value is called. A variable
|
|
281
|
+
hides a function definition of the same name.
|
|
282
|
+
2. Otherwise a function definition named `f` is called.
|
|
283
|
+
3. Otherwise a built-in function named `f` is called.
|
|
284
|
+
4. Otherwise the error is `Unknown function f()`.
|
|
285
|
+
|
|
286
|
+
Calling a value that is not a function is an error: `"f" is a number, not a
|
|
287
|
+
function` for a name, `Can't call a number` otherwise.
|
|
288
|
+
|
|
289
|
+
Arguments are evaluated left to right in the caller's scope, then bound to
|
|
290
|
+
parameters:
|
|
291
|
+
|
|
292
|
+
- a named argument binds the parameter of that name;
|
|
293
|
+
- the remaining parameters take positional arguments in order;
|
|
294
|
+
- a parameter with neither takes its default, evaluated in the new scope
|
|
295
|
+
so it can use the parameters before it, or `undef` without one;
|
|
296
|
+
- positional arguments beyond the parameters are ignored with the warning
|
|
297
|
+
`f(): takes at most N positional arguments, got M; the rest are ignored
|
|
298
|
+
(line n)`;
|
|
299
|
+
- a named argument that starts with `$` sets that special variable for the
|
|
300
|
+
call; any other named argument that matches no parameter is ignored with
|
|
301
|
+
the warning `f(): unknown argument "name" (line n)`.
|
|
302
|
+
|
|
303
|
+
Built-in modules and functions bind arguments the same way, with the
|
|
304
|
+
parameters listed in the signature table (`SIGNATURES` in the JavaScript
|
|
305
|
+
API). Some of their parameters can only be given by name, as `r` in
|
|
306
|
+
`rect(10, r = 2)`; `echo()` takes any named arguments.
|
|
307
|
+
|
|
308
|
+
A function call evaluates its body expression in a new scope whose parent
|
|
309
|
+
is the scope the function was defined or made in. A function value made
|
|
310
|
+
with `function (params) expr` keeps the scope it was made in and reads its
|
|
311
|
+
variables when it is called, so it sees later reassignments in that scope.
|
|
312
|
+
|
|
313
|
+
### Ranges
|
|
314
|
+
|
|
315
|
+
A range value is made by `[start:end]` or `[start:step:end]`; all three must
|
|
316
|
+
be numbers (`Range values must be numbers`). A range is not a list, but
|
|
317
|
+
`for`, `each` and comprehensions go through its values: `start`,
|
|
318
|
+
`start + step`, ... while not past `end`, with `end` included when it is
|
|
319
|
+
reached within 1e-9 of a step. The range is empty when the step is 0 or
|
|
320
|
+
points away from the end. A range with more values than the list limit is
|
|
321
|
+
an error when it is gone through.
|
|
322
|
+
|
|
323
|
+
### List comprehensions
|
|
324
|
+
|
|
325
|
+
Inside `[...]`, items produce zero or more values each:
|
|
326
|
+
|
|
327
|
+
- `expr` produces its value;
|
|
328
|
+
- `each expr` produces each value of a list, range or string, the value
|
|
329
|
+
itself for any other value, and nothing for `undef`;
|
|
330
|
+
- `for (a = A, b = B) item` produces `item` for each value of `A`, and
|
|
331
|
+
inside it for each value of `B`, with the names bound in a new scope;
|
|
332
|
+
- `if (c) item [else item]` produces the chosen item, or nothing;
|
|
333
|
+
- `let (a = 1, b = a + 1) item` binds names in order in a new scope.
|
|
334
|
+
|
|
335
|
+
`for` goes through a list, a range or the characters of a string. Any other
|
|
336
|
+
value is gone through once as itself, and `undef` not at all.
|
|
337
|
+
|
|
338
|
+
## Statements and scope
|
|
339
|
+
|
|
340
|
+
Cases: `scope/*`, `modules/*`.
|
|
341
|
+
|
|
342
|
+
### Evaluation
|
|
343
|
+
|
|
344
|
+
A program is a block. Evaluating a block:
|
|
345
|
+
|
|
346
|
+
1. Every `module` and `function` definition in the block is registered
|
|
347
|
+
first, so it can be called anywhere in the block, also before it.
|
|
348
|
+
`use` statements in the block are processed in this step too (see
|
|
349
|
+
[Libraries](#libraries)). A later definition with the same name replaces
|
|
350
|
+
an earlier one.
|
|
351
|
+
2. Then each statement runs in order.
|
|
352
|
+
|
|
353
|
+
A block has its own scope, created when the block starts. The bodies of
|
|
354
|
+
`if`, `else`, `for` (once per value), `let`, modules, `{ ... }` blocks and
|
|
355
|
+
module children each run in a new scope whose parent is the scope around
|
|
356
|
+
them.
|
|
357
|
+
|
|
358
|
+
### Assignment
|
|
359
|
+
|
|
360
|
+
`name = expr;` evaluates `expr` and binds `name` in the current scope from
|
|
361
|
+
that point on. A later assignment to the same name in the same scope
|
|
362
|
+
replaces the value for the statements after it. Reading a variable before
|
|
363
|
+
its first assignment is an error (`Unknown variable "name"`). An assignment
|
|
364
|
+
inside a block does not change a variable outside it.
|
|
365
|
+
|
|
366
|
+
Assignments at the top level of the program are its parameters. A renderer
|
|
367
|
+
can replace their values by name (render options `params`, `kerf render
|
|
368
|
+
-D`); the replaced assignment's expression is then not evaluated.
|
|
369
|
+
|
|
370
|
+
### Control statements
|
|
371
|
+
|
|
372
|
+
- `if (c) A else B` runs `A` when `c` is true, otherwise `B`.
|
|
373
|
+
- `for (a = A, b = B) body` runs the body for each value of `A`, and inside
|
|
374
|
+
that for each value of `B`, like a comprehension.
|
|
375
|
+
- `let (a = 1, b = a + 1) body` binds names in order, then runs the body.
|
|
376
|
+
|
|
377
|
+
### Module calls
|
|
378
|
+
|
|
379
|
+
A statement `name(args) children` calls a module:
|
|
380
|
+
|
|
381
|
+
1. A module definition named `name` in scope runs its body in a new scope
|
|
382
|
+
whose parent is the scope it was defined in, with the arguments bound
|
|
383
|
+
like a function call's. `children()` inside it runs the call's child
|
|
384
|
+
statements in the caller's scope; `children(index)` runs only the child
|
|
385
|
+
statement at that position from 0, or those at a list or range of
|
|
386
|
+
positions (a position outside them runs nothing);
|
|
387
|
+
`$children` is the number of child statements that are not assignments
|
|
388
|
+
or definitions.
|
|
389
|
+
2. Otherwise a built-in module named `name` runs.
|
|
390
|
+
3. Otherwise the error is `Unknown module name().`, with a hint for
|
|
391
|
+
OpenSCAD's `color()` and `square()`.
|
|
392
|
+
|
|
393
|
+
A modifier changes the call: `*` skips it entirely, so its arguments and
|
|
394
|
+
children are not evaluated; `#` and `%` move all of its shapes to the
|
|
395
|
+
`guide` layer; `!` has no effect and is accepted for OpenSCAD files.
|
|
396
|
+
|
|
397
|
+
### Special variables
|
|
398
|
+
|
|
399
|
+
A name starting with `$` is a special variable. It is looked up through
|
|
400
|
+
the scopes of callers instead of the scopes around the definition: a module
|
|
401
|
+
or function sees the value its caller had, even when the caller set it after
|
|
402
|
+
the definition. Passing `$name = value` as an argument sets it for that
|
|
403
|
+
call. An unset special variable is `undef`.
|
|
404
|
+
|
|
405
|
+
These start with a value:
|
|
406
|
+
|
|
407
|
+
| Name | Value |
|
|
408
|
+
| ------------ | ------------ |
|
|
409
|
+
| `$fn` | 0 |
|
|
410
|
+
| `$fa` | 2 |
|
|
411
|
+
| `$fs` | 0.2 |
|
|
412
|
+
| `$thickness` | 3 |
|
|
413
|
+
| `$kerf` | 0 |
|
|
414
|
+
| `$material` | `"material"` |
|
|
415
|
+
| `$sheet` | `[600, 400]` |
|
|
416
|
+
| `$gap` | 3 |
|
|
417
|
+
| `$layout` | `"pack"` |
|
|
418
|
+
|
|
419
|
+
`PI` is an ordinary variable set to π in the outermost scope.
|
|
420
|
+
|
|
421
|
+
## Libraries
|
|
422
|
+
|
|
423
|
+
Cases: `imports/*`.
|
|
424
|
+
|
|
425
|
+
`use <path>` and `include <path>` read another program, a library. The
|
|
426
|
+
renderer decides which text a path names: the built-in standard library
|
|
427
|
+
answers paths that start with `kerf/`, and the application supplies the
|
|
428
|
+
rest (the `resolve` render option). The command line, the desktop app and
|
|
429
|
+
the language server look in the design's project, the nearest folder at or
|
|
430
|
+
above the design's with a `kerf.json`: `<name/...>` is a file of the
|
|
431
|
+
project when `name` is the project's own name, or of the installed library
|
|
432
|
+
in `kerf_libraries/name/`; any other path is a file in the design's folder.
|
|
433
|
+
Each tries `path`, then `path.kerf` ([Shared libraries](shared-libraries.md)).
|
|
434
|
+
A path that names nothing is the error `Library <path> not found`. Each path is read and parsed once per render.
|
|
435
|
+
|
|
436
|
+
`use <path>` makes the library's modules and functions available in the
|
|
437
|
+
block containing the `use`, from the start of that block:
|
|
438
|
+
|
|
439
|
+
- The library's top-level assignments are evaluated, in order, in a scope
|
|
440
|
+
of its own whose parent is the block that uses it. Its functions and
|
|
441
|
+
modules see those variables; the program does not.
|
|
442
|
+
- The library's other statements, including `echo()` and shapes, do not
|
|
443
|
+
run.
|
|
444
|
+
- Files the library `include`s are part of the library, each once.
|
|
445
|
+
- Using the same library again has no further effect.
|
|
446
|
+
|
|
447
|
+
`include <path>` runs the library's statements in place, in the current
|
|
448
|
+
scope, as if its text were written there: its assignments, definitions and
|
|
449
|
+
shapes all take effect. Including a file twice runs it twice.
|
|
450
|
+
|
|
451
|
+
A library that uses or includes itself, directly or through others, is an
|
|
452
|
+
error at the statement that closes the loop: `Libraries use each other in a
|
|
453
|
+
loop: <a> → <b> → <a>`, or `Library <a> includes itself`. Positions inside a
|
|
454
|
+
library carry its path: `(line 1, column 1 in <a>)`.
|
|
455
|
+
|
|
456
|
+
## Errors
|
|
457
|
+
|
|
458
|
+
Cases: `errors/*`, and the errors in every other group.
|
|
459
|
+
|
|
460
|
+
An error stops the program. It has a message and, in nearly every case, a
|
|
461
|
+
position, written after the message as `(line L, column C)`, or `(line L,
|
|
462
|
+
column C in <path>)` inside a library. The position is the start of the
|
|
463
|
+
token the error is about: the name in a call that fails, the operator of an
|
|
464
|
+
operation that fails, or the name of a built-in given an unusable argument.
|
|
465
|
+
Output from `echo()` before the error is
|
|
466
|
+
kept and reported with it.
|
|
467
|
+
|
|
468
|
+
An unknown variable, function, module or named argument whose name is close
|
|
469
|
+
to a known one (one edit for names of three to five characters, two for
|
|
470
|
+
longer ones, or a difference only in case) adds `. Did you mean "name"?`
|
|
471
|
+
to the message.
|
|
472
|
+
|
|
473
|
+
An error inside a module or function call also records the call: its name
|
|
474
|
+
and the position it was called from, innermost first. A call that is on
|
|
475
|
+
the stack more than once, through recursion, is recorded once with a count.
|
|
476
|
+
`formatError()` in the [JavaScript API](javascript.md#errors) and the
|
|
477
|
+
command line show these under the source line.
|
|
478
|
+
|
|
479
|
+
Errors come from:
|
|
480
|
+
|
|
481
|
+
- text that is not a program (see [Lexical structure](#lexical-structure)
|
|
482
|
+
and [Syntax](#syntax));
|
|
483
|
+
- unknown names, calls of values that are not functions, and operations on
|
|
484
|
+
the wrong types (see [Expressions](#expressions));
|
|
485
|
+
- `assert(condition, message)` with a false condition: `Assertion failed`,
|
|
486
|
+
followed by `: message` when there is one;
|
|
487
|
+
- built-in modules and functions given unusable arguments;
|
|
488
|
+
- limits. A program from someone else can run in a browser, so these are
|
|
489
|
+
enforced:
|
|
490
|
+
|
|
491
|
+
| Limit | Default | Message starts with |
|
|
492
|
+
| ------------------------------------------- | --------- | ------------------------------------------ |
|
|
493
|
+
| Statements, calls and loop steps | 5,000,000 | `Stopped after N steps` |
|
|
494
|
+
| Nested module calls | 1,000 | `Recursion too deep in module name()` |
|
|
495
|
+
| Nested function calls | 2,000 | `Recursion too deep in name()` |
|
|
496
|
+
| Items in one list, characters in one string | 1,000,000 | `a list of N items` / `a range of N items` |
|
|
497
|
+
| Items in all lists and strings | 3,000,000 | `The lists and strings hold more than` |
|
|
498
|
+
| Points in all shapes | 2,000,000 | `The shapes have more than` |
|
|
499
|
+
|
|
500
|
+
The [JavaScript API](javascript.md#running-untrusted-code) lists every
|
|
501
|
+
limit and how to change it.
|
|
502
|
+
|
|
503
|
+
A warning does not stop the program. Warnings are collected in order, each
|
|
504
|
+
message once, and returned with the output. Examples are unknown named
|
|
505
|
+
arguments, positional arguments past the parameters and a program that
|
|
506
|
+
makes no shapes.
|
|
507
|
+
|
|
508
|
+
Running out of interpreter stack in a call that calls itself, before the
|
|
509
|
+
limit on nested calls, is the same error as reaching the limit, at the call
|
|
510
|
+
that went one level too deep.
|
|
511
|
+
|
|
512
|
+
## Output
|
|
513
|
+
|
|
514
|
+
Cases: `shapes/*`.
|
|
515
|
+
|
|
516
|
+
A program's shapes are 2D regions and lines, each on a layer (`cut` unless
|
|
517
|
+
placed on another), grouped into parts. The [language
|
|
518
|
+
reference](../LANGUAGE.md#layers) describes layers, parts, materials, kerf
|
|
519
|
+
compensation and sheet packing, and lists the built-in modules that make
|
|
520
|
+
shapes. Shapes outside every `part()` form one part named `part`.
|
|
521
|
+
|
|
522
|
+
## Known differences
|
|
523
|
+
|
|
524
|
+
These are behaviours of KerfScript 0.4 that this specification records as
|
|
525
|
+
they are, but that may change before language version 1.0:
|
|
526
|
+
|
|
527
|
+
- An expression nested so deeply that the interpreter runs out of stack,
|
|
528
|
+
outside any recursion, stops with `Recursion too deep: the program calls
|
|
529
|
+
itself without stopping` and no position.
|
package/docs/versions.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Versions
|
|
2
2
|
|
|
3
|
-
KerfScript 0.
|
|
3
|
+
KerfScript 0.5.0 implements language version 0.2. The package version tracks
|
|
4
4
|
the software release; the language version tracks the syntax and behavior
|
|
5
5
|
of `.kerf` programs.
|
|
6
6
|
|
|
@@ -30,4 +30,4 @@ files used with a design. Applications that store designs should retain
|
|
|
30
30
|
that information and compare the output before upgrading the interpreter.
|
|
31
31
|
|
|
32
32
|
Pin the npm package to an exact version when your application needs a fixed
|
|
33
|
-
renderer, for example `@mylasertools/kerfscript@0.
|
|
33
|
+
renderer, for example `@mylasertools/kerfscript@0.5.0`.
|
package/docs/webassembly.md
CHANGED
|
@@ -60,7 +60,9 @@ Every response has `ok`. When it is `false`, `error` holds the message and
|
|
|
60
60
|
exits with an error code for a bad program.
|
|
61
61
|
|
|
62
62
|
- `render`: `svg` (all sheets, for preview), `width`, `height`, `parts`,
|
|
63
|
-
`warnings`, `echoes` and `sheets`.
|
|
63
|
+
`assembly`, `warnings`, `echoes` and `sheets`. `assembly` is the same as
|
|
64
|
+
from [`render()`](javascript.md): one entry per copy of a part, with its
|
|
65
|
+
outline, thickness and placement. Each sheet has `width`, `height`,
|
|
64
66
|
`material`, `thickness`, `svg` and `dxf`; write one file per sheet to cut.
|
|
65
67
|
- `params`: `parameters`, as from [`parameters()`](javascript.md).
|
|
66
68
|
- `version`: `version` (package) and `language`.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mylasertools/kerfscript",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "KerfScript: a programming language for laser
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "KerfScript: a programming language and library for laser-cutting generators. Write parametric 2D parts with cut, score and engrave layers, kerf compensation and joinery, then turn settings into SVG or DXF cut files from Node.js, the browser, the command line or WebAssembly.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
7
7
|
"author": "MyLaserTools",
|
|
@@ -20,7 +20,13 @@
|
|
|
20
20
|
"dxf",
|
|
21
21
|
"finger-joints",
|
|
22
22
|
"living-hinge",
|
|
23
|
-
"kerf"
|
|
23
|
+
"kerf",
|
|
24
|
+
"generator",
|
|
25
|
+
"laser-cutter",
|
|
26
|
+
"svg-generator",
|
|
27
|
+
"dxf-generator",
|
|
28
|
+
"language-server",
|
|
29
|
+
"lsp"
|
|
24
30
|
],
|
|
25
31
|
"sideEffects": false,
|
|
26
32
|
"main": "./dist/index.cjs",
|
|
@@ -37,6 +43,46 @@
|
|
|
37
43
|
"default": "./dist/index.cjs"
|
|
38
44
|
}
|
|
39
45
|
},
|
|
46
|
+
"./highlight": {
|
|
47
|
+
"import": {
|
|
48
|
+
"types": "./dist/highlight.d.ts",
|
|
49
|
+
"default": "./dist/highlight.js"
|
|
50
|
+
},
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/highlight.d.cts",
|
|
53
|
+
"default": "./dist/highlight.cjs"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"./signatures": {
|
|
57
|
+
"import": {
|
|
58
|
+
"types": "./dist/signatures.d.ts",
|
|
59
|
+
"default": "./dist/signatures.js"
|
|
60
|
+
},
|
|
61
|
+
"require": {
|
|
62
|
+
"types": "./dist/signatures.d.cts",
|
|
63
|
+
"default": "./dist/signatures.cjs"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"./language": {
|
|
67
|
+
"import": {
|
|
68
|
+
"types": "./dist/language.d.ts",
|
|
69
|
+
"default": "./dist/language.js"
|
|
70
|
+
},
|
|
71
|
+
"require": {
|
|
72
|
+
"types": "./dist/language.d.cts",
|
|
73
|
+
"default": "./dist/language.cjs"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"./libraries": {
|
|
77
|
+
"import": {
|
|
78
|
+
"types": "./dist/libraries.d.ts",
|
|
79
|
+
"default": "./dist/libraries.js"
|
|
80
|
+
},
|
|
81
|
+
"require": {
|
|
82
|
+
"types": "./dist/libraries.d.cts",
|
|
83
|
+
"default": "./dist/libraries.cjs"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
40
86
|
"./cli": "./dist/cli.js",
|
|
41
87
|
"./package.json": "./package.json"
|
|
42
88
|
},
|
|
@@ -66,7 +112,8 @@
|
|
|
66
112
|
},
|
|
67
113
|
"scripts": {
|
|
68
114
|
"build": "node scripts/stdlib.mjs && tsc -p tsconfig.json && node scripts/build.mjs",
|
|
69
|
-
"test": "node --test",
|
|
115
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
116
|
+
"snapshots": "npm run build && node scripts/snapshots.mjs",
|
|
70
117
|
"check": "npm run build && npm test",
|
|
71
118
|
"examples": "node scripts/examples.mjs",
|
|
72
119
|
"site": "npm run build && npm --prefix site run build",
|