From 11bcf772bf8d9aa353eb4c04bfb85378ba392b1e Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 29 May 2022 20:08:12 +0000 Subject: VM Native Functions --- pkg/lang/vm/exec.go | 13 +++++++++++++ pkg/lang/vm/value/data.go | 20 ++++++++++++++++---- pkg/lang/vm/value/value.go | 4 ++++ 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'pkg') diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go index bd4cdf5..10ac604 100644 --- a/pkg/lang/vm/exec.go +++ b/pkg/lang/vm/exec.go @@ -447,6 +447,19 @@ func (vm *VM) execCall() error { return err } + if fn.Native() != nil { + // TODO: Arguments + val, err := fn.Native()([]value.Value{}) + if err != nil { + return err + } + + if !val.IsEmpty() { + vm.stack.Push(val) + } + + return nil + } vm.pc = fn.Pc() return nil diff --git a/pkg/lang/vm/value/data.go b/pkg/lang/vm/value/data.go index 4e39bc8..c0e5e9a 100644 --- a/pkg/lang/vm/value/data.go +++ b/pkg/lang/vm/value/data.go @@ -115,10 +115,13 @@ func (a ArrayData) Push(m mem.Mem, v Value) error { type NullData struct{} type FunctionData struct { - pc int - env mem.Ptr + pc int + env mem.Ptr + native NativeFunc } +type NativeFunc func([]Value) (Value, error) + func (f FunctionData) Pc() int { return f.pc } @@ -127,12 +130,21 @@ func (f FunctionData) Env() mem.Ptr { return f.env } +func (f FunctionData) Native() NativeFunc { + return f.native +} + func (f FunctionData) WithEnv(env mem.Ptr) FunctionData { - return FunctionData{pc: f.pc, env: env} + return FunctionData{pc: f.pc, env: env, native: f.native} } func (f FunctionData) String(_ mem.Mem) (string, error) { - return fmt.Sprintf("", f.pc), nil + if f.native != nil { + return "", nil + } else { + return fmt.Sprintf("", f.pc), nil + } +} } type ObjectData struct{} // TODO diff --git a/pkg/lang/vm/value/value.go b/pkg/lang/vm/value/value.go index a2909ba..cce0f1b 100644 --- a/pkg/lang/vm/value/value.go +++ b/pkg/lang/vm/value/value.go @@ -85,6 +85,10 @@ func (v Value) WithOutlet(outlet mem.Ptr) Value { return Value{t: v.t, d: v.d, outlet: outlet} } +func (v Value) IsEmpty() bool { + return v.t == mem.NullPtr +} + func (v Value) Clone(m mem.Mem) Value { if v.t.Kind == StringType { str := v.d.(StringData) -- cgit 1.4.1