-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline.go
More file actions
69 lines (59 loc) · 1.22 KB
/
Copy pathinline.go
File metadata and controls
69 lines (59 loc) · 1.22 KB
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
package main
import (
"bufio"
"fmt"
"os"
"pepper/compiler"
"pepper/lexer"
"pepper/parser"
"pepper/runtime"
"strings"
"sync"
)
func Prompt() {
fmt.Println("Pepper REPL")
fmt.Println("Type .quit to exit")
fmt.Println("Type .run to run the program")
fmt.Println("Type .list to list the program")
fmt.Println("Type .clear to clear the program")
program := ""
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
line, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading input:", err)
return
}
line = strings.TrimSpace(line)
switch line {
case ".quit":
return
case ".run":
l := lexer.New(program)
p := parser.New(l)
parsedProgram := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, msg := range p.Errors() {
fmt.Fprintln(os.Stderr, msg)
}
continue
}
comp := compiler.NewCompiler()
instr := comp.Compile(parsedProgram)
if err != nil {
fmt.Fprintln(os.Stderr, "Woops! Compilation failed:", err)
continue
}
var wg sync.WaitGroup
vm := runtime.NewVM(instr, &wg)
vm.Run(false, false)
case ".list":
fmt.Println(program)
case ".clear":
program = ""
default:
program += line + "\n"
}
}
}