-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.rsc
262 lines (209 loc) · 6.43 KB
/
Example.rsc
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
module Example
import ParseTree; // import
extend lang::std::Layout; // "inherit"
// Declarations
start syntax Prog // start symbol
= prog: Exp* exps // production
| stats: {Stat ";"}* // separated list
| stats: {Stat ";"}+ // one-or-more sep. list
| "private"? Func; // optional
syntax Exp
= var: Id
| left mul: Exp l "*" Exp r // left associative
| left div: Exp!div "/" Exp!div // reject
> left add: Exp l "+" Exp r // ">" = priority
| bracket "(" Exp ")";
lexical Comment
= ^"#" ![\n]* $; // begin/end markers
lexical Id
= ([a-zA-Z] !<< // look behind restriction
[a-zA-Z][a-zA-Z0-9_]* // character classes
!>> [a-zA-Z0-9_]) // lookahead restriction
\ Reserved; // subtract keywords
layout Layout // for whitespace/comments
= [\ \t\n\r]*;
keyword Reserved // keyword class
= "if" | "else"; // finite langs
private real PI = 3.14; // variables
// Algebraic data types (ADT)
data Exp
= var(str x) // unary constructor
| add(Exp l, Exp r); // binary constructor
data Person // keyword parameter
= person(int id, bool married=false);
alias Age = int; // type alias
anno loc Exp@location; // annotation
// Functions: signatures are lists
// of (typed) patterns; may have
// keyword parameters.
void f(int x) { println(x); } // block style
int inc(int x) = x+1; // rewrite style
int inc0(int x) = x+1 when x == 0; // side condition
default int inc0(int x) = x; // when all others fail
// Test functions (invoke all from console with :test)
test bool add() = 1+2 == 3;
// randomized test function
test bool comm(int x, int y) = x+y == y+x;
// Foreing function interface to Java
@javaClass{name.of.javaClass}
java int doSomethingInJava();
void statements() {
// Standard control-flow
if (E) S;
if (E) S; else S;
while (E) S;
do S; while(E);
continue; break;
return; return E;
// Loop for all bindings
// generated by patterns E,...
for (i <- [0..10]) S;
fail; // control backtracking
append E; // add to loop result list
// Pattern-based switch-case
switch (E) {
case P: S; // do something
case P => E // rewrite it
default: S; // otherwise
}
// Traversal with visit
// Like switch, but matches
// at arbitrary depth of value
visit (E) {
case P: S; // do something
case P => E // rewrite something
case P => E when E
}
insert E; // rewrite subject
try S; // Pattern-based try-catch
catch P: S; // match to catch
finally S;
throw E; // throw values
// fix-point equation solving
// iterates until all params are stable.
solve (out,ins) {
out[b] = ( {} | it + ins[s] | s <- succ[b] );
ins[b] = (out[b] - kill[b]) + gen[b];
};
x = 1; // assignment
nums[0] = 1; // subscript assignment
nums[1,3..10] = 2; // sliced (see below)
p.age = 31; // field assignment
ast@location = l; // annotation update
<p, a> = <"ed", 30>; // destructuring
// A op=E == A = A op E
A += E; A -= E; A *= E;
A /= E; A &= E;
}
void expressions() {
// Standard operators
E + E; E - E; E * E; E / E; E % E;
E && E; E || E; E == E; E != E;
E > E; E >= E; E < E; E >= E; -E; !E;
E ? E : E;
// Projections
p.age; // select field (tuple/constructor)
p[age=31]; // update field
ps<name,age>; // select named column(s)
ps<1,0>; // select/swap columns by position
graph["from"]; // right image (list,str,map/rel/lrel)
alist[-1]; // subscript (last)
graph["from", "label"];
inc(2); // function call
x[1..10]; // slicing (list, string)
x[0..]; x[..10]; // open slices
x[..-1]; // negative slicing (prefix)
x[0,2..10]; // slicing with next
[0..10]; // range (incl/excl)
[0,2..10]; // range with next
// Comprehensions
[ i*i | i <- [1..10] ]; // list
{ <i, i*i> | i <- [1..10] }; // set
( i: i*i | i <- [1..10]); // map
// Reducing comprehension
( 0 | it + i | i <- [1..10] );
// Other operators
E mod E; // modulo
E & E; // intersection
E join E; // relation join
E o E; // compostion
all(i <- [1..10], i > 0); // big and
any(i <- [1..10], i % 2 == 0); // big or
E ==> E; // implication
E <==> E; // equivalence
E in E; // membership
E notin E; // non-membership
E has N; // has label
E is N; // is constructor
E+; // transitive closure
E*; // trans. refl. closure
E[N=E]; // update field
E[@N=E]; // update annotation
// Matching and generation
P := E; // pattern match
P !:= E; // anti-match
P <- E; // generator
// Closures
int(int x) { return x + 1; };
(str x) { println(x); }; // void
() { println("y"); }; // nillary void
// String templates
"x + y = <x + y>"; // interpolation
// Control-flow string interpolation
// (with for, if, while, do-while)
// Single quote (') indicates margin.
// Nested templates are auto-indented
"<for (i<-[0..10]) {>
' <i>
'<}>";
}
void types() {
// Atomic types
bool x = true || false;
int x = 1;
real x = 2.3E-14;
rat x = 1/2;
num x = 1 + 3.0;
str x = "rascal";
loc x = |file:///etc/passwd|;
loc x = |file://foo|(
10, // offset
5, // length
<1, 2> // begin (line/col)
<1, 7>); // end
datetime x = $1948-02-11$;
// Tuples
tuple[str, int] x = <"ed", 30>;
tuple[str name, int age] x = <"ed", 30>;
// Trees (all ADTs are subtype of node)
node x = "person"("ed", 30); // generic node
Exp x = add(var("x"), var("y")); // ADT value
// Collection values
list[int] x = [1,2,3];
set[bool] x = {true,false};
map[int, bool] x = (1: true, 2: false);
map[int n, bool b] x = (1: true, 2: false);
rel[int, bool] x = {<1, true>, <2, false>};
rel[int n, bool b] x = {<1, true>, <2, false>};
lrel[int n, bool b] x = [<1, true>, <1, true>];
// Functions
int(int,int)f = int(int x, int y) { return x+y; };
// Misc
value x = anything; // top type
type[int] t = #int; // reified types
int size(list[&T] l); // generics
}
void patterns() {
int x := 3; // typed pattern
x := 3; // free or bound variable (untyped)
[1, 2, x] := [1, 2, 3]; // list pattern
{x, 2, 3} := {2, 3, 1}; // set pattern
[1, *xs, 4] := [1,2,3,4]; // splice-variable in list or set
<int x, y> := <3, "x">; // tuple pattern
add(l, r) := add(var("x"), var("y")); // constructor
/str x := add(var("x"), var("y")); // deep match
a:add(_,_) := x; // labeled pattern
Exp a:add(_,_) := x; // typed, labeled pattern
/[a-z]/ := "x"; // regular expression
/.<mid:[a-z]>./ := "abc"; // named groups
}