feat(parser): implement full statement parsing and control flow logic
Signed-off-by: erick-alcachofa <erick@artichoke.dev>
Complete the transition from a declarations-only parser to a functional
imperative parser. This commit introduces the implementation for all
major statement types, loop constructs, and core control flow logic.
- **Match Case Update**: Updated `grammar.ebnf` to use pipe delimiters
`|id|` for unwrapped variables in match cases, replacing the previous
parenthetical syntax.
- **Labels**: Implemented loop labeling using the `ident := loop`
syntax. Labels are validated to ensure they only prefix valid loop
constructs.
- **Labels and Ranges**: Standardized the use of the `:=` operator for
both loop labels (`label := loop`) and range-for declarations (`let i
:= range`).
- **Conditional Branches**:
- Fully implemented `if` and `else` statements.
- Added support for optional variable unwrapping (e.g., `if (expr)
|val|`).
- Supported `else if` chaining by recursively parsing if-statements
within else-branches.
- **Loops**:
- **C-Style For**: Implemented `for (init; cond; post)` with
optional initializers and post-loop expressions.
- **Range For**: Implemented `for (let i := range)` with mutability
controls.
- **While & Do-While**: Implemented standard condition-based loops.
- **Infinite Loop**: Added the explicit `loop` keyword for infinite
iteration.
- **Loop Dispatch**: Added a lookahead mechanism in
`parseForLoopStatement` to differentiate between C-style and
Range-style loops based on token positioning.
- **Variables**: Implemented `let`/`def` parsing within local scopes,
including type annotations and initializers.
- **Defer Logic**: Implemented `defer` and `errdefer` for scope-guarded
execution.
- **Jumps**: Implemented `break`, `continue` (with optional label
targets), and `return` (with optional expressions).
- **Match & Switch**: Fully implemented branch parsing, with possible
default cases via the `_` (underscore) keyword.
- **Expression Integration**: Stubbed `parseExpression` in a new
`Expressions.cpp` to serve as the integration point for value parsing.
- **OverloadSet**: Integrated `OverloadSet` utility in `Statements.cpp`
to cleanly handle AST node variant visitation for label injection.
- **Error Handling**: Standardized error reporting across all new paths
using `langException`, providing specific "expected" messages for
delimiters and keywords.
This commit is contained in:
parent
923b8d7e2d
commit
a3d5c0ac68
@ -170,7 +170,7 @@ non_exportable_declaration =
|
||||
"switch" "(" <expression> ")" "{" <switch_case>* <default_case>? "}"
|
||||
|
||||
<match_case> =
|
||||
<type_name> ( "(" <identifier> ")" )? "->" <code_block>
|
||||
<type_name> ( "|" <identifier> "|" )? "->" <code_block>
|
||||
|
||||
<switch_case> =
|
||||
<expression> "->" <code_block>
|
||||
|
||||
@ -121,6 +121,9 @@ namespace arti::lang {
|
||||
Expected<ast::IfStmtNode>
|
||||
parseIfStatement();
|
||||
|
||||
Expected<ast::ElseBranchNode>
|
||||
parseElseStatement();
|
||||
|
||||
Expected<ast::DeferStmtNode>
|
||||
parseDeferStatement();
|
||||
|
||||
@ -142,6 +145,9 @@ namespace arti::lang {
|
||||
Expected<ast::SwitchStmtNode>
|
||||
parseSwitchStatement();
|
||||
|
||||
Expected<ast::StatementNode>
|
||||
parseForLoopStatement();
|
||||
|
||||
Expected<ast::CForStmtNode>
|
||||
parseCForStatement();
|
||||
|
||||
@ -157,6 +163,12 @@ namespace arti::lang {
|
||||
Expected<ast::InfLoopStmtNode>
|
||||
parseInfLoopStatement();
|
||||
|
||||
Expected<ast::ExpressionStmtNode>
|
||||
parseExpressionStatement();
|
||||
|
||||
Expected<ast::ExpressionNode>
|
||||
parseExpression();
|
||||
|
||||
private:
|
||||
std::string unitName;
|
||||
std::string sourceCode;
|
||||
|
||||
@ -20,3 +20,13 @@
|
||||
// //
|
||||
//============================================================================//
|
||||
|
||||
#include <artichoke/Parser/Parser.hpp>
|
||||
|
||||
namespace arti::lang {
|
||||
|
||||
Expected<ast::ExpressionNode>
|
||||
Parser::parseExpression() {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace arti::lang
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user