Skip to content

Commit caa1909

Browse files
author
James Brundage
committed
feat: Turtle.L() ( Fixes #20 )
1 parent 0c7ece8 commit caa1909

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Types/Turtle/L.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
param(
2+
[Alias('Start', 'StartString', 'Initiator')]
3+
[string]
4+
$Axiom,
5+
6+
[Alias('Rules', 'ProductionRules')]
7+
[Collections.IDictionary]
8+
$Rule = [Ordered]@{},
9+
10+
[Alias('Iterations', 'Steps', 'IterationCount','StepCount')]
11+
[int]
12+
$N = 2,
13+
14+
[Collections.IDictionary]
15+
$Variable = @{}
16+
)
17+
18+
if ($n -le 1) { return $Axiom}
19+
20+
$currentState = "$Axiom"
21+
$combinedPattern = "(?>$($Rule.Keys -join '|'))"
22+
foreach ($iteration in 1..$n) {
23+
$currentState = $currentState -replace $combinedPattern, {
24+
$match = $_
25+
$matchingRule = $rule["$match"]
26+
if ($matchingRule -is [ScriptBlock]) {
27+
return "$(& $matchingRule $match)"
28+
} else {
29+
return $matchingRule
30+
}
31+
}
32+
}
33+
34+
$finalState = $currentState
35+
foreach ($character in $finalState.ToCharArray()) {
36+
foreach ($key in $Variable.Keys) {
37+
if ($character -match $key) {
38+
$action = $Variable[$key]
39+
if ($action -is [ScriptBlock]) {
40+
. $action $character
41+
} else {
42+
$action
43+
}
44+
}
45+
}
46+
}
47+
return

0 commit comments

Comments
 (0)