-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.cpp
More file actions
54 lines (46 loc) · 1.56 KB
/
Copy pathlambda.cpp
File metadata and controls
54 lines (46 loc) · 1.56 KB
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
/**
* File: lambda.cpp
* ----------------
*/
#include "genlib.h"
#include "lambda.h"
#include "symbol.h"
#include "strutils.h"
#include "lst.h"
#include <iostream>
using namespace std;
LambdaExpression::LambdaExpression(Expression *parameterList,
Expression *parameterizedExpression){
// cout << "New lambda" << endl;
ListExpression * paraList = dynamic_cast<ListExpression *>(parameterList);
if (paraList == NULL) {
Error("Lambda expression expects its 1st argument to be a list");
}
while (!paraList->isEmpty()) {
SymbolExpression * tmpSymb = dynamic_cast<SymbolExpression *>(paraList->car());
paraList = paraList->cdr();
if (tmpSymb == NULL) {
Error("Lambda expression expects its 1st argument to be a list of symbolic expressions");
}
locals.add(tmpSymb);
}
exp = parameterizedExpression;
}
/**
* Rather than return the full stringification of the full lambda,
* we elect to return an abbreviated string that just makes it clear
* the expression is some sort of a lambda.
*/
Expression *LambdaExpression::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != locals.size()) {
Error("Wrong number of arguments supplied");
}
EvalState localState(state);
for (int i = 0; i <= locals.size() - 1; ++i) {
localState.setExpression(locals[i]->toString(), arguments[i]->eval(state));
}
// cout << locals[0]->toString() << endl;
//cout << exp->toString() << endl;
return exp->eval(localState);
}