This commit is contained in:
bronku 2025-12-06 10:25:37 +01:00
parent 307c51539c
commit c0244b5822
3 changed files with 35 additions and 6 deletions

View file

@ -61,6 +61,7 @@
%type <std::vector<std::unique_ptr<Expr>>> tuple
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
%type <std::vector<std::string>> params
%type <std::vector<std::unique_ptr<Stmt>>> elif_chain
%%
@ -80,11 +81,8 @@ stmt:
| expr ASSIGN expr NEWLINE {
$$ = std::make_unique<Assign>(std::move($1), std::move($3));
}
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT {
$$ = std::make_unique<If>(std::move($2), std::move($6), std::vector<std::unique_ptr<Stmt>>());
}
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT KW_ELSE COLON NEWLINE INDENT stmt_list DEDENT {
$$ = std::make_unique<If>(std::move($2), std::move($6), std::move($12));
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT elif_chain{
$$ = std::make_unique<If>(std::move($2), std::move($6), std::move($8));
}
| KW_FOR IDENTIFIER KW_IN expr COLON NEWLINE INDENT stmt_list DEDENT {
$$ = std::make_unique<For> ($2, std::move($4), std::move($8));
@ -168,6 +166,18 @@ params:
{ $$ = std::vector<std::string>();}
| IDENTIFIER { $$ = std::vector<std::string>(); $$.push_back($1); }
| params COMMA IDENTIFIER {$1.push_back($3); $$ = std::move($1);}
elif_chain:
/* empty */ { $$ = std::vector<std::unique_ptr<Stmt>>(); }
| KW_ELIF expr COLON NEWLINE INDENT stmt_list DEDENT elif_chain {
std::vector<std::unique_ptr<Stmt>> output;
output.push_back(std::make_unique<If>(std::move($2), std::move($6), std::move($8)));
$$ = std::move(output);
}
| KW_ELSE COLON NEWLINE INDENT stmt_list DEDENT {
$$ = std::move($5);
}
;
%%
namespace yy {
void parser::error(const std::string& msg)