File: syntax.bs

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (333 lines) | stat: -rw-r--r-- 8,144 bytes parent folder | download | duplicates (2)
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
use core:lang;
use lang:bs;
use lang:bs:macro;

/**
 * Test declaration.
 */
package class TestDecl extends NamedDecl {
	SStr name;
	Scope scope;
	SFreeOptions options;
	STestBody body;

	init(Scope scope, SStr name, SFreeOptions options, STestBody body) {
		init() {
			name = name;
			scope = scope;
			options = options;
			body = body;
		}
	}

	// Create.
	Named doCreate() {
		TestFn fn(scope, name, body);
		options.transform(fileScope(scope, name.pos), fn);
		fn;
	}

	// Update.
	Named? update(Scope scope) {
		var found = scope.find(Name(name.v));
		if (found as TestFn) {
			found.update(scope, body);
		} else if (found.empty) {
			var c = create();
			resolve();
			return c;
		}

		return null;
	}
}

/**
 * Test function.
 */
package class TestFn extends BSRawFn {
	Scope scope;
	STestBody body;

	init(Scope scope, SStr name, STestBody body) {
		init(Value(named{TestResult}), name, ValParam[], null) {
			scope = scope;
			body = body;
		}
	}

	// Update ourselves.
	void update(Scope scope, STestBody body) {
		this.scope = scope.withPos(pos);
		this.body = body;
		reset();
	}

	FnBody createBody() {
		TestBody body(this, scope.withPos(pos));

		this.body.transform(body);

		// Return the result.
		body.add(LocalVarAccess(SrcPos(), body.resultVar));

		// print(body.toS);
		body;
	}
}

/**
 * Custom body block that stores the result variable for easier access.
 */
package class TestBody extends FnBody {
	init(BSRawFn owner, Scope scope) {
		super(owner, scope);

		Var r(this, Value(named{TestResult}), SStr("test-result"), Actuals());

		init {
			resultVar = r.var;
		}

		add(r);
	}

	LocalVar resultVar;
}

// Find the 'result' variable.
package LocalVar findResult(Block block) {
	BlockLookup lookup = block.lookup;
	do {
		if (test = lookup.block as TestBody) {
			return test.resultVar;
		}

		if (p = lookup.parent as BlockLookup) {
			lookup = p;
		} else {
			throw SyntaxError(block.pos, "Must be used inside a test function!");
		}
	}
}

// Captured expression/text pair.
class ExprText on Compiler {
	Expr expr;
	Str text;

	init(Expr expr, Str text) {
		init {
			expr = expr;
			text = text;
		}
	}
}

// Add catch block for a test.
private CatchBlock createCatch(SrcPos pos, Block parent, LocalVarAccess result, Str exprText) on Compiler {
	CatchBlock block(pos, parent, named{Exception}, SStr("error"));
	ExprBlock sub(pos, block);
	block.expr = sub;

	sub.add(namedExpr(block, pos, "++*", namedExpr(block, pos, "crashed", result)));

	ConcatExpr msg(pos, block);
	msg.append(StrLiteral(pos, "Crashed: ${exprText} ==> got exception "));
	msg.append(namedExpr(block, pos, "error", Actuals()));
	sub.add(FnCall(pos, block.scope, named{print<Str>}, Actuals(msg)));

	block;
}


// Expression for a single check expression.
package Expr checkExpr(SrcPos pos, Block block, ExprText lhs, OpInfo op, ExprText rhs) on Compiler {
	LocalVar resultVar = findResult(block);
	LocalVarAccess result(pos, resultVar);

	TryBlock sub(pos, block);

	// Increment 'total'.
	sub.add(namedExpr(sub, pos, "++*", namedExpr(sub, pos, "total", result)));

	// Put lhs and rhs into an operator to find out what it means. This is so that any automatic
	// casts will be applied as needed.
	Value lhsType = lhs.expr.result.type;
	Value rhsType = rhs.expr.result.type;
	if (call = Operator(sub, lhs.expr, op, rhs.expr).meaning as FnCall) {
		Function toExec = call.function;
		if (toExec.name == "!") {
			// Inverted operator, such as ==.
			if (c = call.parameters.expressions[0] as FnCall) {
				toExec = c.function;
			}
		}

		if (toExec.params.count == 2) {
			lhsType = toExec.params[0].asRef(false);
			rhsType = toExec.params[1].asRef(false);
		} else {
			// This should not happen, it means that something unexpected happened.
		}
	}

	// Evaluate lhs and rhs and save them.
	Var lhsVar(sub, lhsType, SStr(" l", pos), lhs.expr);
	sub.add(lhsVar);
	LocalVarAccess l(pos, lhsVar.var);

	Var rhsVar(sub, rhsType, SStr(" r", pos), rhs.expr);
	sub.add(rhsVar);
	LocalVarAccess r(pos, rhsVar.var);

	// Check if they are equal.
	BoolCondition cond(mkOperator(sub, l, op, r));
	If check(sub, cond);
	sub.add(check);

	ExprBlock fail(pos, check);
	check.fail(fail);

	fail.add(namedExpr(fail, pos, "++*", namedExpr(fail, pos, "failed", result)));

	Str exprText = "${lhs.text.trimWhitespace} ${op.name} ${rhs.text.trimWhitespace}";

	// Generate a nice message on failure.
	ConcatExpr msg(pos, fail);
	msg.append(StrLiteral(pos, "Failed: ${exprText} ==> "));
	msg.append(l);
	msg.append(StrLiteral(pos, " ${reverseName(op.name)} "));
	msg.append(r);

	fail.add(FnCall(pos, fail.scope, named{print<Str>}, Actuals(msg)));

	sub.addCatch(createCatch(pos, block, result, exprText));

	sub;
}

