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:
Tijani Lawal 2022-08-01 16:13:40 +00:00
parent e5d2851c2e
commit 3b013cd917
3 changed files with 41 additions and 14 deletions

View File

@ -176,21 +176,21 @@ func (l_parser *Parser) parse_statement() ast.Statement {
func (l_parser *Parser) parse_let_statement() *ast.LetStatement {
defer untrace(trace("parse_let_statement"))
statement := &ast.LetStatement{ Token: l_parser.current_token }
statement := &ast.LetStatement{Token: l_parser.current_token}
if !l_parser.expect_peek(token.IDENT){
if !l_parser.expect_peek(token.IDENT) {
return nil
}
statement.Name = &ast.Identifier{ Token: l_parser.current_token, Value: l_parser.current_token.Literal }
if !l_parser.expect_peek(token.ASSIGN){
statement.Name = &ast.Identifier{Token: l_parser.current_token, Value: l_parser.current_token.Literal}
if !l_parser.expect_peek(token.ASSIGN) {
return nil
}
l_parser.next_token()
statement.Value = l_parser.parse_expression(LOWEST)
if l_parser.peek_token_is(token.SEMICOLON){
if l_parser.peek_token_is(token.SEMICOLON) {
l_parser.next_token()
}
return statement

View File

@ -647,12 +647,12 @@ func TestLetStatements(l_test *testing.T) {
}
statement := program.Statements[0]
if !testLetStatement(l_test, statement, tt.expected_identifier){
if !testLetStatement(l_test, statement, tt.expected_identifier) {
return
}
val := statement.(*ast.LetStatement).Value
if !testLiteralExpression(l_test, val, tt.expected_value){
if !testLiteralExpression(l_test, val, tt.expected_value) {
return
}
}

View File

@ -4,11 +4,23 @@ import (
"bufio"
"fmt"
"io"
"monkey/lexer"
"monkey/token"
"monkey/parser"
)
const MONKEY_FACE = ` __,__
.--. .-" "-. .--.
/ .. \/ .-. .-. \/ .. \
| | '| / Y \ |' | |
| \ \ \ 0 | 0 / / / |
\ '- ,\.-"""""""-./, -' /
''-' /_ ^ ^ _\ '-''
| \._ _./ |
\ \ '~' / /
'._ '-=-' _.'
'-----'
`
const PROMPT = ">> "
func Start(in io.Reader, out io.Writer) {
@ -22,10 +34,25 @@ func Start(in io.Reader, out io.Writer) {
}
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() {
fmt.Fprintf(out, "%+v\n", tok)
if len(l_parser.Errors()) != 0 {
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")
}
}