From ce78935009d931faf2db7e882929a7a4c95ce3e0 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 20 May 2017 16:27:23 +0200 Subject: 02 --- repl/repl.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'repl') diff --git a/repl/repl.go b/repl/repl.go index 337be46..7fa3e42 100644 --- a/repl/repl.go +++ b/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.Printf("%+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+"\n") } } -- cgit v1.2.3