-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimalLS.sc
executable file
·75 lines (63 loc) · 1.94 KB
/
minimalLS.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
// MLSys : minimal L-System
// Built after wonderful LSys by Batuhan Bozkurt
// no branching, no context, no params, just what I need for strings
// absence of branching allows to use [] as array standard notation
/*
// USAGE
// deterministic. Adding some spice to the sentence
a = MLSys("sempre caro mi fu quest'ermo colle", [" "->"! "])
a.applyRules; a.currentAxiom
// again
a.applyRules; a.currentAxiom
// stochastic, with uniform distro. No more so sure
a = MLSys("sempre caro mi fu quest'ermo colle", [" "->["! ","? "]])
a.applyRules; a.currentAxiom
// again
a.applyRules; a.currentAxiom
// vowel scrambling. Note the ultracompact notation for keys
a = MLSys("sempre caro mi fu quest'ermo colle", [["a", "e", "i", "o", "u"]->["a", "e", "i", "o", "u"]])
a.applyRules; a.currentAxiom
// again, going toward a unique vowel
a.applyRules; a.currentAxiom
// Note that [] means 2 different things on rule sides:
// - on key is: for each key
// - on val: select randomly one val
*/
MLSys {
var <axiom, <currentAxiom, <rules ;
var <axioms ; // used to store all rewriting process
*new
{|argAxiom, argRules|
^super.new.init(argAxiom, argRules);
}
init
{|argAxiom, argRules|
axiom = argAxiom;
currentAxiom = argAxiom;
rules = argRules ;
axioms = [] ;
}
applyRule { |rule|
var val ;
var keyArr = if (rule.key.isString) // so that we can always iterate on arr
{[rule.key]} {rule.key} ;
// for each key in the arr
keyArr.do{|key|
val = if (rule.value.isString.not)
{ rule.value.choose } //if an arr, we choose with uniform distro
{ rule.value } ; // else we get the value directly
currentAxiom = currentAxiom.replace(key, val) ;
// store in the axioms arr
// here we would collect each rule
//axioms = axioms.add(currentAxiom)
}
}
applyRules {|level = 1|
level.do{
rules.do{|rule| this.applyRule(rule) } ;
// store in the axioms arr
// here we collect levels
axioms = axioms.add(currentAxiom)
}
}
}