Compare commits

..

No commits in common. "42bbf6fdb48f96c530b15c96e7d1bcef633fba07" and "a43ac63a82302fc7d4c6c0e03b5ed10464b9621c" have entirely different histories.

5 changed files with 7 additions and 71 deletions

View File

@ -2,6 +2,5 @@
"stubPath": "include/typings", "stubPath": "include/typings",
"reportMissingModuleSource": "none", "reportMissingModuleSource": "none",
"reportArgumentType": "none", "reportArgumentType": "none",
"reportWildcardImportFromLibrary": "none", "pythonVersion": "3.10"
"pythonVersion": "3.12"
} }

View File

@ -1575,7 +1575,7 @@ static Error* exprImag(Compiler* self) {
} }
static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index); static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index);
static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda); static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_hints);
static Error* exprLambda(Compiler* self) { static Error* exprLambda(Compiler* self) {
Error* err; Error* err;
@ -1583,7 +1583,7 @@ static Error* exprLambda(Compiler* self) {
int decl_index; int decl_index;
FuncDecl_ decl = push_f_context(self, (c11_sv){"<lambda>", 8}, &decl_index); FuncDecl_ decl = push_f_context(self, (c11_sv){"<lambda>", 8}, &decl_index);
if(!match(TK_COLON)) { if(!match(TK_COLON)) {
check(_compile_f_args(self, decl, true)); check(_compile_f_args(self, decl, false));
consume(TK_COLON); consume(TK_COLON);
} }
// https://github.com/pocketpy/pocketpy/issues/37 // https://github.com/pocketpy/pocketpy/issues/37
@ -2177,12 +2177,12 @@ static Error* read_literal(Compiler* self, py_Ref out) {
} }
} }
static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) { static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_hints) {
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
Error* err; Error* err;
do { do {
if(!is_lambda) match_newlines();
if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument"); if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument");
match_newlines();
if(match(TK_MUL)) { if(match(TK_MUL)) {
if(state < 1) if(state < 1)
state = 1; state = 1;
@ -2200,7 +2200,7 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) {
} }
// eat type hints // eat type hints
if(!is_lambda && match(TK_COLON)) check(consume_type_hints(self)); if(enable_type_hints && match(TK_COLON)) check(consume_type_hints(self));
if(state == 0 && curr()->type == TK_ASSIGN) state = 2; if(state == 0 && curr()->type == TK_ASSIGN) state = 2;
switch(state) { switch(state) {
case 0: FuncDecl__add_arg(decl, name); break; case 0: FuncDecl__add_arg(decl, name); break;
@ -2221,20 +2221,6 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) {
break; break;
} }
} while(match(TK_COMMA)); } while(match(TK_COMMA));
if(!is_lambda) match_newlines();
return NULL;
}
static Error* consume_pep695_py312(Compiler* self) {
// https://peps.python.org/pep-0695/
Error* err;
if(match(TK_LBRACKET)) {
consume(TK_ID);
if(match(TK_COLON)){
check(consume_type_hints(self));
}
consume(TK_RBRACKET);
}
return NULL; return NULL;
} }
@ -2244,10 +2230,9 @@ static Error* compile_function(Compiler* self, int decorators) {
c11_sv decl_name_sv = Token__sv(prev()); c11_sv decl_name_sv = Token__sv(prev());
int decl_index; int decl_index;
FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index); FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index);
consume_pep695_py312(self);
consume(TK_LPAREN); consume(TK_LPAREN);
if(!match(TK_RPAREN)) { if(!match(TK_RPAREN)) {
check(_compile_f_args(self, decl, false)); check(_compile_f_args(self, decl, true));
consume(TK_RPAREN); consume(TK_RPAREN);
} }
if(match(TK_ARROW)) check(consume_type_hints(self)); if(match(TK_ARROW)) check(consume_type_hints(self));
@ -2296,7 +2281,6 @@ static Error* compile_class(Compiler* self, int decorators) {
consume(TK_ID); consume(TK_ID);
py_Name name = py_namev(Token__sv(prev())); py_Name name = py_namev(Token__sv(prev()));
bool has_base = false; bool has_base = false;
consume_pep695_py312(self);
if(match(TK_LPAREN)) { if(match(TK_LPAREN)) {
if(is_expression(self, false)) { if(is_expression(self, false)) {
check(EXPR(self)); check(EXPR(self));
@ -2421,9 +2405,7 @@ __EAT_DOTS_END:
return NULL; return NULL;
} }
bool has_bracket = match(TK_LPAREN);
do { do {
if(has_bracket) match_newlines();
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
consume(TK_ID); consume(TK_ID);
c11_sv name = Token__sv(prev()); c11_sv name = Token__sv(prev());
@ -2434,10 +2416,6 @@ __EAT_DOTS_END:
} }
Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line); Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line);
} while(match(TK_COMMA)); } while(match(TK_COMMA));
if(has_bracket) {
match_newlines();
consume(TK_RPAREN);
}
Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
consume_end_stmt(); consume_end_stmt();
return NULL; return NULL;

View File

@ -141,21 +141,6 @@ assert f() == (((1,2),3), (4,))
def f(a, b): def f(a, b):
return a + b return a + b
# test multiple lines
def f(
a,
b: int
):
return a + b
assert f(1, 2) == 3
def f(a,
b: int):
return a + b
assert f(1, 2) == 3
# try: # try:
# f(a=1) # f(a=1)
# exit(1) # exit(1)

View File

@ -34,12 +34,3 @@ f()
from math import * from math import *
assert pi > 3 assert pi > 3
from math import (pi, pow, sin, cos)
from math import (
pi,
pow,
sin,
cos
)

View File

@ -1,17 +0,0 @@
class Test[T]:
def __init__(self, value: T):
self.value = value
def get_value(self) -> T:
return self.value
def add[T: int|str|float](a: T, b: T) -> T:
return a + b # type: ignore
res = add(1, 2)
assert res == 3
test = Test(1)
assert test.get_value() == 1