mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-23 21:50:17 +00:00
Compare commits
2 Commits
a43ac63a82
...
42bbf6fdb4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42bbf6fdb4 | ||
|
|
14434b6e02 |
@ -2,5 +2,6 @@
|
||||
"stubPath": "include/typings",
|
||||
"reportMissingModuleSource": "none",
|
||||
"reportArgumentType": "none",
|
||||
"pythonVersion": "3.10"
|
||||
"reportWildcardImportFromLibrary": "none",
|
||||
"pythonVersion": "3.12"
|
||||
}
|
||||
|
||||
@ -1575,7 +1575,7 @@ static Error* exprImag(Compiler* self) {
|
||||
}
|
||||
|
||||
static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index);
|
||||
static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_hints);
|
||||
static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda);
|
||||
|
||||
static Error* exprLambda(Compiler* self) {
|
||||
Error* err;
|
||||
@ -1583,7 +1583,7 @@ static Error* exprLambda(Compiler* self) {
|
||||
int decl_index;
|
||||
FuncDecl_ decl = push_f_context(self, (c11_sv){"<lambda>", 8}, &decl_index);
|
||||
if(!match(TK_COLON)) {
|
||||
check(_compile_f_args(self, decl, false));
|
||||
check(_compile_f_args(self, decl, true));
|
||||
consume(TK_COLON);
|
||||
}
|
||||
// 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 enable_type_hints) {
|
||||
static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) {
|
||||
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
|
||||
Error* err;
|
||||
do {
|
||||
if(!is_lambda) match_newlines();
|
||||
if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument");
|
||||
match_newlines();
|
||||
if(match(TK_MUL)) {
|
||||
if(state < 1)
|
||||
state = 1;
|
||||
@ -2200,7 +2200,7 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_h
|
||||
}
|
||||
|
||||
// eat type hints
|
||||
if(enable_type_hints && match(TK_COLON)) check(consume_type_hints(self));
|
||||
if(!is_lambda && match(TK_COLON)) check(consume_type_hints(self));
|
||||
if(state == 0 && curr()->type == TK_ASSIGN) state = 2;
|
||||
switch(state) {
|
||||
case 0: FuncDecl__add_arg(decl, name); break;
|
||||
@ -2221,6 +2221,20 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_h
|
||||
break;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
@ -2230,9 +2244,10 @@ static Error* compile_function(Compiler* self, int decorators) {
|
||||
c11_sv decl_name_sv = Token__sv(prev());
|
||||
int decl_index;
|
||||
FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index);
|
||||
consume_pep695_py312(self);
|
||||
consume(TK_LPAREN);
|
||||
if(!match(TK_RPAREN)) {
|
||||
check(_compile_f_args(self, decl, true));
|
||||
check(_compile_f_args(self, decl, false));
|
||||
consume(TK_RPAREN);
|
||||
}
|
||||
if(match(TK_ARROW)) check(consume_type_hints(self));
|
||||
@ -2281,6 +2296,7 @@ static Error* compile_class(Compiler* self, int decorators) {
|
||||
consume(TK_ID);
|
||||
py_Name name = py_namev(Token__sv(prev()));
|
||||
bool has_base = false;
|
||||
consume_pep695_py312(self);
|
||||
if(match(TK_LPAREN)) {
|
||||
if(is_expression(self, false)) {
|
||||
check(EXPR(self));
|
||||
@ -2405,7 +2421,9 @@ __EAT_DOTS_END:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool has_bracket = match(TK_LPAREN);
|
||||
do {
|
||||
if(has_bracket) match_newlines();
|
||||
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
|
||||
consume(TK_ID);
|
||||
c11_sv name = Token__sv(prev());
|
||||
@ -2416,6 +2434,10 @@ __EAT_DOTS_END:
|
||||
}
|
||||
Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line);
|
||||
} while(match(TK_COMMA));
|
||||
if(has_bracket) {
|
||||
match_newlines();
|
||||
consume(TK_RPAREN);
|
||||
}
|
||||
Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
|
||||
consume_end_stmt();
|
||||
return NULL;
|
||||
|
||||
@ -141,6 +141,21 @@ assert f() == (((1,2),3), (4,))
|
||||
def f(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:
|
||||
# f(a=1)
|
||||
# exit(1)
|
||||
|
||||
@ -34,3 +34,12 @@ f()
|
||||
|
||||
from math import *
|
||||
assert pi > 3
|
||||
|
||||
from math import (pi, pow, sin, cos)
|
||||
|
||||
from math import (
|
||||
pi,
|
||||
pow,
|
||||
sin,
|
||||
cos
|
||||
)
|
||||
17
tests/96_pep695_py312.py
Normal file
17
tests/96_pep695_py312.py
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user