-
Notifications
You must be signed in to change notification settings - Fork 0
/
LReplacer.sc
97 lines (88 loc) · 2.37 KB
/
LReplacer.sc
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
LReplacer {
var replacedList;
*new {
arg lsysString = String, transformRules = Dictionary.new;
^super.new.init(lsysString, transformRules);
}
init {
arg lsysString = String, transformRules = Dictionary.new;
var splittedLsysList = this.splitString(lsysString);
replacedList = this.prReplaceNodes(lsysList: splittedLsysList, nodesTransformDict: Dictionary.with(*transformRules));
^replacedList;
}
prTransformToList {
arg lsysList;
var transformedList = List.new, openBracketsCount = 0;
var createSublist = {
arg list, level;
var innerLevel = level;
if (innerLevel === 1, {
list.add(List.new);
}, {
innerLevel = innerLevel - 1;
createSublist.value(list.at(list.size - 1), innerLevel);
})
};
var addToList = {
arg list, level, node;
var innerLevel = level;
if (innerLevel === 0, {
list.add(node);
});
if (innerLevel > 0, {
innerLevel = innerLevel - 1;
addToList.value(list: list.at(list.size - 1), level: innerLevel, node: node);
});
};
lsysList.do({
arg nodeItem, index;
switch(nodeItem.asString,
"[", {
openBracketsCount = openBracketsCount + 1;
createSublist.value(transformedList, openBracketsCount);
},
"]", {
openBracketsCount = openBracketsCount - 1;
},
{
addToList.value(list: transformedList, level: openBracketsCount, node: nodeItem);
}
);
});
^transformedList;
}
prReplaceNodes {
arg lsysList = [], nodesTransformDict = Dictionary.new;
var transformedList = this.prTransformToList(lsysList);
var replaceNodes = {
arg list;
list.do({
arg item, index;
if (item.isCollection, {
replaceNodes.(item);
}, {
var dictItem = nodesTransformDict.at(item.asString);
if (dictItem === nil, {
list.put(index, item);
}, {
list.put(index, dictItem.(nodeItem: item, index: index, list: list));
});
});
});
};
replaceNodes.(transformedList);
^transformedList;
}
splitString {
arg lsysString = String;
var splittedList = List.new;
lsysString.do{
arg item;
splittedList.add(item);
};
^splittedList;
}
getTransformedList {
^replacedList;
}
}