blob: bd587decb0cc7ab941bdc9eee80322bded5f0e92 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package parser
import (
"fmt"
"mana/ast"
"mana/lexer"
"mana/tokens"
)
// Parser represents a parser.
type Parser struct {
l *lexer.Lexer
curToken tokens.Token
peekToken tokens.Token
errors []string
}
// New returns a new Parser.
func New(l *lexer.Lexer) *Parser {
var p *Parser = &Parser{
l: l,
errors: []string{},
}
// Read two tokens, so curToken and peekToken are both set.
p.nextToken()
p.nextToken()
return p
}
// nextToken advances the tokens.
func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}
// ParseProgram parses a program.
func (p *Parser) ParseProgram() *ast.Program {
// Initialize the program with an empty slice of statements.
var program *ast.Program = &ast.Program{}
program.Statements = []ast.Statement{}
// Iterate over the tokens until we reach the end of file.
for p.curToken.Type != tokens.EOF {
// Parse the statement and append it to the program.
var stmt ast.Statement = p.parseStatement()
if stmt != nil {
program.Statements = append(program.Statements, stmt)
}
// Advance to the next token.
p.nextToken()
}
// Return the program.
return program
}
// parseStatement parses a statement.
func (p *Parser) parseStatement() ast.Statement {
switch p.curToken.Type {
case tokens.LET:
return p.parseLetStatement()
case tokens.RETURN:
return p.parseReturnStatement()
default:
return nil
}
}
// parseLetStatement parses a let statement.
func (p *Parser) parseLetStatement() *ast.LetStatement {
var stmt *ast.LetStatement = &ast.LetStatement{Token: p.curToken}
if !p.expectPeek(tokens.IDENT) {
return nil
}
stmt.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
if !p.expectPeek(tokens.ASSIGN) {
return nil
}
// TODO: We're skipping the expressions until we
// encounter a semicolon.
for !p.curTokenIs(tokens.SEMICOLON) {
p.nextToken()
}
return stmt
}
// parseReturnStatement parses a return statement.
func (p *Parser) parseReturnStatement() *ast.ReturnStatement {
var stmt *ast.ReturnStatement = &ast.ReturnStatement{Token: p.curToken}
p.nextToken()
// TODO: We're skipping the expressions until we
// encounter a semicolon.
for !p.curTokenIs(tokens.SEMICOLON) {
p.nextToken()
}
return stmt
}
// curTokenIs returns true if the current token is of the given type.
func (p *Parser) curTokenIs(t tokens.TokenType) bool {
return p.curToken.Type == t
}
// peekTokenIs returns true if the next token is of the given type.
func (p *Parser) peekTokenIs(t tokens.TokenType) bool {
return p.peekToken.Type == t
}
// expectPeek expects the next token to be of the given type.
func (p *Parser) expectPeek(t tokens.TokenType) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
} else {
p.peekError(t)
return false
}
}
// Errors returns the parser errors.
func (p *Parser) Errors() []string {
return p.errors
}
// peekError returns an error message.
func (p *Parser) peekError(t tokens.TokenType) {
var msg string = fmt.Sprintf("expected next token to be %s, got %s instead", t, p.peekToken.Type)
p.errors = append(p.errors, msg)
}
|