forked from sweet-js/sweet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
319 lines (263 loc) · 6.78 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
<!doctype html>
<html>
<head>
<title>sweet.js</title>
<link href='http://fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
<link href='prism.css' rel='stylesheet' />
<style>
html {
border-top: solid 1px red;
}
body {
margin: 0;
padding: 0;
/*border-top: 2px solid #676767;*/
}
#body {
width: 600px;
margin: 0 auto;
position: relative;
top: 75px;
}
#logo {
float: left;
width: 275px;
}
#content {
float: left;
width: 650px;
padding-bottom:50px;
}
#slogan {
font-size: 300%;
font-family: Helvetica Neue;
font-weight: 100;
color: #222;
position: relative;
text-transform: uppercase;
color: #444;
font-family: 'Lato', Helvetica Neue, Helvetica, Arial, sans-serif;
font-weight: 100;
}
#slogan span {
border-bottom: solid 1px #eee;
}
#content p {
margin-top: 2em;
}
p.text {
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
/*font-weight: 400;*/
font-size: 16px;
}
#content p#slogan {
margin-top: 1em;
}
#content p#placeholder {
margin-top: 8em;
}
a.mozilla {
position: relative;
float: left;
display: block;
height: 49px;
width: 118px;
background: url(mozilla-tab.png) 100% 0 no-repeat;
text-indent: -200px;
overflow: hidden;
margin-left: 20px;
}
</style>
</head>
<body>
<div id="header">
<a class="mozilla" href="http://www.mozilla.org/">visit <span>mozilla</span></a>
</div>
<a href="http://github.com/mozilla/sweet.js">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" />
</a>
<div id="body">
<div id="logo"><img src="sweetjs.png"></div>
<div id="content">
<p id="slogan"><span>Sweeten your JavaScript</span></p>
<p class="text">
Sweet.js brings hygienic macros from languages like Scheme and Rust to JavaScript.
Macros allow you to sweeten the syntax of JavaScript and craft the language you've always wanted.
</p>
<p class="text">
Wish the <code class="language-javascript">function</code> keyword in JavaScript wasn't so long?
What if you could define functions with <code>def</code> instead?
</p>
<pre><code class="language-javascript">
def sweet(a) {
console.log("Macros are sweet!");
}
</code></pre>
<p class="text">
Macros let you do this!
</p>
<pre><code class="language-javascript">
macro def {
case $name:ident $params $body => {
function $name $params $body
}
}
</code></pre>
<p class="text">
Want a better way to make "classy" objects?
</p>
<pre><code class="language-javascript">
class Person {
constructor(name) {
this.name = name;
}
say(msg) {
console.log(this.name + " says: " + msg);
}
}
var bob = new Person("Bob");
bob.say("Macros are sweet!");
</code></pre>
<p class="text">
Macros can do that too!
</p>
<pre><code class="language-javascript">
macro class {
case $className:ident {
constructor $constParam $constBody
$($methodName:ident $methodParam $methodBody) ... } => {
function $className $constParam $constBody
$($className.prototype.$methodName
= function $methodName $methodParam $methodBody; ) ...
}
}
</code></pre>
<h2>Getting sweet.js</h2>
<p class="text">
Install the sweet.js compiler via npm:
</p>
<pre><code class="language-markup">
$ npm install -g sweet.js
</code></pre>
<p class="text">
Use the <code>sjs</code> binary to compile your sweet.js code:
</p>
<pre><code class="language-markup">
$ sjs -o output.js my_sweet_code.js
</code></pre>
<p class="text">
Sweet.js is still a young project so expect bugs and missing features.
If you find a bug, please report it on <a href="https://github.com/mozilla/sweet.js/issues">github</a>.
</p>
<h2>Writing macros</h2>
<p class="text">
You can think of macros as functions that take little bits of syntax and convert it to
new bits of syntax at compile-time. Macros are created with the <code>macro</code> keyword:
</p>
<pre><code class="language-javascript">
macro id {
case ($x) => { $x }
}
</code></pre>
<p class="text">
This can be read as "define a macro named 'id' that matches a single token surrounded by parenthesis and
when invoked returns just the matched token".
</p>
<pre><code class="language-javascript">
var x = id ("foo")
// compiles to -->
var x = "foo"
</code></pre>
<p class="text">
A pattern name that begin with "<code>$</code>" in the left hand side of the macro definition
matches any token and binds it to that name in macro body while everything else matches literally.
</p>
<pre><code class="language-javascript">
macro m {
case ($x becomes $y) => {
$x = $y;
}
}
m (a becomes b)
// -->
a = b
</code></pre>
<p class="text">
A pattern name can be restricted to a particular parse class by using
<code>$name:class</code> in which case rather than matching only
a single token the pattern matches all the tokens matched by the parse class.
</p>
<pre><code class="language-javascript">
macro m {
case ($x:expr) => {
// ...
}
}
m (2 + 5 * 10)
</code></pre>
<p class="text">
The commonly used parse classes that have short names are
<code>expr</code> (matches an expression),
<code>ident</code> (matches an identifier), and
<code>lit</code> (matches a literal). The full list is described <a href="https://github.com/mozilla/sweet.js/wiki/pattern_class">here</a>.
</p>
<p class="text">
Repeated patterns can be matched with the <code>...</code> syntax.
</p>
<pre><code class="language-javascript">
macro m {
case ($x ...) => {
// ...
}
}
m (1 2 3 4)
</code></pre>
<p class="text">
A repeated pattern with a separator between each item can be matched by
adding <code>(,)</code> between <code>...</code> and the pattern being repeated.
</p>
<pre><code class="language-javascript">
macro m {
case ($x (,) ...) => {
[$x (,) ...]
}
}
m (1, 2, 3, 4)
</code></pre>
<p class="text">
Repeated groups of patterns can be matched using <code>$()</code>.
</p>
<pre><code class="language-javascript">
macro m {
case ( $($id:ident = $val:expr) (,) ...) => {
$(var $id = $val;) ...
}
}
m (x = 10, y = 2+10)
</code></pre>
<p class="text">
Macros can match on multiple cases.
</p>
<pre><code class="language-javascript">
macro m {
case ($x:lit) => { $x }
case ($x:lit, $y:lit) => { [$x, $y] }
}
m (1) // --> 1
m (1, 2) // --> [1, 2]
</code></pre>
</div>
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-34844488-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script src="prism.js"></script>
</body>
</html>