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