-
Notifications
You must be signed in to change notification settings - Fork 58
/
macrocell.js
137 lines (118 loc) · 3.42 KB
/
macrocell.js
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
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
function load_macrocell(universe, text)
{
const lines = text.split("\n");
if(!lines[0].startsWith("[M2]"))
{
return;
}
let tree_start = 0;
for(let i = 1; i < lines.length; i++)
{
if(lines[i][0] !== "#")
{
tree_start = i;
break;
}
}
if(!tree_start)
{
return;
}
const nodes = [];
nodes[0] = undefined; // special: Empty pattern
for(let i = tree_start; i < lines.length; i++)
{
const line = lines[i];
const first = line[0];
if(first === "$" || first === "." || first === "*")
{
const xs = [];
const ys = [];
let x = 0;
let y = 0;
for(let j = 0; j < line.length; j++)
{
const piece = line[j];
if(piece === "$")
{
x = 0;
y++;
console.assert(y <= 8, "x");
}
else if(piece === ".")
{
x++;
console.assert(x <= 8, "y");
}
else if(piece === "*")
{
xs.push(x);
ys.push(y);
x++;
}
else if(piece === "\r")
{
}
else
{
console.assert(false, "Unexpected piece: '" + piece + "'");
}
}
// leaf
const node = universe.setup_field_recurse(
0,
xs.length - 1,
xs,
ys,
3
);
nodes.push(node);
}
else if(line === "")
{
}
else
{
// node
const parts = line.split(" ");
console.assert(parts.length === 5, "length");
let [level, nw, ne, sw, se] = parts;
level = +level;
nw = +nw;
ne = +ne;
sw = +sw;
se = +se;
console.assert(level >= 4);
console.assert(nw >= 0);
console.assert(ne >= 0);
console.assert(sw >= 0);
console.assert(se >= 0);
const nw_node = nw === 0 ? universe.empty_tree(level - 1) : nodes[nw];
const ne_node = ne === 0 ? universe.empty_tree(level - 1) : nodes[ne];
const sw_node = sw === 0 ? universe.empty_tree(level - 1) : nodes[sw];
const se_node = se === 0 ? universe.empty_tree(level - 1) : nodes[se];
console.assert(nw_node.level === level - 1);
console.assert(nw_node);
console.assert(ne_node);
console.assert(sw_node);
console.assert(se_node);
const node = universe.create_tree(nw_node, ne_node, sw_node, se_node);
nodes.push(node);
}
}
universe.root = nodes[nodes.length - 1];
return formats.parse_comments(text.substr(text.indexOf("\n") + 1), "#");
}
//if(false)
//{
// const vm = require("vm");
// const fs = require("fs");
//
// vm.runInThisContext(fs.readFileSync("./formats.js"), { filename: "formats.js" });
// vm.runInThisContext(fs.readFileSync("./life.js"), { filename: "life.js" });
//
// const text = fs.readFileSync("TetrisOTCAMP.mc", "utf8");
//
// load_macrocell(text);
//}