From 7520aa4474ea30985cf26631c6bbdebf38484a0d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 9 Jan 2026 19:10:27 +0100 Subject: feat: initial support for maps --- vm/op_string.go | 39 ++++++++++++++++++++------------------- vm/type.go | 22 ++++++++++++++++++++-- vm/vm.go | 11 +++++------ 3 files changed, 45 insertions(+), 27 deletions(-) (limited to 'vm') diff --git a/vm/op_string.go b/vm/op_string.go index a39dcfa..e5a00b6 100644 --- a/vm/op_string.go +++ b/vm/op_string.go @@ -31,28 +31,29 @@ func _() { _ = x[Grow-20] _ = x[Index-21] _ = x[IndexSet-22] - _ = x[Jump-23] - _ = x[JumpTrue-24] - _ = x[JumpFalse-25] - _ = x[JumpSetTrue-26] - _ = x[JumpSetFalse-27] - _ = x[Lower-28] - _ = x[Loweri-29] - _ = x[Mul-30] - _ = x[New-31] - _ = x[Negate-32] - _ = x[Not-33] - _ = x[Pop-34] - _ = x[Push-35] - _ = x[Return-36] - _ = x[Sub-37] - _ = x[Subi-38] - _ = x[Swap-39] + _ = x[MapSet-23] + _ = x[Jump-24] + _ = x[JumpTrue-25] + _ = x[JumpFalse-26] + _ = x[JumpSetTrue-27] + _ = x[JumpSetFalse-28] + _ = x[Lower-29] + _ = x[Loweri-30] + _ = x[Mul-31] + _ = x[New-32] + _ = x[Negate-33] + _ = x[Not-34] + _ = x[Pop-35] + _ = x[Push-36] + _ = x[Return-37] + _ = x[Sub-38] + _ = x[Subi-39] + _ = x[Swap-40] } -const _Op_name = "NopAddAddrAssignFassignVassignCallCalliCallXDerefDupFdupFnewEqualEqualSetExitFieldFieldSetFieldFsetGreaterGrowIndexIndexSetJumpJumpTrueJumpFalseJumpSetTrueJumpSetFalseLowerLoweriMulNewNegateNotPopPushReturnSubSubiSwap" +const _Op_name = "NopAddAddrAssignFassignVassignCallCalliCallXDerefDupFdupFnewEqualEqualSetExitFieldFieldSetFieldFsetGreaterGrowIndexIndexSetMapSetJumpJumpTrueJumpFalseJumpSetTrueJumpSetFalseLowerLoweriMulNewNegateNotPopPushReturnSubSubiSwap" -var _Op_index = [...]uint8{0, 3, 6, 10, 16, 23, 30, 34, 39, 44, 49, 52, 56, 60, 65, 73, 77, 82, 90, 99, 106, 110, 115, 123, 127, 135, 144, 155, 167, 172, 178, 181, 184, 190, 193, 196, 200, 206, 209, 213, 217} +var _Op_index = [...]uint8{0, 3, 6, 10, 16, 23, 30, 34, 39, 44, 49, 52, 56, 60, 65, 73, 77, 82, 90, 99, 106, 110, 115, 123, 129, 133, 141, 150, 161, 173, 178, 184, 187, 190, 196, 199, 202, 206, 212, 215, 219, 223} func (i Op) String() string { idx := int(i) - 0 diff --git a/vm/type.go b/vm/type.go index 3a8d901..88e3dc0 100644 --- a/vm/type.go +++ b/vm/type.go @@ -38,8 +38,21 @@ type Value struct { } // NewValue returns an addressable zero value for the specified type. -func NewValue(typ *Type) Value { - if typ.Rtype.Kind() == reflect.Func { +func NewValue(typ *Type, arg ...int) Value { + switch typ.Rtype.Kind() { + case reflect.Slice: + if len(arg) == 1 { + v := reflect.New(typ.Rtype).Elem() + v.Set(reflect.MakeSlice(typ.Rtype, arg[0], arg[0])) + return Value{Type: typ, Value: v} + } + case reflect.Map: + if len(arg) == 1 { + v := reflect.New(typ.Rtype).Elem() + v.Set(reflect.MakeMapWithSize(typ.Rtype, arg[0])) + return Value{Type: typ, Value: v} + } + case reflect.Func: typ = TypeOf(0) // Function value is its index in the code segment. } return Value{Type: typ, Value: reflect.New(typ.Rtype).Elem()} @@ -71,6 +84,11 @@ func SliceOf(t *Type) *Type { return &Type{Rtype: reflect.SliceOf(t.Rtype)} } +// MapOf returns the map type with the given key and element types. +func MapOf(k, e *Type) *Type { + return &Type{Rtype: reflect.MapOf(k.Rtype, e.Rtype)} +} + // FuncOf returns the function type with the given argument and result types. func FuncOf(arg, ret []*Type, variadic bool) *Type { a := make([]reflect.Type, len(arg)) diff --git a/vm/vm.go b/vm/vm.go index b5c6cf5..709b459 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -42,6 +42,7 @@ const ( Grow // -- ; sp += $1 Index // a i -- a[i] ; IndexSet // a i v -- a; a[i] = v + MapSet // a i v -- a; a[i] = v Jump // -- ; ip += $1 JumpTrue // cond -- ; if cond { ip += $1 } JumpFalse // cond -- ; if cond { ip += $1 } @@ -170,11 +171,7 @@ func (m *Machine) Run() (err error) { case Fdup: mem = append(mem, mem[c.Arg[0]+fp-1]) case Fnew: - mem = append(mem, NewValue(mem[c.Arg[0]].Type)) - if len(c.Arg) > 1 { - mem[len(mem)-1].Grow(c.Arg[1]) - mem[len(mem)-1].SetLen(c.Arg[1]) - } + mem = append(mem, NewValue(mem[c.Arg[0]].Type, c.Arg[1:]...)) case Field: fv := mem[sp-1].FieldByIndex(c.Arg) if !fv.CanSet() { @@ -268,9 +265,11 @@ func (m *Machine) Run() (err error) { mem[sp-2].Value = mem[sp-2].Index(int(mem[sp-1].Int())) mem = mem[:sp-1] case IndexSet: - log.Println("## IndexSet:", sp-3, mem[sp-3].Type) mem[sp-3].Value.Index(int(mem[sp-2].Int())).Set(mem[sp-1].Value) mem = mem[:sp-2] + case MapSet: + mem[sp-3].SetMapIndex(mem[sp-2].Value, mem[sp-1].Value) + mem = mem[:sp-2] case Vassign: mem[sp-2].Set(mem[sp-1].Value) mem = mem[:sp-2] -- cgit v1.2.3