github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/astcomp/emitters.go (about)

     1  package astcomp
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/arnodel/golua/ast"
     7  	"github.com/arnodel/golua/ir"
     8  )
     9  
    10  // These methods offer the convenience of not having to calculate the line
    11  // number when emitting instructions.
    12  
    13  func (c *compiler) emitInstr(l ast.Locator, instr ir.Instruction) {
    14  	c.CodeBuilder.Emit(instr, getLine(l))
    15  }
    16  
    17  func (c *compiler) emitJump(l ast.Locator, lbl ir.Name) {
    18  	if !c.CodeBuilder.EmitJump(lbl, getLine(l)) {
    19  		panic(Error{
    20  			Where:   l,
    21  			Message: fmt.Sprintf("no visible label '%s'", lbl),
    22  		})
    23  	}
    24  }
    25  
    26  func (c *compiler) emitLoadConst(l ast.Locator, k ir.Constant, reg ir.Register) {
    27  	ir.EmitConstant(c.CodeBuilder, k, reg, getLine(l))
    28  }
    29  
    30  func (c *compiler) emitMove(l ast.Locator, dst, src ir.Register) {
    31  	ir.EmitMove(c.CodeBuilder, dst, src, getLine(l))
    32  }
    33  
    34  func getLine(l ast.Locator) int {
    35  	if l != nil {
    36  		locStart := l.Locate().StartPos()
    37  		if locStart != nil {
    38  			return locStart.Line
    39  		}
    40  	}
    41  	return 0
    42  }