From 22a69393f58abcf3bcf9e7039f994dae78422213 Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 27 Jul 2022 11:01:29 +0000 Subject: Implement VM modules and globals --- pkg/lang/vm/value/cells.go | 21 +++++++++++++++++++-- pkg/lang/vm/value/data.go | 13 +++++++------ pkg/lang/vm/value/value.go | 5 +++-- 3 files changed, 29 insertions(+), 10 deletions(-) (limited to 'pkg/lang/vm/value') diff --git a/pkg/lang/vm/value/cells.go b/pkg/lang/vm/value/cells.go index 5abb032..19ecfde 100644 --- a/pkg/lang/vm/value/cells.go +++ b/pkg/lang/vm/value/cells.go @@ -1,6 +1,9 @@ package value -import "jinx/pkg/lang/vm/mem" +import ( + "jinx/pkg/lang/vm/code" + "jinx/pkg/lang/vm/mem" +) type ArrayCell []Value @@ -55,7 +58,7 @@ func (t TypeCell) DropCell(m mem.Mem) { typ := t.Get() for _, f := range typ.Methods { // Wrap data in a Value to drop it. - val := NewFunction(0, 0).WithData(f) + val := NewFunction(code.Pos{}, 0).WithData(f) val.Drop(m) } @@ -101,3 +104,17 @@ func (e EnvCell) MatchingCellKind() mem.CellKind { func (e EnvCell) Get() Env { return Env(e) } + +type GlobalCell Value + +func (g GlobalCell) DropCell(m mem.Mem) { + panic("global cell cannot be dropped") +} + +func (g GlobalCell) MatchingCellKind() mem.CellKind { + return mem.CellKindGlobal +} + +func (g GlobalCell) Get() Value { + return Value(g) +} diff --git a/pkg/lang/vm/value/data.go b/pkg/lang/vm/value/data.go index 18e1b3d..6c3d762 100644 --- a/pkg/lang/vm/value/data.go +++ b/pkg/lang/vm/value/data.go @@ -2,6 +2,7 @@ package value import ( "fmt" + "jinx/pkg/lang/vm/code" "jinx/pkg/lang/vm/mem" "strconv" "strings" @@ -127,7 +128,7 @@ func (n NullData) String(_ mem.Mem) (string, error) { } type FunctionData struct { - pc int + pos code.Pos args uint env mem.Ptr native NativeFunc @@ -135,8 +136,8 @@ type FunctionData struct { type NativeFunc func([]Value) (Value, error) -func (f FunctionData) Pc() int { - return f.pc +func (f FunctionData) Pos() code.Pos { + return f.pos } func (f FunctionData) Args() uint { @@ -152,18 +153,18 @@ func (f FunctionData) Native() NativeFunc { } func (f FunctionData) WithEnv(env mem.Ptr) FunctionData { - return FunctionData{pc: f.pc, args: f.args, env: env, native: f.native} + return FunctionData{pos: f.pos, args: f.args, env: env, native: f.native} } func (f FunctionData) WithArgs(args uint) FunctionData { - return FunctionData{pc: f.pc, args: args, env: f.env, native: f.native} + return FunctionData{pos: f.pos, args: args, env: f.env, native: f.native} } func (f FunctionData) String(_ mem.Mem) (string, error) { if f.native != nil { return fmt.Sprintf("", f.args), nil } else { - return fmt.Sprintf("", f.args, f.pc), nil + return fmt.Sprintf("", f.args, f.pos.Module, f.pos.PC), nil } } diff --git a/pkg/lang/vm/value/value.go b/pkg/lang/vm/value/value.go index 5dd5012..b051f04 100644 --- a/pkg/lang/vm/value/value.go +++ b/pkg/lang/vm/value/value.go @@ -1,6 +1,7 @@ package value import ( + "jinx/pkg/lang/vm/code" "jinx/pkg/lang/vm/mem" ) @@ -52,8 +53,8 @@ func NewNull() Value { return Value{t: CORE_TYPE_NULL, d: NullData{}} } -func NewFunction(pc int, args uint) Value { - return Value{t: CORE_TYPE_FUNCTION, d: FunctionData{pc: pc, args: args}} +func NewFunction(pos code.Pos, args uint) Value { + return Value{t: CORE_TYPE_FUNCTION, d: FunctionData{pos: pos, args: args}} } func NewNativeFunction(f NativeFunc, args uint) Value { -- cgit 1.4.1