aboutsummaryrefslogtreecommitdiffstats
path: root/docs/reference/01_syntax.md
blob: 4db70f93bc1b00de7b472a5ffca09f9bfaeaab25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
`alv` programs consist of a series of expressions separated by chunks of
whitespace. Each expression may be either a literal constant or a cell.

# literal constants
There are three types of literals with different syntax:

## numbers
Numbers consist of the digits `0`-`9` and can optionally begin with a
negative sign and contain a decimal dot. The digits before or after the
decimal point may be left off, but a number needs to consist of at least one
digit.

The following are all valid numbers:

    0
    12
    -7
    0.1
    10.
    .1
    123.

The regular expression `-?(\d+\.\d*|\d*\.\d+|\d+)` matches all numbers. 

## strings
Strings are enclosed by either single (`'`) or double quotes (`"`). Backslashes
and either type of quote can be escaped by prefixing them with a single
backslash, i.e. the literal notation `"\\\""` evaluates to the string `\"`.

The following are all valid strings:

    "hello world"
    'hello world'
    "it's a beautiful day"
    'it\'s a beautiful day'
    "this is a backslash: \\"
    "this is a double quote: \""
    ""
    ''

## symbols
Symbols must start with a letter (`a`-`z` or `A`-`Z`) or one of the following
special characters:

    - + * /
    _ . , =
    ! ? % $
    > < ~

The remaining characters can be letters, special characters from this set, or
digits (`0`-`9`).

The following are all valid symbols:

    helloWORLD
    -
    /
    *dynamic*
    *+*
    var01
    _test
    foo$

The regular expression `[a-zA-Z\-_+*^%\/.,=~!?$><][a-zA-Z0-9\-_+*^%\/.,=~!?$><]*`
matches all symbols.

# cells
Cells consist of one or more subexpressions separated by chunks of whitespace
and enclosed in parentheses (`(` and `)`). A cell optionally contains a tag
immediately after the opening parenthesis. Whitespace between the opening
parenthesis and the first subexpression or the closing parenthesis and the last
subexpression is optional.

The first subexpression is considered the head of the cell and determines the
behaviour of the cell.

## tags
Tags consist of one or more digits (`0`-`9`) enclosed in square brackets (`[`
and `]`). `[1]` and `[255]` are examples of valid tags.

## template strings
Template Strings are a different syntax for cells that allows embedding alv
expressions inside of a string literal. They can be used to perform string
substitution, especially within code of other languages.

Template strings start with the character `$` followed by an optional tag and
a symbol that functions as the head of the cell and finally a `"`-delimited
string literal. Within the string literal, a `$` character begins an
interpolation expression unless escaped by a single preceding blackslash.

The following are example template strings:

    $empty""
    $hello"world"
    $fmt"three is $3 and four is $"four""
    $[99]fmt"five is $(+ 3 2)"
    $fmt"there is \$no substitution here"

Template strings are syntactic sugar that transforms into an array of string
pieces and the values to substitute, the following cell expressions are
semantically identical to the previous examples respectively:

    (empty [""])
    (hello ["world"])
    (fmt ["three is " " and four is " ""] 3 "four")
    ([99]fmt ["five is" ""] (+ 3 2))
    (fmt ["there is $no substitution here"])

# whitespace
The space, tab, newline, and line-feed special characters constitute whitespace
and may be repeated any number of times to form a chunk. A chunk of whitespace
may also contain any number of comments, but may neither begin nor end with a
comment.

## comments
A comment begins with `#(` and ends with a matching parenthesis `)`. Comments
may contain other comments or cells.