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
|
// Package parser implements a parser and compiler.
package parser
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"github.com/mvertes/parscan/lang"
"github.com/mvertes/parscan/scanner"
)
// Parser represents the state of a parser.
type Parser struct {
*scanner.Scanner
Symbols map[string]*Symbol
function *Symbol
scope string
fname string
pkgName string // current package name
noPkg bool // true if package statement is not mandatory (test, repl).
funcScope string
framelen map[string]int // length of function frames indexed by funcScope
labelCount map[string]int
breakLabel string
continueLabel string
clonum int // closure instance number
}
// Parser errors.
var (
ErrBody = errors.New("missign body")
ErrBreak = errors.New("invalid break statement")
ErrContinue = errors.New("invalid continue statement")
ErrFor = errors.New("invalid for statement")
ErrGoto = errors.New("invalid goto statement")
)
// NewParser returns a new parser.
func NewParser(scanner *scanner.Scanner, noPkg bool) *Parser {
return &Parser{
Scanner: scanner,
noPkg: noPkg,
Symbols: initUniverse(),
framelen: map[string]int{},
labelCount: map[string]int{},
}
}
// Scan performs lexical analysis on s and returns Tokens or an error.
func (p *Parser) Scan(s string, endSemi bool) (Tokens, error) {
return p.Scanner.Scan(s, endSemi)
}
// Parse performs syntax analysis on s and return Tokens or an error.
func (p *Parser) Parse(src string) (out Tokens, err error) {
in, err := p.Scan(src, true)
if err != nil {
return out, err
}
log.Printf("Parse src: %#v\n", src)
return p.parseStmts(in)
}
func (p *Parser) parseStmts(in Tokens) (out Tokens, err error) {
for len(in) > 0 {
endstmt := in.Index(lang.Semicolon)
if endstmt == -1 {
return out, scanner.ErrBlock
}
// Skip over simple init statements for some tokens (if, for, ...)
if lang.HasInit[in[0].Tok] {
for in[endstmt-1].Tok != lang.BraceBlock {
e2 := in[endstmt+1:].Index(lang.Semicolon)
if e2 == -1 {
return out, scanner.ErrBlock
}
endstmt += 1 + e2
}
}
o, err := p.parseStmt(in[:endstmt])
if err != nil {
return out, err
}
out = append(out, o...)
in = in[endstmt+1:]
}
return out, err
}
func (p *Parser) parseStmt(in Tokens) (out Tokens, err error) {
if len(in) == 0 {
return nil, nil
}
log.Println("parseStmt in:", in)
// Preliminary: make sure that a pkgName is defined (or about to be).
if in[0].Tok != lang.Package && p.pkgName == "" {
if !p.noPkg {
return out, errors.New("no package defined")
}
p.pkgName = "main"
}
switch t := in[0]; t.Tok {
case lang.Break:
return p.parseBreak(in)
case lang.Continue:
return p.parseContinue(in)
case lang.Const:
return p.parseConst(in)
case lang.For:
return p.parseFor(in)
case lang.Func:
return p.parseFunc(in)
case lang.Defer, lang.Go, lang.Fallthrough, lang.Select:
return out, fmt.Errorf("not yet implemented: %v", t.Tok)
case lang.Goto:
return p.parseGoto(in)
case lang.If:
return p.parseIf(in)
case lang.Import:
return p.parseImports(in)
case lang.Package:
return p.parsePackage(in)
case lang.Return:
return p.parseReturn(in)
case lang.Switch:
return p.parseSwitch(in)
case lang.Type:
return p.parseType(in)
case lang.Var:
return p.parseVar(in)
case lang.Ident:
if len(in) == 2 && in[1].Tok == lang.Colon {
return p.parseLabel(in)
}
fallthrough
default:
return p.parseExpr(in)
}
}
func (p *Parser) parseBreak(in Tokens) (out Tokens, err error) {
var label string
switch len(in) {
case 1:
label = p.breakLabel
case 2:
if in[1].Tok != lang.Ident {
return nil, ErrBreak
}
// TODO: check validity of user provided label
label = in[1].Str
default:
return nil, ErrBreak
}
out = Tokens{{Tok: lang.Goto, Str: label}}
return out, err
}
func (p *Parser) parseContinue(in Tokens) (out Tokens, err error) {
var label string
switch len(in) {
case 1:
label = p.continueLabel
case 2:
if in[1].Tok != lang.Ident {
return nil, ErrContinue
}
// TODO: check validity of user provided label
label = in[1].Str
default:
return nil, ErrContinue
}
out = Tokens{{Tok: lang.Goto, Str: label}}
return out, err
}
func (p *Parser) parseGoto(in Tokens) (out Tokens, err error) {
if len(in) != 2 || in[1].Tok != lang.Ident {
return nil, ErrGoto
}
// TODO: check validity of user provided label
return Tokens{{Tok: lang.Goto, Str: p.funcScope + "/" + in[1].Str}}, nil
}
func (p *Parser) parseFor(in Tokens) (out Tokens, err error) {
// TODO: detect invalid code.
fc := strconv.Itoa(p.labelCount[p.scope])
p.labelCount[p.scope]++
var init, cond, post, body Tokens
pre := in[1 : len(in)-1].Split(lang.Semicolon)
switch len(pre) {
case 1:
cond = pre[0]
case 3:
init, cond, post = pre[0], pre[1], pre[2]
default:
return nil, ErrFor
}
breakLabel, continueLabel := p.breakLabel, p.continueLabel
p.pushScope("for" + fc)
p.breakLabel, p.continueLabel = p.scope+"e", p.scope+"b"
defer func() {
p.breakLabel, p.continueLabel = breakLabel, continueLabel
p.popScope()
}()
if len(init) > 0 {
if init, err = p.parseStmt(init); err != nil {
return nil, err
}
out = init
}
out = append(out, scanner.Token{Tok: lang.Label, Str: p.scope + "b"})
if len(cond) > 0 {
if cond, err = p.parseExpr(cond); err != nil {
return nil, err
}
out = append(out, cond...)
out = append(out, scanner.Token{Tok: lang.JumpFalse, Str: p.scope + "e"})
}
if body, err = p.Parse(in[len(in)-1].Block()); err != nil {
return nil, err
}
out = append(out, body...)
if len(post) > 0 {
if post, err = p.parseStmt(post); err != nil {
return nil, err
}
out = append(out, post...)
}
out = append(out,
scanner.Token{Tok: lang.Goto, Str: p.scope + "b"},
scanner.Token{Tok: lang.Label, Str: p.scope + "e"})
return out, err
}
func (p *Parser) parseFunc(in Tokens) (out Tokens, err error) {
// TODO: handle anonymous functions (no function name)
// TODO: handle receiver (methods)
// TODO: handle parametric types (generics)
// TODO: handle variadic parameters
var fname string
if in[1].Tok == lang.Ident {
fname = in[1].Str
} else {
fname = "#f" + strconv.Itoa(p.clonum)
p.clonum++
}
ofname := p.fname
p.fname = fname
ofunc := p.function
funcScope := p.funcScope
s, _, ok := p.GetSym(fname, p.scope)
if !ok {
s = &Symbol{Used: true}
p.Symbols[p.scope+fname] = s
}
p.pushScope(fname)
p.funcScope = p.scope
defer func() {
p.fname = ofname // TODO remove if favor of function.
p.function = ofunc
p.funcScope = funcScope
p.popScope()
}()
out = Tokens{
{Tok: lang.Goto, Str: fname + "_end"}, // Skip function definition.
{Tok: lang.Label, Pos: in[0].Pos, Str: fname},
}
bi := in.Index(lang.BraceBlock)
if bi < 0 {
return out, ErrBody
}
typ, err := p.parseTypeExpr(in[:bi])
if err != nil {
return out, err
}
s.Kind = SymFunc
s.Type = typ
p.function = s
toks, err := p.Parse(in[len(in)-1].Block())
if err != nil {
return out, err
}
if l := p.framelen[p.funcScope] - 1; l > 0 {
out = append(out, scanner.Token{Tok: lang.Grow, Beg: l})
}
out = append(out, toks...)
if out[len(out)-1].Tok != lang.Return {
// Ensure that a return statement is always added at end of function.
// TODO: detect missing or wrong returns.
x, err := p.parseReturn(nil)
if err != nil {
return out, err
}
out = append(out, x...)
}
out = append(out, scanner.Token{Tok: lang.Label, Str: fname + "_end"})
return out, err
}
func (p *Parser) parseIf(in Tokens) (out Tokens, err error) {
label := "if" + strconv.Itoa(p.labelCount[p.scope])
p.labelCount[p.scope]++
p.pushScope(label)
defer p.popScope()
// We start from the end of the statement and examine tokens backward to
// get the destination labels already computed when jumps are set.
for sc, i := 0, len(in)-1; i > 0; sc++ {
ssc := strconv.Itoa(sc)
if in[i].Tok != lang.BraceBlock {
return nil, fmt.Errorf("expected '{', got %v", in[i])
}
pre, err := p.Parse(in[i].Block())
if err != nil {
return nil, err
}
if sc > 0 {
pre = append(pre, scanner.Token{Tok: lang.Goto, Str: p.scope + "e0"})
}
pre = append(pre, scanner.Token{Tok: lang.Label, Str: p.scope + "e" + ssc})
out = append(pre, out...)
i--
if in[i].Tok == lang.Else { // Step over final 'else'.
i--
continue
}
pre = Tokens{}
var init, cond Tokens
ifp := in[:i].LastIndex(lang.If)
initcond := in[ifp+1 : i+1]
if ii := initcond.Index(lang.Semicolon); ii < 0 {
cond = initcond
} else {
init = initcond[:ii]
cond = initcond[ii+1:]
}
if len(init) > 0 {
if init, err = p.parseStmt(init); err != nil {
return nil, err
}
pre = append(pre, init...)
}
if cond, err = p.parseExpr(cond); err != nil {
return nil, err
}
pre = append(pre, cond...)
pre = append(pre, scanner.Token{Tok: lang.JumpFalse, Str: p.scope + "e" + ssc})
out = append(pre, out...)
i = ifp
if i > 1 && in[i].Tok == lang.If && in[i-1].Tok == lang.Else { // Step over 'else if'.
i -= 2
}
}
return out, err
}
func (p *Parser) parseSwitch(in Tokens) (out Tokens, err error) {
var init, cond, clauses Tokens
initcond := in[1 : len(in)-1]
if ii := initcond.Index(lang.Semicolon); ii < 0 {
cond = initcond
} else {
init = initcond[:ii]
cond = initcond[ii+1:]
}
label := "switch" + strconv.Itoa(p.labelCount[p.scope])
p.labelCount[p.scope]++
breakLabel := p.breakLabel
p.pushScope(label)
p.breakLabel = p.scope + "e"
defer func() {
p.breakLabel = breakLabel
p.popScope()
}()
if len(init) > 0 {
if init, err = p.parseStmt(init); err != nil {
return nil, err
}
out = init
}
condSwitch := false
if len(cond) > 0 {
if cond, err = p.parseExpr(cond); err != nil {
return nil, err
}
out = append(out, cond...)
condSwitch = true
}
// Split switch body into case clauses.
clauses, err = p.Scan(in[len(in)-1].Block(), true)
sc := clauses.SplitStart(lang.Case)
// Make sure that the default clause is the last.
lsc := len(sc) - 1
for i, cl := range sc {
if cl[1].Tok == lang.Colon && i != lsc {
sc[i], sc[lsc] = sc[lsc], sc[i]
break
}
}
// Process each clause.
nc := len(sc) - 1
for i, cl := range sc {
co, err := p.parseCaseClause(cl, i, nc, condSwitch)
if err != nil {
return nil, err
}
out = append(out, co...)
}
out = append(out, scanner.Token{Tok: lang.Label, Str: p.breakLabel})
return out, err
}
func (p *Parser) parseCaseClause(in Tokens, index, maximum int, condSwitch bool) (out Tokens, err error) {
in = append(in, scanner.Token{Tok: lang.Semicolon}) // Force a ';' at the end of body clause.
var conds, body Tokens
tl := in.Split(lang.Colon)
if len(tl) != 2 {
return nil, errors.New("invalid case clause")
}
conds = tl[0][1:]
if body, err = p.parseStmts(tl[1]); err != nil {
return out, err
}
lcond := conds.Split(lang.Comma)
for i, cond := range lcond {
if cond, err = p.parseExpr(cond); err != nil {
return out, err
}
txt := fmt.Sprintf("%sc%d.%d", p.scope, index, i)
next := ""
if i == len(lcond)-1 { // End of cond: next, go to next clause or exit
if index < maximum {
next = fmt.Sprintf("%sc%d.%d", p.scope, index+1, 0)
} else {
next = p.scope + "e"
}
} else {
next = fmt.Sprintf("%sc%d.%d", p.scope, index, i+1)
}
out = append(out, scanner.Token{Tok: lang.Label, Str: txt})
if len(cond) > 0 {
out = append(out, cond...)
if condSwitch {
out = append(out, scanner.Token{Tok: lang.EqualSet})
}
out = append(out, scanner.Token{Tok: lang.JumpFalse, Str: next})
}
out = append(out, body...)
if i != len(lcond)-1 || index != maximum {
out = append(out, scanner.Token{Tok: lang.Goto, Str: p.scope + "e"})
}
}
return out, err
}
func (p *Parser) parseLabel(in Tokens) (out Tokens, err error) {
return Tokens{{Tok: lang.Label, Str: p.funcScope + "/" + in[0].Str}}, nil
}
func (p *Parser) parseReturn(in Tokens) (out Tokens, err error) {
if l := len(in); l > 1 {
if out, err = p.parseExpr(in[1:]); err != nil {
return out, err
}
} else if l == 0 {
in = Tokens{{Tok: lang.Return}} // Implicit return in functions with no return parameters.
}
// TODO: the function symbol should be already present in the parser context.
// otherwise no way to handle anonymous func.
s := p.function
in[0].Beg = s.Type.Rtype.NumOut()
in[0].End = s.Type.Rtype.NumIn()
out = append(out, in[0])
return out, err
}
func (p *Parser) numItems(s string, sep lang.Token) int {
tokens, err := p.Scan(s, false)
if err != nil {
return -1
}
r := 0
for _, t := range tokens.Split(sep) {
if len(t) == 0 {
continue
}
r++
}
return r
}
func (p *Parser) pushScope(name string) {
if p.scope != "" {
p.scope += "/"
}
p.scope += name
}
func (p *Parser) popScope() {
j := strings.LastIndex(p.scope, "/")
if j == -1 {
j = 0
}
p.scope = p.scope[:j]
}
func (p *Parser) precedence(t scanner.Token) int {
return p.TokenProps[t.Str].Precedence
}
|