-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
47 lines (37 loc) · 860 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
package repl
import (
"bufio"
"compiler/lexer"
parser "compiler/parser"
"fmt"
"io"
)
const PROMPT = "<meow>^..^<meow>"
//helper function for input and output values
func Start(in io.Reader, out io.Writer){
scanner := bufio.NewScanner(in)
for{
fmt.Printf(PROMPT)
scanned := scanner.Scan()
if !scanned{
return
}
line := scanner.Text()
lexer := lexer.New(line)
parser := parser.New(lexer)
program := parser.ParseProgram()
if len(parser.Errors()) != 0 {
printParserErrors(out, parser.Errors())
continue
}
io.WriteString(out, program.String())
io.WriteString(out, "\n")
}
}
func printParserErrors(out io.Writer, errors []string) {
io.WriteString(out, "Woops! WE ARE IN TROUBLES!\n")
io.WriteString(out, "Stack trace of parser errors:\n")
for _, msg := range errors {
io.WriteString(out, "\t"+msg+"\n")
}
}