File: Breakable.cpp

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 (78 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Breakable.h"
#include "Compiler/Exception.h"

namespace storm {
	namespace bs {

		Breakable::Breakable(SrcPos pos, Scope scope) : Block(pos, scope) {}

		Breakable::Breakable(SrcPos pos, Block *parent) : Block(pos, parent) {}

		Breakable::To::To(code::Label lbl, code::Block block) : label(lbl), block(block) {}

		static MAYBE(Breakable *) findBreakable(Block *block) {
			BlockLookup *lookup = block->lookup;

			while (lookup) {
				Block *block = lookup->block;
				if (Breakable *b = as<Breakable>(block))
					return b;

				lookup = as<BlockLookup>(lookup->parent());
			}

			return null;
		}

		Break::Break(SrcPos pos, Block *parent) : Expr(pos) {
			if (!(breakFrom = findBreakable(parent)))
				throw new (this) SyntaxError(pos, S("Nothing to break from here. Use break inside loops."));

			breakFrom->willBreak();
		}

		ExprResult Break::result() {
			return noReturn();
		}

		void Break::code(CodeGen *state, CodeResult *r) {
			Breakable::To to = breakFrom->breakTo();
			*state->l << jmpBlock(to.label, to.block);
		}

		void Break::toS(StrBuf *to) const {
			*to << S("break");
		}

		Bool Break::isolate() {
			return false;
		}


		Continue::Continue(SrcPos pos, Block *parent) : Expr(pos) {
			if (!(continueIn = findBreakable(parent)))
				throw new (this) SyntaxError(pos, S("Nothing to continue from here. Use continue inside loops."));

			continueIn->willContinue();
		}

		ExprResult Continue::result() {
			return noReturn();
		}

		void Continue::code(CodeGen *state, CodeResult *r) {
			Breakable::To to = continueIn->continueTo();
			*state->l << jmpBlock(to.label, to.block);
		}

		void Continue::toS(StrBuf *to) const {
			*to << S("continue");
		}

		Bool Continue::isolate() {
			return false;
		}

	}
}