File: Operator.cpp

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 (282 lines) | stat: -rw-r--r-- 7,091 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
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
#include "stdafx.h"
#include "Operator.h"
#include "Named.h"
#include "Cast.h"
#include "Resolve.h"
#include "Compiler/Type.h"
#include "Compiler/Exception.h"

namespace storm {
	namespace bs {

		OpInfo::OpInfo(syntax::SStr *op, Int prio, Bool leftAssoc)
			: priority(prio), leftAssoc(leftAssoc), name(op->v), pos(op->pos) {}

		Bool OpInfo::before(OpInfo *o) {
			if (priority != o->priority)
				return priority > o->priority;

			// Left associative operators are stronger.
			if (leftAssoc && !o->leftAssoc)
				return true;

			return false;
		}

		Bool OpInfo::after(OpInfo *o) {
			return o->before(this);
		}

		Bool OpInfo::eq(OpInfo *o) {
			return !before(o) && !after(o);
		}

		void OpInfo::toS(StrBuf *to) const {
			*to << S("{name=") << name << S(", left=") << (leftAssoc ? S("true") : S("false")) << S("}");
		}

		Expr *OpInfo::meaning(Block *block, Expr *lhs, Expr *rhs) {
			Expr *fn = find(block, name, lhs, rhs);
			if (!fn) {
				Str *msg = TO_S(engine(), S("Can not find an implementation of the operator ")
								<< name << S(" for ") << lhs->result().type() << S(", ")
								<< rhs->result().type() << S("."));
				throw new (this) SyntaxError(pos, msg);
			}

			return fn;
		}

		Expr *OpInfo::find(Block *block, Str *name, Expr *lhs, Expr *rhs) {
			return find(block, name, lhs, rhs, false);
		}

		Expr *OpInfo::find(Block *block, Str *name, Expr *lhs, Expr *rhs, Bool strict) {
			Actuals *actuals = new (this) Actuals();
			actuals->add(lhs);
			actuals->add(rhs);

			BSNamePart *part = new (this) BSNamePart(name, pos, actuals);
			if (strict)
				part->strictFirst();
			SimpleName *sName = new (this) SimpleName(part);

			Named *found = block->scope.find(sName);
			if (!found) {
				ExprResult r = rhs->result();
				if (r.value()) {
					// Try to find the function inside 'rhs' as well. Auto-casting might make that work.
					found = r.type().type->find(part, block->scope);
				}
			}

			Function *fn = as<Function>(found);
			if (!fn)
				return null;

			return new (this) FnCall(pos, block->scope, fn, actuals);
		}

		OpInfo *lOperator(syntax::SStr *op, Int p) {
			return new (op) OpInfo(op, p, true);
		}

		OpInfo *rOperator(syntax::SStr *op, Int p) {
			return new (op) OpInfo(op, p, false);
		}


		Operator::Operator(Block *block, Expr *lhs, OpInfo *op, Expr *rhs)
			: Expr(op->pos), block(block), lhs(lhs), op(op), rhs(rhs), fnCall(null) {}

		// Left should be true if 'other' is 'our' left child.
		static bool after(bs::OpInfo *our, OpInfo *other, bool left) {
			if (our->after(other))
				return true;
			if (our->leftAssoc == left && our->eq(other))
				return true;
			return false;
		}

