Skip to content

Commit

Permalink
Fix up chunks a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenhombre committed Jul 28, 2023
1 parent b901d44 commit 2cf90bc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
49 changes: 49 additions & 0 deletions chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

// A chunk consists of opcodes or constants, represented
// as "value" types:
const (
typ_opcode = iota
typ_constant
)

type Entry struct {
typ int
opcode
value
}

type chunk struct {
entries []Entry
index int
}

func newChunk() *chunk {
return &chunk{[]Entry{}, 0}
}

func (c *chunk) add(e Entry) {
c.entries = append(c.entries, e)
}

func opEntry(o opcode) Entry {
return Entry{typ_opcode, o, 0}
}

func constantEntry(v value) Entry {
return Entry{typ_constant, 0, v}
}

func (c *chunk) next() Entry {
e := c.entries[c.index]
c.index++
return e
}

func (c *chunk) rewind() {
c.index = 0
}

func (c *chunk) hasMore() bool {
return c.index < len(c.entries)
}
4 changes: 4 additions & 0 deletions opcode_test.go → chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ func TestChunk(t *testing.T) {
if c.hasMore() {
t.Error("chunk should not have more")
}
c.rewind()
if !c.hasMore() {
t.Error("chunk should have more")
}
}
2 changes: 1 addition & 1 deletion constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main

// for now, constants are just ints with integer index
// in the constant table
type constant int
type constant value

var constantTable = []constant{}

Expand Down
48 changes: 0 additions & 48 deletions opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,3 @@ func (o opcode) String() string {
return "UNKNOWN_OPCODE"
}
}

// A chunk consists of opcodes or constants, represented
// as "value" types:
const (
typ_opcode = iota
typ_constant
)

type Entry struct {
typ int
opcode
value
}

type chunk struct {
entries []Entry
index int
}

func newChunk() *chunk {
return &chunk{[]Entry{}, 0}
}

func (c *chunk) add(e Entry) {
c.entries = append(c.entries, e)
}

func opEntry(o opcode) Entry {
return Entry{typ_opcode, o, 0}
}

func constantEntry(v value) Entry {
return Entry{typ_constant, 0, v}
}

func (c *chunk) next() Entry {
e := c.entries[c.index]
c.index++
return e
}

func (c *chunk) rewind() {
c.index = 0
}

func (c *chunk) hasMore() bool {
return c.index < len(c.entries)
}

0 comments on commit 2cf90bc

Please sign in to comment.