aboutsummaryrefslogtreecommitdiff
path: root/docs/grammar.md
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-02-27 07:21:50 -0600
committerCody <cody@codyq.dev>2023-02-27 07:21:50 -0600
commitebfd74ddf0ef6372624ea171e06f8460d0e1351b (patch)
treed59df0e9600a6f2b86b57949048cf990a3e102ae /docs/grammar.md
parenta653a6602fe5ae5eb4739755db7b34bc92ecdadf (diff)
downloadsloth-ebfd74ddf0ef6372624ea171e06f8460d0e1351b.tar.gz
pain
Diffstat (limited to 'docs/grammar.md')
-rw-r--r--docs/grammar.md39
1 files changed, 39 insertions, 0 deletions
diff --git a/docs/grammar.md b/docs/grammar.md
new file mode 100644
index 0000000..d172254
--- /dev/null
+++ b/docs/grammar.md
@@ -0,0 +1,39 @@
+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 ")" ;
+```