Skip to content

Commit 1ee75ee

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.LSystem improvement ( Fixes #116 )
1 parent 6a63eae commit 1ee75ee

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

Turtle.types.ps1xml

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -716,28 +716,28 @@ $Axiom,
716716
[Collections.IDictionary]
717717
$Rule = [Ordered]@{},
718718

719-
[Alias('Iterations', 'Steps', 'IterationCount','StepCount')]
719+
[Alias('Iterations', 'IterationCount', 'N', 'Steps','StepCount')]
720720
[int]
721-
$N = 2,
721+
$Order = 2,
722722

723723
[Collections.IDictionary]
724724
$Variable = @{}
725725
)
726726

727-
if ($n -lt 1) { return $Axiom}
728-
729727
$currentState = "$Axiom"
730-
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
731-
foreach ($iteration in 1..$n) {
732-
$currentState = $currentState -replace $combinedPattern, {
733-
$match = $_
734-
$matchingRule = $rule["$match"]
735-
if ($matchingRule -is [ScriptBlock]) {
736-
return "$(& $matchingRule $match)"
737-
} else {
738-
return $matchingRule
739-
}
740-
}
728+
if ($Order -ge 1) {
729+
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
730+
foreach ($iteration in 1..$Order) {
731+
$currentState = $currentState -replace $combinedPattern, {
732+
$match = $_
733+
$matchingRule = $rule["$match"]
734+
if ($matchingRule -is [ScriptBlock]) {
735+
return "$(. $matchingRule $match)"
736+
} else {
737+
return $matchingRule
738+
}
739+
}
740+
}
741741
}
742742

743743
$localReplacement = [Ordered]@{}
@@ -751,24 +751,34 @@ foreach ($key in $variable.Keys) {
751751
}
752752

753753
$finalState = $currentState
754-
$null = foreach ($character in $finalState.ToCharArray()) {
755-
foreach ($key in $Variable.Keys) {
756-
if ($character -match $key) {
757-
$action = $localReplacement[$key]
758-
if ($action -is [ScriptBlock]) {
759-
. $action $character
760-
} else {
761-
$action
762-
}
763-
}
764-
}
765-
}
754+
766755
$this.PathAttribute = [Ordered]@{
767-
"data-l-order" = $N
756+
"data-l-order" = $Order
768757
"data-l-axiom" = $Axiom
769758
"data-l-rules" = ConvertTo-Json $Rule
770759
"data-l-expanded" = $finalState
771760
}
761+
762+
$MatchesAny = "(?>$($variable.Keys -join '|'))"
763+
$allMatches = @([Regex]::Matches($finalState, $MatchesAny, 'IgnoreCase,IgnorePatternWhitespace'))
764+
$matchCache = @{}
765+
:nextMatch foreach ($match in $allMatches) {
766+
$m = "$match"
767+
if (-not $matchCache[$m]) {
768+
foreach ($key in $Variable.Keys) {
769+
if (-not ($match -match $key)) { continue }
770+
# if ($variable[$key] -isnot [ScriptBlock]) { continue }
771+
$matchCache[$m] = $localReplacement[$key]
772+
break
773+
}
774+
}
775+
776+
if ($matchCache[$m] -is [ScriptBlock]) {
777+
$Orderull = . $matchCache[$m] $match
778+
continue nextMatch
779+
}
780+
}
781+
772782
return $this
773783

774784
</Script>

0 commit comments

Comments
 (0)