-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
215 lines (185 loc) · 3.87 KB
/
main.go
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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
func main() {
Main(os.Args, os.Stdout)
}
func Main(s []string, w io.Writer) {
log := ioutil.Discard
file, err := os.Open(s[1])
if err != nil {
panic(err)
}
defer file.Close()
b := bufio.NewReader(file)
var (
tokens []Token
line int
col int
cur []byte
skipTokens int
vars map[string]interface{}
)
vars = make(map[string]interface{})
line = 1
for {
fmt.Fprintln(log, "ReadByte")
c, err := b.ReadByte()
if err != nil {
fmt.Fprintln(log, "Top EOF")
if err == io.EOF {
if len(cur) > 0 {
tokens = append(tokens, Token{
Pos: Pos{
Line: line,
Col: col - (len(cur) - 1),
},
Type: TTypeFromBytes(cur),
Value: cur,
})
}
break
}
panic(err)
}
fmt.Fprintln(log, "Incr IDX by 1")
col++
fmt.Fprintln(log, "Switch c")
switch c {
case '"':
fmt.Fprintln(log, "Case \"")
fmt.Fprintf(log, "Read to %s\n", string(c))
toToken, err := b.ReadBytes(c)
if err != nil {
if err == io.EOF {
panic(fmt.Sprintf("could not find matching quote at line %d, col %d", line, col))
}
panic(err)
}
fmt.Fprintf(log, "Incr IDX by %d\n", len(toToken))
col += len(toToken)
fmt.Fprintf(log, "Append %s to tokens\n", toToken[:len(toToken)-1])
tokens = append(tokens, Token{
Pos: Pos{
Line: line,
Col: col - len(toToken),
},
Type: STRING,
Value: toToken[:len(toToken)-1],
})
continue
case ' ', '\t', '\n':
fmt.Fprintln(log, "Case space, \\n or \\t")
fmt.Fprintf(log, "Append %s to tokens\n", cur)
if len(cur) > 0 {
tokens = append(tokens, Token{
Pos: Pos{
Line: line,
Col: col - (len(cur) + 1),
},
Type: TTypeFromBytes(cur),
Value: cur,
})
}
cur = make([]byte, 0)
if c == '\n' {
line++
col = 0
}
continue
}
fmt.Fprintf(log, "Append %s to cur\n", string(c))
cur = append(cur, c)
}
fmt.Fprintf(log, "%s\n", tokens)
for idx, b := range tokens {
switch b.Type {
case VAR:
if len(tokens) <= idx+2 {
panic(fmt.Sprintf("expected IDENT, got EOF at %s", b.Pos))
}
if tokens[idx+1].Type != IDENT {
panic(fmt.Sprintf("expected IDENT, got %s at %s", tokens[idx+1].Type, b.Pos))
}
if tokens[idx+2].Type != STRING {
panic(fmt.Sprintf("expected IDENT, got %s at %s", tokens[idx+2].Type, b.Pos))
}
vars[string(tokens[idx+1].Value)] = tokens[idx+2].Value
skipTokens = 2
case P:
print(w, tokens, idx, b, vars, "")
skipTokens = 1
case PLN:
print(w, tokens, idx, b, vars, "\n")
skipTokens = 1
default:
if skipTokens > 0 {
skipTokens--
continue
}
panic(fmt.Sprintf("unexpected token at %s", b.Pos))
}
}
}
func print(w io.Writer, tokens []Token, idx int, b Token, vars map[string]interface{}, suffix string) {
if len(tokens) <= idx+1 {
panic(fmt.Sprintf("expected IDENT, got EOF at %s", b.Pos))
}
switch tokens[idx+1].Type {
case STRING:
fmt.Fprintf(w, "%s%s", tokens[idx+1].Value, suffix)
case IDENT:
fmt.Fprintf(w, "%s%s", vars[string(tokens[idx+1].Value)], suffix)
default:
panic(fmt.Sprintf("expected STRING, got %s at %s", tokens[idx+1].Type, b.Pos))
}
}
type TType int
const (
IDENT TType = iota
STRING
P
PLN
VAR
)
var stringToTType = map[string]TType{
"p": P,
"pln": PLN,
"var": VAR,
}
var tTypeToString = map[TType]string{
P: "p",
PLN: "pln",
IDENT: "IDENT",
STRING: "string",
VAR: "var",
}
func TTypeFromBytes(b []byte) TType {
if tt, ok := stringToTType[string(b)]; ok {
return tt
}
return IDENT
}
func (t TType) String() string {
if str, ok := tTypeToString[t]; ok {
return str
}
return "unknown"
}
type Token struct {
Pos
Type TType
Value []byte
}
type Pos struct {
Line int
Col int
}
func (p Pos) String() string {
return fmt.Sprintf("line %d, col %d", p.Line, p.Col)
}