Don't understand how it works.
git-svn-id: https://svn.tlawal.org/svn/monkey@11 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
parent
f8f7ec04c7
commit
d37bdfa644
@ -1,39 +0,0 @@
|
||||
/* NOTE(tijani):
|
||||
* l_ in variables names stand for local_ identifiers. The reason is because I do not like
|
||||
* single letter variable names, but I also do not want to confuse the myself
|
||||
* when I come back to reading this code after a while hence l_ to signify that it is local to that function.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"monkey/ast"
|
||||
"monkey/lexer"
|
||||
"monkey/token"
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
l_lexer *lexer.Lexer
|
||||
current_token token.Token
|
||||
peek_token token.Token
|
||||
}
|
||||
|
||||
func (l_parser *Parser) next_token(){
|
||||
l_parser.current_token = l_parser.peek_token
|
||||
l_parser.peek_token = l_parser.l_lexer.NextToken()
|
||||
}
|
||||
|
||||
func (p *Parser) ParseProgram() *ast.Program {
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(lexer *lexer.Lexer) *Parser {
|
||||
l_parser := &Parser {l_lexer: lexer}
|
||||
// Read two tokens so current_token and peek_token are set.
|
||||
// NOTE(tijani): the first time l_parser.next_token() is called, current_token and peek_token will be pointing to the same token.
|
||||
l_parser.next_token()
|
||||
l_parser.next_token()
|
||||
|
||||
return l_parser
|
||||
}
|
||||
|
@ -1,64 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"monkey/ast"
|
||||
"monkey/lexer"
|
||||
)
|
||||
|
||||
func TestLetStatement(t *testing.T) {
|
||||
input := `
|
||||
let x = 5;
|
||||
let y = 10;
|
||||
let foobar = 878688;
|
||||
`
|
||||
l_lexer := lexer.New(input)
|
||||
l_parser := New(l_lexer)
|
||||
|
||||
program := l_parser.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
|
||||
}
|
Loading…
Reference in New Issue
Block a user