diff --git a/evaluator/smalltalkEvaluator.go b/evaluator/smalltalkEvaluator.go index 74e474b..2356fa6 100644 --- a/evaluator/smalltalkEvaluator.go +++ b/evaluator/smalltalkEvaluator.go @@ -30,6 +30,13 @@ func NewSmalltalkVM() *Evaluator { return NewEvaluatorWithGlobalScope(globalScope) } +func NewSmalltalkWorkspace() *Evaluator { + globalScope := new(treeNodes.Scope).Initialize() + evaluator := NewEvaluatorWithGlobalScope(globalScope) + evaluator.workspaceScope = new(treeNodes.Scope).Initialize() + return evaluator +} + func NewEvaluatorWithGlobalScope(global *treeNodes.Scope) *Evaluator { evaluator := new(Evaluator) evaluator.programCache = make(map[string]treeNodes.ProgramNodeInterface) @@ -38,8 +45,9 @@ func NewEvaluatorWithGlobalScope(global *treeNodes.Scope) *Evaluator { } type Evaluator struct { - globalScope *treeNodes.Scope - programCache map[string]treeNodes.ProgramNodeInterface + globalScope *treeNodes.Scope + programCache map[string]treeNodes.ProgramNodeInterface + workspaceScope *treeNodes.Scope } func (e *Evaluator) SetGlobalScope(scope *treeNodes.Scope) *Evaluator { @@ -62,7 +70,12 @@ func (e *Evaluator) RunProgram(programString string) treeNodes.SmalltalkObjectIn func (e *Evaluator) EvaluateProgramNode(programNode treeNodes.ProgramNodeInterface) treeNodes.SmalltalkObjectInterface { var result treeNodes.SmalltalkObjectInterface - localScope := new(treeNodes.Scope).Initialize() + var localScope *treeNodes.Scope + if e.workspaceScope != nil { + localScope = e.workspaceScope + } else { + localScope = new(treeNodes.Scope).Initialize() + } localScope.OuterScope = e.globalScope if e.globalScope.IsDirty() || programNode.GetLastValue() == nil { diff --git a/main.go b/main.go index 9d80707..01c97d1 100644 --- a/main.go +++ b/main.go @@ -5,15 +5,31 @@ import ( "fmt" . "github.com/SealNTibbers/GotalkInterpreter/evaluator" "os" + "os/signal" "strings" + "syscall" ) +// SetupCloseHandler creates a 'listener' on a new goroutine which will notify the +// program if it receives an interrupt from the OS. We then handle this by calling +// our clean up procedure and exiting the program. +func SetupCloseHandler() { + c := make(chan os.Signal, 2) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + fmt.Println("\r- Ctrl+C pressed in Terminal") + os.Exit(0) + }() +} + func main() { - vm := NewSmalltalkVM() + vm := NewSmalltalkWorkspace() consoleReader := bufio.NewReader(os.Stdin) fmt.Println("Hello seeker!") + SetupCloseHandler() - for true { + for { input, err := consoleReader.ReadString('\n') // this will prompt the user for input if err == nil { input := strings.Split(input, "\n")[0] diff --git a/treeNodes/nodesEvaluation.go b/treeNodes/nodesEvaluation.go index 0facb15..8d16a1e 100644 --- a/treeNodes/nodesEvaluation.go +++ b/treeNodes/nodesEvaluation.go @@ -1,8 +1,8 @@ package treeNodes import ( - "github.com/SealNTibbers/GotalkInterpreter/scanner" "fmt" + "github.com/SealNTibbers/GotalkInterpreter/scanner" "strconv" )