summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/scheme/eval.hpp
blob: 5074471931f699e3cc028da577b221bb4ac9bc8e (plain)
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
283
284
285
286
287
288
289
290
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_scheme_eval_hpp
#define tuscany_scheme_eval_hpp

/**
 * Core script evaluation logic.
 */

#include <string.h>
#include "list.hpp"
#include "value.hpp"
#include "primitive.hpp"
#include "io.hpp"
#include "environment.hpp"

namespace tuscany {
namespace scheme {

const value evalExpr(const value& exp, Env& env);

const value compoundProcedureSymbol("compound-procedure");
const value procedureSymbol("procedure");
const value applySymbol("apply");
const value beginSymbol("begin");
const value condSymbol("cond");
const value elseSymbol("else");
const value ifSymbol("if");

const bool isBegin(const value& exp) {
    return isTaggedList(exp, beginSymbol);
}

const list<value> beginActions(const value& exp) {
    return cdr((list<value> )exp);
}

const bool isLambdaExpr(const value& exp) {
    return isTaggedList(exp, lambdaSymbol);
}

const list<value> lambdaParameters(const value& exp) {
    return car(cdr((list<value> )exp));
}

static list<value> lambdaBody(const value& exp) {
    return cdr(cdr((list<value> )exp));
}

const value makeProcedure(const list<value>& parameters, const value& body, const Env& env) {
    return mklist<value>(procedureSymbol, parameters, body, env);
}

const bool isApply(const value& exp) {
    return isTaggedList(exp, applySymbol);
}

const bool isApplication(const value& exp) {
    return isList(exp);
}

const value operat(const value& exp) {
    return car((list<value> )exp);
}

const list<value> operands(const value& exp) {
    return cdr((list<value> )exp);
}

const list<value> listOfValues(const list<value> exps, Env& env) {
    if(isNil(exps))
        return list<value> ();
    return cons(evalExpr(car(exps), env), listOfValues(cdr(exps), env));
}

const value applyOperat(const value& exp) {
    return cadr((list<value> )exp);
}

const value applyOperand(const value& exp) {
    return caddr((list<value> )exp);
}

const bool isCompoundProcedure(const value& procedure) {
    return isTaggedList(procedure, procedureSymbol);
}

const list<value> procedureParameters(const value& exp) {
    return car(cdr((list<value> )exp));
}

const value procedureBody(const value& exp) {
    return car(cdr(cdr((list<value> )exp)));
}

const Env procedureEnvironment(const value& exp) {
    return (Env)car(cdr(cdr(cdr((list<value> )exp))));
}

const bool isLastExp(const list<value>& seq) {
    return isNil(cdr(seq));
}

const value firstExp(const list<value>& seq) {
    return car(seq);
}

const list<value> restExp(const list<value>& seq) {
    return cdr(seq);
}

const value makeBegin(const list<value> seq) {
    return cons(beginSymbol, seq);
}

const value evalSequence(const list<value>& exps, Env& env) {
    if(isLastExp(exps))
        return evalExpr(firstExp(exps), env);
    evalExpr(firstExp(exps), env);
    return evalSequence(restExp(exps), env);
}

const value applyProcedure(const value& procedure, list<value>& arguments) {
    if(isPrimitiveProcedure(procedure))
        return applyPrimitiveProcedure(procedure, arguments);
    if(isCompoundProcedure(procedure)) {
        Env env = extendEnvironment(procedureParameters(procedure), arguments, procedureEnvironment(procedure));
        return evalSequence(procedureBody(procedure), env);
    }
    logStream() << "Unknown procedure type " << procedure << endl;
    return nilValue;
}

const value sequenceToExp(const list<value> exps) {
    if(isNil(exps))
        return exps;
    if(isLastExp(exps))
        return firstExp(exps);
    return makeBegin(exps);
}

const list<value> condClauses(const value& exp) {
    return cdr((list<value> )exp);
}

const value condPredicate(const value& clause) {
    return car((list<value> )clause);
}

const list<value> condActions(const value& clause) {
    return cdr((list<value> )clause);
}

const value ifPredicate(const value& exp) {
    return car(cdr((list<value> )exp));
}

const value ifConsequent(const value& exp) {
    return car(cdr(cdr((list<value> )exp)));
}

const value ifAlternative(const value& exp) {
    if(!isNil(cdr(cdr(cdr((list<value> )exp)))))
        return car(cdr(cdr(cdr((list<value> )exp))));
    return false;
}

const bool isCond(const value& exp) {
    return isTaggedList(exp, condSymbol);
}

const bool isCondElseClause(const value& clause) {
    return condPredicate(clause) == elseSymbol;
}

const bool isIf(const value& exp) {
    return isTaggedList(exp, ifSymbol);
}

const value makeIf(value predicate, value consequent, value alternative) {
    return mklist(ifSymbol, predicate, consequent, alternative);
}

const value expandClauses(const list<value>& clauses) {
    if(isNil(clauses))
        return false;
    const value first = car(clauses);
    const list<value> rest = cdr(clauses);
    if(isCondElseClause(first)) {
        if(isNil(rest))
            return sequenceToExp(condActions(first));
        logStream() << "else clause isn't last " << clauses << endl;
        return nilValue;
    }
    return makeIf(condPredicate(first), sequenceToExp(condActions(first)), expandClauses(rest));
}

const value condToIf(const value& exp) {
    return expandClauses(condClauses(exp));
}

const value evalIf(const value& exp, Env& env) {
    if(isTrue(evalExpr(ifPredicate(exp), env)))
        return evalExpr(ifConsequent(exp), env);
    return evalExpr(ifAlternative(exp), env);
}

const value evalDefinition(const value& exp, Env& env) {
    defineVariable(definitionVariable(exp), evalExpr(definitionValue(exp), env), env);
    return definitionVariable(exp);
}

const value evalExpr(const value& exp, Env& env) {
    if(isSelfEvaluating(exp))
        return exp;
    if(isQuoted(exp))
        return textOfQuotation(exp);
    if(isDefinition(exp))
        return evalDefinition(exp, env);
    if(isIf(exp))
        return evalIf(exp, env);
    if(isBegin(exp))
        return evalSequence(beginActions(exp), env);
    if(isCond(exp))
        return evalExpr(condToIf(exp), env);
    if(isLambdaExpr(exp))
        return makeProcedure(lambdaParameters(exp), lambdaBody(exp), env);
    if(isVariable(exp))
        return lookupVariableValue(exp, env);
    if(isApply(exp)) {
        list<value> applyOperandValues = evalExpr(applyOperand(exp), env);
        return applyProcedure(evalExpr(applyOperat(exp), env), applyOperandValues);
    }
    if(isApplication(exp)) {
        list<value> operandValues = listOfValues(operands(exp), env);
        return applyProcedure(evalExpr(operat(exp), env), operandValues);
    }
    logStream() << "Unknown expression type " << exp << endl;
    return nilValue;
}

const list<value> quotedParameters(const list<value>& p) {
    if (isNil(p))
        return p;
    return cons<value>(mklist<value>(quoteSymbol, car(p)), quotedParameters(cdr(p)));
}

/**
 * Evaluate an expression against a script provided as a list of values.
 */
const value evalScriptLoop(const value& expr, const list<value>& script, scheme::Env& env) {
    if (isNil(script))
        return scheme::evalExpr(expr, env);
    scheme::evalExpr(car(script), env);
    return evalScriptLoop(expr, cdr(script), env);
}

const value evalScript(const value& expr, const value& script, Env& env) {
    return evalScriptLoop(expr, script, env);
}

/**
 * Evaluate an expression against a script provided as an input stream.
 */
const value evalScript(const value& expr, istream& is, Env& env) {
    return evalScript(expr, readScript(is), env);
}

}
}
#endif /* tuscany_scheme_eval_hpp */