From b4a0f2209026a90bfd67072526f938e00c00af78 Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 29 Aug 2022 22:21:36 +0000 Subject: Put full function data onto call stack --- pkg/lang/vm/stack/stack.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'pkg/lang/vm/stack/stack.go') diff --git a/pkg/lang/vm/stack/stack.go b/pkg/lang/vm/stack/stack.go index 74d4ab8..c710878 100644 --- a/pkg/lang/vm/stack/stack.go +++ b/pkg/lang/vm/stack/stack.go @@ -19,8 +19,8 @@ type Stack interface { IsEmpty() bool PutRootCall(basePos code.Pos) error - PushCall(newPos, returnPos code.Pos, env mem.Ptr) error - PopCall() (code.Pos, error) + PushCall(fn value.FunctionData, returnPos code.Pos) error + PopCall() (value.FunctionData, code.Pos, error) CurrentCallEnv() mem.Ptr CallDepth() int @@ -104,10 +104,9 @@ func (stack *stackImpl) IsEmpty() bool { func (stack *stackImpl) PutRootCall(basePos code.Pos) error { frame := callFrame{ - pos: basePos, + fn: value.FunctionData{}, returnPos: basePos, base: 0, - env: mem.NullPtr, } if stack.CallDepth() == 0 { @@ -125,38 +124,37 @@ func (stack *stackImpl) PutRootCall(basePos code.Pos) error { return nil } -func (stack *stackImpl) PushCall(newPos, returnPos code.Pos, env mem.Ptr) error { +func (stack *stackImpl) PushCall(fn value.FunctionData, returnPos code.Pos) error { if stack.CallDepth() == 1000 { return ErrReachedMaxCallDepth } stack.calls = append(stack.calls, callFrame{ - pos: newPos, + fn: fn, returnPos: returnPos, base: stack.Len(), - env: env, }) return nil } -func (stack *stackImpl) PopCall() (code.Pos, error) { +func (stack *stackImpl) PopCall() (value.FunctionData, code.Pos, error) { if stack.CallDepth() == 0 { - return code.Pos{}, ErrReachedRootCallFrame + return value.FunctionData{}, code.Pos{}, ErrReachedRootCallFrame } call := stack.topCall() stack.calls = stack.calls[:stack.CallDepth()-1] - return call.returnPos, nil + return call.fn, call.returnPos, nil } func (stack *stackImpl) CurrentCallEnv() mem.Ptr { - if stack.CallDepth() == 0 || stack.topCall().env.IsNull() { + if stack.CallDepth() == 0 || stack.topCall().fn.Env().IsNull() { return mem.NullPtr } - return stack.topCall().env + return stack.topCall().fn.Env() } func (stack *stackImpl) CallDepth() int { @@ -179,8 +177,7 @@ func (stack *stackImpl) topCall() *callFrame { } type callFrame struct { - pos code.Pos // Beginning of the called function. - returnPos code.Pos // Where to return to after the called function returns. - base int // Base of the local variables on the data stack. - env mem.Ptr // Environment of the called function. + fn value.FunctionData // The called function. + returnPos code.Pos // Where to return to after the called function returns. + base int // Base of the local variables on the data stack. } -- cgit 1.4.1