-
Notifications
You must be signed in to change notification settings - Fork 19
/
repl.go
62 lines (58 loc) · 1.43 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
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file provides a REPL.
package golisp
import (
"container/list"
"fmt"
)
func Repl() {
IsInteractive = true
fmt.Printf("Welcome to GoLisp 1.0\n")
fmt.Printf("Copyright 2015 SteelSeries\n")
fmt.Printf("Evaluate '(quit)' to exit.\n\n")
prompt := "> "
LoadHistoryFromFile(".golisp_history")
lastInput := ""
replEnv := NewSymbolTableFrameBelow(Global, "Repl")
for true {
defer func() {
if x := recover(); x != nil {
fmt.Printf("Don't Panic! %v\n", x)
}
}()
DebugCurrentFrame = nil
DebugSingleStep = false
DebugEvalInDebugRepl = false
replEnv.CurrentCode = list.New()
inputp := ReadLine(&prompt)
if inputp == nil {
QuitImpl(nil, nil)
} else {
input := *inputp
// fmt.Printf("input: <%s>\n", inputp)
if input != "" {
code, err := Parse(input)
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
if input != lastInput {
AddHistory(input)
lastInput = input
}
d, err := Eval(code, replEnv)
if err != nil {
fmt.Printf("Error in evaluation: %s\n", err)
if DebugOnError {
DebugRepl(DebugErrorEnv)
}
} else {
fmt.Printf("==> %s\n", String(d))
}
}
}
}
}
}