Skip to content

Commit e0c51c3

Browse files
committed
rename unrollLoops to UnrollPointers and move it
1 parent f7bb913 commit e0c51c3

File tree

3 files changed

+149
-148
lines changed

3 files changed

+149
-148
lines changed

parser/tavor.go

Lines changed: 4 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import (
3030

3131
const zeroRune = 0
3232

33-
const (
34-
MaxRepeat = 2
35-
)
36-
3733
type tokenUse struct {
3834
token token.Token
3935
position scanner.Position
@@ -328,7 +324,7 @@ OUT:
328324
var from, to int
329325

330326
if sym == '*' {
331-
from, to = 0, MaxRepeat
327+
from, to = 0, tavor.MaxRepeat
332328
} else {
333329
if c == scanner.Int {
334330
from, _ = strconv.Atoi(p.scan.TokenText())
@@ -341,7 +337,7 @@ OUT:
341337
// until there is an explicit "to" we can assume to==from
342338
to = from
343339
} else {
344-
from, to = 1, MaxRepeat
340+
from, to = 1, tavor.MaxRepeat
345341
}
346342

347343
if c == ',' {
@@ -358,7 +354,7 @@ OUT:
358354
fmt.Printf("parseTerm repeat after to ( %d:%v -> %v\n", p.scan.Line, scanner.TokenString(c), p.scan.TokenText())
359355
}
360356
} else {
361-
to = MaxRepeat
357+
to = tavor.MaxRepeat
362358
}
363359
}
364360
}
@@ -1109,139 +1105,6 @@ func (p *tavorParser) parseSpecialTokenDefinition() (rune, error) {
11091105
return c, nil
11101106
}
11111107

1112-
func (p *tavorParser) unrollLoops(root token.Token) token.Token {
1113-
type unrollToken struct {
1114-
tok token.Token
1115-
parent *unrollToken
1116-
}
1117-
1118-
if tavor.DEBUG {
1119-
fmt.Println("Unroll loops by cloning pointers")
1120-
}
1121-
1122-
checked := make(map[token.Token]token.Token)
1123-
counters := make(map[token.Token]int)
1124-
1125-
queue := linkedlist.New()
1126-
1127-
queue.Push(&unrollToken{
1128-
tok: root,
1129-
parent: nil,
1130-
})
1131-
1132-
for !queue.Empty() {
1133-
v, _ := queue.Shift()
1134-
iTok, _ := v.(*unrollToken)
1135-
1136-
switch t := iTok.tok.(type) {
1137-
case *primitives.Pointer:
1138-
o := t.InternalGet()
1139-
1140-
parent, ok := checked[o]
1141-
times := 0
1142-
1143-
if ok {
1144-
times = counters[parent]
1145-
} else {
1146-
parent = o.Clone()
1147-
checked[o] = parent
1148-
}
1149-
1150-
if times != MaxRepeat {
1151-
if tavor.DEBUG {
1152-
fmt.Printf("Clone (%p)%#v with parent (%p)%#v\n", t, t, parent, parent)
1153-
}
1154-
1155-
c := parent.Clone()
1156-
1157-
t.Set(c)
1158-
1159-
counters[parent] = times + 1
1160-
checked[c] = parent
1161-
1162-
if iTok.parent != nil {
1163-
switch tt := iTok.parent.tok.(type) {
1164-
case token.ForwardToken:
1165-
tt.InternalReplace(t, c)
1166-
case lists.List:
1167-
tt.InternalReplace(t, c)
1168-
}
1169-
} else {
1170-
root = c
1171-
}
1172-
1173-
queue.Unshift(&unrollToken{
1174-
tok: c,
1175-
parent: iTok.parent,
1176-
})
1177-
} else {
1178-
if tavor.DEBUG {
1179-
fmt.Printf("Reached max repeat of %d for (%p)%#v with parent (%p)%#v\n", MaxRepeat, t, t, parent, parent)
1180-
}
1181-
1182-
t.Set(nil)
1183-
1184-
ta := iTok.tok
1185-
tt := iTok.parent
1186-
1187-
REMOVE:
1188-
for tt != nil {
1189-
switch l := tt.tok.(type) {
1190-
case token.ForwardToken:
1191-
if tavor.DEBUG {
1192-
fmt.Printf("Remove (%p)%#v from (%p)%#v\n", ta, ta, l, l)
1193-
}
1194-
1195-
c := l.InternalLogicalRemove(ta)
1196-
1197-
if c != nil {
1198-
break REMOVE
1199-
}
1200-
1201-
ta = l
1202-
tt = tt.parent
1203-
case lists.List:
1204-
if tavor.DEBUG {
1205-
fmt.Printf("Remove (%p)%#v from (%p)%#v\n", ta, ta, l, l)
1206-
}
1207-
1208-
c := l.InternalLogicalRemove(ta)
1209-
1210-
if c != nil {
1211-
break REMOVE
1212-
}
1213-
1214-
ta = l
1215-
tt = tt.parent
1216-
}
1217-
}
1218-
}
1219-
case token.ForwardToken:
1220-
if v := t.InternalGet(); v != nil {
1221-
queue.Push(&unrollToken{
1222-
tok: v,
1223-
parent: iTok,
1224-
})
1225-
}
1226-
case lists.List:
1227-
for i := 0; i < t.InternalLen(); i++ {
1228-
c, _ := t.InternalGet(i)
1229-
1230-
queue.Push(&unrollToken{
1231-
tok: c,
1232-
parent: iTok,
1233-
})
1234-
}
1235-
}
1236-
}
1237-
1238-
if tavor.DEBUG {
1239-
fmt.Println("Done unrolling")
1240-
}
1241-
1242-
return root
1243-
}
1244-
12451108
func ParseTavor(src io.Reader) (token.Token, error) {
12461109
p := &tavorParser{
12471110
earlyUse: make(map[string][]tokenUse),
@@ -1300,7 +1163,7 @@ func ParseTavor(src io.Reader) (token.Token, error) {
13001163

13011164
start := p.lookup["START"].token
13021165

1303-
start = p.unrollLoops(start)
1166+
start = tavor.UnrollPointers(start)
13041167

13051168
return start, nil
13061169
}

parser/tavor_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
. "github.com/stretchr/testify/assert"
99

10+
"github.com/zimmski/tavor"
1011
"github.com/zimmski/tavor/test"
1112
"github.com/zimmski/tavor/token"
1213
"github.com/zimmski/tavor/token/aggregates"
@@ -403,7 +404,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
403404
Nil(t, err)
404405
Equal(t, tok, lists.NewAll(
405406
primitives.NewConstantInt(1),
406-
lists.NewRepeat(primitives.NewConstantInt(2), 1, MaxRepeat),
407+
lists.NewRepeat(primitives.NewConstantInt(2), 1, tavor.MaxRepeat),
407408
))
408409

409410
// or repeat
@@ -414,7 +415,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
414415
lists.NewRepeat(lists.NewOne(
415416
primitives.NewConstantInt(2),
416417
primitives.NewConstantInt(3),
417-
), 1, MaxRepeat),
418+
), 1, tavor.MaxRepeat),
418419
primitives.NewConstantInt(4),
419420
))
420421

