@@ -68,6 +68,7 @@ const (
68
68
type compiler struct {
69
69
Code * py.Code // code being built up
70
70
Filename string
71
+ Lineno int // current line number
71
72
OpCodes Instructions
72
73
loops loopstack
73
74
SymTable * symtable.SymTable
@@ -144,6 +145,11 @@ func (c *compiler) panicSyntaxErrorf(Ast ast.Ast, format string, a ...interface{
144
145
panic (err )
145
146
}
146
147
148
+ // Sets Lineno from an ast node
149
+ func (c * compiler ) SetLineno (node ast.Ast ) {
150
+ c .Lineno = node .GetLineno ()
151
+ }
152
+
147
153
// Create a new compiler object at Ast, using private for name mangling
148
154
func (c * compiler ) newCompilerScope (compilerScope compilerScopeType , Ast ast.Ast , private string ) (newC * compiler ) {
149
155
newSymTable := c .SymTable .FindChild (Ast )
@@ -186,6 +192,7 @@ func (c *compiler) compileAst(Ast ast.Ast, filename string, futureFlags int, don
186
192
code .Freevars = append (code .Freevars , SymTable .Find (symtable .ScopeFree , symtable .DefFreeClass )... )
187
193
code .Flags = c .codeFlags (SymTable ) | int32 (futureFlags & py .CO_COMPILER_FLAGS_MASK )
188
194
valueOnStack := false
195
+ c .SetLineno (Ast )
189
196
switch node := Ast .(type ) {
190
197
case * ast.Module :
191
198
c .Stmts (c .docString (node .Body , false ))
@@ -283,6 +290,7 @@ func (c *compiler) compileAst(Ast ast.Ast, filename string, futureFlags int, don
283
290
code .Code = c .OpCodes .Assemble ()
284
291
code .Stacksize = int32 (c .OpCodes .StackDepth ())
285
292
code .Nlocals = int32 (len (code .Varnames ))
293
+ code .Lnotab = string (c .OpCodes .Lnotab ())
286
294
return nil
287
295
}
288
296
@@ -377,15 +385,19 @@ func (c *compiler) OpArg(Op vm.OpCode, Arg uint32) {
377
385
if ! Op .HAS_ARG () {
378
386
panic ("OpArg called with an instruction which doesn't take an Arg" )
379
387
}
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 )
381
391
}
382
392
383
393
// Compiles an instruction without an argument
384
394
func (c * compiler ) Op (op vm.OpCode ) {
385
395
if op .HAS_ARG () {
386
396
panic ("Op called with an instruction which takes an Arg" )
387
397
}
388
- c .OpCodes .Add (& Op {Op : op })
398
+ instr := & Op {Op : op }
399
+ instr .SetLineno (c .Lineno )
400
+ c .OpCodes .Add (instr )
389
401
}
390
402
391
403
// Inserts an existing label
@@ -402,14 +414,17 @@ func (c *compiler) NewLabel() *Label {
402
414
403
415
// Compiles a jump instruction
404
416
func (c * compiler ) Jump (Op vm.OpCode , Dest * Label ) {
417
+ var instr Instruction
405
418
switch Op {
406
419
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 }
408
421
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 }
410
423
default :
411
424
panic ("Jump called with non jump instruction" )
412
425
}
426
+ instr .SetLineno (c .Lineno )
427
+ c .OpCodes .Add (instr )
413
428
}
414
429
415
430
/* 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) {
963
978
964
979
// Compile statement
965
980
func (c * compiler ) Stmt (stmt ast.Stmt ) {
981
+ c .SetLineno (stmt )
966
982
switch node := stmt .(type ) {
967
983
case * ast.FunctionDef :
968
984
// Name Identifier
@@ -1578,6 +1594,7 @@ func (c *compiler) Exprs(exprs []ast.Expr) {
1578
1594
1579
1595
// Compile and expression
1580
1596
func (c * compiler ) Expr (expr ast.Expr ) {
1597
+ c .SetLineno (expr )
1581
1598
switch node := expr .(type ) {
1582
1599
case * ast.BoolOp :
1583
1600
// Op BoolOpNumber
0 commit comments