diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 40b9e943..d6432b49 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -72,6 +72,9 @@ struct CodeObject { NameDictInt labels; std::vector func_decls; + int start_line; + int end_line; + const CodeBlock& _get_block_codei(int codei) const{ return blocks[iblocks[codei]]; } diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 91d73199..79bd9f3c 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -3,7 +3,7 @@ namespace pkpy{ CodeObject::CodeObject(std::shared_ptr src, const Str& name): - src(src), name(name) {} + src(src), name(name), start_line(-1), end_line(-1) {} void CodeObject::_gc_mark() const { for(PyObject* v : consts) PK_OBJ_MARK(v); diff --git a/src/compiler.cpp b/src/compiler.cpp index e230556a..94f4cf13 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -10,6 +10,7 @@ namespace pkpy{ CodeObject_ Compiler::push_global_context(){ CodeObject_ co = std::make_shared(lexer->src, lexer->src->filename); + co->start_line = prev().line; contexts.push(CodeEmitContext(vm, co, contexts.size())); return co; } @@ -17,6 +18,7 @@ namespace pkpy{ FuncDecl_ Compiler::push_f_context(Str name){ FuncDecl_ decl = std::make_shared(); decl->code = std::make_shared(lexer->src, name); + decl->code->start_line = prev().line; decl->nested = name_scope() == NAME_LOCAL; contexts.push(CodeEmitContext(vm, decl->code, contexts.size())); contexts.top().func = decl; @@ -31,6 +33,8 @@ namespace pkpy{ // previously, we only do this if the last opcode is not a return // however, this is buggy...since there may be a jump to the end (out of bound) even if the last opcode is a return ctx()->emit_(OP_RETURN_VALUE, 1, BC_KEEPLINE); + ctx()->co->end_line = prev().line; + // some check here std::vector& codes = ctx()->co->codes; if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){