-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
544 lines (511 loc) · 13.8 KB
/
parser.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
)
const (
DefaultBasename = "frame" // Default frame basename
FramesDirectory = "frames" // FramesDirectory is the directory containing all animation frames
MaxWorkers = 2 // maximum number of workers
)
var knobs map[string][]float64 // knob table
// Lighting
var ambient []float64 // ambient lighting
var lightSources map[string]LightSource // light table
var constants map[string][][]float64 // constants table
var formatString string // format string for each frame of the animation
func init() {
knobs = make(map[string][]float64)
lightSources = make(map[string]LightSource)
constants = make(map[string][][]float64)
}
// Parser is a script parser
type Parser struct {
lexer *Lexer // lexer
backup []Token // token backup
isAnimated bool // whether or not to parse as an animation
frames int // number of frames in the animation
basename string // animation basename
}
// NewParser returns a new parser
func NewParser() *Parser {
return &Parser{
backup: make([]Token, 0, 10),
isAnimated: false,
}
}
// ParseInput parses a file for commands and executes them
func (p *Parser) ParseInput() error {
scanner := bufio.NewScanner(os.Stdin)
var input bytes.Buffer
for scanner.Scan() {
input.Write(scanner.Bytes())
input.WriteRune('\n')
}
err := p.ParseString(input.String())
return err
}
// ParseFile parses a file for commands and executes them
func (p *Parser) ParseFile(filename string) error {
input, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = p.ParseString(string(input))
return err
}
// ParseString parses a string for commands and executes them
func (p *Parser) ParseString(input string) error {
p.lexer = Lex(input)
commands, err := p.parse()
if err == nil {
err = p.process(commands)
}
return err
}
func (p *Parser) parse() ([]Command, error) {
commands := make([]Command, 0, 50)
for {
t := p.nextToken()
switch t.tt {
case tError:
return nil, errors.New(t.value)
case tEOF:
if p.isAnimated {
if p.basename == "" {
fmt.Fprintf(os.Stderr, "No basename provided: using default basename '%s'\n", DefaultBasename)
p.basename = DefaultBasename
formatString = fmt.Sprintf("%s/%s-%%0%dd.png", FramesDirectory, p.basename, len(strconv.Itoa(p.frames)))
}
}
return commands, nil
case tIdent:
var command Command
switch LookupIdent(t.value) {
case MOVE:
c := MoveCommand{}
c.args = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.knob, _ = p.next(tString)
command = c
case SCALE:
c := ScaleCommand{}
c.args = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.knob, _ = p.next(tString)
command = c
case ROTATE:
c := RotateCommand{}
c.axis = p.nextIdent()
c.degrees = p.nextFloat()
c.knob, _ = p.next(tString)
command = c
case LINE:
c := LineCommand{}
c.constants, _ = p.next(tString)
c.p1 = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.cs, _ = p.next(tString)
c.p2 = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.cs2, _ = p.next(tString)
command = c
case SPHERE:
c := SphereCommand{}
c.constants, _ = p.next(tString)
c.center = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.radius = p.nextFloat()
c.cs, _ = p.next(tString)
command = c
case TORUS:
c := TorusCommand{}
c.constants, _ = p.next(tString)
c.center = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.r1 = p.nextFloat()
c.r2 = p.nextFloat()
c.cs, _ = p.next(tString)
command = c
case BOX:
c := BoxCommand{}
c.constants, _ = p.next(tString)
c.p1 = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
c.width = p.nextFloat()
c.height = p.nextFloat()
c.depth = p.nextFloat()
c.cs, _ = p.next(tString)
command = c
case POP:
command = PopCommand{}
case PUSH:
command = PushCommand{}
case SAVE:
command = SaveCommand{
filename: p.nextString(),
}
case DISPLAY:
command = DisplayCommand{}
case VARY:
if p.frames == 0 {
return nil, errors.New("number of frames is not set")
}
name := p.nextString()
knob, found := knobs[name]
if !found {
knob = make([]float64, p.frames)
}
startFrame := p.nextInt()
if startFrame < 0 || startFrame >= p.frames {
return nil, fmt.Errorf("invalid start frame %d for knob %s", startFrame, name)
}
endFrame := p.nextInt()
if endFrame < 0 || endFrame >= p.frames || endFrame < startFrame {
return nil, fmt.Errorf("invalid end frame %d for knob %s", endFrame, name)
}
startValue := p.nextFloat()
endValue := p.nextFloat()
length := endFrame - startFrame
delta := (endValue - startValue) / float64(length+1)
for frame := startFrame; frame <= endFrame; frame++ {
knob[frame] = startValue
startValue += delta
}
knobs[name] = knob
p.isAnimated = true
case BASENAME:
if p.basename != "" {
fmt.Fprintln(os.Stderr, "Setting the basename multiple times")
}
p.basename = p.nextString()
formatString = fmt.Sprintf("%s/%s-%%0%dd.png", FramesDirectory, p.basename, len(strconv.Itoa(p.frames)))
p.isAnimated = true
case FRAMES:
if p.frames != 0 {
fmt.Fprintln(os.Stderr, "Setting the number of frames multiple times")
}
p.frames = p.nextInt()
if p.frames <= 0 {
return nil, errors.New("number of frames must be greater than zero")
}
p.isAnimated = true
case SET:
c := SetCommand{
name: p.nextString(),
value: p.nextFloat(),
}
command = c
case SETKNOBS:
c := SetKnobsCommand{
value: p.nextFloat(),
}
command = c
case MESH:
c := MeshCommand{
filename: p.nextString(),
}
command = c
case LIGHT:
name := p.nextString()
_, found := lightSources[name]
if found {
return nil, fmt.Errorf("light %s is already defined", name)
}
lightSource := LightSource{
color: Color{byte(p.nextInt()), byte(p.nextInt()), byte(p.nextInt())},
location: []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()},
}
lightSources[name] = lightSource
case AMBIENT:
ambient = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
case CONSTANTS:
constant := make([][]float64, 4)
name := p.nextString()
kar, kdr, ksr, kag, kdg, ksg, kab, kdb, ksb := p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat(), p.nextFloat()
constant[0] = []float64{kar, kag, kab} // ambient
constant[1] = []float64{kdr, kdg, kdb} // diffuse
constant[2] = []float64{ksr, ksg, ksb} // specular
next := p.peek().tt
if next == tFloat || next == tInt {
constant[3] = []float64{p.nextFloat(), p.nextFloat(), p.nextFloat()}
} else {
constant[3] = []float64{0, 0, 0}
}
constants[name] = constant
}
if command != nil {
commands = append(commands, command)
}
next := p.nextToken()
if next.tt != tNewline && next.tt != tEOF {
return nil, fmt.Errorf("unexpected %v at end of statement", next)
}
case tString:
return nil, fmt.Errorf("unrecognized identifier: \"%s\"", t.value)
}
}
}
func (p *Parser) process(commands []Command) error {
if p.isAnimated {
os.RemoveAll(FramesDirectory)
os.Mkdir(FramesDirectory, 0755)
} else {
p.frames = 1
}
var wg sync.WaitGroup
jobs := make(chan Job, 100)
for i := 0; i < MaxWorkers; i++ {
wg.Add(1)
go worker(NewDrawer(DefaultHeight, DefaultWidth), commands, jobs, &wg)
}
var err error
for frame := 0; frame < p.frames; frame++ {
jobs <- Job{
animated: p.isAnimated,
frame: frame,
}
}
close(jobs)
wg.Wait()
if p.isAnimated {
fmt.Println("Making animation...")
err = MakeAnimation(p.basename)
}
return err
}
func renderFrame(drawer *Drawer, commands []Command, frame int) error {
var err error
for _, command := range commands {
switch command.(type) {
case MoveCommand:
c := command.(MoveCommand)
x, y, z := c.args[0], c.args[1], c.args[2]
if c.knob != "" {
if knob, err := getKnob(c.knob, frame); err == nil {
x *= knob
y *= knob
z *= knob
} else {
return err
}
}
err = drawer.Move(x, y, z)
case ScaleCommand:
c := command.(ScaleCommand)
x, y, z := c.args[0], c.args[1], c.args[2]
if c.knob != "" {
if knob, err := getKnob(c.knob, frame); err == nil {
x *= knob
y *= knob
z *= knob
} else {
return err
}
}
err = drawer.Scale(x, y, z)
case RotateCommand:
c := command.(RotateCommand)
degrees := c.degrees
if c.knob != "" {
if knob, err := getKnob(c.knob, frame); err == nil {
degrees *= knob
} else {
return err
}
}
err = drawer.Rotate(c.axis, degrees)
case LineCommand:
c := command.(LineCommand)
err = drawer.Line(c.p1[0], c.p1[1], c.p1[2], c.p2[0], c.p2[1], c.p2[2])
if err != nil {
return err
}
err = drawer.DrawLines(White)
case SphereCommand:
c := command.(SphereCommand)
err = drawer.Sphere(c.center[0], c.center[1], c.center[2], c.radius)
if err != nil {
return err
}
if c.constants != "" {
if constant, err := getConstants(c.constants); err == nil {
err = drawer.DrawShadedPolygons(constant, lightSources)
} else {
return err
}
} else {
drawer.DrawPolygons(White)
}
case TorusCommand:
c := command.(TorusCommand)
err = drawer.Torus(c.center[0], c.center[1], c.center[2], c.r1, c.r2)
if err != nil {
return err
}
if c.constants != "" {
if constant, err := getConstants(c.constants); err == nil {
err = drawer.DrawShadedPolygons(constant, lightSources)
} else {
return err
}
} else {
drawer.DrawPolygons(White)
}
case BoxCommand:
c := command.(BoxCommand)
err = drawer.Box(c.p1[0], c.p1[1], c.p1[2], c.width, c.height, c.depth)
if err != nil {
return err
}
if c.constants != "" {
if constant, err := getConstants(c.constants); err == nil {
err = drawer.DrawShadedPolygons(constant, lightSources)
} else {
return err
}
} else {
drawer.DrawPolygons(White)
}
case PopCommand:
drawer.Pop()
case PushCommand:
drawer.Push()
case SaveCommand:
c := command.(SaveCommand)
err = drawer.Save(c.filename)
case DisplayCommand:
err = drawer.Display()
case SetCommand:
c := command.(SetCommand)
knobs[c.name][frame] = c.value
case SetKnobsCommand:
c := command.(SetKnobsCommand)
for key := range knobs {
knobs[key][frame] = c.value
}
case MeshCommand:
c := command.(MeshCommand)
f, err := os.Open(c.filename)
if err != nil {
return err
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// TODO: Legitimize
var x, y, z float64
num, _ := fmt.Sscanf(scanner.Text(), "vertex %f %f %f", &x, &y, &z)
if num == 3 {
drawer.AddPoint(x, y, z)
}
}
drawer.apply()
drawer.DrawPolygons(White)
}
if err != nil {
return err
}
}
return err
}
func getKnob(name string, frame int) (float64, error) {
if knob, found := knobs[name]; found {
return knob[frame], nil
}
return 0, fmt.Errorf("undefined knob '%s'", name)
}
func getConstants(name string) ([][]float64, error) {
if constant, found := constants[name]; found {
return constant, nil
}
return nil, fmt.Errorf("undefined constant '%s'", name)
}
// nextToken returns the nextToken token from the lexer
func (p *Parser) nextToken() Token {
lenBackup := len(p.backup)
// Use the token from backup if it exists
if lenBackup > 0 {
token := p.backup[lenBackup-1]
p.backup = p.backup[:lenBackup-1]
return token
}
token := p.lexer.NextToken()
return token
}
// next returns the next token if it matches the given token types
// If the token does not match, error is non-nil
func (p *Parser) next(typs ...TokenType) (string, error) {
next := p.peek()
for _, tt := range typs {
if next.tt == tt {
p.nextToken()
return next.value, nil
}
}
return "", fmt.Errorf("expected %v, got %v", typs, next.tt)
}
// nextRequired returns the value of the nextRequired token if its type is valid
// Panics if none of the token types match
func (p *Parser) nextRequired(typs ...TokenType) string {
next, err := p.next(typs...)
if err != nil {
panic(err)
}
return next
}
// nextInt returns the next integer token from the lexer
func (p *Parser) nextInt() int {
v, _ := strconv.Atoi(p.nextRequired(tInt))
return v
}
// nextFloat returns the next token from the lexer as a float.
func (p *Parser) nextFloat() float64 {
v, _ := strconv.ParseFloat(p.nextRequired(tInt, tFloat), 64)
return v
}
// nextString returns the next token from the lexer.
func (p *Parser) nextString() string {
return p.nextRequired(tString)
}
// nextIdent returns the next identifier from the lexer as a string.
func (p *Parser) nextIdent() string {
return p.nextRequired(tIdent)
}
// unread adds the token to the list of backup tokens.
// Since channels cannot be "unread", we use a list to backup these tokens
func (p *Parser) unread(token Token) {
p.backup = append(p.backup, token)
}
// peek returns the next token without consuming it
func (p *Parser) peek() Token {
token := p.nextToken()
p.unread(token)
return token
}
// Job is a struct that tells a worker thread which frames to render
type Job struct {
frame int // frame to render
animated bool // whether the frame is part of an animation
}
// worker is a worker thread that renders frames
func worker(drawer *Drawer, commands []Command, jobs chan Job, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case job, ok := <-jobs:
if !ok {
return
}
if job.animated {
fmt.Println("Rendering frame", job.frame)
}
err := renderFrame(drawer, commands, job.frame)
if job.animated {
err = drawer.Save(fmt.Sprintf(formatString, job.frame))
if err != nil {
return
}
drawer.Reset()
}
}
}
}