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

     1  package code
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  )
     7  
     8  // A Constant is a literal that can be loaded into a register (that include code
     9  // chunks).
    10  type Constant interface {
    11  	ShortString() string
    12  }
    13  
    14  // Code is a constant representing a chunk of code.  It doesn't contain any
    15  // actual opcodes, but refers to a range in the code unit it belongs to.
    16  type Code struct {
    17  	Name                   string   // Name of the function (if it has one)
    18  	StartOffset, EndOffset uint     // Where to find the opcode in the code Unit this belongs to
    19  	UpvalueCount           int16    // Number of upvalues
    20  	CellCount              int16    // Number of cell registers needed to run the code
    21  	RegCount               int16    // Number of registers needed to run the coee
    22  	UpNames                []string // Names of the upvalues
    23  }
    24  
    25  var _ Constant = Code{}
    26  var _ spanGetter = Code{}
    27  
    28  // ShortString returns a short string describing this constant (e.g. for disassembly)
    29  func (c Code) ShortString() string {
    30  	return fmt.Sprintf("function %s [%d - %d]", c.nameString(), c.StartOffset, c.EndOffset-1)
    31  }
    32  
    33  // GetSpan returns the name of the function this code is compiled from, and the
    34  // range of instructions in the code Unit this code belongs to.
    35  func (c Code) GetSpan() (name string, start, end int) {
    36  	name = c.nameString()
    37  	start = int(c.StartOffset)
    38  	end = int(c.EndOffset - 1)
    39  	return
    40  }
    41  
    42  func (c Code) nameString() string {
    43  	if c.Name == "" {
    44  		return "<anon>"
    45  	}
    46  	return c.Name
    47  }
    48  
    49  // A Float is a floating point literal.
    50  type Float float64
    51  
    52  var _ Constant = Float(0)
    53  
    54  // ShortString returns a short string describing this constant (e.g. for disassembly)
    55  func (f Float) ShortString() string {
    56  	return strconv.FormatFloat(float64(f), 'g', -1, 64)
    57  }
    58  
    59  // An Int is an integer literal.
    60  type Int int64
    61  
    62  var _ Constant = Int(0)
    63  
    64  // ShortString returns a short string describing this constant (e.g. for disassembly)
    65  func (i Int) ShortString() string {
    66  	return strconv.FormatInt(int64(i), 10)
    67  }
    68  
    69  // A Bool is a boolean literal.
    70  type Bool bool
    71  
    72  var _ Constant = Bool(false)
    73  
    74  // ShortString returns a short string describing this constant (e.g. for disassembly)
    75  func (b Bool) ShortString() string {
    76  	return strconv.FormatBool(bool(b))
    77  }
    78  
    79  // A String is a string literal.
    80  type String string
    81  
    82  var _ Constant = String("")
    83  
    84  // ShortString returns a short string describing this constant (e.g. for disassembly)
    85  func (s String) ShortString() string {
    86  	return strconv.Quote(string(s))
    87  }
    88  
    89  // NilType is the type of the nil literal.
    90  type NilType struct{}
    91  
    92  var _ Constant = NilType(struct{}{})
    93  
    94  // ShortString returns a short string describing this constant (e.g. for disassembly)
    95  func (n NilType) ShortString() string {
    96  	return "nil"
    97  }