Skip to content

Commit 6a63eae

Browse files
feat: Turtle.LSystem improvement ( Fixes #116 )
1 parent fd31969 commit 6a63eae

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

Types/Turtle/LSystem.ps1

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,28 @@ $Axiom,
112112
[Collections.IDictionary]
113113
$Rule = [Ordered]@{},
114114

115-
[Alias('Iterations', 'Steps', 'IterationCount','StepCount')]
115+
[Alias('Iterations', 'IterationCount', 'N', 'Steps','StepCount')]
116116
[int]
117-
$N = 2,
117+
$Order = 2,
118118

119119
[Collections.IDictionary]
120120
$Variable = @{}
121121
)
122122

123-
if ($n -lt 1) { return $Axiom}
124-
125123
$currentState = "$Axiom"
126-
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
127-
foreach ($iteration in 1..$n) {
128-
$currentState = $currentState -replace $combinedPattern, {
129-
$match = $_
130-
$matchingRule = $rule["$match"]
131-
if ($matchingRule -is [ScriptBlock]) {
132-
return "$(& $matchingRule $match)"
133-
} else {
134-
return $matchingRule
135-
}
136-
}
124+
if ($Order -ge 1) {
125+
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
126+
foreach ($iteration in 1..$Order) {
127+
$currentState = $currentState -replace $combinedPattern, {
128+
$match = $_
129+
$matchingRule = $rule["$match"]
130+
if ($matchingRule -is [ScriptBlock]) {
131+
return "$(. $matchingRule $match)"
132+
} else {
133+
return $matchingRule
134+
}
135+
}
136+
}
137137
}
138138

139139
$localReplacement = [Ordered]@{}
@@ -147,22 +147,32 @@ foreach ($key in $variable.Keys) {
147147
}
148148

149149
$finalState = $currentState
150-
$null = foreach ($character in $finalState.ToCharArray()) {
151-
foreach ($key in $Variable.Keys) {
152-
if ($character -match $key) {
153-
$action = $localReplacement[$key]
154-
if ($action -is [ScriptBlock]) {
155-
. $action $character
156-
} else {
157-
$action
158-
}
159-
}
160-
}
161-
}
150+
162151
$this.PathAttribute = [Ordered]@{
163-
"data-l-order" = $N
152+
"data-l-order" = $Order
164153
"data-l-axiom" = $Axiom
165154
"data-l-rules" = ConvertTo-Json $Rule
166155
"data-l-expanded" = $finalState
167156
}
157+
158+
$MatchesAny = "(?>$($variable.Keys -join '|'))"
159+
$allMatches = @([Regex]::Matches($finalState, $MatchesAny, 'IgnoreCase,IgnorePatternWhitespace'))
160+
$matchCache = @{}
161+
:nextMatch foreach ($match in $allMatches) {
162+
$m = "$match"
163+
if (-not $matchCache[$m]) {
164+
foreach ($key in $Variable.Keys) {
165+
if (-not ($match -match $key)) { continue }
166+
# if ($variable[$key] -isnot [ScriptBlock]) { continue }
167+
$matchCache[$m] = $localReplacement[$key]
168+
break
169+
}
170+
}
171+
172+
if ($matchCache[$m] -is [ScriptBlock]) {
173+
$Orderull = . $matchCache[$m] $match
174+
continue nextMatch
175+
}
176+
}
177+
168178
return $this

0 commit comments

Comments
 (0)