aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang/vm/code
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm/code')
-rw-r--r--pkg/lang/vm/code/builder.go60
-rw-r--r--pkg/lang/vm/code/code.go8
2 files changed, 65 insertions, 3 deletions
diff --git a/pkg/lang/vm/code/builder.go b/pkg/lang/vm/code/builder.go
new file mode 100644
index 0000000..3aa6b6b
--- /dev/null
+++ b/pkg/lang/vm/code/builder.go
@@ -0,0 +1,60 @@
+package code
+
+import (
+ "encoding/binary"
+ "math"
+)
+
+type Builder struct {
+ code []byte
+ debugInfo DebugInfo
+}
+
+func NewBuilder() Builder {
+ return Builder{
+ code: make([]byte, 0, 64),
+ debugInfo: NewDebugInfo("unknown file"),
+ }
+}
+
+func (b *Builder) AppendOp(op Op) {
+ b.code = append(b.code, byte(op))
+}
+
+func (b *Builder) AppendInt(x int64) {
+ b.code = append(b.code, make([]byte, 8)...)
+ binary.LittleEndian.PutUint64(b.code[len(b.code)-8:], uint64(x))
+}
+
+func (b *Builder) AppendFloat(x float64) {
+ b.AppendInt(int64(math.Float64bits(x)))
+}
+
+func (b *Builder) AppendString(s string) {
+ b.code = append(b.code, []byte(s)...)
+ b.code = append(b.code, 0)
+}
+
+func (b *Builder) AppendRaw(raw Raw) {
+ b.code = append(b.code, raw...)
+}
+
+func (b *Builder) AppendLine(line int) {
+ b.debugInfo.AppendLine(len(b.code)-1, line)
+}
+
+func (b *Builder) SetInt(at int, x int64) {
+ binary.LittleEndian.PutUint64(b.code[at:], uint64(x))
+}
+
+func (b *Builder) Code() Raw {
+ return Raw(b.code)
+}
+
+func (b *Builder) Len() int {
+ return len(b.code)
+}
+
+func (b *Builder) Build() Code {
+ return New(b.code, b.debugInfo)
+}
diff --git a/pkg/lang/vm/code/code.go b/pkg/lang/vm/code/code.go
index 38bbcf5..94fd612 100644
--- a/pkg/lang/vm/code/code.go
+++ b/pkg/lang/vm/code/code.go
@@ -7,12 +7,14 @@ import (
"strings"
)
+type Raw []byte
+
type Code struct {
- code []byte
+ code Raw
debugInfo DebugInfo
}
-func New(code []byte, info DebugInfo) Code {
+func New(code Raw, info DebugInfo) Code {
return Code{
code: code,
debugInfo: info,
@@ -23,7 +25,7 @@ func (c *Code) Len() int {
return len(c.code)
}
-func (c *Code) Code() []byte {
+func (c *Code) Code() Raw {
return c.code
}