@@ -423,7 +424,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
423424
Nil(t, err)
424425
Equal(t, tok, lists.NewAll(
425426
primitives.NewConstantInt(1),
426-
lists.NewRepeat(primitives.NewConstantInt(2), 0, MaxRepeat),
427+
lists.NewRepeat(primitives.NewConstantInt(2), 0, tavor.MaxRepeat),
427428
))
428429

429430
// or optional repeat
@@ -434,7 +435,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
434435
lists.NewRepeat(lists.NewOne(
435436
primitives.NewConstantInt(2),
436437
primitives.NewConstantInt(3),
437-
), 0, MaxRepeat),
438+
), 0, tavor.MaxRepeat),
438439
primitives.NewConstantInt(4),
439440
))
440441

@@ -443,7 +444,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
443444
Nil(t, err)
444445
Equal(t, tok, lists.NewAll(
445446
primitives.NewConstantInt(1),
446-
lists.NewRepeat(primitives.NewConstantInt(2), 0, MaxRepeat),
447+
lists.NewRepeat(primitives.NewConstantInt(2), 0, tavor.MaxRepeat),
447448
))
448449

449450
// exact repeat
@@ -459,7 +460,7 @@ func TestTavorParserAlternationsAndGroupings(t *testing.T) {
459460
Nil(t, err)
460461
Equal(t, tok, lists.NewAll(
461462
primitives.NewConstantInt(1),
462-
lists.NewRepeat(primitives.NewConstantInt(2), 3, MaxRepeat),
463+
lists.NewRepeat(primitives.NewConstantInt(2), 3, tavor.MaxRepeat),
463464
))
464465

465466
// at most repeat
@@ -499,7 +500,7 @@ func TestTavorParserTokenAttributes(t *testing.T) {
499500
primitives.NewConstantInt(1),
500501
primitives.NewConstantInt(2),
501502
primitives.NewConstantInt(3),
502-
), 0, MaxRepeat),
503+
), 0, tavor.MaxRepeat),
503504
primitives.NewConstantString("->"),
504505
aggregates.NewLen(list),
505506
))

0 commit comments

Comments
 (0)