File: NamePart.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 (171 lines) | stat: -rw-r--r-- 4,258 bytes parent folder | download
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
#include "stdafx.h"
#include "NamePart.h"
#include "Core/SrcPos.h"
#include "Scope.h"
#include "Name.h"
#include "Type.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Core/CloneEnv.h"
#include "NameSet.h"
#include "Exception.h"
#include <limits>

namespace storm {

	NamePart::NamePart(Str *name) : name(name) {}

	NamePart::NamePart(const wchar *name) : name(new (engine()) Str(name)) {}

	NamePart::NamePart(const NamePart &o) : ObjectOn<Compiler>(o), name(o.name) {}

	SimplePart *NamePart::find(Scope scope) {
		return new (this) SimplePart(name);
	}

	void NamePart::toS(StrBuf *to) const {
		*to << name;
	}


	/**
	 * SimplePart.
	 */

	SimplePart::SimplePart(Str *name) : NamePart(name), params(new (engine()) Array<Value>()) {}

	SimplePart::SimplePart(syntax::SStr *name) : NamePart(name->v), params(new (engine()) Array<Value>()) {}

	SimplePart::SimplePart(const wchar *name) : NamePart(name), params(new (engine()) Array<Value>()) {}

	SimplePart::SimplePart(Str *name, Array<Value> *params) : NamePart(name), params(params) {}

	SimplePart::SimplePart(Str *name, Value param) : NamePart(name), params(new (engine()) Array<Value>(1, param)) {}

	SimplePart::SimplePart(const SimplePart &o) : NamePart(o), params(new (engine()) Array<Value>(*o.params)) {}

	SimplePart *SimplePart::find(Scope scope) {
		return this;
	}

	Int SimplePart::matches(Named *candidate, Scope source) const {
		Array<Value> *c = candidate->params;
		if (c->count() != params->count())
			return -1;

		int distance = 0;

		for (nat i = 0; i < c->count(); i++) {
			const Value &match = c->at(i);
			const Value &ours = params->at(i);

			if (match == Value() && ours != Value())
				return -1;
			if (!match.matches(ours, candidate->flags))
				return -1;
			if (ours.type && match.type)
				distance += ours.type->distanceFrom(match.type);
		}

		return distance;
	}

	MAYBE(SimplePart *) SimplePart::nextOption() const {
		return null;
	}

	Bool SimplePart::scopeParam(Nat id) const {
		return false;
	}

	Bool SimplePart::visible(Named *candidate, Scope source) const {
		return candidate->visibleFrom(source);
	}

	static void outputPart(StrBuf *to, const Value &v) {
		if (v.type)
			v.type->safePath()->toS(to);
		else
			*to << S("void");
		if (v.ref)
			*to << S("&");
	}

	void SimplePart::toS(StrBuf *to) const {
		*to << name;
		if (params != null && params->count() > 0) {
			*to << L"(";
			outputPart(to, params->at(0));
			for (nat i = 1; i < params->count(); i++) {
				*to << L", ";
				outputPart(to, params->at(i));
			}
			*to << L")";
		}
	}

	Bool SimplePart::sameAs(const SimplePart *o) const {
		if (!sameType(this, o))
			return false;

		if (*name != *o->name)
			return false;
		if (params->count() != o->params->count())
			return false;

		for (Nat i = 0; i < params->count(); i++)
			if (params->at(i) != o->params->at(i))
				return false;

		return true;
	}

	/**
	 * RecPart.
	 */

	RecPart::RecPart(Str *name) : NamePart(name), params(new (engine()) Array<Name *>()) {}

	RecPart::RecPart(syntax::SStr *name) : NamePart(name->v), params(new (engine()) Array<Name *>()) {}

	RecPart::RecPart(Str *name, Array<Name *> *params) : NamePart(name), params(params) {}

	RecPart::RecPart(const RecPart &o) : NamePart(o), params(new (engine()) Array<Name *>(*o.params)) {}

	SimplePart *RecPart::find(Scope scope) {
		Array<Value> *v = new (this) Array<Value>();
		v->reserve(params->count());

		try {
			for (nat i = 0; i < params->count(); i++) {
				Name *n = params->at(i);
				if (SrcName *s = as<SrcName>(n)) {
					v->push(scope.value(s, s->pos));
				} else {
					v->push(scope.value(n, SrcPos()));
				}
			}
		} catch (const CodeError *) {
			// It seems like a compilation error, propagate it further now. Otherwise, we get really
			// strange error messages in some cases (e.g. type X not found due to a compilation
			// error inside that type).
			throw;
		} catch (const Exception *) {
			// Return null as per specification.
			return null;
		}

		return new (this) SimplePart(name, v);
	}

	void RecPart::toS(StrBuf *to) const {
		*to << name;
		if (params->count() > 0) {
			*to << L"(" << params->at(0);
			for (nat i = 1; i < params->count(); i++)
				*to << L", " << params->at(i);
			*to << L")";
		}
	}

}