aboutsummaryrefslogtreecommitdiff
path: root/examples/docs/grammar.md
blob: 6edb686861dbaca3723980ac99756f567bbcbb50 (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
Formal grammar definition for Sloth.

```
program             → statement* ;
block               → "{" statement* "}"

statement           → exprStmt
                    | valStmt
                    | varStmt
                    | returnStmt
                    | printStmt
                    | functionStmt
                    | ifStmt
                    | forStmt ;

exprStmt            → expression ";" ;
valStmt             → "val" IDENTIFIER "=" expression ";" ;
varStmt             → "var" IDENTIFIER "=" expression ";" ;
returnStmt          → "return" expression ";" ;
printStmt           → "print" expression ";" ;

functionStmt        → "fn" IDENTIFIER "(" (IDENTIFIER ":" IDENTIFIER)* ")" block ;
ifStmt              → "if" expression block ;
forStmt             → "for" IDENTIFIER "in" expression ".." expression block ;

expression          → logical_or ;

logical_or          → logical_and ( "||" logical_and )* ;
logical_and         → equality ( "&&" equality )* ;
equality            → comparison ( ( "!=" | "==" ) comparison )* ;
comparison          → bitwise_shift ( ( "<" | ">" | "<=" | ">=" ) bitwise_shift )* ;
bitwise_shifting    → additive ( ( "<<" | ">>" ) additive )* ;
additive            → multiplicative ( ( "+" | "-" ) multiplicative )* ;
multiplicative      → unary ( ( "*" | "/" | "%" ) unary )* ;
unary               → ( "!" | "+" | "-" ) unary | call ;

call                → primary ( "(" arguments? ")" )* ;
primary             → "true" | "false" | NUMBER | STRING | IDENTIFIER | "(" expression ")" ;

## Operations
logical_or          : logical_and ( "||" logical_and )* ;
logical_and         : bitwise ( "&&" bitwise )* ;
bitwise             : equality ( ( "&" | "^" | "|" ) equality )* ;
equality            : comparison ( ( "==" | "!=" ) comparison )* ;
comparison          : bitwise_shift ( ( ">" | ">=" | "<" | "<=" ) bitwise_shift )*;
bitwise_shift       : additive ( ( "<<" | ">>" ) additive )* ;
additive            : multiplicative ( ( "+" | "-" ) multiplicative )* ;
multiplicative      : unary ( ( "*" | "/" | "%" ) unary )* ;
unary               : ( "!" | "+" | "-" ) unary | call ;

## Related to calling of functions
call                : primary ( "(" arguments ")" )* ;
arguments           : ( "labeled"? IDENTIFIER ( ":" IDENTIFIER )? ","? )* ;

## Represents a base value
primary             : "true"
                    | "false"
                    | NUMBER
                    | STRING
                    | IDENTIFIER
                    | "(" expression ")" ;

```