-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
453 lines (424 loc) · 18.9 KB
/
index.html
File metadata and controls
453 lines (424 loc) · 18.9 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>T-Regs Programming Language</title>
<script src="vendor/jquery.js"></script>
<script src="vendor/sugar.js"></script>
<script src="lib/xregexp/build/xregexp-min.js"></script>
<script src="lib/codemirror/lib/codemirror.js"></script>
<script src="codemirror-mode/tregs.js"></script>
<script src="util.js"></script>
<script src="editor.js"></script>
<script src="executionstate.js"></script>
<script src="program.js"></script>
<script src="interpreter.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" href="lib/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="tabBar" id="menu">
<h1><a href="#home">T-Regs</a></h1>
<ul>
<li><a href="#spec">Spec</a></li>
<li><a href="#demo">Demo</a></li>
<li><a href="#interpreter">Interpreter</a></li>
<li><a href="#about">About</a></li>
</ul>
</div>
<div id="mainSections">
<div id="home" class="content active">
<h2>The <em>T-Regs</em> Programming Language</h2>
<p>
T-Regs is a Next Generation Regular Expression Based Programming Language,
brought to you by the world leader in Next Generation Regular Expression Based Programming Languages.
</p>
<p>
T-Regs (Pronounced "T-Regs") stands for <em>T</em>ext and <em>Reg</em>ular Expression<em>s</em>.
We feel strongly that free text is the most natural way of expressing one's thoughts.
Therefore T-Regs does away with the restrictions found in traditional programming languages and
is entirely based on free text. Free text which is conveniently manipulated by lots and lots of regular expressions.
</p>
<h2>Quick examples</h2>
<h3>Hello world</h3>
<p>
See, pretty straightforward. <a href="#" data-load-demo="helloWorld">You can try this out right here.</a>
</p>
<div><code>10 /.*/Hello world!/</code></div>
<h3>Subtracting 2 numbers</h3>
<p>This is admittedly more advanced, but will become clear enough once you <a href="#" data-load-demo="subtractingTwoNumbers">step through it in the online interpreter.</a></p>
<div>
<code id="subtractingTwoNumbers" data-input="12-5"># Goto main program (skip functions)
1 /.*/$0;1000/
2 /^(.*?);(?<goto>1000)$/$1/
# Minus 1
# input: number;returnlabel;rest
# output: result;rest
10 /^(\d+)(;.*)$/$1;15$2/
15 /^(?<left>[1-9]\d*?-*)0(?=0*;)(?<right>.*(?<goto>15).*)/${left}-${right}/
20 /(\d*);15(;.*)/$1;0123456789$2/
25 /^(\d*)(?<lastDigit>\d)(?<lookup>-*;\d*(?<newLastDigit>\d)\2.*)(;.*)$/$1${newLastDigit}${lookup}$5/
30 /(\d*);\d+(;.*)/$1;35$2/
35 /-(?=-*;)(?<right>.*(?<goto>35).*)/9${right}/
40 /^0+([^0;])/$1/
45 /^(\d*);[^;]+;(?<goto>\d+)(;.*)$/$1$3/
# Main calculate "a - b" for positive integers
# infinite loop when b>9
1000 /^(\d+)\s*-\s*(\d+)$/$1;$2;0;1050/
1003 /^(\d+);([^0]\d*);\1;(?<goto>\d+)$/-$2/
1005 /^(\d+);(\d+);\2;(?<goto>\d+)$/$1/
1010 /^(\d+);(\d+).*$/10;$1;1020;$2/
1015 /^(?<goto>10);(.*)$/$2/
1020 /^(\d+);(\d+)$/10;$2;1040;$1/
1030 /^(?<goto>10);(.*)$/$2/
1040 /^(\d+);(\d+)$/$2-$1;1000/
1045 /^(\d+-\d+);(?<goto>\d+)$/$1/
1050 /^(\d+).*$/$1/</code>
</div>
<p>
<a href="#spec">Learn more about the syntax</a>
</p>
<h2>Taking it further</h2>
<p>
The T-Regs Language Commitee will soon set forth guidelines for the Certified T-Regs Programmer Initiative.
Follow <a href="https://twitter.com/ProgramTRegs">@ProgramTRegs</a> for further information.
</p>
</div>
<div id="spec" class="content">
<h2>The <em>T-Regs</em> Language <em>Specification</em></h2>
<h2>Overview</h2>
<p>
T-Regs programs consist of lines of find and replace instructions,
which are applied to one body of text, which exists as long as the program runs.
</p>
<p>
This body of text is either input passed to the program on startup or an empty string if no input was defined.
</p>
<p>
When the program has executed its last line, this text is returned as output.
</p>
<h2>Syntax</h2>
<h3>Comments start with a <em>hash</em>.</h3>
<div><code><em>#</em> this is a comment</code></div>
<h3>Code starts with a <em>unique numeric label</em> like this:</h3>
<div><code><em>10</em> /.*/Hello World/</code></div>
<h3>followed by a <em>find</em> regular expression</h3>
<div><code>10 /<em>.*</em>/Hello World/</code></div>
<h3>and a <em>replace</em> string.</h3>
<div><code>10 /.*/<em>Hello World</em>/</code></div>
<h3>These are separated by <em>slashes</em></h3>
<div><code>10 <em>/</em>.*<em>/</em>Hello World<em>/</em></code></div>
<h3>and optionally followed by <em>flags</em></h3>
<div><code>10 /^(?<greetings>h(a?i|ello)).*$/${greetings} world/<em>in</em></code></div>
<h2>Program flow</h2>
<h3>The lines in a program are executed in order of their numerical label.</h3>
<p>The program</p>
<div>
<code><em>10</em> /.*/aaa/
<em>30</em> /.*/bbb/
<em>20</em> /.*/ccc/</code>
</div>
<p>
will be executed in order <tt>10, 20, 30</tt> and as such result in <tt>bbb</tt>.
</p>
<h3>And then there was <em>goto</em>…</h3>
<p>All looping and decision making is done using <em>goto</em>.</p>
<p>If a find has a named capture "<em>goto</em>", the program will attempt to jump to that label.</p>
<div>
<code>10 /.*/hello <em>40</em>/
20 /(?<greeting>\w+) <em>(?<goto>\d+)</em>/${greeting}/
30 /.*/$0 cruel/
<em>40</em> /.*/$0 world/
# output: "hello world"</code>
</div>
<p>In this example at label 20 the value "40" is captured with name goto and the program will jump to label 40 as a consequence.
<h2>Regular Expression Specifics</h2>
<h3>The Regex dialect</h3>
<p>used in this implementation is the one as implemented by <a href="http://xregexp.com/">xregexp</a>.</p>
<p>The important thing to know is that it has named captures which are used like this:</p>
<div>
<code># given an input "3 + 4"
# outputs "4 + 3"
10 /^<em>(?<left></em>\d+<em>)</em>(?<operator>\s*[-+\/*]\s*)(?<right>\d+)$/<br>${right}${operator}<em>${left}</em>/</code>
</div>
<p>But of course nameless captures are equally fine.</p>
<div>
<code># given an input "3 + 4"
# outputs "4 + 3"
10 /^<em>(</em>\d+<em>)</em>(\s*[-+\/*]\s*)(\d+)$/$3$2<em>$1</em>/</code>
</div>
<h3>Matching behaviour</h3>
<p>By default the regular expression is case sensitive and applied only once</p>
<div>
<code># input "aaaa"
10 /<em>a</em>/<em>b</em>/
# results in "<em>b</em>aaa"</code>
</div>
<div>
<code># input "AAAA"
10 /<em>a</em>/<em>b</em>/
# results in "AAAA"</code>
</div>
<h3>By setting <em>flags</em> the matching behaviour can be modified.</h3>
<h3><em>g</em> is for <em>global</em></h3>
<div>
<code># input "aaaa"
10 /a/b/<em>g</em>
# results in "<em>bbbb</em>"</code>
</div>
<h3><em>i</em> is for <em>ignore case</em></h3>
<div>
<code># input "AAAA"
10 /a/b/<em>i</em>
# results in "<em>b</em>AAA"</code>
</div>
<h3><em>m</em> is for <em>multiline anchors</em></h3>
<p>
Which makes <em>^</em> and <em>$</em> match at the begining and end of lines, rather than at the begining and end of the whole string.
So this:
<div>
<code># input "aa\naa"
10 /<em>^</em>a/b/g<em>m</em>
# results in "<em>b</em>a\n<em>b</em>a</code>
</div>
<p>instead of this:</p>
<div>
<code># input "aa\naa"
10 /^a/b/g
# results in "<em>b</em>a\naa"</code>
</div>
<h3><em>n</em> is for <em>explicit capture</em></h3>
<p>Making only named groups capturing groups.</p>
<p>
In the following example the <em>second group</em> does not capture.
This is a great technique for writing highly performant T-Regs
and is widely used in enterprise applications.
</p>
<div><code>10 /^(?<greetings>H<em>(</em>a?i|ello<em>)</em>).*$/${greetings} World/<em>n</em></code></div>
<p>The above is basically a shorthand for this:</p>
<div><code>10 /^(?<greetings>H<em>(?:</em>a?i|ello<em>)</em>).*$/${greetings} World/</code></div>
<h3><em>s</em> is for <em>singleline</em></h3>
<p>
where dot matches all characters, including linebreaks.
So we get this:
</p>
<div>
<code># input "aaa\nbbb"
10 /./c/g<em>s</em>
# results in "<em>ccccccc</em>"</code>
</div>
<p>Instead of this:</p>
<div>
<code># input "aaa\nbbb"
10 /./c/g
# results in "<em>ccc</em>\n<em>ccc</em>"</code>
</div>
<h3>For more information on regular expressions we recommend</h3>
<p>
<a href="http://www.regular-expressions.info/">
regular-expressions.info
</a>
</p>
</div>
<div id="demo" class="content">
<h2><em>T-Regs</em> demonstration code</h2>
<h3>Hello world <a href="#" data-load-demo="helloWorld">run</a></h3>
<p>Observe T-Regs' <em>concise syntax</em>.</p>
<div><code id="helloWorld">10 /.*/Hello world!/</code></div>
<h3>Object Oriented T-Regs <a href="#" data-load-demo="oo">run</a></h3>
<p>Being a modern language, T-Regs is absolutely <em>multi paradigm</em> and adapts well to an <em>object-oriented</em> programming style.</p>
<div>
<code id="oo" data-input='{\n\t"greetings" : "Hello",\n\t"planet" : "world"\n}'>10 /"greetings"\s*:\s*"(?<greetings>[^"]+)"\s*,\s*"planet"\s*:\s*"(?<planet>[^"]+)"/"output" : "${greetings} ${planet}"/s</code>
</div>
<h3>Palindrome checker <a href="#" data-load-demo="palindrome">run</a></h3>
<p>T-Regs is exceptionally well suited for <em>natural language processing</em> tasks, as demonstrated here.</p>
<div>
<code id="palindrome" data-input="racecar">### Palindrome
10 /.*/$0;20;yes,no/
20 /^(?<ZeroOrOneCharacter>.?;.*(?<yes>yes))|(?<FirstSameAsLast>(.)(?<middle>.*)\4;(?<goto>20)(?<remainder>.*))|(?<NotAPalindrome>.*(?<no>no)).*$/${yes}${no}${middle};${goto}${remainder}/
30 /^(?<output>[^;]+);.*/${output}/</code>
</div>
<h3>Conway's Game of Life <a href="#" data-load-demo="gameOfLife">run</a></h3>
<p>T-Regs is gaining popularity in the <em>scientific community</em>. It has become to be recognized as a <em>pragmatic tool</em> for a wide variety of simulations. It's expressive power is illustrated here by this 13 line Game of Life implementation.</p>
<div>
<code id="gameOfLife" data-input="------##--------------------------------
-##---##----##--------#----#-#----#-----
##----------##--------#----#-#----#-----
-#--------------------#----#-#----#-----
-------###--------------###---###-------
----------------------------------------
------------------------###---###-------
----------------------#----#-#----#-----
-#---#-#----#--#------#----#-#----#-----
--#---##--#-#---##----#----#-#----#-----
###---#----##--##-----------------------
------------------------###---###-------
----------------------------------------
#-------------------------------------##
#-----####----------------------------#-
#------####---------------------------##
----------------------------------------
----------------------------------------
----##----------------------------------
----##------------------###---###-------">1 /^([^\n]+).*\n([^\n]+)$/$2\n$0\n$1/s
2 /^(.).*(.)$/$2$0$1/mg
3 /(#)|-/$0w$1W/g
4 /w(?=\W*W(?:\Ww\W*W\n?){0}#)/w#/gs
5 /w(?=\W*W(?:\Ww\W*W\n?){1}#)/w#/gs
6 /w(?=\W*W(?:\Ww\W*W\n?){41}#)/w#/gs
7 /w(?=\W*W(?:\Ww\W*W\n?){43}#)/w#/gs
8 /w(?=\W*W(?:\Ww\W*W\n?){83}#)/w#/gs
9 /w(?=\W*W(?:\Ww\W*W\n?){84}#)/w#/gs
10 /w(?=\W*W(?:\Ww\W*W\n?){85}#)/w#/gs
11 /\Ww(?:###|#(#))W(?=(?:\Ww\W*W\n?){42}\1)/#/gs
12 /\Ww#*W/-/g
13 /(?:\n[^\n]+){2}$|.{2}(?=\n|$)//gs</code>
</div>
<h3>99 Bottles of beer <a href="#" data-load-demo="bottlesOfBeer">run</a></h3>
<p>T-Regs can hold up a conversation, while attentively commenting on real world events:</p>
<div>
<code id="bottlesOfBeer">### 99 Bottles of beer
1 /.*/100/
2 /(?<goto>.*)//
# Minus 1
# input: number;returnlabel;rest
# output: result;rest
10 /^(\d+)/$1;15/
15 /^(?<left>[1-9]\d*?-*)0(?=0*;)(?<right>.*(?<goto>15).*)/${left}-${right}/
20 /(\d*);15(;.*)/$1;0123456789$2/
25 /^(\d*)(?<lastDigit>\d)(?<lookup>-*;\d*(?<newLastDigit>\d)\2.*)/$1${newLastDigit}${lookup}/
30 /(\d*);\d+/$1;35/
35 /-(?=-*;)(?<right>.*(?<goto>35).*)/9${right}/
40 /^0+([^0;])/$1/
45 /^(\d*);[^;]+;(?<goto>\d+)/$1/
# init at 99
100 /.*/98;01Nno more bottles;\n99 bottles of beer on the wall, 99 bottles of beer./
# add next line of lyrics
# input: number;rest
110 /^(?:(\d+)(;\k<1>1(N)(n)([^;]+))|(\d+)(;0\6N.*?( b[^s]+).)|(\d+)(;01N.*?( b[^;]+)));(.*)$/$1$6$9;10-110-150$2$7$10;$12\nTake one down and pass it around, $4$6$9$5$8$11 of beer on the wall.\n\n$3$6$9$5$8$11 of beer on the wall, $4$6$9$5$8$11 of beer./s
# if at 0, skip to end
120 /^0;\d+-\d+-(?<goto>\d+);/$0/
# minus 1 and goto print line
140 /^(\d+;)(?<goto>\d+)-(\d+)-\d+/$1$3/
150 /^[^\n]+\n(.*)/$1\nGo to the store and buy some more, 99 bottles of beer on the wall./s</code>
</div>
<h3>Fibonacci sequence <a href="#" data-load-demo="fibonacci">run</a></h3>
<p>In T-Regs one can easily define custom libraries to solve <em>advanced mathematical problems</em>.</p>
<div><code id="fibonacci">### Fibonacci
1 /.*/100;$0/
2 /(?<goto>\d+);//
# Main
100 /.*/500;1000;200;7;0 1/
110 /^(?<goto>\d+);//
# Add Fibonacci number
200 /^(?<return>\d+);(([ 0-9]+ )?(\d+) (\d+))/500;220;50;$4;$5;$2;${return}/
210 /^(?<goto>\d+);//
220 /^(\d+);([ 0-9]+);(?<goto>\d+)/${goto};$2 $1/
1000 ///
# Minus 1
# input: returnlabel;number;rest
# output: returnlabel;result;rest
10 /^(\d+);(\d+)(;.*)?$/$2;15;$1$3/
15 /^(?<left>[1-9]\d*?-*)0(?=0*;)(?<right>.*(?<goto>15).*)/${left}-${right}/
20 /(\d*);15(;.*)/$1;0123456789$2/
25 /^(\d*)(?<lastDigit>\d)(?<lookup>-*;\d*(?<newLastDigit>\d)\2.*)(;.*)$/$1${newLastDigit}${lookup}$5/
30 /(\d*);\d+(;.*)/$1;35$2/
35 /-(?=-*;)(?<right>.*(?<goto>35).*)/9${right}/
40 /^(\d*);[^;]+;(?<goto>\d+)(;.*)?$/$2;$1$3/
# Plus 1
# input: returnlabel;number;rest
# output: returnlabel;result;rest
50 /^(\d+);(\d+)(;.*)$/$2;0123456789;60;$1$3/
55 /^9+;/0$0/
60 /^(?<left>[0-8]\d*?-*)9(?=9*;)(?<right>.*(?<goto>60).*)/${left}-${right}/
65 /^(\d*)(?<lastDigit>\d)(?<lookup>-*;\d*\2(?<newLastDigit>\d).*)(;.*)$/$1${newLastDigit}${lookup}$5/
75 /(\d*);\d+;\d+(;.*)/$1;80$2/
80 /-(?=-*;)(?<right>.*(?<goto>80).*)/0${right}/
85 /^(\d*);[^;]+;(?<goto>\d+)(.*)$/$2;$1$3/
# repeatedly calls a function
# input: returnlabel;label;itterations;input for function
# output: ouput from function
# the function will get it's own exact output as input again, so should in- and out-put in the same format
# 1. initialize
500 /^(?<returnlabel>\d+);(?<label>\d+);(?<itterations>\d+);(?<rest>.*)$/${label};${rest};${itterations}-0;10-530-${label}-${returnlabel}/s
# 2. goto returnlabel if itterations == 0
510 /^(\d+);(?<result>.*?);(?<itterations>\d+)-\3;(\d+)-(\d+)-(\d+)-(?<goto>\d+)$/${result}/
# 3. itterations--
520 /^(.*);(?<itterations>\d+)-0;(?<goto>\d+)-(?<return>\d+)-(\d+)-(\d+)$/${return};${itterations};$0/
530 /^(\d+);(?<itterations>\d+);(.*);(\d+)(-0;)\d+-\d+(-\d+-\d+)$/$3;${itterations}${5}550$6/
# 4. call function
540 /^(?<goto>\d+);(?<arguments>.*;(?<return>\d+)-\d+-\d+)$/${return};${arguments}/
550 /^\d+;(?<arguments>.*);(?<itterations>\d+)-0;\d+-(?<label>\d+)-(?<return>\d+)$/500;${return};${label};${itterations};${arguments}/
560 /^(?<goto>\d+);//</code>
</div>
<h3>Prime numbers <a href="#" data-load-demo="primeNumbers">run</a></h3>
<p>This is left as an exercise for the reader.</p>
<div><code id="primeNumbers"># ...</code></div>
<h3><em>Challenge!</em> a Quine has not yet been written</h3>
<div><code id="fibonacci"># ... </code></div>
</div>
<div id="interpreter">
<textarea rows="20" cols="80" id="code"></textarea>
<div class="tabBar">
<div class="actions">
<input type="button" value="run" id="run"/><input type="button" value="debug" id="debug"/><input type="button" value="step" id="step"/><input type="button" value="continue" id="continue"/>
</div>
<ol class="section-headers">
<li>Input</li>
<li>Output</li>
<li>Debug Info</li>
<li>Trace</li>
<li>Errors</li>
</ol>
</div>
<div class="section-panes">
<div id="input"><textarea>racecar</textarea></div>
<div id="output"></div>
<div id="debugInfo"></div>
<div id="trace"></div>
<div id="errors"></div>
</div>
</div>
<div id="about" class="content">
<h2>About</h2>
<p>
T-Regs was designed by <a href="https://twitter.com/mathiasbaert">@mathiasbaert</a> and fits in a long tradition of <a href="http://esolangs.org">Esoteric Programming Languages</a> of which is said
</p>
<div>
<blockquote>
Some languages are designed to solve a problem. <br>
Others are designed to prove a point. <br>
— Dennis M. Ritchie
<a href="#footnote1" class="footnote">1</a>
</blockquote>
</div>
<h2>Thanks</h2>
<p>
Several existing libraries made this implementation rather easy.
</p>
<ol>
<li>The extended Regular Expressions syntax was delivered by the excellent <a href="http://xregexp.com/"><em>xregexp</em></a> by <a href="https://twitter.com/slevithan">Steven Levithan</a>.</li>
<li>The beautiful editor is <a href="#"><em>CodeMirror</em></a> by <a href="https://twitter.com/marijnjh">Marijn Haverbeke</a>.</li>
</ol>
<h2>Prior art</h2>
<p>
T-Regs is not the first language to be based on regular expressions.
Several different approaches can be found for instance on the <a href="http://esolangs.org/">esolangs wiki</a>
like
<a href="http://esolangs.org/wiki/RegexPL">RegexPL</a> and
<a href="http://esolangs.org/wiki/Thutu">Thutu</a>
(which added regular expressions to <a href="http://esolangs.org/wiki/Thue">Thue</a>'s mechanism of computation through text replacement).
</p>
<p>
Compared to these languages T-Regs stays very close to plain regular expressions.
T-Regs is not intentionally hard or unreadable,
but to the contrary is trying to be as practical as possible.
Given of course, that you want to do general purpose programming through text manipulation.
</p>
<p class="footnote"><a id="footnote1">1</a> He said this in fact about a whole other class of languages, but let us ignore the facts for a moment.</p>
</div>
</div>
</body>
</html>