From 71de3bd59899e69a24dfceceb61d5f50e7d49999 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 19 Oct 2024 21:21:49 -0500 Subject: [PATCH] feat: document grammar of expressions --- src/pages/spec/ast/ast.md | 30 +++++++++++++++++++++++++++++- src/pages/spec/ast/expression.md | 24 +++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/pages/spec/ast/ast.md b/src/pages/spec/ast/ast.md index 81db789..b62280f 100644 --- a/src/pages/spec/ast/ast.md +++ b/src/pages/spec/ast/ast.md @@ -20,11 +20,20 @@ Module = (Statement | Expression)* ## Statement -(At the moment) a statement is either a variable binding or a function declaration +A statement is either a variable binding, a function declaration, +a conditional (for now, until those becom expressions), +for loop, while loop, or an assignment. + +Assignment includes the operators `= += -= *= /=`, etc. Those operators +cannot be used elsewhere, only as part of an Assignment. ```ebnf Statement = VariableBinding | FunctionDeclaration + | Conditional + | ForLoop + | WhileLoop + | Assignment ``` ## Expression @@ -61,3 +70,22 @@ BlockMember = Statement ``` +## Assignment + +The target of an assignment can only be an identifier for now. +In the future this will include other things like maps, arrays, +pattern matching, destructuring, etc. + +```ebnf +Assignment = AssignmentTarget, AssignmentOperator, Expression + +AssignmentTarget = Identifier +AssignmentOperator = "=" + | "+=" + | "-=" + | "*/" + | "/=" + | "%=" +``` + + diff --git a/src/pages/spec/ast/expression.md b/src/pages/spec/ast/expression.md index 62e2eed..6204c2d 100644 --- a/src/pages/spec/ast/expression.md +++ b/src/pages/spec/ast/expression.md @@ -5,4 +5,26 @@ title: Expression # Expression -Expression +The expression parser effectively implements a precedence table. + +| Operator | Precedence | +|------------|------------| +| == != | 4 | +| > >= < <= | 3 | +| - + ++ | 2 | +| / * % | 1 | + + + +```ebnf +Expression = Equality + +Equality = Comparison, (("==" | "!="), Comparison)* +Comparison = Term, ((">" | ">=" | "<" | "<="), Term)* +Term = Factor, (("-" | "+" | "++"), Factor)* +Factor = Unary, (("/" | "*" | "%"), Unary)* +Unary = ("!" | "-"), Expression + | FunctionCallExpression +``` + +