-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
216 lines (187 loc) · 5.75 KB
/
parser.go
File metadata and controls
216 lines (187 loc) · 5.75 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package parser
import (
"fmt"
"github.com/svader0/yarnball/pkg/lexer"
)
type Parser struct {
l *lexer.Lexer
cur, peek lexer.Token
}
func New(l *lexer.Lexer) *Parser {
p := &Parser{l: l}
p.nextToken() // Initialize current token
p.nextToken() // Initialize peek token
return p
}
// nextToken advances the parser to the next token, updating current and peek tokens.
func (p *Parser) nextToken() {
p.cur = p.peek
p.peek = p.l.NextToken()
}
// Parses the entire program, which consists of a sequence of instructions.
func (p *Parser) ParseProgram() (*Program, error) {
prog := &Program{}
for p.cur.Type != lexer.EOF {
instr, err := p.parseInstruction()
if err != nil {
return nil, err
}
prog.Instructions = append(prog.Instructions, instr)
}
return prog, nil
}
// Parses an instruction based on the current token type.
func (p *Parser) parseInstruction() (Instruction, error) {
switch p.cur.Type {
case lexer.SUBPATTERN:
return p.parseSubpattern()
case lexer.USE:
return p.parseUse()
case lexer.CH:
return p.parseCh()
case lexer.ASTERISK:
return p.parseRep()
case lexer.IDENT:
return &UseInstr{Name: p.cur.Literal}, nil
case lexer.IF:
return p.parseIf()
case lexer.SC, lexer.SLST, lexer.SWAP,
lexer.INC, lexer.DEC, lexer.BOB,
lexer.HDC, lexer.DC, lexer.TR, lexer.CL,
lexer.GREATERTHAN, lexer.LESSERTHAN, lexer.TURN,
lexer.EQUALS, lexer.NOTEQUALS,
lexer.YO, lexer.PIC, lexer.FO:
instr := &SimpleInstr{Token: p.cur.Literal}
p.nextToken() // consume the instruction token
return instr, nil
default:
return nil, fmt.Errorf("unexpected token %q at line %d", p.cur.Literal, p.cur.Line)
}
}
// parseCh parses the 'ch' instruction, which expects an INT argument, which is
// why it is separated from generic SimpleInstr parsing stuff
func (p *Parser) parseCh() (Instruction, error) {
instr := &SimpleInstr{Token: p.cur.Literal}
p.nextToken() // consume 'ch'
// If next token is INT, just store it
if p.cur.Type == lexer.INT {
instr.Args = append(instr.Args, p.cur.Literal)
p.nextToken() // consume INT
} else {
return nil, fmt.Errorf("expected INT after ch, got %s", p.cur.Literal)
}
return instr, nil
}
func (p *Parser) parseSubpattern() (Instruction, error) {
p.nextToken() // consume 'subpattern' keyword
if p.cur.Type != lexer.IDENT {
return nil, fmt.Errorf("expected subpattern name, got %s", p.cur.Literal)
}
pat := &SubpatternDef{Name: p.cur.Literal}
// Expect '='
if p.peek.Literal != "=" {
return nil, fmt.Errorf("expected '=', got %s", p.peek.Literal)
}
p.nextToken() // consume name
p.nextToken() // consume '='
if p.cur.Type != lexer.LPAREN {
return nil, fmt.Errorf("expected '(', got %s", p.cur.Literal)
}
p.nextToken() // consume '('
// Parse instructions until closing ')'
for p.cur.Type != lexer.RPAREN && p.cur.Type != lexer.EOF {
instr, err := p.parseInstruction()
if err != nil {
return nil, err
}
pat.Body = append(pat.Body, instr)
}
p.nextToken() // consume ')'
return pat, nil
}
func (p *Parser) parseUse() (Instruction, error) {
p.nextToken() // consume 'use'
if p.cur.Type != lexer.IDENT {
return nil, fmt.Errorf("expected subpattern name, got %s", p.cur.Literal)
}
use := &UseInstr{Name: p.cur.Literal}
p.nextToken() // advance past the IDENT token
return use, nil
}
// parseRep parses a rep block, which starts with '*' and contains a sequence of instructions
// until it hits a semicolon, followed by 'rep from *' and an optional INT count.
// It returns a RepInstr containing the body of instructions and the count expression.
func (p *Parser) parseRep() (Instruction, error) {
ri := &RepInstr{}
// Consume the '*' that starts the rep block.
p.nextToken()
// Parse the rep block body until we hit a semicolon.
for p.cur.Type != lexer.SEMICOLON && p.cur.Type != lexer.EOF {
instr, err := p.parseInstruction()
if err != nil {
return nil, err
}
ri.Body = append(ri.Body, instr)
}
if p.cur.Type != lexer.SEMICOLON {
return nil, fmt.Errorf("expected ';' after rep block, got %q at line %d", p.cur.Literal, p.cur.Line)
}
// Consume the semicolon.
p.nextToken()
// 'rep' keyword.
if p.cur.Type != lexer.REP {
return nil, fmt.Errorf("expected 'rep' after ';', got %q at line %d", p.cur.Literal, p.cur.Line)
}
p.nextToken() // Consume 'rep'
// Expect literal "from"
if p.cur.Literal != "from" {
return nil, fmt.Errorf("expected 'from' after 'rep', got %q at line %d", p.cur.Literal, p.cur.Line)
}
p.nextToken() // Consume "from"
if p.cur.Type != lexer.ASTERISK {
return nil, fmt.Errorf("expected '*' after 'from', got %q at line %d", p.cur.Literal, p.cur.Line)
}
p.nextToken() // Consume '*'
// consume INT count. (if present)
if p.cur.Type == lexer.INT {
ri.CountExpr = p.cur.Literal
p.nextToken()
}
// optional 'times' keyword
if p.cur.Type == lexer.IDENT && p.cur.Literal == "times" {
p.nextToken()
}
return ri, nil
}
func (p *Parser) parseIf() (Instruction, error) {
// Consume the 'if' token
p.nextToken()
var ifBody []Instruction
// Parse IF branch until we hit ELSE or END
for p.cur.Type != lexer.ELSE && p.cur.Type != lexer.END && p.cur.Type != lexer.EOF {
instr, err := p.parseInstruction()
if err != nil {
return nil, err
}
ifBody = append(ifBody, instr)
}
var elseBody []Instruction
// If an ELSE is encountered, parse ELSE branch
if p.cur.Type == lexer.ELSE {
p.nextToken() // consume 'else'
for p.cur.Type != lexer.END && p.cur.Type != lexer.EOF {
instr, err := p.parseInstruction()
if err != nil {
return nil, err
}
elseBody = append(elseBody, instr)
}
}
// Make sure we have an END token
if p.cur.Type != lexer.END {
return nil, fmt.Errorf("expected 'end' token, got %q at line %d", p.cur.Literal, p.cur.Line)
}
// Consume the END token
p.nextToken()
return &IfInstr{IfBody: ifBody, ElseBody: elseBody}, nil
}