slices
This commit is contained in:
parent
2c72168e18
commit
d294daa307
12 changed files with 104 additions and 41 deletions
|
|
@ -100,6 +100,54 @@ struct Subscript : Expr
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Slice : Node
|
||||||
|
{
|
||||||
|
ptr<Expr> lower;
|
||||||
|
ptr<Expr> upper;
|
||||||
|
ptr<Expr> step;
|
||||||
|
Slice(ptr<Expr> l, ptr<Expr> u, ptr<Expr> s) : upper(std::move(u)), lower(std::move(l)), step(std::move(s)) {}
|
||||||
|
std::string dump(int indent = 0) const override
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << indent_str(indent) << "Slice";
|
||||||
|
if (lower)
|
||||||
|
{
|
||||||
|
oss << "\n"
|
||||||
|
<< indent_str(indent + 1) << "Lower\n";
|
||||||
|
oss << lower->dump(indent + 2);
|
||||||
|
}
|
||||||
|
if (upper)
|
||||||
|
{
|
||||||
|
oss << "\n"
|
||||||
|
<< indent_str(indent + 1) << "Upper\n";
|
||||||
|
oss << upper->dump(indent + 2);
|
||||||
|
}
|
||||||
|
if (step)
|
||||||
|
{
|
||||||
|
oss << "\n"
|
||||||
|
<< indent_str(indent + 1) << "Step\n";
|
||||||
|
oss << step->dump(indent + 2);
|
||||||
|
}
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SliceSubscript : Expr
|
||||||
|
{
|
||||||
|
ptr<Expr> value;
|
||||||
|
ptr<Slice> index;
|
||||||
|
SliceSubscript(ptr<Expr> v, ptr<Slice> i)
|
||||||
|
: value(std::move(v)), index(std::move(i)) {}
|
||||||
|
std::string dump(int indent = 0) const override
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << indent_str(indent) << "SliceSubscript\n";
|
||||||
|
oss << value->dump(indent + 1) << '\n';
|
||||||
|
oss << index->dump(indent + 1);
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct List : Expr
|
struct List : Expr
|
||||||
{
|
{
|
||||||
std::vector<ptr<Expr>> elements;
|
std::vector<ptr<Expr>> elements;
|
||||||
|
|
|
||||||
43
src/parser.y
43
src/parser.y
|
|
@ -53,6 +53,8 @@
|
||||||
%right BIT_AND_ASSIGN BIT_OR_ASSIGN BIT_XOR_ASSIGN LSHIFT_ASSIGN RSHIFT_ASSIGN
|
%right BIT_AND_ASSIGN BIT_OR_ASSIGN BIT_XOR_ASSIGN LSHIFT_ASSIGN RSHIFT_ASSIGN
|
||||||
|
|
||||||
%type <std::unique_ptr<Expr>> expr
|
%type <std::unique_ptr<Expr>> expr
|
||||||
|
%type <std::unique_ptr<Expr>> opt_expr
|
||||||
|
%type <std::unique_ptr<Slice>> slice
|
||||||
%type <std::unique_ptr<Stmt>> stmt
|
%type <std::unique_ptr<Stmt>> stmt
|
||||||
%type <std::vector<std::unique_ptr<Expr>>> args
|
%type <std::vector<std::unique_ptr<Expr>>> args
|
||||||
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
|
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
|
||||||
|
|
@ -70,12 +72,16 @@ stmt_list:
|
||||||
;
|
;
|
||||||
|
|
||||||
stmt:
|
stmt:
|
||||||
expr NEWLINE { $$ = std::make_unique<ExprStmt>(std::move($1)); }
|
expr NEWLINE {
|
||||||
| expr ASSIGN expr NEWLINE { $$ = std::make_unique<Assign>(std::move($1), std::move($3));}
|
$$ = std::make_unique<ExprStmt>(std::move($1));
|
||||||
|
}
|
||||||
|
| expr ASSIGN expr NEWLINE {
|
||||||
|
$$ = std::make_unique<Assign>(std::move($1), std::move($3));
|
||||||
|
}
|
||||||
| KW_IF expr COLON NEWLINE INDENT stmt_list DEDENT {
|
| 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>>());
|
$$ = 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{
|
| 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));
|
$$ = std::make_unique<If>(std::move($2), std::move($6), std::move($12));
|
||||||
}
|
}
|
||||||
| KW_FOR IDENTIFIER KW_IN expr COLON NEWLINE INDENT stmt_list DEDENT {
|
| KW_FOR IDENTIFIER KW_IN expr COLON NEWLINE INDENT stmt_list DEDENT {
|
||||||
|
|
@ -84,25 +90,40 @@ stmt:
|
||||||
| KW_DEF IDENTIFIER L_PAREN params R_PAREN COLON NEWLINE INDENT stmt_list DEDENT {
|
| KW_DEF IDENTIFIER L_PAREN params R_PAREN COLON NEWLINE INDENT stmt_list DEDENT {
|
||||||
$$ = std::make_unique<FunctionDef>($2, std::move($4), std::move($9));
|
$$ = std::make_unique<FunctionDef>($2, std::move($4), std::move($9));
|
||||||
}
|
}
|
||||||
| KW_RETURN expr NEWLINE { $$ = std::make_unique<Return>(std::move($2));}
|
| KW_RETURN expr NEWLINE {
|
||||||
|
$$ = std::make_unique<Return>(std::move($2));
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
NUMBER { $$ = std::make_unique<Number>($1); }
|
NUMBER { $$ = std::make_unique<Number>($1); }
|
||||||
| IDENTIFIER { $$ = std::make_unique<Identifier>($1); }
|
| IDENTIFIER { $$ = std::make_unique<Identifier>($1); }
|
||||||
| STRING { $$ = std::make_unique<String>($1); }
|
| STRING { $$ = std::make_unique<String>($1); }
|
||||||
| expr PLUS expr { $$ = std::make_unique<BinOp>(
|
| expr PLUS expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::ADD), std::move($1), std::move($3)); }
|
||||||
BinaryOperator(BinaryOperator::ADD),
|
| expr MULTIPLY expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::MUL), std::move($1), std::move($3)); }
|
||||||
std::move($1), std::move($3)); }
|
| expr LT expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::LT) , std::move($1), std::move($3)); }
|
||||||
| expr MULTIPLY expr { $$ = std::make_unique<BinOp>(
|
| expr KW_AND expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::AND) , std::move($1), std::move($3)); }
|
||||||
BinaryOperator(BinaryOperator::MUL),
|
| expr KW_OR expr { $$ = std::make_unique<BinOp>( BinaryOperator(BinaryOperator::OR) , std::move($1), std::move($3)); }
|
||||||
std::move($1), std::move($3)); }
|
| expr L_BRACKET expr R_BRACKET { $$ = std::make_unique<Subscript>(std::move($1), std::move($3));}
|
||||||
| L_PAREN expr R_PAREN {$$ = std::move($2); /* #todo: test for precedence*/}
|
| expr L_BRACKET slice R_BRACKET{ $$ = std::make_unique<SliceSubscript>(std::move($1), std::move($3)); }
|
||||||
|
| L_PAREN expr R_PAREN {$$ = std::move($2); }
|
||||||
| expr L_PAREN args R_PAREN { $$ = std::make_unique<Call>(std::move($1), std::move($3));}
|
| 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);}
|
| expr DOT IDENTIFIER { $$ = std::make_unique<Attribute>(std::move($1), $3);}
|
||||||
| L_BRACKET args R_BRACKET { $$ = std::make_unique<List>(std::move($2));}
|
| L_BRACKET args R_BRACKET { $$ = std::make_unique<List>(std::move($2));}
|
||||||
|
| MINUS expr { $$ = std::make_unique<UnaryOpExpr>(UnaryOperator(UnaryOperator::NEG), std::move($2));}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
slice:
|
||||||
|
opt_expr COLON opt_expr
|
||||||
|
{ $$ = std::make_unique<Slice>(std::move($1), std::move($3), nullptr); }
|
||||||
|
| opt_expr COLON opt_expr COLON opt_expr
|
||||||
|
{ $$ = std::make_unique<Slice>(std::move($1), std::move($3), std::move($5)); }
|
||||||
|
;
|
||||||
|
|
||||||
|
opt_expr:
|
||||||
|
{ $$ = nullptr;}
|
||||||
|
| expr { $$ = std::move($1);}
|
||||||
|
|
||||||
args:
|
args:
|
||||||
/* empty */ { $$ = std::vector<std::unique_ptr<Expr>>(); }
|
/* empty */ { $$ = std::vector<std::unique_ptr<Expr>>(); }
|
||||||
| expr { $$ = std::vector<std::unique_ptr<Expr>>();
|
| expr { $$ = std::vector<std::unique_ptr<Expr>>();
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,13 @@ public:
|
||||||
|
|
||||||
if (eof_emitted)
|
if (eof_emitted)
|
||||||
{
|
{
|
||||||
//std::cout << "lex: 0\n";
|
// std::cout << "lex: 0\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indent_stack.popDedent())
|
if (indent_stack.popDedent())
|
||||||
{
|
{
|
||||||
//std::cout << "lex: dedent\n";
|
// std::cout << "lex: dedent\n";
|
||||||
return yy::parser::token::TOK_DEDENT;
|
return yy::parser::token::TOK_DEDENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,14 +43,14 @@ public:
|
||||||
if (!last_was_newline)
|
if (!last_was_newline)
|
||||||
{
|
{
|
||||||
last_was_newline = true;
|
last_was_newline = true;
|
||||||
//std::cout << "lex: newline\n";
|
// std::cout << "lex: newline\n";
|
||||||
return yy::parser::token::TOK_NEWLINE;
|
return yy::parser::token::TOK_NEWLINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = indent_stack.closeBlock();
|
auto result = indent_stack.closeBlock();
|
||||||
if (result == IndentAction::Dedent)
|
if (result == IndentAction::Dedent)
|
||||||
{
|
{
|
||||||
//std::cout << "lex: dedent\n";
|
// std::cout << "lex: dedent\n";
|
||||||
return yy::parser::token::TOK_DEDENT;
|
return yy::parser::token::TOK_DEDENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,7 +59,7 @@ public:
|
||||||
|
|
||||||
last_was_newline = (token == yy::parser::token::TOK_NEWLINE);
|
last_was_newline = (token == yy::parser::token::TOK_NEWLINE);
|
||||||
|
|
||||||
//std::cout << "lex: " << Parser::symbol_name(static_cast<kind>(token)) << '\n';
|
// std::cout << "lex: " << Parser::symbol_name(static_cast<kind>(token)) << '\n';
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,3 @@ Module
|
||||||
ExprStmt
|
ExprStmt
|
||||||
UnaryOp(-)
|
UnaryOp(-)
|
||||||
Number(5)
|
Number(5)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
Module
|
Module
|
||||||
ExprStmt
|
ExprStmt
|
||||||
Compare(<)
|
BinOp(<)
|
||||||
Number(1)
|
Number(1)
|
||||||
Number(2)
|
Number(2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
Module
|
Module
|
||||||
ExprStmt
|
ExprStmt
|
||||||
BoolOp(and)
|
BinOp(and)
|
||||||
Identifier(x)
|
Identifier(x)
|
||||||
Identifier(y)
|
Identifier(y)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
Module
|
Module
|
||||||
ExprStmt
|
ExprStmt
|
||||||
BoolOp(or)
|
BinOp(or)
|
||||||
Identifier(x)
|
Identifier(x)
|
||||||
BoolOp(and)
|
BinOp(and)
|
||||||
Identifier(y)
|
Identifier(y)
|
||||||
Identifier(z)
|
Identifier(z)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,3 @@ Module
|
||||||
Identifier(foo)
|
Identifier(foo)
|
||||||
Number(1)
|
Number(1)
|
||||||
Number(2)
|
Number(2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
arr[5]
|
arr[5]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,3 @@ Module
|
||||||
Subscript
|
Subscript
|
||||||
Identifier(arr)
|
Identifier(arr)
|
||||||
Number(5)
|
Number(5)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
arr[1:3]
|
arr[1:3]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
Module
|
Module
|
||||||
ExprStmt
|
ExprStmt
|
||||||
Slice
|
SliceSubscript
|
||||||
Identifier(arr)
|
Identifier(arr)
|
||||||
Number(1)
|
Slice
|
||||||
Number(3)
|
Lower
|
||||||
|
Number(1)
|
||||||
|
Upper
|
||||||
|
Number(3)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue