-
Notifications
You must be signed in to change notification settings - Fork 3
/
LANGUAGE
336 lines (240 loc) · 4.85 KB
/
LANGUAGE
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
Sugar
=====
Overview
========
Sugar is a source-to-source compiler that targets JavaScript. It is designed
with the following principles in mind:
- indentation-based syntax
- remove non-essential syntax (`.`, `;`, etc)
- offer syntax sugar for complex operations
- focus on architecture/program structure
- minimize personal differences in style between programmers
Basic example
-------------
```
console log "Hello, World!"
```
Class example
-------------
```
@class Animal
@property name
@constructor name
self name = name
@end
@method getName
return name
@end
@end
@class Dog: Animal
@property name = "dog"
@end
```
Module example
-------------
```
@module hello
@version 0.0.0
# Shared variables are module-level variables
@shared MESSAGE = "Hello, World!"
# You can define functions/classes here
@function say message=MESSAGE
console log (message)
@end
# This is the initialization function
say ()
```
Data types
==========
Variables
=========
Allocations
-----------
```
var a=0, b=0, c=0
```
```
# a=0, b=1, b=2
var a, b, c = [0, 1, 2]
```
```
# a=0, b=1, c=[2]
var a, b | c = [0, 1, ,2]
```
Assignment
----------
Conditional::
Assigns a default value to the lvalue if it is not defined
```
# <expression> <slot> ?= <expression>
user name ?= "anonymous"
``
Operations
==========
Conditionals
============
Expressions::
The following expression can be assigned anywhere
```
(if <predicate> -> <expression> | <predicate> -> <expression> | <expression>)
```
One-liners::
```
if <predicate> -> <statement>
elif <predicate> -> <statement>
else <expression>
```
Full-blocks::
```
if <predicate>
<statement>
...
elif <predicate>
<statement>
...
else
<statement>
...
end
```
Iterations
==========
For loops::
```
# for <variable> in <expression>
for i in 0..10
<statement>
...
end
```
For loops one-liner::
Iterations have a compact format
```
0..10 :: {<statement>;...}
```
Mapping one-liner::
Iterations can also produce a value, which can then be used
as an expression.
```
console log (0..10 ::= {<statement>;...})
```
Filter one-liner::
```
0..10 ::? {_|_ > 5}
```
Filter+map one-liner::
```
0..10 ::? {<statement>;...} ::= {<statement>;...}
```
Invocation
==========
Closures
========
No arguments::
```{<statement>...}```
With arguments::
```
{arg,...|
<statement>
...
}
```
Implicit arguments:
Any closure/function can have implicit arguments. They are `_`, `_1`, ... `_N`,
where `_` == `_0` and `N` corresponds to the index of the argument.
```
{_ + _1 + _2 + _3}
```
Functions
=========
```
@function f a, b=1, c=2, ...rest
<statement>
...
@end
```
When annotation::
Functions can be _guarded_ against execution in case a condition is not
met using the `@when` annotation.
```
@function f a
@when a > 10
return True
@end
```
Pre/post decoration::
Design-by-contract annotations `@pre` and `@post` allow to express pre
and post conditions that will be asserted at function start and before
termination.
```
@function f i
@pre i > 0
@post i > 10
i += 10
return i
@end
```
As annotation::
It is possible to _tag_ a function as belonging to a specific group using
the `@as` annotation. This has no effect on the code but is used as
meta-information.
```
@function f i
@as helper
return i + 1
@end
```
Classes
=======
Modules
======
History
=======
New features in 2016-04
-----------------------
Multiple allocation::
`var a=0, b=1`
Decomposition allocation::
```
var a, b = [0, 1] # a=0, b=1
var a, b | c = [0, 1, 3, 4] # a=0, b=1, c=[3,4]
```
Implicit returns in closures::
The last operation's value will be implicitely returned in closures
```
var a = {10 + 20}() # Is equivalent to {return 10 + 20}
```
Implicit arguments in functions::
If `_`, `_1`, ..., `_N` is referenced but not resolves, it will automatically
resolve to the nth argument of the current closure/function.
```
{ _ + _1 + _2} # Equivalent to {_,_1,_2|_ + _1 + -2}
```
Map/filter syntax::
```
var a = 0..10 ::= {_ + 1} # Produces [1,2,3,4,5,6,7,8,9,10]
var b = 0..10 ::? {_ > 5} # Produces [6,7,8,9]
var b = 0..10 ::? {_ > 5} = {_ * 10} # Produces [60,70,80,90]
```
New `__scope__` and `__name__` automatic variables::
```
@module m
@function f
console log (__name__) # prints `f`
console log (__scope__) # prints `m.f`
@end
```
`assert` improvement::
Failing assertions now show both the scope and the original predicate,
```
@module m
@function f
# prints "m.f: 10 * 10 < 50 failed"
assert (10 * 10 < 50, "failed!")#
@end
```
Native `for` loops::
For loops and iterations now use the native JavaScript `for` iteration,
closing over variables to prevent hoisting. As a result, iterations
don't use `extend.iterate` anymore.
# EOF