impl lambda

Update compiler.h
This commit is contained in:
blueloveTH 2022-11-08 22:05:18 +08:00
parent d78dfb9d7f
commit 56c2ef36db

View File

@ -337,7 +337,18 @@ public:
}
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() {
@ -771,16 +782,7 @@ __LISTCOMP:
emitCode(OP_BUILD_CLASS, clsNameIdx);
}
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(")"))) {
void __compileFunctionArgs(_Func& func){
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
do {
if(state == 3){
@ -810,6 +812,19 @@ __LISTCOMP:
case 3: func.doubleStarredArg = name; break;
}
} 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(")"));
}