/
example.omd
387 lines (293 loc) · 6.92 KB
/
example.omd
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
Example
=======
List
----
- un
- deux
{html}
<ul>
<li>deux point un</li>
<li>deux point deux</li>
</ul>
{html}
- trois
Code
----
`one line code`[js]
```{.opa}
r = {
id(x) = x
pair(x) = (x, x)
one = 1
}
> r : {id : 'v0 -> 'v0; pair : 'v1 -> tuple_2('v1, 'v1); one : int} / ... =
> { id = <fun> ; one = 1 ; pair = <fun> }
i = 5
> i : int = 5
f = 3.14159
> f : float = 3.14159
s = "foo"
> s : string = "foo"
v = {}
> v : {} / ... = void
b = true
> b : bool = { true = void ; }
```
```{.opa}
////////////////////////////////////////////////////////////
// TYPE :
////////////////////////////////////////////////////////////
// 1 - Type constant
type const0 = int
type const1 = float
type const3 = string
// 2 - Type record
type record0 = {int x0, float y0, string z0}
type record1 = {int x1, float y1, string z1,}
// 3 - Type sum
type sum0 =
{int s00, float s01, string s02}
or {int s10, float s11, string s12}
type sum1 = sum0 or record0 or record1
// 4 - Type arrow
type arrow0('a, 'b) = 'a, 'b -> 'b
// 5 - Type named
type name('a) = arrow0(int, 'a)
// 6 - Type forall
type arrow1 = forall('a, 'b). arrow0('a, 'b)
// 7 - Type module
type IModule = module {
string get_message()
'a do_message(string -> 'a)
}
// 8 - Type functor
type IFunctor = module(option(string)) {
string get_message()
'a do_message(string -> 'a)
}
////////////////////////////////////////////////////////////
// EXPRESSIONS :
////////////////////////////////////////////////////////////
// NB : An unbinded expression @toplvl is equivalent to _ = expr
// 1 - Constant expression
1
"Hello"
2.
// 2 - Record
// { (typ? name : expr,)* }
{x : 1, y : "y", z : 3,}
// Last ; is optionnal
{x : 1, y : 2, z : 3}
// tilt binding
{
x = 1
y = 2
z = 3
ex1 = ~{x, y, z}
ex2 = ~{x, y:8, z}
ex3 = {x:10, y:20, ~z}
void
}
// 3 - Extend Record
{
x = 7
z = 6
record = {x : 1, y : 2, z : 3,}
record = {record with x : 0, z : 5}
record = ~{record with x, z}
void
}
// 4 - Match
// match(expr){
// case p1 : e1
// case p2 : e2
// default : e3
// }
match({x : 958}){
case {x : 2} : 1
case {x : 1} : 2
case {int ~x} : x
case {x : 3, ...} : 9
default : 4
}
// 5 - Expression block
// {
// (binding / do_expr)*
// final_expr
// }
{
jlog("let x")
x = 1
jlog("let y")
y = 2
jlog("let z")
z = 3
x + y + z
}
// 6 - Lambda
// function return_type? (pattern(,pattern)*)block
function int (x, y, z){
x + y + z
}
function(~{int x, y, z} as r){
jlog("{r}")
x+y+z
}
// 7 - Anonymous module
module{
private msg = "This is my message "
function get_message(){ msg }
function do_message(string -> void f){ f(msg) }
}
// 8 - Anonymous functor
module (option(string) custom){
private msg = custom ? "This is my message "
function get_message(){ msg }
function do_message(string -> void f){ f(msg) }
}
// 9 - Coerce
// typ expr
int 89
string "42"
// 10 - Database declaration
// 10.1 - Grouped
// database db0 @local("/tmp/tototest0") {
// int /path0
// int /path1 = 10
// }
// 10.2 - Single
database int /db0/path2 = 999
// 11 - Dom action sugar :
// action = domselector (insertaction / cssaction)
//
// domselector = #id / .class / *selector
//
// insertaction = (= / += / -=) expr
//
// cssaction = css= css_properties
private function dom_sugar(){
#toto = <h1>Replace</h1>
#toto += <h1>Prepend</h1>
#toto =+ <h1>Append</h1>
dom_pointer = Dom.select_children(#toto)
*dom_pointer += <h2>before</h2>
*dom_pointer =+ <h2>after</h2>
dom_pointer = Dom.select_inside(dom_pointer, Dom.select_tag("h2"))
*dom_pointer css={color : blue}
}
// 12 - Recursives let
// 12.1 Recursive value
recursive x = {
x : function(){void x.x()}
}
{
// Local recursive lambda
recursive function aux(){aux()}
recursive x = {
y : function(){u()}
}
and z = {
y : function(){x.y()}
}
and function u(){
void x.y()
}
recursive x = 1
aux
}
// 13 - Subsumption
option(int) :> ({none} {none})
////////////////////////////////////////////////////////////
// DECLARATIONS
////////////////////////////////////////////////////////////
// 1 - Values
// var? type? pattern = expr
var int x = 1
int xcx = 1
var ~{v1, v2, v3} = {v1 : 1, v2 : 2, v3 :3}
// 2 - Function safe replace @publish
safe function my_fun(){
/db0/path0 <- 90
jlog("my_fun"); jlog(" is")
jlog(" fun")
}
// 3 - Module
module MyModule{
private msg = "This is my message "
function get_message(){ msg }
function 'a do_message(string -> 'a f){ f(msg) }
private module P{
function transform(){
msg ^ msg
}
}
}
IModule module MyModule{
private msg = "This is my message "
function get_message(){ msg }
function 'a do_message(string -> 'a f){ f(msg) }
}
// 4 - Functor
IFunctor module MyFunctor(option(string) custom){
private msg = custom ? "This is my message "
function string get_message(){ msg }
function do_message(f){ f(msg) }
}
package MyModule2 = MyFunctor(some("My custom message"))
MyModule2.do_message(jlog)
////////////////////////////////////////////////////////////
// STARTING THE SERVER
////////////////////////////////////////////////////////////
// Proposal
type Server.conf = {
int port,
ip netmask,
Server.encryption encryption,
string name,
}
/**
* Different types of request handler.
*/
type Server.handler =
/** The most simple request handler. It replies to all incoming
request. With a title page [title] and given body by the [page]
function */
or {string title, xhtml page()}
/** The most configurable request handler. The [custom] parser takes
as input the non-decoded uri from incoming requests and should
compute corresponding resource. */
or {Parser.general_parser(resource) custom}
/** Request handler on decoded incoming uri. This handler takes as
input the decoded uri from incoming requests which through the
[filter]. */
or {Server.filter filter, resource dispatch(Uri.relative) }
/** Request handler which performs on non-decoded uri. Returns
resource which uri matches into the [bundle] map. */
or {stringmap(resource) bundle}
// Sure we can provide Server.compose(Server.handler, Server.handler)
// With a function : void Server.start(Server.conf, Server.handler)
http = { port : 8080, netmask : 0.0.0.0, encryption : {no_encryption}, name : "http"}
Server.start(
http,
{title : "My first opa server", page : function(){<h1>Hello world</h1>}}
)
Server.start(
{service with name:"secure", port:4343},
{title : "My first opa server secured", page : function(){<h1>Hello secure</h1>}}
)
```
Block
-----
{block}[TIP]
###About _inserts_
Opa provides _inserts_ to insert expressions inside HTML, inside strings and in
a few other places that we will introduce as we meet _them_
{block}
OpaCode
-------
{opa}
Hello world
{opa}
OpaFile
----
opa-file://../../src/main.opa