package Str reverseName(Str original) {
	if (original == "==")
		return "!=";
	else if (original == "!=")
		return "==";
	else if (original == "<")
		return ">=";
	else if (original == ">")
		return "<=";
	else if (original == "<=")
		return ">";
	else if (original == ">=")
		return "<";
	else if (original == "is")
		return "!is";
	else if (original == "!is")
		return "is";
	return "!" + original;
}

package Expr checkExpr(SrcPos pos, Block block, Condition cond, Str text) on Compiler {
	LocalVar resultVar = findResult(block);
	LocalVarAccess result(pos, resultVar);

	TryBlock sub(pos, block);

	// Increment 'total'.
	sub.add(namedExpr(sub, pos, "++*", namedExpr(sub, pos, "total", result)));

	// Check if the condition evaluates to true.
	If check(sub, cond);
	sub.add(check);

	ExprBlock fail(pos, check);
	check.fail(fail);

	fail.add(namedExpr(fail, pos, "++*", namedExpr(fail, pos, "failed", result)));

	// Generate a message. In some cases, we could be more specific (e.g. for downcasts).
	text = text.trimWhitespace;
	StrLiteral msg(pos, "Failed: ${text} ==> the check was false.");
	fail.add(FnCall(pos, fail.scope, named{print<Str>}, Actuals(msg)));

	sub.addCatch(createCatch(pos, block, result, text));

	sub;
}

package Expr checkExpr(SrcPos pos, Block block, ExprText expr) on Compiler {
	LocalVar resultVar = findResult(block);
	LocalVarAccess result(pos, resultVar);

	TryBlock sub(pos, block);

	// Increment 'total'.
	sub.add(namedExpr(sub, pos, "++*", namedExpr(sub, pos, "total", result)));

	// Run the expression.
	sub.add(expr.expr);

	// If we got here, it did not throw.
	sub.addCatch(createCatch(pos, block, result, expr.text));

	sub;
}

package Expr checkExpr(SrcPos pos, Block block, ExprText expr, SrcName exception) on Compiler {
	LocalVar resultVar = findResult(block);
	LocalVarAccess result(pos, resultVar);

	TryBlock sub(pos, block);

	// Increment 'total'.
	sub.add(namedExpr(sub, pos, "++*", namedExpr(sub, pos, "total", result)));

	// Run the expression.
	sub.add(expr.expr);

	// If we got here, it did not throw.
	sub.add(namedExpr(sub, pos, "++*", namedExpr(sub, pos, "failed", result)));

	Str text = expr.text.trimWhitespace;
	StrLiteral msg(pos, "Failed: ${text} ==> did not throw ${exception} as expected.");
	sub.add(FnCall(pos, sub.scope, named{print<Str>}, Actuals(msg)));

	// Add catch handler for the expected case.
	{
		CatchBlock catch(pos, block, exception, null);
		catch.expr = Expr(pos);
		sub.addCatch(catch);
	}

	// Add catch handler for other exceptions.
	{
		CatchBlock catch(pos, block, named{Exception}, SStr("error"));
		ExprBlock handler(pos, catch);
		catch.expr = handler;
		sub.addCatch(catch);

		handler.add(namedExpr(sub, pos, "++*", namedExpr(handler, pos, "failed", result)));

		ConcatExpr msg(pos, block);
		msg.append(StrLiteral(pos, "Crashed: ${text} ==> did not get exception ${exception} as expected, got "));
		msg.append(namedExpr(handler, pos, "error", Actuals()));
		handler.add(FnCall(pos, handler.scope, named{print<Str>}, Actuals(msg)));
	}

	// print(sub.toS);

	sub;
}

package Expr abortExpr(SrcPos pos, Block block) on Compiler {
	LocalVar resultVar = findResult(block);
	LocalVarAccess result(pos, resultVar);

	ExprBlock sub(pos, block);
	AssignOpInfo assignOp(SStr("=", pos), 0, false);
	sub.add(mkOperator(sub, namedExpr(sub, pos, "aborted", result), assignOp, BoolLiteral(pos, true)));

	sub.add(Return(pos, sub, result));

	sub;
}