¯\_(ツ)_/¯

types
Jordan Orelli 8 years ago
parent 96cf379192
commit 7521751732

@ -62,41 +62,33 @@ func ReadUBitVarFP(r Reader) uint64 {
// continuation bit.
func ReadVarInt(r Reader) uint64 {
var x uint64
var s uint
for i := 0; ; i++ {
for s := uint64(0); s < 70; s += 7 {
b := r.ReadBits(8)
if r.Err() != nil {
return 0
}
x |= b & 0x7f << s
if b < 0x80 {
if i > 9 || i == 9 && b > 1 {
panic("varint overflow")
}
return x | b<<s
break
}
x |= b & 0x7f << s
s += 7
}
return x
}
// reads a 32bit varint
func ReadVarInt32(r Reader) uint32 {
var x uint32
var s uint
for i := 0; ; i++ {
b := r.ReadBits(8)
for s := uint32(0); s < 35; s += 7 {
b := uint32(r.ReadBits(8))
if r.Err() != nil {
return 0
}
x |= b & 0x7f << s
if b < 0x80 {
if i > 4 || i == 4 && b > 0xf {
panic("varint32 overflow")
}
return x | uint32(b)<<s
break
}
x |= uint32(b&0x7f) << s
s += 7
}
return x
}
func ReadBool(r Reader) bool {

@ -0,0 +1,9 @@
package bit
import (
"math"
)
func Length(n uint) uint {
return uint(math.Floor(math.Log2(float64(n)) + 1))
}

@ -20,6 +20,9 @@ func arrayType(spec *typeSpec, env *Env) tÿpe {
elemSpec := *spec
elemSpec.typeName = elemName
elemType := parseTypeSpec(&elemSpec, env)
if elemName == "char" {
return string_t(count)
}
return &array_t{elem: elemType, count: count}
}
@ -95,3 +98,52 @@ func (a array) setSlotValue(slot int, v value) {
a.slots[slot] = v
}
func (a array) getSlotValue(slot int) value { return a.slots[slot] }
// ------------------------------------------------------------------------------
// strings are a special case of arrays
// ------------------------------------------------------------------------------
type string_t int
func (t string_t) nü() value {
return &string_v{t: t, buf: make([]byte, int(t))}
}
func (t string_t) typeName() string { return "string" }
type string_v struct {
t string_t
buf []byte // the buffer of all possible bytes
valid []byte // selection of current valid bytes within buf
}
func (s *string_v) tÿpe() tÿpe { return s.t }
func (s *string_v) read(r bit.Reader) error {
for i := 0; i < int(s.t); i++ {
b := r.ReadBits(8)
if b == 0 {
s.valid = s.buf[:i]
return r.Err()
}
s.buf[i] = byte(b & 0xff)
}
s.valid = s.buf
return r.Err()
}
func (s *string_v) String() string {
return string(s.valid)
}
func (s *string_v) slotType(int) tÿpe { return char_t }
func (s *string_v) slotName(n int) string { return strconv.Itoa(n) }
func (s *string_v) setSlotValue(slot int, v value) {
s.buf[slot] = byte(*v.(*char_v))
if slot >= len(s.valid) {
s.valid = s.buf[:slot+1]
}
}
func (s *string_v) getSlotValue(slot int) value {
v := char_v(s.buf[slot])
return &v
}

@ -317,8 +317,32 @@ func (v *cutl_string_v) read(r bit.Reader) error {
return r.Err()
}
// ------------------------------------------------------------------------------
// char
//
// this seems absolutely wrong, but it's how it's done in clarity. a char is a
// cstring? wtf?
// ------------------------------------------------------------------------------
var char_t = typeLiteral{
name: "char",
newFn: func() value {
return new(char_v)
},
}
type char_v byte
func (v char_v) tÿpe() tÿpe { return char_t }
func (v char_v) String() string { return string(v) }
func (v *char_v) read(r bit.Reader) error {
*v = char_v(r.ReadByte())
return r.Err()
}
var atom_types = []tÿpe{
bool_t,
char_t,
uint8_t,
uint16_t,
uint32_t,

@ -2,7 +2,6 @@ package ent
import (
"github.com/golang/protobuf/proto"
"os"
"strconv"
"github.com/jordanorelli/hyperstone/bit"
@ -11,12 +10,13 @@ import (
)
type Env struct {
symbols symbolTable
source bit.BufReader
classes map[string]*classHistory
netIds map[int]string
fields []field
strings *stbl.Dict
symbols symbolTable
source bit.BufReader
classes map[string]*classHistory
netIds map[int]string
fields []field
strings *stbl.Dict
entities map[int]*entity
}
func NewEnv() *Env {
@ -183,7 +183,7 @@ func (e *Env) syncBaselineTable(t *stbl.Table) {
for _, s := range selections {
if err := s.fillSlots(ent, r); err != nil {
Debug.Printf("syncBaseline fill error: %v", err)
os.Exit(1)
break
}
}
}

@ -88,9 +88,7 @@ type cutl_vector struct {
func (v *cutl_vector) tÿpe() tÿpe { return v.t }
func (v *cutl_vector) read(r bit.Reader) error {
count := bit.ReadVarInt32(r)
Debug.Printf("allocating cutl_vector of size %d with element type %s", count, v.t.elem.typeName())
v.slots = make([]value, count)
bit.ReadVarInt32(r) // ??
return r.Err()
}
@ -110,4 +108,10 @@ func (v *cutl_vector) setSlotValue(slot int, val value) {
v.slots[slot] = val
}
}
func (v *cutl_vector) getSlotValue(slot int) value { return v.slots[slot] }
func (v *cutl_vector) getSlotValue(slot int) value {
if slot >= len(v.slots) {
return nil
}
return v.slots[slot]
}

@ -66,5 +66,5 @@ func TestTree(t *testing.T) {
}
}
testWalk(htree, "")
testWalk(huffRoot, "")
}

Loading…
Cancel
Save