-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
295 lines (214 loc) · 6.97 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
<html>
<head>
<title>
CokeScript language, whitespace sensitive language that compile to JavaScript
</title>
<link rel="stylesheet" href="css/reboot.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body class="grid typography">
<div class="jumbotron">
<div class="container">
<h1>CokeScript</h1>
<p>When CoffeeScript is not enough. <a href="https://github.com/batiste/CokeScript/" class="btn btn-lg btn-info">Github</a></p>
</div>
</div>
<p>CokeScript is a simple whitespace sensitive language that compile to JavaScript inspired by Python and Ruby. CokeScript doesn't try to be too smart and is mainly syntactic sugar around common JavasScript annoyances. Features:</p>
<ol>
<li>Significant whitespace</li>
<li>String interpolation</li>
<li>Multiline strings</li>
<li>Shorter function declaration and lambda expression</li>
<li>Function parameters can have a default and computed value</li>
<li>Simple <strong>for</strong> loop syntax that works on Array or Object</li>
<li>Sensible scoping rules unlike <a href="https://donatstudios.com/CoffeeScript-Madness">CoffeeScript</a></li>
<li>Classes with "classical" inheritance</li>
<li>Opiniated syntax rules that enforce a rigid coding style</li>
<li>A quite good grammar parser that hints about what is wrong</li>
<li><a href="#egDOM">Virtual Dom support</a> directly within the language</li>
<li>A rather compact source code which is <strong>smaller than 1000 lines of code</strong></li>
</ol>
<p>More info on the github page <a href="https://github.com/batiste/CokeScript/">https://github.com/batiste/CokeScript/</a></p>
<label><input type="checkbox" id="notrycatch"> Do not catch errors in the example.</label>
<div class="pad-1 first-row">
<div id="egClass" class="row">
<div class="col-6">
<h2>Classes</h2>
<div style="position:relative">
<textarea class="in form-control">
class Animal
def constructor(name="unknown", diet="grass")
this.name = name
this.diet = diet
def eat()
return "#{this.name} eat #{this.diet}"
class Cats(Animal)
def constructor(name)
Animal.call(this, name, "meat")
class Tiger(Cats)
def constructor(name)
Cats.call(this, name)
tiger = Tiger("Wilson")
tiger.eat()
</textarea>
<pre class="error alert alert-danger" style=""></pre>
</div>
</div>
<div class="col-6">
<h2>JavaScript Output <span class="percent"></span></h2>
<textarea class="code-output form-control"></textarea>
<button class="evalButton btn btn-primary">Eval</button>
<pre class="evalResult"></pre>
</div>
</div>
<div id="egScope" class="row">
<div class="col-6">
<h2>Scope</h2>
<textarea class="in form-control">
a = "string
on multiple
lines"
c = 1 + 1
def testScope(b)
a = "does not erase the outer scope string"
b = "No var here because it's parameter of the function"
c := "Explicit assignment to parent scope variable with \":=\""
return a
testScope()
"#{a} , #{c}"
</textarea>
<pre class="error alert alert-danger" style=""></pre>
</div>
<div class="col-6">
<h2>JavaScript Output <span class="percent"></span></h2>
<textarea class="code-output form-control"></textarea>
<button class="evalButton btn btn-primary">Eval</button>
<pre class="evalResult"></pre>
</div>
</div>
<div id="egFunction" class="row">
<div class="col-6">
<h2>Functions and Loops</h2>
<textarea class="in form-control">
memo = {}
exist = def(n) memo[n] != undefined
def fib(n)
if exist(n)
return memo[n]
if n < 2
return n
memo[n] = fib(n - 2) + fib(n - 1)
return memo[n]
array = [1, 2, 3]
fibo_square = array.map(def(i) fib(i * i))
out = ""
for key, value in memo
out += "fib(#{key}) = #{value}, "
out
</textarea>
<pre class="error alert alert-danger" style=""></pre>
</div>
<div class="col-6">
<h2>JavaScript Output <span class="percent"></span></h2>
<textarea class="code-output form-control"></textarea>
<button class="evalButton btn btn-primary">Eval</button>
<pre class="evalResult"></pre>
</div>
</div>
<div id="egDOM" class="row">
<div class="row">
<div class="col-6">
<h2>Virtual DOM support</h2>
<p>CokeScript support the
<a href="https://github.com/Matt-Esch/virtual-dom#example---creating-a-vtree-using-virtual-hyperscript">
virtual-dom</a> library for React-Like features.<br>
</p>
</div>
<div class="col-6">
<h2>JavaScript Output <span class="percent"></span></h2>
</div>
</div>
<div class="col-6">
<textarea class="in form-control">
menu = {home: "Homepage", products: "Products"}
# a function declared with the "dom" keyword
# automatically return an array of DOM elements
dom generateVirtualDom(links)
<h1>
="Menu"
<ul className="nav">
for index, content in links
=listItem("cls#{index}", content)
dom listItem(className, text)
<li className="#{className}">
=text
virtual_dom = cokescript.h("div", generateVirtualDom(menu))
real_dom = cokescript.create(virtual_dom)
target = document.getElementById("domOut")
target.innerHTML = ""
target.appendChild(real_dom)
# let's modify the menu
menu = {home: "Homepage", products: "Products", contacts: "Contacts"}
new_virtual_dom = cokescript.h("div", generateVirtualDom(menu))
patches = cokescript.diff(virtual_dom, new_virtual_dom)
cokescript.patch(real_dom, patches)
</textarea>
<pre class="error alert alert-danger" style=""></pre>
</div>
<div class="col-6">
<textarea class="code-output form-control"></textarea>
<button class="evalButton btn btn-primary">Eval</button>
<pre class="evalResult"></pre>
<div id="domOut"></div>
</div>
</div>
</div>
<script src='dist/cokescript.js'></script>
<script>
window.onload = function() {
var elTC = document.getElementById('notrycatch');
var notrycatch = localStorage.getItem("notrycatch") == "true";
elTC.checked = notrycatch;
elTC.onclick = function(e) {
notrycatch = this.checked;
localStorage.setItem('notrycatch', notrycatch);
};
function initExample(dom) {
function dGet(cls){ return dom.getElementsByClassName(cls)[0]; }
var din = dGet('in');
//var dout = dGet('out');
var code = dGet('code-output');
dGet('evalButton').onclick = function() {
var result = eval(code.value);
dGet('evalResult').textContent = result;
};
din.onkeyup = function() {
outit();
};
function outit() {
if(notrycatch) {
var result = cokescript.generateModule(din.value);
code.value = result.code;
dGet("error").textContent = "";
} else {
try {
var result = cokescript.generateModule(din.value);
code.value = result.code;
var percent = Math.round((result.code.length / din.value.length) * 100);
//dout.value = JSON.stringify(result.ast, false, 2);
dGet("percent").textContent = '(' + percent + '% of original)';
dGet("error").textContent = "";
} catch(e) {
dGet("error").textContent = String(e);
throw e;
}
}
}
outit();
}
initExample(document.getElementById('egClass'));
initExample(document.getElementById('egFunction'));
initExample(document.getElementById('egScope'));
initExample(document.getElementById('egDOM'));
};
</script>