From 3f3df597610471ca5953d7af546e8a4acadc7343 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Sun, 7 Sep 2025 16:37:00 -0500 Subject: [PATCH] Work on expressions. --- src/parser/ast.yaml | 158 +++++++++++++++++++++++++++++++++++++++++ src/parser/deimos.pest | 132 ++++++++++++++++++++-------------- 2 files changed, 238 insertions(+), 52 deletions(-) diff --git a/src/parser/ast.yaml b/src/parser/ast.yaml index 15aad26..bf0b3c3 100644 --- a/src/parser/ast.yaml +++ b/src/parser/ast.yaml @@ -440,4 +440,162 @@ ForStatement: vec: true - end_kw: rule: End + vec: true + +# Expressions +Expression: + children: + - ternary_expression +TernaryExpression: + children: + - or_expression + - ternary_alternatives: + optional: true +TernaryAlternatives: + children: + - ternary_true_alternative + - ternary_false_alternative +TernaryTrueAlternative: + children: + - expression +TernaryFalseAlternative: + children: + - expression +OrExpression: + children: + - left: + rule: AndExpression + - or_sym: + rule: Or + skip: true + - right: + rule: Expression + optional: true +AndExpression: + children: + - left: + rule: ComparisonExpression + - and_sym: + rule: And + skip: true + - right: + rule: Expression + optional: true +ComparisonExpression: + children: + - left: + rule: ShiftExpression + - operator: + rule: ComparisonOperator + optional: true + - right: + rule: Expression + optional: true +ComparisonOperator: + rules: + - Greater + - Less + - GreaterEqual + - LessEqual + - EqualTo + - NotEqualTo +ShiftExpression: + children: + - left: + rule: AdditiveExpression + - operator: + rule: ShiftOperator + optional: true + - right: + rule: Expression +ShiftOperator: + rules: + - LeftShift + - RightShift +AdditiveExpression: + children: + - left: + rule: MultiplicativeExpression + - operator: + rule: AdditiveOperator + optional: true + - right: + rule: Expression + optional: true +MultiplicativeExpression: + children: + - left: + rule: PrefixExpression + - operator: + rule: MultiplicativeOperator + optional: true + - right: + rule: Expression +MultiplicativeOperator: + type: leaf_enum + rules: + - Multiply + - Divide + - Modulo +PrefixExpression: + children: + - operators: + rule: PrefixOperator + vec: true +PrefixOperator: + type: leaf_enum + rules: + - Spread + - Not + - Negative +SuffixExpression: + children: + - left: + rule: PrimaryExpression + - operators: + rule: SuffixOperator + vec: true +SuffixOperator: + type: leaf_enum + rules: + - PlusPlus + - MinusMinus + - ObjectProperty: + child: true + - ObjectIndex: + child: true + - Call: + child: true +ObjectProperty: + children: + - identifier +ObjectIndex: + children: + - expression +PrimaryExpression: + rules: + - Literal + - FullyQualifiedName + - Closure + - ParenthesizedExpression +ParenthesizedExpression: + children: + - expression + +# Calls +Call: + children: + - turbo_fish: + optional: true + - expression_list: + optional: true + - closure: + optional: true +TurboFish: + children: + - generic_arguments +ExpressionList: + children: + - expressions: + rule: Expression vec: true \ No newline at end of file diff --git a/src/parser/deimos.pest b/src/parser/deimos.pest index 8b6a3e1..88a344e 100644 --- a/src/parser/deimos.pest +++ b/src/parser/deimos.pest @@ -81,6 +81,10 @@ Keyword = { | True | False | Use + | Then + | Do + | End + | Companion | Byte | Short | Char @@ -627,12 +631,22 @@ Expression = { TernaryExpression = { OrExpression - ~ ( - "?" - ~ Expression - ~ ":" - ~ Expression - )? + ~ ( TernaryAlternatives )? +} + +TernaryAlternatives = { + TernaryTrueAlternative + ~ TernaryFalseAlternative +} + +TernaryTrueAlternative = { + "?" + ~ Expression +} + +TernaryFalseAlternative = { + ":" + ~ Expression } OrExpression = { @@ -648,52 +662,93 @@ AndExpression = { ComparisonExpression = { ShiftExpression ~ ( - ( Greater | Less | GreaterEqual | LessEqual | EqualTo | NotEqualTo ) + ComparisonOperator ~ Expression )? } +ComparisonOperator = { + Greater + | Less + | GreaterEqual + | LessEqual + | EqualTo + | NotEqualTo +} + ShiftExpression = { AdditiveExpression - ~ ( ( LeftShift | RightShift ) ~ Expression )? + ~ ( + ShiftOperator + ~ Expression + )? +} + +ShiftOperator = { + LeftShift + | RightShift } AdditiveExpression = { MultiplicativeExpression ~ ( - ( Add | Subtract ) + AdditiveOperator ~ Expression )? } +AdditiveOperator = { + Add + | Subtract +} + MultiplicativeExpression = { PrefixExpression ~ ( - ( Multiply | Divide | Modulo ) + MultiplicativeOperator ~ Expression )? } +MultiplicativeOperator = { + Multiply + | Divide + | Modulo +} + PrefixExpression = { - ( - Spread - | BorrowMut - | Borrow - | Mut - | Not - | Negative - )* + PrefixOperator* ~ SuffixExpression } +PrefixOperator = { + Spread + | Not + | Negative +} + SuffixExpression = { PrimaryExpression - ~ ( - ObjectAccess - | ParenthesesCall - | PlusPlus - | MinusMinus - )* + ~ SuffixOperator* +} + +SuffixOperator = { + PlusPlus + | MinusMinus + | ObjectProperty + | ObjectIndex + | Call +} + +ObjectProperty = { + "." + ~ Identifier +} + +ObjectIndex = { + "[" + ~ Expression + ~ "]" } PrimaryExpression = { @@ -709,36 +764,9 @@ ParenthesizedExpression = { ~ ")" } -ObjectAccess = { - ( ObjectProperty | ObjectIndex )+ -} - -ObjectProperty = { - "." - ~ Identifier -} - -ObjectIndex = { - "[" - ~ Expression - ~ "]" -} - // Calls -CallExpression = { - PrimaryExpression - ~ ObjectAccess? - ~ ParenthesesCall - ~ ( - ObjectAccess - | ParenthesesCall - | PlusPlus - | MinusMinus - )* -} - -ParenthesesCall = { +Call = { TurboFish? ~ "(" ~ ExpressionList?