-
Notifications
You must be signed in to change notification settings - Fork 0
/
LispEval.cpp
68 lines (63 loc) · 1.71 KB
/
LispEval.cpp
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
#include "LispObj.h"
#include "LispNil.h"
#include "LispChar.h"
#include "LispCons.h"
#include "LispEnv.h"
#include "LispFixNum.h"
#include "LispFloatNum.h"
#include "LispString.h"
#include "LispSymbol.h"
#include "LispPrimitive.h"
#include "LispEval.h"
#include "LispLambda.h"
namespace Lisp {
LispObjRef Eval::operator()(LispObjRef obj, LispEnvRef env, bool fplace) {
// trivial case :-)
if (is_nil(obj))
return nil;
// constant literal
if (is_char(obj) || is_fixnum(obj) || is_floatnum(obj) || is_string(obj) || is_primitive(obj))
return obj;
// symbol lookup
if (is_symbol(obj)) {
LispObjRef result = fplace ? env->fref(get_ctype<SymbolType>(obj).name) : env->ref(get_ctype<SymbolType>(obj).name);
return result;
}
// cons cell
if (is_cons(obj))
{
// must be function invocation -- function symbol
LispObjRef fnsym(eval(car(obj), env, is_symbol(car(obj))));
if ((is_symbol(fnsym)) | (is_primitive(fnsym)))
{
// it's a function
if (is_primitive(fnsym)) {
CPrim cfn = (CPrim)(boost::get<PrimType>(*fnsym));
// call it on the cdr
LispObjRef result = cfn(cdr(obj), env);
return result;
}
}
// else -- apply
if (is_lambda(fnsym)) {
// fnsym is the lambda
// cdr(obj) is the values applied
LispObjRef params = cdr(obj);
LispEnvRef newEnv(new LispEnv(env));
LispObjRef args = get_ctype<LambdaType>(fnsym).args;
while (!is_nil(args)) {
LispObjRef param = car(params);
LispObjRef arg = car(args);
if (is_symbol(arg)) {
newEnv->set(get_ctype<SymbolType>(arg).name,param);
}
if (is_cons(params))
params = cdr(params);
args = cdr(args);
}
return eval(get_ctype<LambdaType>(fnsym).body, newEnv);
}
}
return nil;
};
}