ast and parser

git-svn-id: https://svn.tlawal.org/svn/monkey@7 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
Tijani Lawal 2022-05-22 06:51:19 +00:00
parent 7a74beea8a
commit cf651aa1b8
4 changed files with 152 additions and 0 deletions

50
ast/ast.go Normal file
View File

@ -0,0 +1,50 @@
package ast
import "monkey/token"
type Node interface {
TokenLiteral() string
}
type Statement interface {
Node
statementNode()
}
type Expression interface {
Node
expressionNode()
}
type Identifier struct {
Token token.Token // the token.IDENT token
Value string
}
type Program struct {
Statements []Statement
}
type LetStatement struct {
Token token.Token // the token.LET token
Name *Identifier
Value Expression
}
func (ls *LetStatement) stamementNode() {}
func (ls *LetStatement) TokenLiteral() string {
return ls.Token.Literal
}
func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string {
return i.Token.Literal
}
func (p *Program) TokenLiteral() string {
if len(p.Statements) > 0 {
return p.Statements[0].TokenLiteral()
} else {
return ""
}
}

BIN
debug.rdbg Normal file

Binary file not shown.

39
parser/parser.go Normal file
View File

@ -0,0 +1,39 @@
/* 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
}

63
parser/parser_test.go Normal file
View File

@ -0,0 +1,63 @@
package parser
import (
"monkey/ast"
"monkey/lexer"
"testing"
)
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 {
expetedIdentifier 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 {
e.Errorf("letStmt.Name.TokenLiteral() not '%s', got=%s", name, letStmt.Name.TokenLiteral())
}
return true
}