Add error message to the REPL
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
44f936affb
commit
acc6707740
1 changed files with 30 additions and 3 deletions
|
|
@ -6,7 +6,7 @@ import (
|
|||
"io"
|
||||
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/lexer"
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/token"
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/parser"
|
||||
)
|
||||
|
||||
const PROMPT = ">> "
|
||||
|
|
@ -19,8 +19,35 @@ func Start(in io.Reader, out io.Writer) {
|
|||
return
|
||||
}
|
||||
l := lexer.New(scanner.Text())
|
||||
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
|
||||
fmt.Fprintf(out, "%+v\n", tok)
|
||||
p := parser.New(l)
|
||||
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+"\n")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue