Skip to content

Commit d0c05a9

Browse files
authored
Revise documentation for code evaluation and parsing
Updated sections on code evaluation, parsing, and extending the parser and compiler in the documentation.
1 parent 0ff326c commit d0c05a9

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

docs.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ subscript('a * b')({ a: 3, b: 4 }) // 12
1414
```
1515

1616

17-
## Evaluate Code
17+
## Evaluate
1818

1919
### Function Call
2020

@@ -66,7 +66,7 @@ subscript`${['+', 'a', 'b']} * 2`({ a: 1, b: 2 }) // 6
6666

6767

6868

69-
## Parse / Compile Separately
69+
## Parse / Compile
7070

7171
```js
7272
import { parse, compile } from 'subscript'
@@ -99,10 +99,15 @@ Minimal [common syntax](https://en.wikipedia.org/wiki/Comparison_of_programming_
9999
* `"abc"` — double-quoted strings
100100
* `0.1`, `1.2e3` — decimal numbers
101101

102+
```js
103+
import subscript from 'subscript'
104+
105+
subscript('a.b + c * 2')({ a: { b: 1 }, c: 3 }) // 7
106+
```
102107

103108
### Justin
104109

105-
_Just-in_ is no-keywords JS subset, _JSON_ + _expressions ([thread](https://github.com/endojs/Jessie/issues/66)) — extends subscript with:
110+
_Just-in_ is no-keywords JS subset, _JSON_ + _expressions_ ([thread](https://github.com/endojs/Jessie/issues/66)) — extends subscript with:
106111

107112
+ `'abc'` — single-quoted strings
108113
+ `0x1f`, `0b11`, `0o17` — hex, binary, octal
@@ -163,7 +168,9 @@ jessie`
163168

164169
## Extend Parser
165170

166-
### `binary(op, prec, right?)`
171+
### `binary(op, prec, right?)`**
172+
173+
Binary operator `a ⚬ b`, optionally right-associative.
167174

168175
```js
169176
import { binary } from 'subscript'
@@ -174,6 +181,8 @@ binary('%%', 110) // modulo precedence
174181

175182
### `unary(op, prec, post?)`
176183

184+
Unary operator, either prefix `⚬a` or postfix `a⚬`
185+
177186
```js
178187
import { unary } from 'subscript'
179188

@@ -183,7 +192,7 @@ unary('°', 150, true) // postfix: x°
183192

184193
### `nary(op, prec, right?)`
185194

186-
Sequence operators (allows empty slots):
195+
N-ary (sequence) operator like a; b; or a, b, allows empty slots.
187196

188197
```js
189198
import { nary } from 'subscript'
@@ -193,7 +202,7 @@ nary(';', 10, true) // a; b; c → [';', 'a', 'b', 'c']
193202

194203
### `group(op, prec)`
195204

196-
Grouping constructs:
205+
Group construct, like `[a]`, `{a}` etc.
197206

198207
```js
199208
import { group } from 'subscript'
@@ -204,7 +213,7 @@ group('[]', 200) // [a] → ['[]', 'a']
204213

205214
### `access(op, prec)`
206215

207-
Member access:
216+
Member access operator, like `a[b]`, `a(b)` etc.
208217

209218
```js
210219
import { access } from 'subscript'
@@ -246,11 +255,12 @@ token('?', 25, left => {
246255
> This applies to: `=`/`==`/`===`, `!`/`!=`/`!==`, `|`/`||`, `&`/`&&`, etc.
247256
248257

249-
250258
## Extend Compiler
251259

252260
### `operator(op, handler)`
253261

262+
Register evaluator for an operator.
263+
254264
```js
255265
import { operator, compile } from 'subscript'
256266

@@ -307,6 +317,27 @@ import {
307317
} from 'subscript'
308318
```
309319

320+
## Syntax Tree
321+
322+
AST has simplified lispy tree structure (inspired by [frisk](https://ghub.io/frisk) / [nisp](https://github.com/ysmood/nisp)), opposed to [ESTree](https://github.com/estree/estree):
323+
324+
* portable to any language, not limited to JS;
325+
* reflects execution sequence, rather than code layout;
326+
* has minimal overhead, directly maps to operators;
327+
* simplifies manual evaluation and debugging;
328+
* has conventional form and one-liner docs:
329+
330+
```js
331+
'x' // identifier
332+
[, 1] // literal
333+
['+', 'a', 'b'] // binary
334+
['-', 'a'] // unary prefix
335+
['++', 'a', null] // unary postfix
336+
['?', a, b, c] // ternary
337+
['if', cond, then, else] // control flow
338+
```
339+
340+
See full [spec](./spec.md)
310341

311342
[**Playground →**](https://dy.github.io/subscript/) — interactive DSL builder
312343

0 commit comments

Comments
 (0)