-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlsys.js
More file actions
125 lines (112 loc) · 3.03 KB
/
Copy pathlsys.js
File metadata and controls
125 lines (112 loc) · 3.03 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Symbol(rep) {
this.rep = rep;
this.logoCmd = new EmptyCommand();
}
function Set() {
this._map = {};
}
Set.prototype.addOrGet = function (item) {
if (this.has(item)) {
return this.get(item);
}
var sym = new Symbol(item);
this._map[item] = sym;
return sym;
};
Set.prototype.has = function (item) {
return this._map.hasOwnProperty(item);
};
Set.prototype.get = function (item) {
return this._map[item];
};
Set.prototype.getItems = function() {
var items = [], key, i;
for (key in this._map) {
i = this._map[key];
if (i instanceof Symbol) {
items.push(i);
}
}
return items;
};
function flatten (rep) {
var res = [], i;
for (i = 0; i < rep.length; i++) {
if (rep[i] instanceof Array) {
res.push.apply(res, flatten(rep[i]));
} else {
res.push(rep[i]);
}
}
return res;
}
function RewriteRule(startSym, expansion) {
this.start = startSym;
this.exp = expansion;
}
RewriteRule.prototype.consume = function (cStr) {
var i, ret = [], self = this;
return cStr.map(function (term) {
if (term === self.start) {
return self.exp;
}
return term;
});
};
function Lsystem(syms, rules, axiom) {
this.syms = syms;
this.rules = rules;
this.axiom = axiom;
this._string = axiom;
}
Lsystem.prototype.iterate = function () {
var nextString = this._string;
this.rules.forEach(function (rule) {
nextString = rule.consume(nextString);
});
this._string = flatten(nextString);
};
function stringNotEmpty(str) {
return str.length !== 0;
}
function isValidSym(str) {
return str.match(/^\w[\w\d]*$/) !== null;
}
function parseLsys(axiomTxt, rulesTxt, drawsTxt) {
var syms = new Set(), rules, axiom;
function toSymArr(str) {
var toks = str.replace(/\s/g, ' ').split(' ').filter(stringNotEmpty);
return toks.map(function (tok) {
if (!isValidSym(tok)) {
throw new Error('Invalid symbol ' + tok);
}
return syms.addOrGet(tok);
});
}
function parseRule(ruleTxt) {
var parts = ruleTxt.split(':'), startStr, startSym, exp;
if (parts.length !== 2) {
throw new Error('Invalid rule, ' + ruleTxt);
}
startStr = parts[0].trim();
if (!isValidSym(startStr)) {
throw new Error('Invalid start symbol, ' + startStr);
}
startSym = syms.addOrGet(startStr);
exp = toSymArr(parts[1]);
return new RewriteRule(startSym, exp);
}
function parseDrawTxt(drawCmd) {
var parts = drawCmd.split('='),
sym;
if (parts.length !== 2) {
throw new Error('Invalid draw command, ' + drawCmd);
}
sym = syms.addOrGet(parts[0].trim());
sym.logoCmd = parseLogoCmd(parts[1]);
}
axiom = toSymArr(axiomTxt);
rules = rulesTxt.map(parseRule);
drawsTxt.forEach(parseDrawTxt);
return new Lsystem(syms, rules, axiom);
}