Skip to content

Commit

Permalink
2.9: Implement Read-Parse-Print-Loop (RPPL)
Browse files Browse the repository at this point in the history
  • Loading branch information
nibral committed Jan 13, 2019
1 parent dd38cef commit ea32869
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions ast/ast.go
Expand Up @@ -66,6 +66,7 @@ func (ls *LetStatement) String() string {
out.WriteString(ls.TokenLiteral() + " ")
out.WriteString(ls.Name.String())
out.WriteString(" = ")
out.WriteString(ls.Value.String())

return out.String()
}
Expand Down
17 changes: 14 additions & 3 deletions repl/repl.go
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"
"monkey_interpreter/lexer"
"monkey_interpreter/token"
"monkey_interpreter/parser"
)

const PROMPT = ">> "
Expand All @@ -23,9 +23,20 @@ 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())
}

io.WriteString(out, program.String())
io.WriteString(out, "\n")
}
}

func printParserErrors(out io.Writer, errors []string) {
for _, msg := range errors {
io.WriteString(out, "\t"+msg+"\n")
}
}

0 comments on commit ea32869

Please sign in to comment.