vise/go/state/state.go

233 lines
4.5 KiB
Go
Raw Normal View History

2023-03-31 11:52:04 +02:00
package state
import (
2023-03-31 13:56:11 +02:00
"fmt"
2023-03-31 14:06:59 +02:00
"log"
2023-03-31 11:52:04 +02:00
)
type State struct {
Flags []byte
CacheSize uint32
CacheUseSize uint32
2023-03-31 11:59:55 +02:00
Cache []map[string]string
2023-03-31 14:24:14 +02:00
CacheMap map[string]string
2023-03-31 11:59:55 +02:00
ExecPath []string
Arg *string
2023-03-31 21:42:13 +02:00
sizes map[string]uint16
2023-03-31 22:08:06 +02:00
sink *string
//sizeIdx uint16
2023-03-31 11:52:04 +02:00
}
2023-03-31 14:06:59 +02:00
func NewState(bitSize uint64) State {
2023-03-31 11:52:04 +02:00
if bitSize == 0 {
panic("bitsize cannot be 0")
}
n := bitSize % 8
if n > 0 {
bitSize += (8 - n)
}
2023-03-31 16:24:29 +02:00
st := State{
2023-03-31 11:52:04 +02:00
Flags: make([]byte, bitSize / 8),
CacheSize: 0,
CacheUseSize: 0,
}
2023-03-31 16:24:29 +02:00
st.Down("")
return st
2023-03-31 11:52:04 +02:00
}
2023-03-31 20:24:30 +02:00
func(st State) Where() string {
if len(st.ExecPath) == 0 {
return ""
}
l := len(st.ExecPath)
return st.ExecPath[l-1]
}
2023-03-31 11:52:04 +02:00
func(st State) WithCacheSize(cacheSize uint32) State {
st.CacheSize = cacheSize
return st
}
2023-03-31 11:59:55 +02:00
2023-03-31 19:17:43 +02:00
func(st *State) PutArg(input string) error {
st.Arg = &input
2023-03-31 19:17:43 +02:00
return nil
}
2023-03-31 19:17:43 +02:00
func(st *State) PopArg() (string, error) {
if st.Arg == nil {
return "", fmt.Errorf("arg is not set")
}
return *st.Arg, nil
}
2023-03-31 16:03:54 +02:00
func(st *State) Down(input string) {
2023-03-31 11:59:55 +02:00
m := make(map[string]string)
st.Cache = append(st.Cache, m)
2023-03-31 21:42:13 +02:00
st.sizes = make(map[string]uint16)
2023-03-31 16:24:29 +02:00
st.ExecPath = append(st.ExecPath, input)
2023-03-31 22:18:54 +02:00
st.resetCurrent()
}
func(st *State) Up() error {
l := len(st.Cache)
if l == 0 {
return fmt.Errorf("exit called beyond top frame")
}
l -= 1
m := st.Cache[l]
for k, v := range m {
sz := len(v)
st.CacheUseSize -= uint32(sz)
log.Printf("free frame %v key %v value size %v", l, k, sz)
}
st.Cache = st.Cache[:l]
st.ExecPath = st.ExecPath[:l]
st.resetCurrent()
return nil
2023-03-31 11:59:55 +02:00
}
2023-03-31 21:23:45 +02:00
func(st *State) Add(key string, value string, sizeHint uint16) error {
if sizeHint > 0 {
l := uint16(len(value))
if l > sizeHint {
return fmt.Errorf("value length %v exceeds value size limit %v", l, sizeHint)
}
}
2023-03-31 15:04:08 +02:00
checkFrame := st.frameOf(key)
if checkFrame > -1 {
return fmt.Errorf("key %v already defined in frame %v", key, checkFrame)
}
sz := st.checkCapacity(value)
2023-03-31 13:56:11 +02:00
if sz == 0 {
return fmt.Errorf("Cache capacity exceeded %v of %v", st.CacheUseSize + sz, st.CacheSize)
}
2023-03-31 15:04:08 +02:00
log.Printf("add key %s value size %v", key, sz)
st.Cache[len(st.Cache)-1][key] = value
2023-03-31 13:56:11 +02:00
st.CacheUseSize += sz
2023-03-31 21:42:13 +02:00
st.sizes[key] = sizeHint
2023-03-31 16:03:54 +02:00
return nil
}
func(st *State) Update(key string, value string) error {
2023-03-31 21:42:13 +02:00
sizeHint := st.sizes[key]
if st.sizes[key] > 0 {
l := uint16(len(value))
if l > sizeHint {
return fmt.Errorf("update value length %v exceeds value size limit %v", l, sizeHint)
}
}
2023-03-31 16:03:54 +02:00
checkFrame := st.frameOf(key)
if checkFrame == -1 {
return fmt.Errorf("key %v not defined", key)
}
r := st.Cache[checkFrame][key]
l := uint32(len(r))
st.Cache[checkFrame][key] = ""
if st.CacheMap[key] != "" {
st.CacheMap[key] = value
}
2023-03-31 16:03:54 +02:00
st.CacheUseSize -= l
sz := st.checkCapacity(value)
if sz == 0 {
baseUseSize := st.CacheUseSize
st.Cache[checkFrame][key] = r
st.CacheUseSize += l
return fmt.Errorf("Cache capacity exceeded %v of %v", baseUseSize + sz, st.CacheSize)
}
2023-03-31 11:59:55 +02:00
return nil
}
2023-03-31 22:08:06 +02:00
func(st *State) Map(key string) error {
2023-03-31 14:24:14 +02:00
m, err := st.Get()
if err != nil {
return err
}
2023-03-31 22:08:06 +02:00
l := st.sizes[key]
if l == 0 {
if st.sink != nil {
return fmt.Errorf("sink already set to symbol '%v'", *st.sink)
}
st.sink = &key
}
st.CacheMap[key] = m[key]
2023-03-31 14:24:14 +02:00
return nil
}
2023-03-31 15:04:08 +02:00
func(st *State) Depth() uint8 {
return uint8(len(st.Cache))
}
2023-03-31 11:59:55 +02:00
func(st *State) Get() (map[string]string, error) {
2023-03-31 15:04:08 +02:00
if len(st.Cache) == 0 {
return nil, fmt.Errorf("get at top frame")
}
2023-03-31 11:59:55 +02:00
return st.Cache[len(st.Cache)-1], nil
}
2023-03-31 13:56:11 +02:00
func(st *State) Val(key string) (string, error) {
r := st.CacheMap[key]
if len(r) == 0 {
return "", fmt.Errorf("key %v not mapped", key)
}
return r, nil
}
2023-03-31 13:56:11 +02:00
2023-03-31 16:03:54 +02:00
func(st *State) Reset() {
if len(st.Cache) == 0 {
return
}
2023-03-31 15:04:08 +02:00
st.Cache = st.Cache[:1]
st.CacheUseSize = 0
2023-03-31 16:03:54 +02:00
return
2023-03-31 15:04:08 +02:00
}
func(st *State) Check(key string) bool {
return st.frameOf(key) == -1
}
2023-03-31 22:08:06 +02:00
// Returns size used by values, and remaining size available
2023-03-31 21:42:13 +02:00
func(st *State) Size() (uint32, uint32) {
var l int
var c uint16
for k, v := range st.CacheMap {
l += len(v)
c += st.sizes[k]
}
2023-03-31 22:08:06 +02:00
r := uint32(l)
return r, uint32(c)-r
2023-03-31 21:42:13 +02:00
}
2023-03-31 16:03:54 +02:00
// return 0-indexed frame number where key is defined. -1 if not defined
2023-03-31 15:04:08 +02:00
func(st *State) frameOf(key string) int {
log.Printf("--- %s", key)
for i, m := range st.Cache {
for k, _ := range m {
if k == key {
return i
}
}
}
return -1
}
2023-03-31 16:03:54 +02:00
// bytes that will be added to cache use size for string
// returns 0 if capacity would be exceeded
2023-03-31 13:56:11 +02:00
func(st *State) checkCapacity(v string) uint32 {
sz := uint32(len(v))
if st.CacheSize == 0 {
return sz
}
if st.CacheUseSize + sz > st.CacheSize {
return 0
}
return sz
}
2023-03-31 22:18:54 +02:00
func(st *State) resetCurrent() {
st.sink = nil
st.CacheMap = make(map[string]string)
}