Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea/ | ||
monkey_interpreter.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ast | ||
|
||
import "monkey_interpreter/token" | ||
|
||
type Node interface { | ||
TokenLiteral() string | ||
} | ||
|
||
type Statement interface { | ||
Node | ||
statementNode() | ||
} | ||
|
||
type Expression interface { | ||
Node | ||
expressionNode() | ||
} | ||
|
||
type Program struct { | ||
Statements []Statement | ||
} | ||
|
||
func (p *Program) TokenLiteral() string { | ||
if len(p.Statements) > 0 { | ||
return p.Statements[0].TokenLiteral() | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
type LetStatement struct { | ||
Token token.Token // the token.LET token | ||
Name *Identifier | ||
Value Expression | ||
} | ||
|
||
func (ls *LetStatement) statementNode() {} | ||
func (ls *LetStatement) TokenLiteral() string { return ls.Token.Literal } | ||
|
||
type Identifier struct { | ||
Token token.Token // the token.IDENT token | ||
Value string | ||
} | ||
|
||
func (i *Identifier) statementNode() {} | ||
func (i *Identifier) TokenLiteral() string { return i.Token.Literal } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package parser | ||
|
||
import ( | ||
"monkey_interpreter/ast" | ||
"monkey_interpreter/lexer" | ||
"monkey_interpreter/token" | ||
) | ||
|
||
type Parser struct { | ||
l *lexer.Lexer | ||
|
||
curToken token.Token | ||
peekToken token.Token | ||
} | ||
|
||
func New(l *lexer.Lexer) *Parser { | ||
p := &Parser{l: l} | ||
|
||
// Read two tokens, curToken and peekToken are both set | ||
p.nextToken() | ||
p.nextToken() | ||
|
||
return p | ||
} | ||
|
||
func (p *Parser) nextToken() { | ||
p.curToken = p.peekToken | ||
p.peekToken = p.l.NextToken() | ||
} | ||
|
||
func (p *Parser) ParseProgram() *ast.Program { | ||
program := &ast.Program{} | ||
program.Statements = []ast.Statement{} | ||
|
||
for p.curToken.Type != token.EOF { | ||
stmt := p.ParseStatement() | ||
if stmt != nil { | ||
program.Statements = append(program.Statements, stmt) | ||
} | ||
p.nextToken() | ||
} | ||
|
||
return program | ||
} | ||
|
||
func (p *Parser) ParseStatement() ast.Statement { | ||
switch p.curToken.Type { | ||
case token.LET: | ||
return p.ParseLetStatement() | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (p *Parser) ParseLetStatement() ast.Statement { | ||
stmt := &ast.LetStatement{Token: p.curToken} | ||
|
||
if !p.expectPeek(token.IDENT) { | ||
return nil | ||
} | ||
|
||
stmt.Name = &ast.Identifier{ | ||
Token: p.curToken, | ||
Value: p.curToken.Literal, | ||
} | ||
|
||
if !p.expectPeek(token.ASSIGN) { | ||
return nil | ||
} | ||
|
||
for !p.curTokenIs(token.SEMICOLON) { | ||
p.nextToken() | ||
} | ||
|
||
return stmt | ||
} | ||
|
||
func (p *Parser) curTokenIs(t token.TokenType) bool { | ||
return p.curToken.Type == t | ||
} | ||
|
||
func (p *Parser) peekTokenIs(t token.TokenType) bool { | ||
return p.peekToken.Type == t | ||
} | ||
|
||
func (p *Parser) expectPeek(t token.TokenType) bool { | ||
if p.peekTokenIs(t) { | ||
p.nextToken() | ||
return true | ||
} else { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package parser | ||
|
||
import ( | ||
"monkey_interpreter/ast" | ||
"monkey_interpreter/lexer" | ||
"testing" | ||
) | ||
|
||
func TestLetStatements(t *testing.T) { | ||
input := ` | ||
let x = 5; | ||
let y = 10; | ||
let foobar = 838383; | ||
` | ||
l := lexer.New(input) | ||
p := New(l) | ||
|
||
program := p.ParseProgram() | ||
if program == nil { | ||
t.Fatalf("ParseProgram() returned nil") | ||
} | ||
if len(program.Statements) != 3 { | ||
t.Fatalf("program.Statements does not contain 3 statements. got=%d", len(program.Statements)) | ||
} | ||
|
||
tests := []struct { | ||
expectedIdentifier string | ||
}{ | ||
{"x"}, | ||
{"y"}, | ||
{"foobar"}, | ||
} | ||
|
||
for i, tt := range tests { | ||
stmt := program.Statements[i] | ||
if !testLetStatement(t, stmt, tt.expectedIdentifier) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testLetStatement(t *testing.T, s ast.Statement, name string) bool { | ||
if s.TokenLiteral() != "let" { | ||
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral()) | ||
return false | ||
} | ||
letStmt, ok := s.(*ast.LetStatement) | ||
if !ok { | ||
t.Errorf("s not *ast.LetStatement. got=%T", s) | ||
return false | ||
} | ||
if letStmt.Name.Value != name { | ||
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value) | ||
return false | ||
} | ||
if letStmt.Name.TokenLiteral() != name { | ||
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s", | ||
name, letStmt.Name.TokenLiteral()) | ||
return false | ||
} | ||
return true | ||
} |