-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
56 lines (43 loc) · 1005 Bytes
/
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/user"
"github.com/radlinskii/interpreter/object"
"github.com/radlinskii/interpreter/evaluator"
"github.com/radlinskii/interpreter/lexer"
"github.com/radlinskii/interpreter/parser"
)
// PROMPT defines how the REPL's prompt will look like.
const PROMPT = "👉 "
// Start runs the REPL loop.
func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
env := object.NewEnvironment()
for {
fmt.Printf(PROMPT)
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()
l := lexer.New(line)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) == 0 {
evaluated := evaluator.EvalProgram(program, env)
fmt.Println(evaluated)
}
}
}
func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Monkey programming language!\n", user.Username)
fmt.Println("Feel free to type in commands")
Start(os.Stdin, os.Stdout)
}