aboutsummaryrefslogtreecommitdiff
path: root/repl
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-05-20 16:27:23 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-05-20 16:27:23 +0200
commitce78935009d931faf2db7e882929a7a4c95ce3e0 (patch)
tree47054996395b6e23b668f8c94cdb800bb308d79d /repl
parent562c190b394b0ad76d4d591be74e2f37513c964a (diff)
02
Diffstat (limited to 'repl')
-rw-r--r--repl/repl.go34
1 files changed, 31 insertions, 3 deletions
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")
}
}