		Operator *Operator::prioritize() {
			Operator *l = as<Operator>(lhs);
			Operator *r = as<Operator>(rhs);
			Operator *me = this;

			invalidate();

			if (l && r) {
				// In:  (1 l 2) me (3 r 4)
				// Out1 (1 l (2 me 3)) r 4 if l after me && l before r && me before r
				// Out2 1 l ((2 me 3) r 4) if l after me && l after r && me before r
				// Out3 1 l (2 me (3 r 4)) if l after me && me after r
				// Out4 ((1 l 2) me 3) r 4 if l before me && me before r
				// Out5 (1 l 2) me (3 r 4) if l before me && me after r

				if (after(op, l->op, true)) {
					// Equal to: X me (3 r 4)
					// Out4 X me (3 r 4) if me before r
					// Out5 (X me 3) r 4 if me after r

					// Ignore l, use the r swap below...
					l = null;
				} else if (after(op, r->op, false)) {
					// Equal to: (1 l 2) me X
					// Out3 1 l (2 me X) if l after me
					// Out5 (1 l 2) me X if l before me

					// Ignore r, use the l swap below.
					r = null;
				} else {
					// Out1 (1 l (2 me 3)) r 4 if l before r
					// Out2 1 l ((2 me 3) r 4) if l after r
					// We can set: X = 2 me 3 and get:
					// Out1 (1 l X) r 4 if l before r
					// Out2 1 l (X r 4) if l after r

					lhs = l->rhs;
					rhs = r->lhs;

					if (after(l->op, r->op, false)) {
						// Out2
						r->lhs = me->prioritize();
						l->rhs = r;

						return l;
					} else {
						// Out1
						l->rhs = me->prioritize();
						r->lhs = l;

						return r;
					}
				}
			}

			if (l) {
				// In:  (1 l 2) me 3
				// Out1 1 l (2 me 3) if me before l
				// Out2 (1 l 2) me 3 if me after l

				// Should the other one run first?
				if (after(op, l->op, true))
					return me;

				// Switch.
				lhs = l->rhs;
				l->rhs = me->prioritize();

				return l;
			} else if (r) {
				// In:  1 me (2 r 3)
				// Out1 1 me (2 r 3) if me after r
				// Out2 (1 me 2) r 3 if me before r

				// Should the other one run first?
				if (after(op, r->op, false))
					return me;

				// Switch ourselves with the other one.
				rhs = r->lhs;
				r->lhs = me->prioritize();

				return r;
			} else {
				// We're a leaf operator.
				return me;
			}
		}

		void Operator::invalidate() {
			fnCall = null;
		}

		SrcPos Operator::largePos() {
			return pos.extend(lhs->largePos()).extend(rhs->largePos());
		}

		Expr *Operator::meaning() {
			if (!fnCall)
				fnCall = op->meaning(block, lhs, rhs);
			return fnCall;
		}

		ExprResult Operator::result() {
			Expr *m = meaning();
			return m->result();
		}

		void Operator::code(CodeGen *s, CodeResult *r) {
			Expr *m = meaning();
			return m->code(s, r);
		}

		void Operator::toS(StrBuf *to) const {
			if (Operator *l = as<Operator>(lhs))
				*to << S("<") << l << S(">");
			else
				*to << lhs;
			*to << S(" ") << op->name << S(" ");
			if (Operator *r = as<Operator>(rhs))
				*to << S("<") << r << S(">");
			else
				*to << rhs;
		}

		Operator *mkOperator(Block *block, Expr *lhs, OpInfo *op, Expr *rhs) {
			Operator *o = new (op) Operator(block, lhs, op, rhs);
			return o->prioritize();
		}

		/**
		 * Parens.
		 */

		ParenExpr::ParenExpr(Expr *wrap) : Expr(wrap->pos), wrap(wrap) {}

		ExprResult ParenExpr::result() {
			return wrap->result();
		}

		void ParenExpr::code(CodeGen *s, CodeResult *r) {
			wrap->code(s, r);
		}

		void ParenExpr::toS(StrBuf *to) const {
			*to << S("(") << wrap << S(")");
		}

		/**
		 * Create standard operators.
		 */

		Expr *STORM_FN accessExpr(Block *block, Expr *lhs, Expr *par) {
			Actuals *actual = new (block) Actuals();
			actual->add(lhs);
			actual->add(par);
			syntax::SStr *m = new (block) syntax::SStr(S("[]"));
			return namedExpr(block, m, actual);
		}

		Expr *STORM_FN prefixOperator(Block *block, syntax::SStr *o, Expr *expr) {
			Actuals *actual = new (block) Actuals();
			actual->add(expr);
			syntax::SStr *altered = new (o) syntax::SStr(*o->v + new (o) Str(S("*")), o->pos.extend(expr->largePos()));
			return namedExpr(block, altered, actual);
		}

		Expr *STORM_FN postfixOperator(Block *block, syntax::SStr *o, Expr *expr) {
			Actuals *actual = new (block) Actuals();
			actual->add(expr);
			syntax::SStr *altered = new (o) syntax::SStr(*new (o) Str(S("*")) + o->v, o->pos.extend(expr->largePos()));
			return namedExpr(block, altered, actual);
		}

		Expr *STORM_FN prePostOperator(Block *block, syntax::SStr *o, Expr *expr) {
			Actuals *actual = new (block) Actuals();
			actual->add(expr);
			return namedExpr(block, o, actual);
		}

	}
}