Skip to content

Commit 0630d6f

Browse files
committed
feat: allow reset all global variable
1 parent f41d4e2 commit 0630d6f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

engine/vm.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"io/fs"
88
"strings"
9-
"sync/atomic"
109
)
1110

1211
type bytecode []instruction
@@ -303,8 +302,17 @@ func (vm *VM) SetUserOutput(s *Stream) {
303302
vm.output = s
304303
}
305304

306-
func (vm *VM) ResetVariableCounter() {
307-
atomic.StoreInt64(&varCounter, 1) // Set the varCounter to 1 instead of 0 because NewVariable() will first increment this counter and a global root variable has already been set to 1. (See engine/env.go L3)
305+
// ResetEnv is used to reset all global variable
306+
func (vm *VM) ResetEnv() {
307+
varCounter = 0
308+
varContext = NewVariable()
309+
rootContext = NewAtom("root")
310+
rootEnv = &Env{
311+
binding: binding{
312+
key: newEnvKey(varContext),
313+
value: rootContext,
314+
},
315+
}
308316
}
309317

310318
// Predicate0 is a predicate of arity 0.

interpreter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type Interpreter struct {
2323
// NewEmpty creates a new Prolog interpreter without any predicates/operators defined.
2424
func NewEmpty() *Interpreter {
2525
var i Interpreter
26-
i.ResetVariableCounter()
26+
i.ResetEnv()
2727
return &i
2828
}
2929

3030
// New creates a new Prolog interpreter with predefined predicates/operators.
3131
func New(in io.Reader, out io.Writer) *Interpreter {
3232
var i Interpreter
33-
i.ResetVariableCounter()
33+
i.ResetEnv()
3434
i.FS = defaultFS{}
3535
i.SetUserInput(engine.NewInputTextStream(in))
3636
i.SetUserOutput(engine.NewOutputTextStream(out))

0 commit comments

Comments
 (0)