This commit is contained in:
bronku 2025-11-30 11:07:51 +01:00
parent d294daa307
commit 140d01eac4
3 changed files with 28 additions and 2 deletions

View file

@ -191,3 +191,23 @@ struct Dict : Expr
return oss.str();
}
};
struct Tuple : Expr
{
std::vector<ptr<Expr>> elements;
Tuple(std::vector<ptr<Expr>> elems)
: elements(std::move(elems)) {}
std::string dump(int indent = 0) const override
{
std::ostringstream oss;
oss << indent_str(indent) << "Tuple";
for (const auto &elem : elements)
{
oss << "\n"
<< elem->dump(indent + 1);
}
return oss.str();
}
};

View file

@ -57,6 +57,7 @@
%type <std::unique_ptr<Slice>> slice
%type <std::unique_ptr<Stmt>> stmt
%type <std::vector<std::unique_ptr<Expr>>> args
%type <std::vector<std::unique_ptr<Expr>>> tuple
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
%type <std::vector<std::string>> params
@ -110,6 +111,7 @@ expr:
| expr L_PAREN args R_PAREN { $$ = std::make_unique<Call>(std::move($1), std::move($3));}
| expr DOT IDENTIFIER { $$ = std::make_unique<Attribute>(std::move($1), $3);}
| L_BRACKET args R_BRACKET { $$ = std::make_unique<List>(std::move($2));}
| L_PAREN tuple R_PAREN { $$ = std::make_unique<Tuple>(std::move($2));}
| MINUS expr { $$ = std::make_unique<UnaryOpExpr>(UnaryOperator(UnaryOperator::NEG), std::move($2));}
;
@ -131,6 +133,11 @@ args:
| args COMMA expr { $1.push_back(std::move($3)); $$ = std::move($1); }
;
tuple:
expr { $$ = std::vector<std::unique_ptr<Expr>>();
$$.push_back(std::move($1)); }
| tuple COMMA expr { $1.push_back(std::move($3)); $$ = std::move($1); }
params:
{ $$ = std::vector<std::string>();}
| IDENTIFIER { $$ = std::vector<std::string>(); $$.push_back($1); }

View file

@ -4,4 +4,3 @@ Module
Number(1)
Number(2)
Number(3)