From 3c16864eba5af1a17f6b8b4e431ae18a8c515fe0 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Sun, 9 Apr 2023 16:09:23 +0000 Subject: [PATCH] up --- src/ceval.h | 2 +- src/codeobject.h | 2 +- src/compiler.h | 46 +++++++++++++++++++++++----------------------- src/expr.h | 17 ++++++----------- src/frame.h | 4 ++-- src/namedict.h | 1 + src/vm.h | 6 +++--- 7 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index 871b4eb4..51268cf2 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -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 diff --git a/src/codeobject.h b/src/codeobject.h index 9ccb6414..dc7e8f22 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -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 codes; + std::vector lines; // line number for each bytecode List consts; std::vector names; std::set global_names; diff --git a/src/compiler.h b/src/compiler.h index 1bc1daf8..3ad6fed2 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -173,17 +173,17 @@ class Compiler { return expr; } - // PASS + void exprLiteral(){ ctx()->s_expr.push(make_expr(prev().value)); } - // PASS + void exprFString(){ ctx()->s_expr.push(make_expr(std::get(prev().value))); } - // PASS + void exprLambda(){ auto e = make_expr(name_scope()); if(!match(TK(":"))){ @@ -197,7 +197,7 @@ class Compiler { ctx()->s_expr.push(std::move(e)); } - // PASS + void exprTuple(){ std::vector items; items.push_back(ctx()->s_expr.popx()); @@ -210,7 +210,7 @@ class Compiler { )); } - // PASS + void exprOr(){ auto e = make_expr(); 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(); 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(); 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(); 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(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 void _consume_comp(Expr_ expr){ static_assert(std::is_base_of::value); @@ -298,7 +298,7 @@ class Compiler { match_newlines_repl(); } - // PASS + void exprList() { int line = prev().line; std::vector 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 items; @@ -359,7 +359,7 @@ class Compiler { } } - // PASS + void exprCall() { auto e = make_expr(); 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(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(); 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(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()); diff --git a/src/expr.h b/src/expr.h index e22890a7..3a0f71ac 100644 --- a/src/expr.h +++ b/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 args; @@ -679,7 +674,7 @@ struct BinaryExpr: Expr{ } }; -// PASS + struct TernaryExpr: Expr{ Expr_ cond; Expr_ true_expr; diff --git a/src/frame.h b/src/frame.h index d6e2a984..ed18bf8e 100644 --- a/src/frame.h +++ b/src/frame.h @@ -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); } diff --git a/src/namedict.h b/src/namedict.h index 987ae840..7e2866b9 100644 --- a/src/namedict.h +++ b/src/namedict.h @@ -35,6 +35,7 @@ inline static uint16_t find_perfect_hash_seed(uint16_t capacity, const std::vect struct NameDict { using Item = std::pair; 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; diff --git a/src/vm.h b/src/vm.h index e63129cc..f58a6ba5 100644 --- a/src/vm.h +++ b/src/vm.h @@ -574,11 +574,11 @@ inline Str VM::disassemble(CodeObject_ co){ int prev_line = -1; for(int i=0; icodes.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;