-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctionManager.cpp
executable file
·86 lines (71 loc) · 2.64 KB
/
FunctionManager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "FunctionManager.hpp"
#include "functions.hpp"
#include "cons.hpp"
#include <ctime>
/// define static members
map<string, FunctionManager::func> FunctionManager::func_defs_m;
FunctionManager* FunctionManager::instance = NULL;
FunctionManager::FunctionManager() {
/// init -> is only supposed to be called once
add_function("ceiling", &ceiling_func);
add_function("floor", &floor_func);
add_function("quote", "e_func);
add_function("cons", &cons_func);
add_function("car", &car_func);
add_function("cdr", &cdr_func);
add_function("intp", &intp_func);
add_function("doublep", &doublep_func);
add_function("symbolp", &symbolp_func);
add_function("nullp", &nullp_func);
add_function("listp", &listp_func);
add_function("if", &if_func);
add_function("define", &define_func);
add_function("<", &less_than_func);
add_function("not", ¬_func);
add_function("print", &print_func);
add_function("eval", &eval_func);
add_function("parse", &parse_func);
add_function("parse-eval", &parse_eval_func);
add_function("lambda", &lambda_func);
add_function("apply", &apply_func);
add_function("let", &let_func);
add_function("rand", &rand_func);
srand(time(0)); // seed for rand function
add_function("str", &str_func);
add_function("substr", &substr_func);
add_function("appstr", &appstr_func);
/// CSI compatability
add_function("int?", &intp_func);
add_function("double?", &doublep_func);
add_function("symbol?", &symbolp_func);
add_function("null?", &nullp_func);
add_function("list?", &listp_func);
}
/// Singleton Pattern
FunctionManager* FunctionManager::Instance() {
if (instance == NULL) {
instance = new FunctionManager();
}
return instance;
}
void FunctionManager::add_function(string key, func function) throw (logic_error) {
pair<map<string, func>::iterator,bool> ret;
ret = func_defs_m.insert(pair<string, func>(key, function));
if (ret.second == false) {
/// logic_error, since the user can not define functions by themselves (yet)
throw logic_error("Can not redefine function!");
}
}
bool FunctionManager::is_function(string fname) {
return !(func_defs_m.find(fname) == func_defs_m.end());
}
Cell* FunctionManager::call_function(const FunctionCell* func_cell, Cell* args) throw (runtime_error) {
string fname = func_cell->get_symbol();
if (!is_function(fname)) {
throw runtime_error( fname + " is undefined" );
}
/// resolve function pointer to a callable function
Cell* (*func)(const FunctionCell*, Cell*);
func = func_defs_m[fname];
return func(func_cell, args);
}