monna/repl/repl.go
tijani 3b013cd917 Formatting changes.
REPL can now use the parser.

NOTE: REPL currently outputs the parsed tokens because tracing is still turned on.

git-svn-id: https://svn.tlawal.org/svn/monkey@41 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
2022-08-01 16:13:40 +00:00

59 lines
1.1 KiB
Go

package repl
import (
"bufio"
"fmt"
"io"
"monkey/lexer"
"monkey/parser"
)
const MONKEY_FACE = ` __,__
.--. .-" "-. .--.
/ .. \/ .-. .-. \/ .. \
| | '| / Y \ |' | |
| \ \ \ 0 | 0 / / / |
\ '- ,\.-"""""""-./, -' /
''-' /_ ^ ^ _\ '-''
| \._ _./ |
\ \ '~' / /
'._ '-=-' _.'
'-----'
`
const PROMPT = ">> "
func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
for {
fmt.Fprintf(out, PROMPT)
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()
l_lexer := lexer.New(line)
l_parser := parser.New(l_lexer)
program := l_parser.ParseProgram()
if len(l_parser.Errors()) != 0 {
print_parser_errors(out, l_parser.Errors())
continue
}
io.WriteString(out, program.String())
io.WriteString(out, "\n")
}
}
func print_parser_errors(out io.Writer, errors []string) {
io.WriteString(out, MONKEY_FACE)
io.WriteString(out, "Woops! I ran into some monkey business here!\n")
io.WriteString(out, " parser errors:\n")
for _, message := range errors {
io.WriteString(out, "\t"+message+"\n")
}
}