Fix up REPL
This commit is contained in:
34
repl/repl.go
34
repl/repl.go
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"monkey/lexer"
|
||||
"monkey/token"
|
||||
"monkey/parser"
|
||||
)
|
||||
|
||||
const PROMPT = ">> "
|
||||
@@ -22,9 +22,37 @@ func Start(in io.Reader, out io.Writer) {
|
||||
|
||||
line := scanner.Text()
|
||||
l := lexer.New(line)
|
||||
p := parser.New(l)
|
||||
|
||||
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
|
||||
fmt.Fprintf(out, "%+v\n", tok)
|
||||
program := p.ParseProgram()
|
||||
if len(p.Errors()) != 0 {
|
||||
printParserErrors(out, p.Errors())
|
||||
continue
|
||||
}
|
||||
|
||||
io.WriteString(out, program.String())
|
||||
io.WriteString(out, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
const MONKEY_FACE = ` __,__
|
||||
.--. .-" "-. .--.
|
||||
/ .. \/ .-. .-. \/ .. \
|
||||
| | '| / Y \ |' | |
|
||||
| \ \ \ 0 | 0 / / / |
|
||||
\ '- ,\.-"""""""-./, -' /
|
||||
''-' /_ ^ ^ _\ '-''
|
||||
| \._ _./ |
|
||||
\ \ '~' / /
|
||||
'._ '-=-' _.'
|
||||
'-----'
|
||||
`
|
||||
|
||||
func printParserErrors(out io.Writer, errors []string) {
|
||||
io.WriteString(out, MONKEY_FACE)
|
||||
io.WriteString(out, "Woops! We ran into some monkey business here!\n")
|
||||
io.WriteString(out, " parser errors:\n")
|
||||
for _, msg := range errors {
|
||||
io.WriteString(out, "\t"+msg+"\t")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user