aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/grammar.md39
-rw-r--r--docs/precedence.md16
2 files changed, 55 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 ")" ;
+```
diff --git a/docs/precedence.md b/docs/precedence.md
new file mode 100644
index 0000000..417fa45
--- /dev/null
+++ b/docs/precedence.md
@@ -0,0 +1,16 @@
+Operating precedence in sloth from highest to lowest.
+
+| Name | Operators | Associates |
+| -------------- | --------- | ---------- |
+| parentheses | () | Left |
+| member access | . ! !! ?. | Left |
+| defaulting | ?: | Right |
+| unary | ! + - | Right |
+| multiplicative | \* / % | Left |
+| additive | + - | Left |
+| bitwise shift | << >> | Left |
+| comparison | < > <= >= | Left |
+| equality | == != | Left |
+| bitwise | & ^ \| | Left |
+| logical and | && | Left |
+| logical or | \|\| | Left |