aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-03-30 02:44:54 -0500
committerCody <cody@codyq.dev>2023-03-30 02:44:54 -0500
commit2970520a9592b5c6d45291f54073552a474b71b4 (patch)
treef42ecc1be0989367cf7c70d0b7909bac0b86904e /examples
parentbb95375f8b24141bf7dfe5a8b1bba5c995f61253 (diff)
downloadsloth-2970520a9592b5c6d45291f54073552a474b71b4.tar.gz
Restructure
Diffstat (limited to 'examples')
-rw-r--r--examples/docs/grammar.md63
-rw-r--r--examples/docs/precedence.md17
2 files changed, 80 insertions, 0 deletions
diff --git a/examples/docs/grammar.md b/examples/docs/grammar.md
new file mode 100644
index 0000000..6edb686
--- /dev/null
+++ b/examples/docs/grammar.md
@@ -0,0 +1,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 ")" ;
+
+```
diff --git a/examples/docs/precedence.md b/examples/docs/precedence.md
new file mode 100644
index 0000000..32cc5de
--- /dev/null
+++ b/examples/docs/precedence.md
@@ -0,0 +1,17 @@
+Operating precedence in sloth from highest to lowest.
+
+| Name | Operators | Associates |
+| -------------- | --------- | ---------- |
+| parentheses | () | Left |
+| member access | . ! !! ?. | Left |
+| defaulting | ?: | Right |
+| function call | () | Left |
+| unary | ! + - | Right |
+| multiplicative | \* / % | Left |
+| additive | + - | Left |
+| bitwise shift | << >> | Left |
+| comparison | < > <= >= | Left |
+| equality | == != | Left |
+| bitwise | & ^ \| | Left |
+| logical and | && | Left |
+| logical or | \|\| | Left |