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
217
218
219
220
221
222
|
package main
import (
"fmt"
"main/subex"
"strings"
"unicode/utf8"
)
type parser struct {
tokenStream chan Token
rewinds []Token
labels map[rune]int
}
func (p *parser) next() Token {
var token Token
if len(p.rewinds) == 0 {
token = <- p.tokenStream
} else {
token = p.rewinds[len(p.rewinds)-1]
p.rewinds = p.rewinds[:len(p.rewinds)-1]
}
if token.typ == TokenErr {
fmt.Println(token)
panic("Lexing error")
}
return token
}
func (p *parser) rewind(token Token) {
p.rewinds = append(p.rewinds, token)
}
func (p *parser) peek() Token {
token := p.next()
p.rewind(token)
return token
}
func (p *parser) parseSubex() subex.SubexAST {
delim := p.next()
if delim.typ != TokenSubstituteDelimiter {
panic("Missing substitute delimiter")
}
subexProgramToken := p.next()
if subexProgramToken.typ != TokenSubex {
panic("Missing subex from substitution")
}
var subexProgram string
if delim.val == "=" || delim.val == "~" || delim.val == "\"" || delim.val == "`" || delim.val == "^" {
subexProgram = delim.val + subexProgramToken.val + delim.val
} else {
subexProgram = subexProgramToken.val
}
reader := subex.NewStringRuneReader(subexProgram)
subexAST := subex.Parse(reader)
delim = p.next()
if delim.typ != TokenSubstituteDelimiter {
panic("Missing end substitute delimiter")
}
return subexAST
}
func (p *parser) parseBasicCommand(commands []Command, commandChar rune) []Command {
switch commandChar {
case 'p':
return append(commands, PrintValueCommand{})
case 'd':
return append(commands, DeleteValueCommand{})
case 'D':
return append(commands, DeletePathCommand{})
case 'n':
return append(commands, NextCommand{})
case 'N':
return append(commands, AppendNextCommand{})
case 's', 'S', 'f', 'F', 'l', 'L', 'a', 'A':
ast := p.parseSubex()
switch commandChar {
case 'f':
ast = subex.SubexASTConcat {
First: ast,
Second: subex.SubexASTRepeat {
Content: subex.SubexASTCopyAny{},
Acceptable: []subex.ConvexRange{{Start: -1, End: 0}},
},
}
case 'F':
ast = subex.SubexASTConcat {
First: subex.SubexASTStore {
Slot: '_',
Match: ast,
},
Second: subex.SubexASTRepeat {
Content: subex.SubexASTCopyAny{},
Acceptable: []subex.ConvexRange{{Start: -1, End: 0}},
},
}
case 'l':
ast = subex.SubexASTConcat {
First: subex.SubexASTRepeat {
Content: subex.SubexASTCopyAny{},
Acceptable: []subex.ConvexRange{{Start: 0, End: -1}},
},
Second: ast,
}
case 'L':
ast = subex.SubexASTConcat {
First: subex.SubexASTRepeat {
Content: subex.SubexASTCopyAny{},
Acceptable: []subex.ConvexRange{{Start: 0, End: -1}},
},
Second: subex.SubexASTStore {
Slot: '_',
Match: ast,
},
}
case 'a', 'A':
ast = subex.SubexASTRepeat {
Acceptable: []subex.ConvexRange{{Start: -1, End: 0}},
Content: subex.SubexASTOr {
First: ast,
Second: subex.SubexASTCopyAny{},
},
}
}
subex := subex.CompileTransducer(ast)
switch commandChar {
case 's', 'a':
return append(commands, SubstituteValueCommand {subex}, JumpCommand {len(commands) + 3})
case 'S', 'f', 'F', 'l', 'L', 'A':
return append(commands, SubstitutePathCommand {subex}, JumpCommand {len(commands) + 3})
default:
panic("Unreachable!?!?")
}
case 'o':
return append(commands, NoopCommand{})
case 'x':
return append(commands, SwapXRegCommand{})
case 'X':
return append(commands, AppendXRegCommand{})
case 'y':
return append(commands, SwapYRegCommand{})
case 'Y':
return append(commands, AppendYRegCommand{})
case 'z':
return append(commands, SwapZRegCommand{})
case 'Z':
return append(commands, AppendZRegCommand{})
case 'k':
return append(commands, SwapPathCommand{})
case 'K':
return append(commands, AppendPathCommand{})
case ':':
labelToken := p.next()
if labelToken.typ != TokenLabel {
panic("Missing branch label")
}
label, _ := utf8.DecodeRuneInString(labelToken.val)
p.labels[label] = len(commands)
return commands
case 'b':
labelToken := p.next()
if labelToken.typ != TokenLabel {
panic("Missing branch label")
}
label, _ := utf8.DecodeRuneInString(labelToken.val)
return append(commands, BranchPlaceholderCommand {label})
default:
panic("Invalid command")
}
}
func (p *parser) parseCommand(commands []Command) []Command {
token := p.next()
switch token.typ {
case TokenLBrace:
jumpToBlockCommand := &JumpCommand{0}
commands = append(commands, JumpCommand {len(commands) + 2}, jumpToBlockCommand)
commands = p.parseCommands(commands)
if p.next().typ != TokenRBrace {
panic("Missing matching }")
}
jumpToBlockCommand.destination = len(commands)
return commands
case TokenCommand:
commandChar, _, err := strings.NewReader(token.val).ReadRune()
if err != nil {
panic("Error reading a command character!?")
}
return p.parseBasicCommand(commands, commandChar)
default:
panic("Invalid token, expected command")
}
}
func (p *parser) parseCommands(commands []Command) []Command {
for {
nextToken := p.peek()
if nextToken.typ == TokenEOF || nextToken.typ == TokenRBrace {
return commands
}
commands = p.parseCommand(commands)
}
}
func Parse(tokens chan Token) []Command {
p := parser {
tokenStream: tokens,
rewinds: nil,
labels: make(map[rune]int),
}
program := p.parseCommands(nil)
for i, command := range program {
switch branch := command.(type) {
case BranchPlaceholderCommand:
destination, exists := p.labels[branch.label]
if !exists {
panic("Tried to branch to a label that doesn't exist")
}
program[i] = JumpCommand {destination}
}
}
return program
}
|