Skip to content

Commit

Permalink
Added workspace mode into evaluator and #SetupCloseHandler method int…
Browse files Browse the repository at this point in the history
…o console app.
  • Loading branch information
Alexander K committed Nov 9, 2018
1 parent fdcfb22 commit 4a64966
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
19 changes: 16 additions & 3 deletions evaluator/smalltalkEvaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion treeNodes/nodesEvaluation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package treeNodes

import (
"github.com/SealNTibbers/GotalkInterpreter/scanner"
"fmt"
"github.com/SealNTibbers/GotalkInterpreter/scanner"
"strconv"
)

Expand Down

0 comments on commit 4a64966

Please sign in to comment.