Foundations Syntax and Atoms Lesson 02
Your first document
Mental Model
SJON source is a sequence of values. A value can be an atom, vector, or form. A common authoring shape is:
(head :key value
(child :key value))
Whitespace is only separation. Newlines and indentation are for humans, not for meaning.
Source order is preserved. If you write keys before children, tools and readers see that order. SJON allows kvpairs and positional children to be intermixed, but the house style for readable documents is:
- Required keys.
- Optional keys.
- Positional child forms.
Worked Example
Start with an atomic root:
42
Now use a single form root:
(scene :name "intro")
Add a nested child:
(scene :name "intro"
(canvas :name "main" :size [1920 1080]))
Add a second level:
(scene :name "intro"
(canvas :name "main" :size [1920 1080]
(camera :ortho :zoom 2)))
All four snippets are syntactically valid SJON. Whether the heads and keys are accepted depends on the host-loaded schema.
Multiple roots are also valid:
(layer :name "background" :z 0)
(layer :name "midground" :z 1)
(layer :name "foreground" :z 2)
Use multi-root documents for batches, fixtures, palettes, and other cases where the host expects a list of peer values.
Exercises
Write:
- A single atomic root containing the number
130. - A single form root named
scenewith:name "intro". - A
scenewith a nestedcanvas. - A
canvaswith a nestedcameraandstack. - A multi-root document containing three
(layer ...)forms.
Predict:
(scene
(canvas :name "main")
:name "intro")
This is syntactically valid. The scene form has a positional child
before its :name kvpair. The authoring convention would usually move
:name "intro" before the child:
(scene :name "intro"
(canvas :name "main"))
Repair:
:name "intro"
(canvas :name "main")
The first line tries to put a kvpair at the document root. Repair it by choosing a containing form:
(scene :name "intro"
(canvas :name "main"))
Mastery Check
-
What are the roots in a document with three top-level
(layer ...)forms? -
Does indentation change the tree?
-
Why do keys-first documents make later edits easier to review?