-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSystem.ts
66 lines (54 loc) · 1.86 KB
/
LSystem.ts
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
module LSystems {
export class ProductionRule
{
constructor(public predecessor: string = "", public successor: string = "")
{ }
public static makeConstant(symbol: string): ProductionRule
{
return new ProductionRule(symbol, symbol);
}
public toString() : string
{
return this.predecessor + " -> " + this.successor
}
}
export class System
{
public alphabet: {};
public start: string;
public rules: ProductionRule[];
public recursions: string[] = [];
public recursionCounter: number = 0;
public currentRecursion: string;
constructor (start: string = "", rules: ProductionRule[] = [])
{
this.rules = rules;
this.start = start;
this.currentRecursion = start;
this.recursions.push(start);
}
public rewrite(): string
{
console.log("rewriting " + this.currentRecursion + "...");
var newRecursion = "";
var currentRule: ProductionRule;
for (var symbolIndex: number = 0; symbolIndex < this.currentRecursion.length; symbolIndex++)
{
for (var ruleIndex: number = 0; ruleIndex < this.rules.length; ruleIndex++)
{
currentRule = this.rules[ruleIndex];
if (this.currentRecursion[symbolIndex] === currentRule.predecessor)
{
newRecursion += currentRule.successor;
break;
}
}
}
this.recursionCounter++;
this.currentRecursion = newRecursion;
this.recursions.push(newRecursion);
console.log("rewritten to " + newRecursion);
return newRecursion;
}
}
}