You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package repl
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
|
|
"gitea.paas.celticinfo.fr/oabrivard/monkeylang/monkey2/lexer"
|
|
"gitea.paas.celticinfo.fr/oabrivard/monkeylang/monkey2/parser"
|
|
)
|
|
|
|
const PROMPT = ">> "
|
|
|
|
func Start(in io.Reader, out io.Writer) {
|
|
scanner := bufio.NewScanner(in)
|
|
|
|
for {
|
|
fmt.Printf(PROMPT)
|
|
scanned := scanner.Scan()
|
|
if !scanned {
|
|
return
|
|
}
|
|
|
|
line := scanner.Text()
|
|
l := lexer.New(line)
|
|
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")
|
|
}
|
|
}
|