File: Node.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (73 lines) | stat: -rw-r--r-- 1,958 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
#include "stdafx.h"
#include "Node.h"
#include "Exception.h"
#include "Type.h"
#include "Engine.h"
#include "Package.h"
#include "Core/Str.h"

namespace storm {
	namespace syntax {

		Node::Node() {}

		Node::Node(SrcPos pos) : pos(pos) {}

		void Node::throwError() {
			throw new (this) SyntaxError(pos, S("Trying to transform a node not representing a match."));
		}

		Array<Node *> *Node::children() {
			return new (this) Array<Node *>();
		}

		Array<Node *> *Node::allChildren() {
			Array<Node *> *result = new (this) Array<Node *>();
			allChildren(result, null);
			return result;
		}

		Array<Node *> *Node::allChildren(Type *type) {
			Array<Node *> *result = new (this) Array<Node *>();
			allChildren(result, type);
			return result;
		}

		void Node::allChildren(Array<Node *> *result, MAYBE(Type *) type) {
			Array<Node *> *here = children();
			for (Nat i = 0; i < here->count(); i++) {
				Node *at = here->at(i);

				if (!type || runtime::typeOf(at)->isA(type))
					result->push(at);

				at->allChildren(result, type);
			}
		}


		const void *transformFunction(Type *type, const Value &result, const Value &param1, const Value &param2) {
			Scope root = type->engine.scope();
			Array<Value> *par = new (type) Array<Value>();
			par->push(thisPtr(type));
			if (param1 != Value())
				par->push(param1);
			if (param2 != Value())
				par->push(param2);
			SimplePart *part = new (type) SimplePart(new (type) Str(L"transform"), par);
			if (Function *fn = as<Function>(type->find(part, root))) {
				if (!result.mayReferTo(fn->result)) {
					Str *msg = TO_S(type, S("The function ") << fn->identifier() << S(" returns ")
									<< fn->result << S(", which is incompatible with ") << result);
					throw new (type) InternalError(msg);
				}

				return fn->ref().address();
			} else {
				Str *msg = TO_S(type, S("Can not find ") << part << S(" in ") << type->identifier());
				throw new (type) InternalError(msg);
			}
		}

	}
}