projects > from_scratch > interpreter.go
1
A Monkey interpreter, in Go[github]
1
2
Interpreter for the Monkey programming language, built in Go
3
following 'Writing An Interpreter In Go' by Thorsten Ball.
4
5
Features a lexer, parser, AST representation, and tree-walking
6
evaluator supporting let/return statements, closures, integers,
7
booleans, strings, arrays, and hash maps.
8
9
[Go][GitHub]
10
12
13
func Repl() {
14
in := os.Stdin
15
out := os.Stdout
16
scanner := bufio.NewScanner(in)
17
18
scope := object.NewGlobalScope()
19
fmt.Println("Hello bro! This is the Monkey programming language!")
20
fmt.Println("Feel free to type in commands:")
21
for {
22
fmt.Fprint(out, PROMPT)
23
scanned := scanner.Scan()
24
if !scanned {
25
return
26
}
27
28
line := scanner.Text()
29
if line == "q" || line == "quit" {
30
fmt.Printf("Bye bye!")
31
os.Exit(0)
32
}
33
l := lexer.New(line)
34
p := parser.New(l)
35
program := p.ParseProgram()
36
37
38
NORMAL~/oleksandr/projects/from_scratch/interpreter.go.../from_scratch/interpreter.gomainutf-8
Enter to open · :q to quit
keys
j / kmove the cursor line, or the tree cursor
hfocus the tree; in the tree, fold or go to the parent
l / Enterin the tree, unfold or open the file
gg / Gfirst / last line
clickput the cursor on a line; on README.sh, run the session again
:qquit to the shell
:help ?this table
q / Escclose this