-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
523 lines (421 loc) · 19.6 KB
/
index.html
File metadata and controls
523 lines (421 loc) · 19.6 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!DOCTYPE html>
<meta charset="utf-8">
<html lang='en'>
<head>
<meta charset="UTF-8">
<title>Amorphous Computing Simulator</title>
</head>
<body>
<link rel="stylesheet" href="style.css">
<link rel='stylesheet' href='fonts/nunito_sans/nunito_sans.css'>
<link rel='stylesheet' href='fonts/open_sans/open_sans.css'>
<!-- <center><h1> PINGUIN </h1></center> -->
<center><img width=250px src="pinguin.png"></center>
<center><div class='subtitle'> Click or drag to interact with the simulation.
Use the drop-down menu to change the trigger mode.</div></center>
<div style='border: 1px solid black; width: 1240px; margin: auto;'>
<button onclick="setExample(0)" id='ex0btn' class='examplebtn'>EXAMPLE 0</button>
<button onclick="setExample(1)" id='ex1btn' class='examplebtn'>EXAMPLE 1</button>
<button onclick="setExample(2)" id='ex2btn' class='examplebtn'>EXAMPLE 2</button>
<button onclick="setExample(3)" id='ex3btn' class='examplebtn'>EXAMPLE 3</button>
<div align='right'>
<h5 id="stepnum">stepnum 0</h5>
<h5 id="fps">0 fps</h5>
<label class="custom-select"><select id="triggerMenu" name="options"></select></label>
</div>
<hr>
<div id="editor"></div>
<canvas id="myCanvas" width=600 height=615></canvas>
<hr>
<div align="center" style="margin:10px">
<button onclick="compile()" id='compilebtn' class="compilebtn">COMPILE</button>
<button onclick="startstop()" id='startstopbtn' class="startbtn">START</button>
<button onclick="reset()" id="resetbtn" class="resetbtn"> RESET </button>
</div>
<script src="ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src='scripts/cell.js' type="text/javascript" charset="utf-8"></script>
<script src='scripts/blob.js' type="text/javascript" charset="utf-8"></script>
<script src='scripts/tokenizer.js' type="text/javascript" charset="utf-8"></script>
<script src='scripts/compiler.js' type="text/javascript" charset="utf-8"></script>
<script src='scripts/helpers.js' type="text/javascript" charset="utf-8"></script>
<script src='scripts/main.js' type="text/javascript" charset="utf-8"></script>
</div>
<div class='readme'>
<!-- BEGIN README -->
<h2 id="gettingstarted">Getting Started</h2>
<h3 id="amorphouscomputing">Amorphous Computing</h3>
<p>Amorphous computing leverages the collective behavior of locally interacting
processing units to produce complex computation and behavior. Each cell
interacts only with its immediate neighbors by passing small messages called
<strong>pings</strong>. Cell behavior is programmed by defining how to respond to pings of
different types. The cells are programmed identically, but can store local state
information to differentiate behavior. Pings originate from cell neighbors or
from the user’s mouse events.</p>
<h3 id="thepingprogramminglanguage">The Ping Programming Language</h3>
<p>The simulation is programmed in Ping, a language designed specifically
for amorphous computing. Ping uses semantic whitespace, dynamic typing, and
deliberately peculiar syntax.</p>
<h3 id="basics">Basics</h3>
<p>This simple Ping program prints <code>hello world</code> to the browser’s debug console
when a cell is clicked.</p>
<pre><code class="ping">@hello_world()
PUTS 'hello world'
</code></pre>
<p>A more visual example is this Ping program that turns a cell green when clicked:</p>
<pre><code class="ping">@turn_green()
!'color' -> (0, 1, 0)
</code></pre>
<p>Ping definitions are marked with the <code>@</code> symbol. In this example, we define a
new ping called <code>turn_green</code> with no arguments. Upon receiving this ping, a cell
will set its <code>color</code> attribute to the RGB tuple <code>(0, 1, 0)</code>. The amorphous
simulator automatically renders this as the color green. In the simulator, you
can use this ping to change the color of a single cell by clicking on it. If
instead you wish for all cells to automatically turn green on reset, you can use
the special <code>_init</code> ping.</p>
<pre><code class="ping">@_init()
!'color' -> (0, 1, 0)
</code></pre>
<p>Data can be associated with a cell or with an individual ping with a
dictionary-like interface. Cell data is referenced with the <code>!</code> operator; for
example, <code>!'color'</code> returns the cell’s current color. Ping data is referenced
with the <code>?</code> operator; for example <code>?'color'</code> could be the color a cell is
instructed to change to. Here is an example of setting and accessing cell and
ping data:</p>
<pre><code class="ping">@propagate_red()
NQUE ._propagate_color((1, 0, 0))
@propagate_green()
NQUE ._propagate_color((0, 1, 0))
@propagate_blue()
NQUE ._propagate_color((0, 0, 1))
@_propagate_color(color)
IF !'color' <> ?'color' \ if not already the color
!'color' -> ?'color' \ change the color
PING .? \ re-broadcast the same ping to neighbors
</code></pre>
<p>The <code>NQUE</code> command pushes (enqueues) a new ping to the current cell’s own queue.
The <code>PING</code> command pushes the specified ping to all of the cell’s neighbors'
queues. The first three methods can be triggered by the user, but the fourth
method is hidden from the user because it begins with an underscore. It is
advisable to make any methods with arguments hidden. (An unspecified ping
arguments will default to <code>NULL</code>). Here, the <code>propagate_red</code>, <code>propagate_green</code>,
and <code>propagate_blue</code> methods all simply enqueue a new ping of type
<code>_propagate_color</code> with different arguments. The hidden method checks if the
cell’s color has already been changed (<code><></code> is the “not equals” operator); if
not, it modifies its color to that specified by the ping (<code>-></code> is the assignment
operator) and sends copies of the same message to its neighbors. Note that the
last line is a shorthand equivalent to <code>PING ._propagate_color(?'color')</code>.
Inline comments begin with a backlash.</p>
<p>In the program above, we checked if the color had already been changed before
propagating the message further. Consider what would have happened if we
excluded this check; since every step would multiply the number of pings in
circulation, the total number of pings would grow exponentially. Even after all
the cells had changed colors, extraneous pings would continue flying about left
and right. Such “ping explosions” are the amorphous computing equivalent of
infinite loops.</p>
<h3 id="example1-globalgeometry">Example 1 - Global Geometry</h3>
<p>Example 1 demonstrates the ability to measure long-range distances by drawing
a Vornoi pattern from cells.</p>
<pre><code class="ping">@_init()
!'id' -> RAND
\ place a Vornoi cell vertex
@vertex()
NQUE ._vertex('dist_' + !'id', 0, (RAND, RAND, RAND))
@_vertex(id, dist, color)
IF !?'id' == NULL || !?'id' >= ?'dist'
!?'id' -> ?'dist'
?'dist' += 1
NQUE ._color_if_closest(?'id', ?'color')
PING .?
\ match the color of the closest vertex
@_color_if_closest(id, color)
dist -> !?'id'
is_closest -> 1
LOOP KEYS WITH key IF [key <= 'dist' && key <> ?'id']
IF !key <= dist
is_closest -> 0
IF is_closest == 1
!'color' -> ?'color'
</code></pre>
<p>Ping can loop over iterables with a supplied temporary variable. The syntax
<code>LOOP <iterable> WITH <variable></code> is equivalent to Python’s
<code>for <variable> in <iterable></code>. To compactly iterate over a subset of
the iterable, use the syntax <code>LOOP <iterable> WITH <variable> IF <conditional></code>
(this is similar to Python’s list comprehension).
Iterable objects include tuples like <code>(1, 2, 3)</code> and the builtins <code>KEYS</code> and
<code>VALS</code> which iterate over the cell data keys and values respectively. <code>RAND</code>
returns a random number between 0 and 1.</p>
<p>When a cell modifies the data associated with a ping (e.g. <code>?'radius' -= 1</code>),
it does not affect any similar pings in the queues of neighboring cells. In
other words, the <code>PING</code> command creates copies of the local ping object to put
in the queues of neighboring cells.</p>
<h3 id="example2-differentiatedbehavior">Example 2 - Differentiated Behavior</h3>
<p>Example 1 demonstrates the use of local state to support cell differentiation.</p>
<pre><code class="ping">@_init()
start_color -> (0.516, 0.902, 0.106)
!'color' -> start_color
!'reset_color' -> start_color
!'layer_zero' -> 'standby'
\ paint a region of layer one
@layer_one()
NQUE ._layer((0.154, 0.583, 1.0), 'layer_one', 3)
\ paint a region of layer two
@layer_two()
NQUE ._layer((1, 0.64, 0.04), 'layer_two', 3)
@_layer(color, layer, radius)
IF ?'radius' >> 0
!'color' -> ?'color'
!'reset_color' -> ?'color'
!?'layer' -> 'standby'
!'layer_zero' -> NULL
?'radius' -= 1
PING .?
\ create a single propagating pulse
@pulse()
color -> (RAND, RAND, RAND)
LOOP KEYS WITH key IF key <= 'layer_'
NQUE ._pulse_color(color, key)
@_pulse_color(color, layer)
IF !?'layer' == 'standby'
!?'layer' -> 'pulsing'
NQUE ._reset_color(?'layer', 1)
!'color' -> ?'color'
PING .?
@_reset_color(layer, delay)
IF !?'layer' == 'pulsing'
IF ?'delay' >> 0
?'delay' -= 1
NQUE .?
ELSE
!'color' -> !'reset_color'
!?'layer' -> 'standby'
\ create node emanating repeated pulses
@blinker()
NQUE .pulse()
NQUE ._blink_delay(5)
@_blink_delay(delay)
IF ?'delay' <= 0
NQUE .blinker()
ELSE
?'delay' -= 1
NQUE .?
</code></pre>
<p>In addition to explosions, collisions between multiple propagating pings are a
common cause of unexpected behavior. In Example 3, we use the <code>!'reset_color'</code>
attribute to ensure the proper color is obtained.</p>
<p>In Ping, booleans only exist implicitly inside of conditionals. This means that
variables cannot be assigned boolean values and there are no boolean primitives;
instead, you should use descriptive strings to store binary states.</p>
<h3 id="example3-gradientdescent">Example 3 - Gradient Descent</h3>
<p>Example 3 demonstrates an optimization problem using gradient descent where
multiple particles can parallelize the traversal of a global loss function.
It is also reminiscent of biological cells navigating a chemical gradient.</p>
<pre><code class="ping">@_init()
!'id' -> RAND
!'reset_delay' -> 0
NQUE .__reset_color()
\ this looping ping should only be called in init
@__reset_color()
IF !'reset_delay' >> 0
!'reset_delay' -= 1
ELIF !'grad' <> NULL
r -> 0.5 + [0.4 / !'grad']
g -> 0.9
b -> 0.1 + [0.8 * [1 - 1 / !'grad']]
!'color' -> (r, g, b)
NQUE .__reset_color()
@gradient()
NQUE ._gradient(1)
@_gradient(grad)
IF !'grad' == NULL || !'grad' >= ?'grad'
!'grad' -> ?'grad'
?'grad' += 1
\ NQUE ._reset_color(3)
PING .?
\ spawn a new searching particle
@spawn()
NQUE ._spawn(!'id', (RAND, RAND, RAND), 0)
@_spawn(id, color)
IF !'grad' <> 1
IF !'state' == NULL && ?'id' == !'id'
PUTS !'id' + ' is active'
!'state' -> 'alive'
!'color' -> ?'color'
!'reset_delay' -> 3
PING ._poll_grad(!'color')
\ poll neighboring cells to estimate local gradient
@_poll_grad(color)
IF !'state' == NULL
!'color' -> ?'color'
!'reset_delay' -> 3
PING ._reply_grad(!'id', !'grad')
\ step in a direction of non-increasing gradient
@_reply_grad(id, grad)
IF !'state' == 'alive'
IF ?'grad' << !'grad'
!'state' -> NULL
PING ._spawn(?'id', !'color')
</code></pre>
<p>Ping uses square brackets <code>[</code> and <code>]</code> to disambiguate order of operations. When
ambiguity arises, Ping always executes operations right to left; there are no
operation priorities. For example <code>1 + 2 * 3</code> is equivalent to <code>1 + [2 * 3]</code>,
which evaluates to <code>7</code>, while <code>[1 + 2] * 3</code> evaluates to <code>9</code>.</p>
<h2 id="languagereference">Language Reference</h2>
<h3 id="keywords">Keywords</h3>
<pre><code class="ping">PUTS \ prints to the browser console log
PING \ send copies of ping to all neighboring cells
NQUE \ enqueues ping to current cell's queue
IF \ starts an if conditional
ELIF \ starts an else-if conditional
ELSE \ starts an else conditional
LOOP \ used with the WITH keyword for loops
WITH \ used with the LOOP keyword for loops
</code></pre>
<h3 id="constants">Constants</h3>
<pre><code class="ping">RAND \ random number between 0 and 1
NULL \ default value associated with all unassigned keys
INFT \ infinity evaluates larger than any other number
KEYS \ list of keys in local cell data
VALS \ list of values in local cell data
</code></pre>
<h3 id="symbolsandoperators">Symbols and Operators</h3>
<pre><code class="ping">&& \ logical and
|| \ logical or
== \ equal (data, not reference)
<> \ not equal
>> \ greater than
<< \ less than
>= \ ints: less than or equal to, strings: ends with
<= \ ints: greater than or equal to, strings: starts with
+ \ ints: addition, strings: concatenation
- \ ints: subtraction, strings: undefined
* \ multiplication
/ \ division
+= \ increase by
-= \ decrease by
*= \ multiply by
/= \ divide by
-> \ assignment (a -> assigns a the value of b)
! \ cell data accessor
? \ ping data accessor
@ \ new ping definition
. \ new ping instantiation
( \ tuple or arg list open
) \ tuple or arg list close
[ \ operation grouping open
] \ operation grouping close
\ \ start of inline comment
</code></pre>
<h3 id="grammar">Grammar</h3>
<p>Following is a context-free grammar for the Ping programming language in
Backus-Naur form.</p>
<ul>
<li><code><identifier></code> is a variable name (a letter or underscore followed by letters
underscores and numbers).</li>
<li><code><number></code> is an integer or floating point decimal value.</li>
<li><code><string></code> is a string literal enclosed in single or double quotes.</li>
<li>The pre-compiler tokenizes the input and converts indentation changes
into <code>"{"</code> and <code>"}"</code>braces. The grammar below contains brace symbols from the
tokenized pre-compiler output.</li>
</ul>
<pre><code class="code"><ping-defs> ::= "" | <ping-defs> <ping-defs>
| "@" <identifier> "(" <arg-list> ")" <block>
<arg-list> ::= "" | <identifier> | <identifier> "," <arg-list>
<block> ::= "{" <statements> "}"
<statements> ::= <statements> <statements>
| "NQUE" <ping> | "PING" <ping>
| <if> | <loop>
| <value> <assignment> <expression>
| "PUTS" <expression>
<ping> ::= "." <identifier> <tuple>
| "." "?"
<if> ::= "IF" <boolean> <block> <elifs> <else>
<elifs> ::= "" | "ELIF" <boolean> <block> <elifs>
<else> ::= "" | "ELSE" <boolean> <block>
<loop> ::= "LOOP" <data> "WITH" <variable> <block>
| "LOOP" <data> "WITH" <variable> "IF" <boolean> <block>
<boolean> ::= <data> <comparator> <data>
| <boolean> "&&" <boolean>
| <boolean> "||" <boolean>
| "[" <boolean> "]"
<comparator> ::= "==" | "<<" | ">>" | "<=" | ">="
<value> ::= <identifier>
| "!" + <data>
| "?" + <string>
<assignment> ::= "->" | "+=" | "-="
<expression> ::= <data>
| <expression> <bin-op> <expression>
| "[" <expression> "]"
<bin-op> ::= "+" | "-" | "*" | "/"
<data> ::= "RAND" | "NULL" | "IMFT" | "KEYS" | "VALS"
| <number> | <string>
| "(" <data-list> ")"
| <value>
<data-list> ::= "" | <data> | <data> "," <data-list>
</code></pre>
<h3 id="additionalnotes">Additional Notes</h3>
<p>Pinguin was inspired by the work of Hal Abelson, Daniel Coore, Radhika Nagpal,
Erik Rauch, Ron Weiss, and other amorphous computing researchers.</p>
<p>Cells are distributed using an implementation of Mitchell’s Best Candidate
algorithm for Poisson-disc sampling (based on Mike Bostock’s <a href="https://bl.ocks.org/mbostock/d7bf3bd67d00ed79695b">d3.js implementation</a>).</p>
<p>This project is in beta; please report issues <a href="https://github.com/parkersruth/amorphous/issues">here</a>.</p>
<!-- END README -->
</div>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/pinguin");
editor.session.setMode("ace/mode/ping");
editor.setOptions({
tabSize: 2,
highlightActiveLine: false,
highlightGutterLine: false,
})
editor.renderer.setOptions({
highlightGutterLine: false
})
editor.renderer.setScrollMargin(10, 10);
editor.$blockScrolling = Infinity;
for (elem of document.getElementsByClassName('ping')) {
let stuff = elem.innerHTML;
stuff = stuff.substring(0, stuff.lastIndexOf("\n")); // trim last line
const code = resymbolize(stuff);
editor = ace.edit(elem)
editor.setTheme('ace/theme/pinguin');
editor.session.setMode('ace/mode/ping');
editor.setOptions({
tabSize: 2,
highlightActiveLine: false,
highlightGutterLine: false,
maxLines: 100
})
editor.$blockScrolling = Infinity;
editor.renderer.$cursorLayer.element.style.display = "none"
editor.renderer.setScrollMargin(10, 10)
editor.setValue(code, -1);
editor.setReadOnly(true);
}
for (elem of document.getElementsByClassName('code')) {
let stuff = elem.innerHTML;
stuff = stuff.substring(0, stuff.lastIndexOf("\n")); // trim last line
const code = resymbolize(stuff);
editor = ace.edit(elem)
editor.setTheme('ace/theme/pinguin');
editor.session.setMode('ace/mode/ping');
editor.setOptions({
tabSize: 2,
highlightActiveLine: false,
highlightGutterLine: false,
maxLines: 100
})
editor.$blockScrolling = Infinity;
editor.renderer.$cursorLayer.element.style.display = "none"
editor.renderer.setShowGutter(false);
editor.setShowPrintMargin(false);
editor.renderer.setScrollMargin(10, 10);
editor.setOption("displayIndentGuides", false);
editor.setValue(code, -1);
editor.setReadOnly(true);
}
</script>
</body>
<footer></footer>
</html>