Skip to content

Commit 71ad086

Browse files
docs: Turtle.LSystem improvement ( Fixes #116 )
Improving parameter docs and inner docs
1 parent 5fc6267 commit 71ad086

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Types/Turtle/LSystem.ps1

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,50 +120,61 @@ $Order = 2,
120120
$Variable = @{}
121121
)
122122

123+
# First, let us expand our axiom
123124
$currentState = "$Axiom"
125+
# (at least, as long as we're supposed to)
124126
if ($Order -ge 1) {
125127
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
126128
foreach ($iteration in 1..$Order) {
129+
# To expand each iteration, we replace any matching characters
127130
$currentState = $currentState -replace $combinedPattern, {
128131
$match = $_
129132
$matchingRule = $rule["$match"]
133+
# a matching rule could be dynamically specified with a script block
130134
if ($matchingRule -is [ScriptBlock]) {
131135
return "$(. $matchingRule $match)"
132136
} else {
137+
# but is often statically expanded with a string.
133138
return $matchingRule
134139
}
135140
}
136141
}
137142
}
138143

139-
$localReplacement = [Ordered]@{}
140-
foreach ($key in $variable.Keys) {
141-
$localReplacement[$key] =
142-
if ($variable[$key] -is [ScriptBlock]) {
143-
[ScriptBlock]::Create($variable[$key])
144-
} else {
145-
$variable[$key]
146-
}
147-
}
148-
144+
# Now we know our final state
149145
$finalState = $currentState
150146

147+
# and can add the appropriate data attributes.
151148
$this.PathAttribute = [Ordered]@{
152149
"data-l-order" = $Order
153150
"data-l-axiom" = $Axiom
154151
"data-l-rules" = ConvertTo-Json $Rule
155152
"data-l-expanded" = $finalState
156153
}
157154

155+
# Next, prepare our replacements.
156+
# The provided script block will almost always be scoped differently
157+
# so we need to recreate it.
158+
$localReplacement = [Ordered]@{}
159+
foreach ($key in $variable.Keys) {
160+
$localReplacement[$key] =
161+
if ($variable[$key] -is [ScriptBlock]) {
162+
[ScriptBlock]::Create($variable[$key])
163+
} else {
164+
$variable[$key]
165+
}
166+
}
167+
168+
# Now we need to find all potential matches
158169
$MatchesAny = "(?>$($variable.Keys -join '|'))"
159170
$allMatches = @([Regex]::Matches($finalState, $MatchesAny, 'IgnoreCase,IgnorePatternWhitespace'))
171+
# we want to minimize rematching, so create a temporary cache.
160172
$matchCache = @{}
161173
:nextMatch foreach ($match in $allMatches) {
162174
$m = "$match"
163175
if (-not $matchCache[$m]) {
164176
foreach ($key in $Variable.Keys) {
165-
if (-not ($match -match $key)) { continue }
166-
# if ($variable[$key] -isnot [ScriptBlock]) { continue }
177+
if (-not ($match -match $key)) { continue }
167178
$matchCache[$m] = $localReplacement[$key]
168179
break
169180
}

0 commit comments

Comments
 (0)