mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
up
This commit is contained in:
parent
fba313ce0d
commit
3c16864eba
@ -31,7 +31,7 @@ __NEXT_STEP:;
|
|||||||
heap._auto_collect();
|
heap._auto_collect();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const Bytecode& byte = frame->next_bytecode();
|
Bytecode byte = frame->next_bytecode();
|
||||||
#if DEBUG_CEVAL_STEP
|
#if DEBUG_CEVAL_STEP
|
||||||
std::cout << frame->stack_info() << " " << OP_NAMES[byte.op] << std::endl;
|
std::cout << frame->stack_info() << " " << OP_NAMES[byte.op] << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
@ -23,7 +23,6 @@ struct Bytecode{
|
|||||||
uint16_t op;
|
uint16_t op;
|
||||||
uint16_t block;
|
uint16_t block;
|
||||||
int arg;
|
int arg;
|
||||||
int line;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CodeBlockType {
|
enum CodeBlockType {
|
||||||
@ -55,6 +54,7 @@ struct CodeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Bytecode> codes;
|
std::vector<Bytecode> codes;
|
||||||
|
std::vector<int> lines; // line number for each bytecode
|
||||||
List consts;
|
List consts;
|
||||||
std::vector<StrName> names;
|
std::vector<StrName> names;
|
||||||
std::set<Str> global_names;
|
std::set<Str> global_names;
|
||||||
|
@ -173,17 +173,17 @@ class Compiler {
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprLiteral(){
|
void exprLiteral(){
|
||||||
ctx()->s_expr.push(make_expr<LiteralExpr>(prev().value));
|
ctx()->s_expr.push(make_expr<LiteralExpr>(prev().value));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprLambda(){
|
void exprLambda(){
|
||||||
auto e = make_expr<LambdaExpr>(name_scope());
|
auto e = make_expr<LambdaExpr>(name_scope());
|
||||||
if(!match(TK(":"))){
|
if(!match(TK(":"))){
|
||||||
@ -197,7 +197,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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());
|
||||||
@ -210,7 +210,7 @@ class Compiler {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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();
|
||||||
@ -219,7 +219,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprAnd(){
|
void exprAnd(){
|
||||||
auto e = make_expr<AndExpr>();
|
auto e = make_expr<AndExpr>();
|
||||||
e->lhs = ctx()->s_expr.popx();
|
e->lhs = ctx()->s_expr.popx();
|
||||||
@ -228,7 +228,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprTernary(){
|
void exprTernary(){
|
||||||
auto e = make_expr<TernaryExpr>();
|
auto e = make_expr<TernaryExpr>();
|
||||||
e->cond = ctx()->s_expr.popx();
|
e->cond = ctx()->s_expr.popx();
|
||||||
@ -240,7 +240,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprBinaryOp(){
|
void exprBinaryOp(){
|
||||||
auto e = make_expr<BinaryExpr>();
|
auto e = make_expr<BinaryExpr>();
|
||||||
e->op = prev().type;
|
e->op = prev().type;
|
||||||
@ -250,13 +250,13 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprUnaryOp(){
|
void exprUnaryOp(){
|
||||||
TokenIndex op = prev().type;
|
TokenIndex op = prev().type;
|
||||||
parse_expression(PREC_UNARY + 1);
|
parse_expression(PREC_UNARY + 1);
|
||||||
@ -271,7 +271,7 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprGroup(){
|
void exprGroup(){
|
||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
EXPR_TUPLE(); // () is just for change precedence
|
EXPR_TUPLE(); // () is just for change precedence
|
||||||
@ -279,7 +279,7 @@ class Compiler {
|
|||||||
consume(TK(")"));
|
consume(TK(")"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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);
|
||||||
@ -298,7 +298,7 @@ class Compiler {
|
|||||||
match_newlines_repl();
|
match_newlines_repl();
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprList() {
|
void exprList() {
|
||||||
int line = prev().line;
|
int line = prev().line;
|
||||||
std::vector<Expr_> items;
|
std::vector<Expr_> items;
|
||||||
@ -321,7 +321,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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;
|
||||||
@ -359,7 +359,7 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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();
|
||||||
@ -385,7 +385,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprName(){
|
void exprName(){
|
||||||
Str name = prev().str();
|
Str name = prev().str();
|
||||||
NameScope scope = name_scope();
|
NameScope scope = name_scope();
|
||||||
@ -395,7 +395,7 @@ class Compiler {
|
|||||||
ctx()->s_expr.push(make_expr<NameExpr>(name, scope));
|
ctx()->s_expr.push(make_expr<NameExpr>(name, scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprAttrib() {
|
void exprAttrib() {
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
ctx()->s_expr.push(
|
ctx()->s_expr.push(
|
||||||
@ -403,7 +403,7 @@ class Compiler {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprSubscr() {
|
void exprSubscr() {
|
||||||
auto e = make_expr<SubscrExpr>();
|
auto e = make_expr<SubscrExpr>();
|
||||||
e->a = ctx()->s_expr.popx();
|
e->a = ctx()->s_expr.popx();
|
||||||
@ -470,7 +470,7 @@ __SUBSCR_END:
|
|||||||
ctx()->s_expr.push(std::move(e));
|
ctx()->s_expr.push(std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void exprLiteral0() {
|
void exprLiteral0() {
|
||||||
ctx()->s_expr.push(make_expr<Literal0Expr>(prev().type));
|
ctx()->s_expr.push(make_expr<Literal0Expr>(prev().type));
|
||||||
}
|
}
|
||||||
@ -559,7 +559,7 @@ __SUBSCR_END:
|
|||||||
if(!push_stack) ctx()->emit_expr();
|
if(!push_stack) ctx()->emit_expr();
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
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);
|
||||||
@ -579,7 +579,7 @@ __SUBSCR_END:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void compile_while_loop() {
|
void compile_while_loop() {
|
||||||
ctx()->enter_block(WHILE_LOOP);
|
ctx()->enter_block(WHILE_LOOP);
|
||||||
EXPR(false); // condition
|
EXPR(false); // condition
|
||||||
@ -590,7 +590,7 @@ __SUBSCR_END:
|
|||||||
ctx()->exit_block();
|
ctx()->exit_block();
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void compile_for_loop() {
|
void compile_for_loop() {
|
||||||
Expr_ vars = EXPR_VARS();
|
Expr_ vars = EXPR_VARS();
|
||||||
consume(TK("in"));
|
consume(TK("in"));
|
||||||
@ -802,7 +802,7 @@ __SUBSCR_END:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PASS
|
|
||||||
void compile_class(){
|
void compile_class(){
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
int namei = ctx()->add_name(prev().str());
|
int namei = ctx()->add_name(prev().str());
|
||||||
|
17
src/expr.h
17
src/expr.h
@ -72,12 +72,13 @@ struct CodeEmitContext{
|
|||||||
|
|
||||||
int emit(Opcode opcode, int arg, int line) {
|
int emit(Opcode opcode, int arg, int line) {
|
||||||
co->codes.push_back(
|
co->codes.push_back(
|
||||||
Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg, line}
|
Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg}
|
||||||
);
|
);
|
||||||
|
co->lines.push_back(line);
|
||||||
int i = co->codes.size() - 1;
|
int i = co->codes.size() - 1;
|
||||||
if(line==BC_KEEPLINE){
|
if(line==BC_KEEPLINE){
|
||||||
if(i>=1) co->codes[i].line = co->codes[i-1].line;
|
if(i>=1) co->lines[i] = co->lines[i-1];
|
||||||
else co->codes[i].line = 1;
|
else co->lines[i] = 1;
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -112,7 +113,6 @@ struct CodeEmitContext{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct NameExpr: Expr{
|
struct NameExpr: Expr{
|
||||||
StrName name;
|
StrName name;
|
||||||
NameScope scope;
|
NameScope scope;
|
||||||
@ -176,7 +176,7 @@ struct StarredExpr: Expr{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct NotExpr: Expr{
|
struct NotExpr: Expr{
|
||||||
Expr_ child;
|
Expr_ child;
|
||||||
NotExpr(Expr_&& child): child(std::move(child)) {}
|
NotExpr(Expr_&& child): child(std::move(child)) {}
|
||||||
@ -188,7 +188,6 @@ struct NotExpr: Expr{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct AndExpr: Expr{
|
struct AndExpr: Expr{
|
||||||
Expr_ lhs;
|
Expr_ lhs;
|
||||||
Expr_ rhs;
|
Expr_ rhs;
|
||||||
@ -202,7 +201,6 @@ struct AndExpr: Expr{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct OrExpr: Expr{
|
struct OrExpr: Expr{
|
||||||
Expr_ lhs;
|
Expr_ lhs;
|
||||||
Expr_ rhs;
|
Expr_ rhs;
|
||||||
@ -282,7 +280,6 @@ struct LiteralExpr: Expr{
|
|||||||
bool is_json_object() const override { return true; }
|
bool is_json_object() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct NegatedExpr: Expr{
|
struct NegatedExpr: Expr{
|
||||||
Expr_ child;
|
Expr_ child;
|
||||||
NegatedExpr(Expr_&& child): child(std::move(child)) {}
|
NegatedExpr(Expr_&& child): child(std::move(child)) {}
|
||||||
@ -314,7 +311,6 @@ struct NegatedExpr: Expr{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct SliceExpr: Expr{
|
struct SliceExpr: Expr{
|
||||||
Expr_ start;
|
Expr_ start;
|
||||||
Expr_ stop;
|
Expr_ stop;
|
||||||
@ -599,7 +595,6 @@ struct AttribExpr: Expr{
|
|||||||
bool is_attrib() const override { return true; }
|
bool is_attrib() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct CallExpr: Expr{
|
struct CallExpr: Expr{
|
||||||
Expr_ callable;
|
Expr_ callable;
|
||||||
std::vector<Expr_> args;
|
std::vector<Expr_> args;
|
||||||
@ -679,7 +674,7 @@ struct BinaryExpr: Expr{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// PASS
|
|
||||||
struct TernaryExpr: Expr{
|
struct TernaryExpr: Expr{
|
||||||
Expr_ cond;
|
Expr_ cond;
|
||||||
Expr_ true_expr;
|
Expr_ true_expr;
|
||||||
|
@ -35,13 +35,13 @@ struct Frame {
|
|||||||
Frame(Frame&& other) noexcept = default;
|
Frame(Frame&& other) noexcept = default;
|
||||||
Frame& operator=(Frame&& other) noexcept = default;
|
Frame& operator=(Frame&& other) noexcept = default;
|
||||||
|
|
||||||
const Bytecode& next_bytecode() {
|
Bytecode next_bytecode() {
|
||||||
_ip = _next_ip++;
|
_ip = _next_ip++;
|
||||||
return co->codes[_ip];
|
return co->codes[_ip];
|
||||||
}
|
}
|
||||||
|
|
||||||
Str snapshot(){
|
Str snapshot(){
|
||||||
int line = co->codes[_ip].line;
|
int line = co->lines[_ip];
|
||||||
return co->src->snapshot(line);
|
return co->src->snapshot(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ inline static uint16_t find_perfect_hash_seed(uint16_t capacity, const std::vect
|
|||||||
struct NameDict {
|
struct NameDict {
|
||||||
using Item = std::pair<StrName, PyObject*>;
|
using Item = std::pair<StrName, PyObject*>;
|
||||||
static constexpr uint16_t __Capacity = 128/sizeof(Item);
|
static constexpr uint16_t __Capacity = 128/sizeof(Item);
|
||||||
|
static_assert( (__Capacity & (__Capacity-1)) == 0, "__Capacity must be power of 2" );
|
||||||
float _load_factor;
|
float _load_factor;
|
||||||
uint16_t _capacity;
|
uint16_t _capacity;
|
||||||
uint16_t _size;
|
uint16_t _size;
|
||||||
|
6
src/vm.h
6
src/vm.h
@ -574,11 +574,11 @@ inline Str VM::disassemble(CodeObject_ co){
|
|||||||
int prev_line = -1;
|
int prev_line = -1;
|
||||||
for(int i=0; i<co->codes.size(); i++){
|
for(int i=0; i<co->codes.size(); i++){
|
||||||
const Bytecode& byte = co->codes[i];
|
const Bytecode& byte = co->codes[i];
|
||||||
Str line = std::to_string(byte.line);
|
Str line = std::to_string(co->lines[i]);
|
||||||
if(byte.line == prev_line) line = "";
|
if(co->lines[i] == prev_line) line = "";
|
||||||
else{
|
else{
|
||||||
if(prev_line != -1) ss << "\n";
|
if(prev_line != -1) ss << "\n";
|
||||||
prev_line = byte.line;
|
prev_line = co->lines[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string pointer;
|
std::string pointer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user