This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.cpp
More file actions
122 lines (116 loc) · 3.86 KB
/
Copy pathbuiltins.cpp
File metadata and controls
122 lines (116 loc) · 3.86 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
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
#include "builtins.h"
#include "error.h"
const std::unordered_map<std::string, BuiltinFuncType> BUILTIN_FUNCS{
//核心库
{"apply", applyFunc},
{"display", displayFunc},
{"displayln", displaylnFunc},
{"error", errorFunc},
{"eval", evalFunc},
{"exit", exitFunc},
{"newline", newlineFunc},
{"print", printFunc},
{"readline", readlineFunc},
//类型检查库
{"atom?", typeCheckFunc([](ValuePtr v) {
return (v->isSelfEvaluating() &&
typeid(*v) != typeid(BuiltinProcValue)) ||
typeid(*v) == typeid(NilValue) ||
typeid(*v) == typeid(SymbolValue);
})},
{"boolean?", typeCheckFunc([](ValuePtr v) {
return typeid(*v) == typeid(BooleanValue);
})},
{"integer?", typeCheckFunc([](ValuePtr v) {
return typeid(*v) == typeid(NumericValue) &&
floor(v->asNumber()) == ceil(v->asNumber());
})},
{"list?", typeCheckFunc([](ValuePtr v) {
return (typeid(*v) == typeid(PairValue) && v->isList()) || (v->isNil());
})},
{"number?", typeCheckFunc([](ValuePtr v) {
return typeid(*v) == typeid(NumericValue);
})},
{"null?",
typeCheckFunc([](ValuePtr v) { return typeid(*v) == typeid(NilValue); })},
{"pair?",
typeCheckFunc([](ValuePtr v) { return typeid(*v) == typeid(PairValue); })},
{"procedure?", typeCheckFunc([](ValuePtr v) {
return (typeid(*v) == typeid(BuiltinProcValue) ||
typeid(*v) == typeid(LambdaValue));
})},
{"string?", typeCheckFunc([](ValuePtr v) {
return typeid(*v) == typeid(StringValue);
})},
{"symbol?", typeCheckFunc([](ValuePtr v) {
return typeid(*v) == typeid(SymbolValue);
})},
// 对子与列表操作库
{"append", appendFunc},
{"car", carFunc},
{"cdr", cdrFunc},
{"cons", consFunc},
{"length", lengthFunc},
{"list", listFunc},
{"map", mapFunc},
{"filter", filterFunc},
{"reduce", reduceFunc},
// 算术运算库
{"+", addFunc},
{"-", minusFunc},
{"*", multiplyFunc},
{"/", divideFunc},
{"abs", absFunc},
{"expt", exptFunc},
{"quotient", quotientFunc},
{"modulo", moduloFunc},
{"remainder", remainderFunc},
//比较库
{"eq?", eqFunc},
{"equal?", equalFunc},
{"not", notFunc},
{"=", optEqFunc},
{"<", ltFunc},
{">", gtFunc},
{"<=", lteFunc},
{">=", gteFunc},
{"even?", evenFunc},
{"odd?", oddFunc},
{"zero?", zeroFunc},
//字符串处理
{"strcat", strcatFunc},
{"strcmp", strcmpFunc},
{"stod", stodFunc},
{"substr", substrFunc},
//彩蛋
{"python", easterEggFunc("The Zen of Python, by Tim Peters\n\
Beautiful is better than ugly.\n\
Explicit is better than implicit.\n\
Simple is better than complex.\n\
Complex is better than complicated.\n\
Flat is better than nested.\n\
Sparse is better than dense.\n\
Readability counts.\n\
Special cases aren't special enough to break the rules.\n\
Although practicality beats purity.\n\
Errors should never pass silently.\n\
Unless explicitly silenced.\n\
In the face of ambiguity, refuse the temptation to guess.\n\
There should be one-- and preferably only one --obvious way to do it.\n\
Although that way may not be obvious at first unless you're Dutch.\n\
Now is better than never.\n\
Although never is often better than *right* now.\n\
If the implementation is hard to explain, it's a bad idea.\n\
If the implementation is easy to explain, it may be a good idea.\n\
Namespaces are one honking great idea -- let's do more of those!")},
{"madoka", easterEggFunc("Don't forget.\n\
Always, somewhere,\n\
someone is fighting for you.\n\
As long as you remember her,\n\
you're not alone.")},
{"eva", easterEggFunc("👏 Omedetou! 👏\n\
👏 Omedetou! 👏\n\
👏 Omedetou! 👏\n\
👏 Omedetou! 👏\n\
👏 Omedetou! 👏")}
};