-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.go
141 lines (124 loc) · 3.05 KB
/
interpreter.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Interpreter is a scheme source code interpreter.
// It owns a role of API for executing scheme program.
// Interpreter embeds Parser and delegates syntactic analysis to it.
package scheme
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type Interpreter struct {
*Parser
closure *Closure
}
func NewInterpreter(source string) *Interpreter {
i := &Interpreter{
Parser: NewParser(source),
closure: &Closure{
ObjectBase: ObjectBase{parent: nil},
localBinding: defaultBinding(),
},
}
i.loadBuiltinLibrary("builtin")
return i
}
func (i *Interpreter) PrintResults(dumpAST bool) {
results := i.EvalResults(dumpAST)
if dumpAST {
fmt.Printf("\n*** Result ***\n")
}
for _, result := range results {
fmt.Println(result)
}
}
func (i *Interpreter) PrintErrors(dumpAST bool) {
results := i.EvalResults(dumpAST)
if dumpAST {
fmt.Printf("\n*** Result ***\n")
}
re, err := regexp.Compile("^\\*\\*\\* ERROR:")
if err != nil {
panic(err)
}
for _, result := range results {
if re.MatchString(result) {
fmt.Println(result)
}
}
}
func (i *Interpreter) EvalResults(dumpAST bool) (results []string) {
defer func() {
if err := recover(); err != nil {
results = append(results, fmt.Sprintf("*** ERROR: %s", err))
}
}()
i.Peek()
for _, e := range i.Parser.Parse(i.closure) {
if dumpAST {
fmt.Printf("\n*** AST ***\n")
i.DumpAST(e, 0)
}
results = append(results, e.Eval().String())
}
return
}
// Load new source code with current environment
func (i *Interpreter) ReloadSourceCode(source string) {
i.Parser = NewParser(source)
}
func (i *Interpreter) DumpAST(object Object, indentLevel int) {
if object == nil {
return
}
switch object.(type) {
case *Application:
i.printWithIndent("Application", indentLevel)
i.DumpAST(object.(*Application).procedure, indentLevel+1)
i.DumpAST(object.(*Application).arguments, indentLevel+1)
case *Pair:
pair := object.(*Pair)
if pair.Car == nil && pair.Cdr == nil {
i.printWithIndent("()", indentLevel)
return
}
i.printWithIndent("Pair", indentLevel)
i.DumpAST(pair.Car, indentLevel+1)
i.DumpAST(pair.Cdr, indentLevel+1)
case *String:
i.printWithIndent(fmt.Sprintf("String(%s)", object), indentLevel)
case *Number:
i.printWithIndent(fmt.Sprintf("Number(%s)", object), indentLevel)
case *Boolean:
i.printWithIndent(fmt.Sprintf("Boolean(%s)", object), indentLevel)
case *Variable:
i.printWithIndent(fmt.Sprintf("Variable(%s)", object.(*Variable).identifier), indentLevel)
}
}
func (i *Interpreter) printWithIndent(text string, indentLevel int) {
fmt.Printf("%s%s\n", strings.Repeat(" ", indentLevel), text)
}
func (i *Interpreter) loadBuiltinLibrary(name string) {
originalParser := i.Parser
buffer, err := ioutil.ReadFile(i.libraryPath(name))
if err != nil {
log.Fatal(err)
}
i.Parser = NewParser(string(buffer))
i.EvalResults(false)
i.Parser = originalParser
}
func (i *Interpreter) libraryPath(name string) string {
return filepath.Join(
os.Getenv("GOPATH"),
"src",
"github.com",
"k0kubun",
"gosick",
"lib",
name+".scm",
)
}