-
Notifications
You must be signed in to change notification settings - Fork 171
/
repl.go
78 lines (63 loc) · 1.85 KB
/
repl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package vm
import (
"fmt"
"github.com/goby-lang/goby/compiler/bytecode"
)
// InitForREPL does following things:
// - Initialize instruction sets' index tables
// - Set vm to REPL mode
// - Create and push main object frame
func (vm *VM) InitForREPL() {
vm.SetClassISIndexTable("REPL")
vm.SetMethodISIndexTable("REPL")
// REPL should maintain a base call frame so that the whole program won't exit
cf := newNormalCallFrame(&instructionSet{name: "REPL base"}, "REPL", 1)
cf.self = vm.mainObj
vm.mode = REPLMode
vm.mainThread.callFrameStack.push(cf)
}
// REPLExec executes instructions differently from normal program execution.
func (vm *VM) REPLExec(sets []*bytecode.InstructionSet) {
p := newInstructionTranslator("REPL")
p.vm = vm
p.transferInstructionSets(sets)
for setType, table := range p.setTable {
for name, is := range table {
vm.isTables[setType][name] = is
}
}
vm.blockTables[p.filename] = p.blockTable
oldFrame := vm.mainThread.callFrameStack.pop()
cf := newNormalCallFrame(p.program, p.filename, oldFrame.SourceLine())
cf.self = vm.mainObj
cf.locals = oldFrame.Locals()
cf.ep = oldFrame.EP()
cf.isBlock = oldFrame.IsBlock()
cf.isSourceBlock = oldFrame.IsSourceBlock()
cf.self = oldFrame.Self()
cf.lPr = oldFrame.LocalPtr()
vm.mainThread.callFrameStack.push(cf)
defer func() {
_, ok := recover().(*Error)
if !ok && recover() != nil {
fmt.Printf("%v\n", recover())
}
}()
vm.mainThread.startFromTopFrame()
}
// GetExecResult returns stack's top most value. Normally it's used in tests.
func (vm *VM) GetExecResult() Object {
top := vm.mainThread.Stack.top()
if top != nil {
return top.Target
}
return NULL
}
// GetREPLResult returns strings that should be showed after each evaluation.
func (vm *VM) GetREPLResult() string {
top := vm.mainThread.Stack.Pop()
if top != nil {
return top.Target.toString()
}
return ""
}