mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
impl lambda
Update compiler.h
This commit is contained in:
parent
d78dfb9d7f
commit
56c2ef36db
@ -337,7 +337,18 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void exprLambda() {
|
void exprLambda() {
|
||||||
throw SyntaxError(path, parser->previous, "lambda is not implemented yet");
|
_Func func;
|
||||||
|
func.name = "<lambda>";
|
||||||
|
__compileFunctionArgs(func);
|
||||||
|
consume(TK(":"));
|
||||||
|
func.code = std::make_shared<CodeObject>();
|
||||||
|
func.code->co_name = func.name;
|
||||||
|
func.code->co_filename = path;
|
||||||
|
this->codes.push(func.code);
|
||||||
|
EXPR_TUPLE();
|
||||||
|
emitCode(OP_RETURN_VALUE);
|
||||||
|
this->codes.pop();
|
||||||
|
emitCode(OP_LOAD_CONST, getCode()->addConst(vm->PyFunction(func)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void exprAssign() {
|
void exprAssign() {
|
||||||
@ -771,16 +782,7 @@ __LISTCOMP:
|
|||||||
emitCode(OP_BUILD_CLASS, clsNameIdx);
|
emitCode(OP_BUILD_CLASS, clsNameIdx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void compileFunction(){
|
void __compileFunctionArgs(_Func& func){
|
||||||
if(isCompilingClass){
|
|
||||||
if(match(TK("pass"))) return;
|
|
||||||
consume(TK("def"));
|
|
||||||
}
|
|
||||||
_Func func;
|
|
||||||
consume(TK("@id"));
|
|
||||||
func.name = parser->previous.str();
|
|
||||||
|
|
||||||
if (match(TK("(")) && !match(TK(")"))) {
|
|
||||||
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
|
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
|
||||||
do {
|
do {
|
||||||
if(state == 3){
|
if(state == 3){
|
||||||
@ -810,6 +812,19 @@ __LISTCOMP:
|
|||||||
case 3: func.doubleStarredArg = name; break;
|
case 3: func.doubleStarredArg = name; break;
|
||||||
}
|
}
|
||||||
} while (match(TK(",")));
|
} while (match(TK(",")));
|
||||||
|
}
|
||||||
|
|
||||||
|
void compileFunction(){
|
||||||
|
if(isCompilingClass){
|
||||||
|
if(match(TK("pass"))) return;
|
||||||
|
consume(TK("def"));
|
||||||
|
}
|
||||||
|
_Func func;
|
||||||
|
consume(TK("@id"));
|
||||||
|
func.name = parser->previous.str();
|
||||||
|
|
||||||
|
if (match(TK("(")) && !match(TK(")"))) {
|
||||||
|
__compileFunctionArgs(func);
|
||||||
consume(TK(")"));
|
consume(TK(")"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user