@@ -68,6 +68,7 @@ const (
6868type compiler struct {
6969 Code * py.Code // code being built up
7070 Filename string
71+ Lineno int // current line number
7172 OpCodes Instructions
7273 loops loopstack
7374 SymTable * symtable.SymTable
@@ -144,6 +145,11 @@ func (c *compiler) panicSyntaxErrorf(Ast ast.Ast, format string, a ...interface{
144145 panic (err )
145146}
146147
148+ // Sets Lineno from an ast node
149+ func (c * compiler ) SetLineno (node ast.Ast ) {
150+ c .Lineno = node .GetLineno ()
151+ }
152+
147153// Create a new compiler object at Ast, using private for name mangling
148154func (c * compiler ) newCompilerScope (compilerScope compilerScopeType , Ast ast.Ast , private string ) (newC * compiler ) {
149155 newSymTable := c .SymTable .FindChild (Ast )
@@ -186,6 +192,7 @@ func (c *compiler) compileAst(Ast ast.Ast, filename string, futureFlags int, don
186192 code .Freevars = append (code .Freevars , SymTable .Find (symtable .ScopeFree , symtable .DefFreeClass )... )
187193 code .Flags = c .codeFlags (SymTable ) | int32 (futureFlags & py .CO_COMPILER_FLAGS_MASK )
188194 valueOnStack := false
195+ c .SetLineno (Ast )
189196 switch node := Ast .(type ) {
190197 case * ast.Module :
191198 c .Stmts (c .docString (node .Body , false ))
@@ -283,6 +290,7 @@ func (c *compiler) compileAst(Ast ast.Ast, filename string, futureFlags int, don
283290 code .Code = c .OpCodes .Assemble ()
284291 code .Stacksize = int32 (c .OpCodes .StackDepth ())
285292 code .Nlocals = int32 (len (code .Varnames ))
293+ code .Lnotab = string (c .OpCodes .Lnotab ())
286294 return nil
287295}
288296
@@ -377,15 +385,19 @@ func (c *compiler) OpArg(Op vm.OpCode, Arg uint32) {
377385 if ! Op .HAS_ARG () {
378386 panic ("OpArg called with an instruction which doesn't take an Arg" )
379387 }
380- c .OpCodes .Add (& OpArg {Op : Op , Arg : Arg })
388+ instr := & OpArg {Op : Op , Arg : Arg }
389+ instr .SetLineno (c .Lineno )
390+ c .OpCodes .Add (instr )
381391}
382392
383393// Compiles an instruction without an argument
384394func (c * compiler ) Op (op vm.OpCode ) {
385395 if op .HAS_ARG () {
386396 panic ("Op called with an instruction which takes an Arg" )
387397 }
388- c .OpCodes .Add (& Op {Op : op })
398+ instr := & Op {Op : op }
399+ instr .SetLineno (c .Lineno )
400+ c .OpCodes .Add (instr )
389401}
390402
391403// Inserts an existing label
@@ -402,14 +414,17 @@ func (c *compiler) NewLabel() *Label {
402414
403415// Compiles a jump instruction
404416func (c * compiler ) Jump (Op vm.OpCode , Dest * Label ) {
417+ var instr Instruction
405418 switch Op {
406419 case vm .JUMP_IF_FALSE_OR_POP , vm .JUMP_IF_TRUE_OR_POP , vm .JUMP_ABSOLUTE , vm .POP_JUMP_IF_FALSE , vm .POP_JUMP_IF_TRUE , vm .CONTINUE_LOOP : // Absolute
407- c . OpCodes . Add ( & JumpAbs {OpArg : OpArg {Op : Op }, Dest : Dest })
420+ instr = & JumpAbs {OpArg : OpArg {Op : Op }, Dest : Dest }
408421 case vm .JUMP_FORWARD , vm .SETUP_WITH , vm .FOR_ITER , vm .SETUP_LOOP , vm .SETUP_EXCEPT , vm .SETUP_FINALLY :
409- c . OpCodes . Add ( & JumpRel {OpArg : OpArg {Op : Op }, Dest : Dest })
422+ instr = & JumpRel {OpArg : OpArg {Op : Op }, Dest : Dest }
410423 default :
411424 panic ("Jump called with non jump instruction" )
412425 }
426+ instr .SetLineno (c .Lineno )
427+ c .OpCodes .Add (instr )
413428}
414429
415430/* The test for LOCAL must come before the test for FREE in order to
@@ -963,6 +978,7 @@ func (c *compiler) Stmts(stmts []ast.Stmt) {
963978
964979// Compile statement
965980func (c * compiler ) Stmt (stmt ast.Stmt ) {
981+ c .SetLineno (stmt )
966982 switch node := stmt .(type ) {
967983 case * ast.FunctionDef :
968984 // Name Identifier
@@ -1578,6 +1594,7 @@ func (c *compiler) Exprs(exprs []ast.Expr) {
15781594
15791595// Compile and expression
15801596func (c * compiler ) Expr (expr ast.Expr ) {
1597+ c .SetLineno (expr )
15811598 switch node := expr .(type ) {
15821599 case * ast.BoolOp :
15831600 // Op BoolOpNumber
0 commit comments