This repository has been archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.go
192 lines (174 loc) · 3.53 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/peterh/liner"
eval "github.com/sbinet/go-eval"
)
var fset = token.NewFileSet()
var filename = flag.String("f", "", "file to run")
var histname string
var term *liner.State = nil
func init() {
fmt.Println(`
********************************
** Interactive Go interpreter **
********************************
`)
term = liner.NewLiner()
histname = filepath.Join(os.Getenv("HOME"), ".go.history")
fname := histname
f, err := os.Open(fname)
if err != nil {
fmt.Printf("**warning: could not access history file [%s]\n", fname)
return
}
defer f.Close()
_, err = term.ReadHistory(f)
if err != nil {
fmt.Printf("**warning: could not read history file [%s]\n", fname)
return
}
}
func atexit() {
fname := histname
f, err := os.OpenFile(fname, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("**warning: could not access history file [%s]\n", fname)
return
}
defer f.Close()
_, err = term.WriteHistory(f)
if err != nil {
fmt.Printf("**warning: could not write history file [%s]\n", fname)
return
}
err = term.Close()
if err != nil {
fmt.Printf("**warning: problem closing term: %v\n", err)
return
}
}
func main() {
rc := run()
os.Exit(rc)
}
func run() int {
defer atexit()
flag.Parse()
w := eval.NewWorld()
if *filename != "" {
data, err := ioutil.ReadFile(*filename)
if err != nil {
fmt.Println(err.Error())
return 1
}
file, err := parser.ParseFile(fset, *filename, data, 0)
if err != nil {
fmt.Println(err.Error())
return 1
}
files := []*ast.File{file}
code, err := w.CompilePackage(fset, files, "main")
if err != nil {
if list, ok := err.(scanner.ErrorList); ok {
for _, e := range list {
fmt.Println(e.Error())
}
} else {
fmt.Println(err.Error())
}
return 1
}
code, err = w.Compile(fset, "main()")
if err != nil {
fmt.Println(err.Error())
return 1
}
_, err = code.Run()
if err != nil {
fmt.Println(err.Error())
return 1
}
return 0
}
var ierr error = nil // previous interpreter error
ps1 := "igo> "
ps2 := "... "
prompt := &ps1
codelet := ""
// initialize main package
{
codelet := "package main\n"
f, err := parser.ParseFile(fset, "input", codelet, 0)
code, err := w.CompilePackage(fset, []*ast.File{f}, "main")
if err == nil {
code.Run()
}
}
for {
line, err := term.Prompt(*prompt)
if err != nil {
if err != io.EOF {
ierr = err
} else {
ierr = nil
}
break //os.Exit(0)
}
if line == "" || line == ";" {
// no more input
prompt = &ps1
}
codelet += line
if codelet != "" {
for _, ll := range strings.Split(codelet, "\n") {
term.AppendHistory(ll)
}
}
code, err := w.Compile(fset, codelet+";")
if err != nil {
if ierr != nil && prompt == &ps1 {
fmt.Println(err.Error())
fmt.Printf("(error %T)\n", err)
// reset state
codelet = ""
ierr = nil
prompt = &ps1
continue
}
// maybe multi-line command ?
prompt = &ps2
ierr = err
codelet += "\n"
continue
}
v, err := code.Run()
if err != nil {
fmt.Println(err.Error())
fmt.Printf("(error %T)\n", err)
codelet = ""
continue
}
if v != nil {
fmt.Println(v.String())
}
// resetstate:
// reset state
codelet = ""
ierr = nil
}
return 0
}