Fix up REPL
This commit is contained in:
34
repl/repl.go
34
repl/repl.go
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"monkey/token"
|
"monkey/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PROMPT = ">> "
|
const PROMPT = ">> "
|
||||||
@@ -22,9 +22,37 @@ func Start(in io.Reader, out io.Writer) {
|
|||||||
|
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
l := lexer.New(line)
|
l := lexer.New(line)
|
||||||
|
p := parser.New(l)
|
||||||
|
|
||||||
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
|
program := p.ParseProgram()
|
||||||
fmt.Fprintf(out, "%+v\n", tok)
|
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