-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
522 lines (445 loc) · 11.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="The first language I made.">
<meta property="og:locale" content="en_US">
<meta property="og:title" content="Toy Lang">
<meta property="og:type" content="programming language">
<meta property="og:url" content="https://openhome.cc/Gossip/Computation/toy_lang/index.html">
<meta property="og:image" content="https://openhome.cc/Gossip/images/caterpillar_small.jpg">
<meta property="og:site_name" content="OPENHOME.CC">
<meta property="og:description" content="The first language I made.">
<title>Toy Lang</title>
<style type="text/css">
textarea {
width: 100%;
height: 300px;
resize: none;
overflow: auto;
background: #000;
color: whitesmoke;
}
li {
color: brown;
font-family: Arial, Helvetica, sans-serif;
}
</style>
<link rel="stylesheet" href="https://openhome.cc/Gossip/css/caterpillar.css">
<script async src="https://openhome.cc/Gossip/google-code-prettify/run_prettify.js"></script>
</head>
<body>
<a href="https://openhome.cc">
<img src="https://openhome.cc/Gossip/images/caterpillar_small_front.jpg">
</a>
<p>View it on
<a href="https://github.com/JustinSDK/toy_lang">Github</a>. Toy lang is the first language I made. ES6 modules support is required. </p>
<hr>
<h1>Playground</h1>
<textarea id="code"># Is a language easier if 'Hello World' is easier?
import '/lib/toy'
println('Hello World')
println('Hello {0}'.format('World'))
println(String.format('Hello {0}', 'World'))
toy.hello()</textarea>
<br>
<br>
<button id="run">Run It</button>
<br>
<br>
<textarea readonly="readonly" id="tty" style="background: #000;color:#fff;"></textarea>
<hr>
<h1>Examples (<a href="https://github.com/JustinSDK/toy_lang/tree/master/toy_lang/examples">More examples</a>)</h1>
<ul>
<li># Multiplication Table</li>
</ul>
<pre class="prettyprint"><code>def print_row(n) {
i = 2
while i < 10 {
print('{0}*{1}={2}\t'.format(i, n, i * n))
i += 1
}
println()
}
iterate(1, 10).forEach(print_row)</code></pre>
<ul>
<li># Tower of Hanoi</li>
</ul>
<pre class="prettyprint"><code>def hanoi(n, a, b, c) {
if n == 1 {
println('Move sheet from {0} to {1}'.format(a , c))
}
else {
hanoi(n - 1, a, c, b)
hanoi(1, a, b, c)
hanoi(n - 1, b, a, c)
}
}
hanoi(3, 'A', 'B', 'C')</code></pre>
<ul>
<li># Factorial</li>
</ul>
<pre class="prettyprint"><code>def factorial(n) {
if n == 0 {
return 1
}
return n * factorial(n - 1)
}
# use \ to wrap long lines
range(1, 6).map(n -> '{0}! = {1}'.format(n, factorial(n))) \
.forEach(println)
</code></pre>
<ul>
<li># Tricolour</li>
</ul>
<pre class="prettyprint"><code>def adjust(flags) {
b = 0
w = 0
r = flags.length() - 1
while flags.get(w) == 'B' and w < flags.length() {
w += 1
}
while flags.get(r) == 'R' and r > 0 {
r -= 1
}
while w <= r {
switch flags.get(w) {
case 'W'
w += 1
case 'B'
flags.swap(b, w)
w += 1
b += 1
default
flags.swap(r, w)
r -= 1
}
}
return flags
}
flags = 'RWBBWRWR'.split('')
println(adjust(flags))</code></pre>
<ul>
<li># Class</li>
</ul>
<pre class="prettyprint"><code>class AccountException(Exception) {
def init() {
this.super(Exception, 'init', arguments)
}
}
class Account {
# it's a field
balance = 0
def init(number, name) {
this.number = number
this.name = name
}
def deposit(amount) {
if amount <= 0 {
throw new AccountException('must be positive')
}
this.balance += amount
}
def toString() {
return '{0}, {1}, {2}'.format(this.number, this.name, this.balance)
}
}
acct = new Account('123', 'Justin')
acct.deposit(100)
println(acct)
try {
acct.deposit(-100)
}
catch e {
e.printStackTrace()
}</code></pre>
<ul>
<li># Built-in Classes</li>
</ul>
<pre class="prettyprint"><code>def sum(lt) {
return 0 if lt.isEmpty() else (lt.get(0) + sum(lt.slice(1)))
}
lt = range(1, 11)
println('{0}={1}'.format(lt.join('+'), sum(lt)))
println((new String('aBc')).toUpperCase())
println((new String('aBc')).toLowerCase())</code></pre>
<ul>
<li># Closure</li>
</ul>
<pre class="prettyprint"><code>def foo() {
x = 10
def inner(y) {
# closure closes the variable x, not its value
return x + y
}
x = 30
return inner
}
f = foo()
println('f(20) is ' + f(20))
def orz() {
x = 10
class Inner {
y = x
def init(p) {
# closure closes the variable x, not its value
this.z = x + p
}
def x() {
# closure closes the variable x, not its value
return x
}
}
x = 30
return Inner
}
clz = orz()
obj = new clz(20)
println('obj.x() is ' + obj.x())
println('obj.y is ' + obj.y)
println('obj.z is ' + obj.z)
def foo2(x) {
def getX() {
return x
}
def setX(v) {
nonlocal x = v
}
return [getX, setX]
}
accessor = foo2(10)
getX = accessor.get(0)
setX = accessor.get(1)
println(getX())
setX(100)
println(getX())</code></pre>
<ul>
<li># Lambda expression</li>
</ul>
<pre class="prettyprint"><code>max = (n1, n2) -> n1 if n1 > n2 else n2
println(max(10, 20))
# you may also use parentheses to wrap wrapping long lines
([1, 2, 3, 4, 5].filter(elem -> elem >= 2)
.map(elem -> elem * 100)
.forEach(println))
(range(1, 10)
.map(n -> range(2, 10).map(i -> '{0}*{1}={2}'.format(i, n, i * n)).join('\t'))
.forEach(println))
def foo3(x, y) {
return () -> x + y
}
println(foo3(10, 20)())
def orz() {
x = 10
return (y, z) -> x + y + z
}
println(orz()(100, 200))
# IIFE
(() -> println('XD'))()
(x -> println(x))('XD')
println(((x, y) -> x + y)(1, 2))</code></pre>
<ul>
<li># Mixin</li>
</ul>
<pre class="prettyprint"><code>class Ordered {
def lessThan(that) {
return this.compare(that) < 0
}
def lessEqualsThan(that) {
return this.lessThan(that) or this.equals(that)
}
def greaterThan(that) {
return not this.lessEqualsThan(that)
}
def greaterEqualsThan(that) {
return not this.lessThan(that)
}
}
class Circle {
def init(radius) {
this.radius = radius
}
def compare(that) {
return this.radius - that.radius
}
def equals(that) {
return this.radius == that.radius
}
}
Circle.mixin(Ordered)
c1 = new Circle(10)
c2 = new Circle(20)
println(c1.lessThan(c2))
println(c1.lessEqualsThan(c2))
println(c1.greaterThan(c2))
println(c1.greaterEqualsThan(c2))</code></pre>
<ul>
<li># Inheritance 1</li>
</ul>
<pre class="prettyprint"><code>class PA {
def init() {
println('PA init')
}
def ma(x, y) {
println(x)
println(y)
}
}
class PB {
def mb() {
println('mb')
}
}
class C(PA, PB) {
def init() {
this.super(PA, 'init')
println('C init')
}
def mc() {
println('mc')
}
def ma(x, y) {
this.super(PA, 'ma', arguments)
println('c.ma()')
}
}
c = new C()
c.ma(10, 20)
c.mb()
c.mc()</code></pre>
<ul>
<li># Inheritance 2</li>
</ul>
<pre class="prettyprint"><code>class Ordered {
def lessThan(that) {
return this.compare(that) < 0
}
def lessEqualsThan(that) {
return this.lessThan(that) or this.equals(that)
}
def greaterThan(that) {
return not this.lessEqualsThan(that)
}
def greaterEqualsThan(that) {
return not this.lessThan(that)
}
}
class Circle(Ordered) {
def init(radius) {
this.radius = radius
}
def compare(that) {
return this.radius - that.radius
}
def equals(that) {
return this.radius == that.radius
}
}
c1 = new Circle(10)
c2 = new Circle(20)
println(c1.lessThan(c2))
println(c1.lessEqualsThan(c2))
println(c1.greaterThan(c2))
println(c1.greaterEqualsThan(c2))</code></pre>
<ul>
<li># meta programming 1</li>
</ul>
<pre class="prettyprint"><code>println(Object.ownMethods())
Object.deleteOwnMethod('toString')
println(Object.ownMethods())
def toString() {
props = this.ownProperties()
# each prop is a List instance which contains name and value
return props.map(prop -> prop.join()).join('\n')
}
o1 = new Object()
o1.x = 1
o1.y = 2
o1.z = 3
println(toString.apply(o1))
(o2 = new Object([
['x', 10],
['y', 20]
]))
println(toString.apply(o2))
def foo4(p) {
return this.x + this.y + this.z + p
}
# The 2nd parameter of the apply method accepts a List instance.
println(foo4.apply(o1, [40]))</code></pre>
<ul>
<li># meta programming 2</li>
</ul>
<pre class="prettyprint"><code>class PA {
def pa() {
println('pa')
}
}
class PB {
def pb() {
println('pb')
}
}
class C {
def c() {
println('c')
}
}
println(C.parents())
C.parents([PA, PB])
println(C.parents())
(new C()).pa()
(new C()).pb()
(new C()).c()
def toString() {
return this.class().name()
}
Orz = new Class('Orz', [PA, PB], [toString])
orz = new Orz()
orz.pa()
orz.pb()
println(orz)</code></pre>
<hr>
<center>
<a href="https://openhome.cc">openhome.cc</a>
</center>
<script type="module">
import {Module} from './toy_lang/js/module.js';
const TOY_MODUEL_PATH = 'toy_lang';
const ENVIRONMENT = {
input(message) {
const msg = prompt(message, '');
if(msg) {
return msg;
}
return '';
},
output(value) {
tty.textContent += value;
},
TOY_MODUEL_PATH,
readFile : function(fileName) {
return fetch(fileName)
.then(resp => {
if(resp.ok) {
return resp.text();
}
throw new Error(`GET ${fileName} ${resp.status} (${resp.statusText})`);
})
.then(code => [fileName, code]);
}
};
Module.initialize(ENVIRONMENT)
.then(_ => playToy());
function playToy() {
document.getElementById('run').addEventListener('click', _ => {
let code = document.getElementById('code').value;
let tty = document.getElementById('tty');
tty.textContent = '';
Module.run('/main.toy', code);
});
}
</script>
</body>
</html>