File: Join.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 (144 lines) | stat: -rw-r--r-- 4,423 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
#include "stdafx.h"
#include "Join.h"
#include "Array.h"
#include "Fn.h"
#include "Compiler/Function.h"
#include "Compiler/Exception.h"
#include "Compiler/Engine.h"
#include "Code/Listing.h"

namespace storm {

	Join::Join(ArrayBase *array, Str *separator)
		: array(array), separator(separator), lambda(null), thunk(null) {}

	Join::Join(ArrayBase *array, Str *separator, FnBase *lambda, void *lambdaCall, os::CallThunk thunk)
		: array(array), separator(separator), lambda(lambda), lambdaCall(lambdaCall), thunk(thunk) {}

	void Join::toS(StrBuf *to) const {
		if (lambda && thunk) {
			for (Nat i = 0; i < array->count(); i++) {
				if (i > 0 && separator)
					*to << separator;

				void *params[2] = { (void *)&lambda, array->getRaw(i) };
				Str *result = null;
				(*thunk)(lambdaCall, true, params, null, &result);
				*to << result;
			}
		} else {
			const Handle &handle = array->handle;

			for (Nat i = 0; i < array->count(); i++) {
				if (i > 0 && separator)
					*to << separator;
				(*handle.toSFn)(array->getRaw(i), to);
			}
		}
	}

	Join *CODECALL joinBase(ArrayBase *array) {
		return new (array) Join(array, null);
	}

	Join *CODECALL joinSeparator(ArrayBase *array, Str *separator) {
		return new (array) Join(array, separator);
	}

	Join *CODECALL createJoin(ArrayBase *array, Str *separator, FnBase *lambda, void *lambdaCall, os::CallThunk thunk) {
		return new (array) Join(array, separator, lambda, lambdaCall, thunk);
	}


	JoinTemplate::JoinTemplate() : Template(new (engine()) Str(S("join"))) {
		created2 = new (this) Map<Type *, Named *>();
		created3 = new (this) Map<Type *, Named *>();
	}

	MAYBE(Named *) JoinTemplate::generate(SimplePart *part) {
		Array<Value> *params = part->params;
		if (params->count() < 1)
			return null;
		if (params->count() > 3)
			return null;

		Value array = params->at(0).asRef(false);
		Value element = unwrapArray(array).asRef(false);
		// It wasn't an array - type did not change.
		if (array.type == element.type)
			return null;

		Engine &e = engine();
		Type *strType = Str::stormType(e);
		Type *joinType = Join::stormType(e);

		if (params->count() == 1) {
			// join(<array>)
			Array<Value> *params = new (this) Array<Value>(1, Value());
			params->at(0) = array;
			return nativeFunction(e, Value(joinType), S("join"), params, address(&joinBase));
		} else if (params->count() == 2 && params->at(1).type == strType) {
			// join(<array>, <string>)
			Array<Value> *params = new (this) Array<Value>(2, Value());
			params->at(0) = array;
			params->at(1) = Value(strType);
			return nativeFunction(e, Value(joinType), S("join"), params, address(&joinSeparator));
		} else {
			// either: join(<array>, <lambda>) or join(<array>, <string>, <lambda>)
			if (params->count() == 3 && params->at(1).type != strType)
				return null;

			// See if we created this version earlier.
			Map<Type *, Named *> *created = created2;
			if (params->count() == 3)
				created = created3;

			if (Named *x = created->get(element.type, null))
				return x;

			// Just generate what we need, otherwise auto-inference does not work.
			Array<Value> *fnParams = new (this) Array<Value>(2, Value());
			fnParams->at(0) = Value(strType);
			fnParams->at(1) = element.asRef(false);
			Type *f = fnType(fnParams);

			Array<Value> *par = new (this) Array<Value>(*params);
			par->at(0) = array;
			par->last() = Value(f);

			Function *lambdaCall = as<Function>(f->find(S("call"), valList(e, 2, Value(f), element), Scope()));
			if (!lambdaCall)
				throw new (this) InternalError(S("Unable to find the 'call' member of a function object."));

			code::Listing *l = new (this) code::Listing(false, e.ptrDesc());
			{
				using namespace code;

				TypeDesc *ptrDesc = e.ptrDesc();

				Var array = l->createParam(ptrDesc);
				Operand sep = ptrConst(0);
				if (params->count() == 3)
					sep = l->createParam(ptrDesc);
				Var lambda = l->createParam(ptrDesc);

				*l << prolog();
				*l << fnParam(ptrDesc, array);
				*l << fnParam(ptrDesc, sep);
				*l << fnParam(ptrDesc, lambda);
				*l << fnParam(ptrDesc, lambdaCall->ref());
				*l << fnParam(ptrDesc, lambdaCall->thunkRef());
				*l << fnCall(e.ref(builtin::createJoin), false, ptrDesc, ptrA);
				*l << fnRet(ptrA);
			}

			Named *result = dynamicFunction(e, Value(joinType), S("join"), par, l);
			created->put(element.type, result);
			return result;
		}

		// Should not happen:
		return null;
	}

}