mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
...
TODO: starred expr usage needs to be improved (whitelist vs blacklist)
This commit is contained in:
parent
f2d87467df
commit
a421377116
2
build.py
2
build.py
@ -56,3 +56,5 @@ em++ src/main.cpp -fno-rtti -fexceptions -O3 -sEXPORTED_FUNCTIONS=_pkpy_new_repl
|
|||||||
''')
|
''')
|
||||||
DONE()
|
DONE()
|
||||||
|
|
||||||
|
print("invalid usage!!")
|
||||||
|
exit(2)
|
@ -199,12 +199,10 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(make_expr<LiteralExpr>(prev().value));
|
ctx()->s_expr.push(make_expr<LiteralExpr>(prev().value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprFString(){
|
void exprFString(){
|
||||||
ctx()->s_expr.push(make_expr<FStringExpr>(std::get<Str>(prev().value)));
|
ctx()->s_expr.push(make_expr<FStringExpr>(std::get<Str>(prev().value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprLambda(){
|
void exprLambda(){
|
||||||
FuncDecl_ decl = push_f_context("<lambda>");
|
FuncDecl_ decl = push_f_context("<lambda>");
|
||||||
auto e = make_expr<LambdaExpr>(decl);
|
auto e = make_expr<LambdaExpr>(decl);
|
||||||
@ -219,7 +217,6 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprTuple(){
|
void exprTuple(){
|
||||||
std::vector<Expr_> items;
|
std::vector<Expr_> items;
|
||||||
items.push_back(ctx()->s_expr.popx());
|
items.push_back(ctx()->s_expr.popx());
|
||||||
@ -232,7 +229,6 @@ class Compiler {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprOr(){
|
void exprOr(){
|
||||||
auto e = make_expr<OrExpr>();
|
auto e = make_expr<OrExpr>();
|
||||||
e->lhs = ctx()->s_expr.popx();
|
e->lhs = ctx()->s_expr.popx();
|
||||||
@ -240,7 +236,6 @@ class Compiler {
|
|||||||
e->rhs = ctx()->s_expr.popx();
|
e->rhs = ctx()->s_expr.popx();
|
||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprAnd(){
|
void exprAnd(){
|
||||||
auto e = make_expr<AndExpr>();
|
auto e = make_expr<AndExpr>();
|
||||||
@ -249,7 +244,6 @@ class Compiler {
|
|||||||
e->rhs = ctx()->s_expr.popx();
|
e->rhs = ctx()->s_expr.popx();
|
||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprTernary(){
|
void exprTernary(){
|
||||||
auto e = make_expr<TernaryExpr>();
|
auto e = make_expr<TernaryExpr>();
|
||||||
@ -263,7 +257,6 @@ class Compiler {
|
|||||||
e->false_expr = ctx()->s_expr.popx();
|
e->false_expr = ctx()->s_expr.popx();
|
||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprBinaryOp(){
|
void exprBinaryOp(){
|
||||||
auto e = make_expr<BinaryExpr>();
|
auto e = make_expr<BinaryExpr>();
|
||||||
@ -274,12 +267,10 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprNot() {
|
void exprNot() {
|
||||||
parse_expression(PREC_LOGICAL_NOT + 1);
|
parse_expression(PREC_LOGICAL_NOT + 1);
|
||||||
ctx()->s_expr.push(make_expr<NotExpr>(ctx()->s_expr.popx()));
|
ctx()->s_expr.push(make_expr<NotExpr>(ctx()->s_expr.popx()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprUnaryOp(){
|
void exprUnaryOp(){
|
||||||
TokenIndex op = prev().type;
|
TokenIndex op = prev().type;
|
||||||
@ -295,7 +286,6 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprGroup(){
|
void exprGroup(){
|
||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
EXPR_TUPLE(); // () is just for change precedence
|
EXPR_TUPLE(); // () is just for change precedence
|
||||||
@ -303,7 +293,6 @@ class Compiler {
|
|||||||
consume(TK(")"));
|
consume(TK(")"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void _consume_comp(Expr_ expr){
|
void _consume_comp(Expr_ expr){
|
||||||
static_assert(std::is_base_of<CompExpr, T>::value);
|
static_assert(std::is_base_of<CompExpr, T>::value);
|
||||||
@ -322,7 +311,6 @@ class Compiler {
|
|||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprList() {
|
void exprList() {
|
||||||
int line = prev().line;
|
int line = prev().line;
|
||||||
std::vector<Expr_> items;
|
std::vector<Expr_> items;
|
||||||
@ -331,6 +319,7 @@ class Compiler {
|
|||||||
if (curr().type == TK("]")) break;
|
if (curr().type == TK("]")) break;
|
||||||
EXPR();
|
EXPR();
|
||||||
items.push_back(ctx()->s_expr.popx());
|
items.push_back(ctx()->s_expr.popx());
|
||||||
|
if(items.back()->is_starred()) SyntaxError();
|
||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
if(items.size()==1 && match(TK("for"))){
|
if(items.size()==1 && match(TK("for"))){
|
||||||
_consume_comp<ListCompExpr>(std::move(items[0]));
|
_consume_comp<ListCompExpr>(std::move(items[0]));
|
||||||
@ -345,7 +334,6 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprMap() {
|
void exprMap() {
|
||||||
bool parsing_dict = false; // {...} may be dict or set
|
bool parsing_dict = false; // {...} may be dict or set
|
||||||
std::vector<Expr_> items;
|
std::vector<Expr_> items;
|
||||||
@ -360,9 +348,12 @@ class Compiler {
|
|||||||
auto dict_item = make_expr<DictItemExpr>();
|
auto dict_item = make_expr<DictItemExpr>();
|
||||||
dict_item->key = ctx()->s_expr.popx();
|
dict_item->key = ctx()->s_expr.popx();
|
||||||
dict_item->value = ctx()->s_expr.popx();
|
dict_item->value = ctx()->s_expr.popx();
|
||||||
|
if(dict_item->key->is_starred()) SyntaxError();
|
||||||
|
if(dict_item->value->is_starred()) SyntaxError();
|
||||||
items.push_back(std::move(dict_item));
|
items.push_back(std::move(dict_item));
|
||||||
}else{
|
}else{
|
||||||
items.push_back(ctx()->s_expr.popx());
|
items.push_back(ctx()->s_expr.popx());
|
||||||
|
if(items.back()->is_starred()) SyntaxError();
|
||||||
}
|
}
|
||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
if(items.size()==1 && match(TK("for"))){
|
if(items.size()==1 && match(TK("for"))){
|
||||||
@ -383,7 +374,6 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprCall() {
|
void exprCall() {
|
||||||
auto e = make_expr<CallExpr>();
|
auto e = make_expr<CallExpr>();
|
||||||
e->callable = ctx()->s_expr.popx();
|
e->callable = ctx()->s_expr.popx();
|
||||||
@ -409,7 +399,6 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprName(){
|
void exprName(){
|
||||||
Str name = prev().str();
|
Str name = prev().str();
|
||||||
NameScope scope = name_scope();
|
NameScope scope = name_scope();
|
||||||
@ -419,14 +408,12 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(make_expr<NameExpr>(name, scope));
|
ctx()->s_expr.push(make_expr<NameExpr>(name, scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprAttrib() {
|
void exprAttrib() {
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
ctx()->s_expr.push(
|
ctx()->s_expr.push(
|
||||||
make_expr<AttribExpr>(ctx()->s_expr.popx(), prev().str())
|
make_expr<AttribExpr>(ctx()->s_expr.popx(), prev().str())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprSubscr() {
|
void exprSubscr() {
|
||||||
auto e = make_expr<SubscrExpr>();
|
auto e = make_expr<SubscrExpr>();
|
||||||
@ -494,7 +481,6 @@ __SUBSCR_END:
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void exprLiteral0() {
|
void exprLiteral0() {
|
||||||
ctx()->s_expr.push(make_expr<Literal0Expr>(prev().type));
|
ctx()->s_expr.push(make_expr<Literal0Expr>(prev().type));
|
||||||
}
|
}
|
||||||
@ -577,7 +563,6 @@ __SUBSCR_END:
|
|||||||
if(!push_stack) ctx()->emit_expr();
|
if(!push_stack) ctx()->emit_expr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void compile_if_stmt() {
|
void compile_if_stmt() {
|
||||||
EXPR(false); // condition
|
EXPR(false); // condition
|
||||||
int patch = ctx()->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, prev().line);
|
int patch = ctx()->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, prev().line);
|
||||||
@ -597,7 +582,6 @@ __SUBSCR_END:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void compile_while_loop() {
|
void compile_while_loop() {
|
||||||
ctx()->enter_block(WHILE_LOOP);
|
ctx()->enter_block(WHILE_LOOP);
|
||||||
EXPR(false); // condition
|
EXPR(false); // condition
|
||||||
@ -608,7 +592,6 @@ __SUBSCR_END:
|
|||||||
ctx()->exit_block();
|
ctx()->exit_block();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void compile_for_loop() {
|
void compile_for_loop() {
|
||||||
Expr_ vars = EXPR_VARS();
|
Expr_ vars = EXPR_VARS();
|
||||||
consume(TK("in"));
|
consume(TK("in"));
|
||||||
@ -817,6 +800,9 @@ __SUBSCR_END:
|
|||||||
// eat variable's type hint
|
// eat variable's type hint
|
||||||
if(match(TK(":"))) consume_type_hints();
|
if(match(TK(":"))) consume_type_hints();
|
||||||
if(!try_compile_assignment()){
|
if(!try_compile_assignment()){
|
||||||
|
if(!ctx()->s_expr.empty() && ctx()->s_expr.top()->is_starred()){
|
||||||
|
SyntaxError();
|
||||||
|
}
|
||||||
ctx()->emit_expr();
|
ctx()->emit_expr();
|
||||||
if((mode()==CELL_MODE || mode()==REPL_MODE) && name_scope()==NAME_GLOBAL){
|
if((mode()==CELL_MODE || mode()==REPL_MODE) && name_scope()==NAME_GLOBAL){
|
||||||
ctx()->emit(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
|
ctx()->emit(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
|
||||||
|
@ -502,7 +502,7 @@ struct Lexer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void throw_err(Str type, Str msg, int lineno, const char* cursor){
|
void throw_err(Str type, Str msg, int lineno, const char* cursor){
|
||||||
auto e = Exception("SyntaxError", msg);
|
auto e = Exception(type, msg);
|
||||||
e.st_push(src->snapshot(lineno, cursor));
|
e.st_push(src->snapshot(lineno, cursor));
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user