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
This commit is contained in:
parent
e5d2851c2e
commit
3b013cd917
37
repl/repl.go
37
repl/repl.go
@ -4,11 +4,23 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"monkey/token"
|
"monkey/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MONKEY_FACE = ` __,__
|
||||||
|
.--. .-" "-. .--.
|
||||||
|
/ .. \/ .-. .-. \/ .. \
|
||||||
|
| | '| / Y \ |' | |
|
||||||
|
| \ \ \ 0 | 0 / / / |
|
||||||
|
\ '- ,\.-"""""""-./, -' /
|
||||||
|
''-' /_ ^ ^ _\ '-''
|
||||||
|
| \._ _./ |
|
||||||
|
\ \ '~' / /
|
||||||
|
'._ '-=-' _.'
|
||||||
|
'-----'
|
||||||
|
`
|
||||||
|
|
||||||
const PROMPT = ">> "
|
const PROMPT = ">> "
|
||||||
|
|
||||||
func Start(in io.Reader, out io.Writer) {
|
func Start(in io.Reader, out io.Writer) {
|
||||||
@ -22,10 +34,25 @@ func Start(in io.Reader, out io.Writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
l := lexer.New(line)
|
l_lexer := lexer.New(line)
|
||||||
|
l_parser := parser.New(l_lexer)
|
||||||
|
program := l_parser.ParseProgram()
|
||||||
|
|
||||||
for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
|
if len(l_parser.Errors()) != 0 {
|
||||||
fmt.Fprintf(out, "%+v\n", tok)
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user