File: repeat.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (41 lines) | stat: -rw-r--r-- 915 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
use lang:bs;
use lang:bs:macro;
use core:lang;
use core:asm;

class RepeatStmt extends Expr {
	private Expr times;
	private Expr body;

	init(SrcPos pos, Block parent, Expr times, Expr body) {
		init(pos) {
			times = expectCastTo(times, named{Nat}, parent.scope);
			body = body;
		}
	}

	void code(CodeGen gen, CodeResult result) : override {
		// Compute the number of iterations.
		VarInfo counter = gen.createVar(named{Nat});
		times.code(gen, CodeResult(named{Nat}, counter));

		// Top of the loop.
		Label loopStart = gen.l.label();
		gen.l << loopStart;

		// Check number of iterations.
		Label loopEnd = gen.l.label();
		gen.l << cmp(counter.v, natConst(0));
		gen.l << jmp(loopEnd, CondFlag:ifEqual);

		// Evaluate the body.
		body.code(gen, CodeResult());

		// Decrement the counter and jump to the start.
		gen.l << sub(counter.v, natConst(1));
		gen.l << jmp(loopStart);

		gen.l << loopEnd;
	}

}