Skip to content

Commit 9a9f57d

Browse files
authored
Docs: Add notes about concatenation AST forms (#39)
* Docs: Add notes about concatenation AST forms * Add embedded JuliaCon talk
1 parent 5214bc0 commit 9a9f57d

File tree

1 file changed

+99
-5
lines changed

1 file changed

+99
-5
lines changed

README.md

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ handful of failures remaining in the Base tests and standard library.
3232
The tree data structures should be somewhat usable but will evolve as we try
3333
out various use cases.
3434

35+
A talk from JuliaCon 2022 covered some aspects of this package.
36+
37+
[![Youtube video thumbnail](https://img.youtube.com/vi/CIiGng9Brrk/mqdefault.jpg)](https://youtu.be/CIiGng9Brrk)
38+
3539
# Examples
3640

3741
Here's what parsing of a small piece of code currently looks like in various
@@ -243,14 +247,104 @@ JuliaSyntax currently deals in three types of trees:
243247
associated `GreenTree` nodes.
244248
* `Expr` is used as a conversion target for compatibility
245249

246-
Wherever possible, the tree structure of `GreenNode`/`SyntaxNode` is 1:1 with
247-
`Expr`. There are, however, some exceptions.
250+
## Julia AST structures
251+
252+
In this section we describe some features of Julia's AST structures.
253+
254+
### Concatenation syntax
255+
256+
Concatenation syntax comes in two syntax forms:
257+
* The traditional `hcat`/`vcat`/`row` which deal with concatenation or matrix
258+
construction along dimensions one and two.
259+
* The new `ncat`/`nrow` syntax which deals with concatenation or array
260+
construction along arbitrary dimensions.
261+
262+
We write `ncat-3` for concatenation along the third dimension. (The `3` is
263+
stored in the head flags for `SyntaxNode` trees, and in the first `arg` for
264+
`Expr` trees.) Semantically the new syntax can work like the old:
265+
* `ncat-1` is the same as `vcat`
266+
* `ncat-2` is the same as `hcat`
267+
* `row` is the same as `nrow-2`
268+
269+
#### Vertical concatenation (dimension 1)
270+
271+
Vertical concatenation along dimension 1 can be done with semicolons or newlines
272+
273+
```julia
274+
julia> print_tree(:([a
275+
b]))
276+
Expr(:vcat)
277+
├─ :a
278+
└─ :b
279+
280+
julia> print_tree(:([a ; b]))
281+
Expr(:vcat)
282+
├─ :a
283+
└─ :b
284+
```
285+
286+
#### Horizontal concatenation (dimension 2)
287+
288+
For horizontal concatenation along dimension 2, use spaces or double semicolons
289+
290+
```julia
291+
julia> print_tree(:([a b]))
292+
Expr(:hcat)
293+
├─ :a
294+
└─ :b
295+
296+
julia> print_tree(:([a ;; b]))
297+
Expr(:ncat)
298+
├─ 2
299+
├─ :a
300+
└─ :b
301+
```
302+
303+
#### Mixed concatenation
304+
305+
Concatenation along dimensions 1 and 2 can be done with spaces and single
306+
semicolons or newlines, producing a mixture of `vcat` and `row` expressions:
307+
308+
```julia
309+
julia> print_tree(:([a b
310+
c d]))
311+
# OR
312+
julia> print_tree(:([a b ; c d]))
313+
Expr(:vcat)
314+
├─ Expr(:row)
315+
│ ├─ :a
316+
│ └─ :b
317+
└─ Expr(:row)
318+
├─ :c
319+
└─ :d
320+
```
321+
322+
General n-dimensional concatenation results in nested `ncat` and `nrow`, for
323+
example
324+
325+
```julia
326+
julia> print_tree(:([a ; b ;; c ; d ;;; x]))
327+
Expr(:ncat)
328+
├─ 3
329+
├─ Expr(:nrow)
330+
│ ├─ 2
331+
│ ├─ Expr(:nrow)
332+
│ │ ├─ 1
333+
│ │ ├─ :a
334+
│ │ └─ :b
335+
│ └─ Expr(:nrow)
336+
│ ├─ 1
337+
│ ├─ :c
338+
│ └─ :d
339+
└─ :x
340+
```
248341

249342
## Tree differences between GreenNode and Expr
250343

251-
First, `GreenNode` inherently stores source position, so there's no need for
252-
the `LineNumberNode`s used by `Expr`. There's also a small number of other
253-
differences
344+
Wherever possible, the tree structure of `GreenNode`/`SyntaxNode` is 1:1 with
345+
`Expr`. There are, however, some exceptions. First, `GreenNode` inherently
346+
stores source position, so there's no need for the `LineNumberNode`s used by
347+
`Expr`. There's also a small number of other differences
254348

255349
### Flattened generators
256350

0 commit comments

Comments
 (0)