-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheme_std_lib.cpp
226 lines (206 loc) · 7.74 KB
/
scheme_std_lib.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
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
#include <iostream>
#include "type.h"
TPairType* GetPairType(ExprType* expr) {
switch (expr->Type) {
case T_Bool: {
return (new TPairTypeBool(dynamic_cast<BoolType*>(expr)->GetValue()));
}
case T_String : {
return (new TPairTypeString(dynamic_cast<StringType*>(expr)->GetValue()));
}
case T_Symbol : {
return (new TPairTypeSymbol(dynamic_cast<SymbolType*>(expr)->GetValue()));
}
case T_Char : {
return (new TPairTypeChar(dynamic_cast<CharType*>(expr)->GetValue()));
}
case T_Int : {
return (new TPairTypeInt(dynamic_cast<NumberIntType*>(expr)->GetValue()));
}
case T_Double : {
return (new TPairTypeDouble(dynamic_cast<NumberDoubleType*>(expr)->GetValue()));
}
case T_List : {
return (new TPairTypeList(dynamic_cast<ListType*>(expr)->GetValue()));
}
case T_Ident : {
return GetPairType(dynamic_cast<IdentType*>(expr)->GetValue().get());
}
default: {
break;
//error
}
}
}
ExprType* GetList(std::vector< std::shared_ptr<ExprType> >& exprs) {
if (!exprs.size()) {
return (new ListType(new VList(TPairTypePtr(nullptr))));
} else {
VList* list = new VList(TPairTypePtr(GetPairType(exprs.at(exprs.size()-1).get())));
for (int i = exprs.size()-2; i >= 0; i--) {
list->InsertAfter(std::shared_ptr<VList>(new VList(TPairTypePtr(GetPairType(exprs.at(i).get())))));
}
return (new ListType(list));
}
}
ExprType* cons(std::vector<std::shared_ptr<ExprType>>& expr) {
if (!expr.size()) {
return (new ListType(new VList(TPairTypePtr(nullptr))));
}
VList* list = new VList(TPairTypePtr(GetPairType(expr.at(1).get())));
list->ConvetToPair(GetPairType(expr.at(0).get()));
return (new ListType(list));
}
ExprType* append(std::vector<std::shared_ptr<ExprType>>& expr) {
if (!expr.size()) {
return (new ListType(new VList(TPairTypePtr(nullptr))));
}
std::shared_ptr<TPairType> list(GetPairType(expr.at(expr.size()-1).get()));
if (list->GetType() != PT_List) {
//error
}
for (size_t i = expr.size()-2; i >= 1; i--) {
std::shared_ptr<TPairType> addList(GetPairType(expr.at(i).get()));
if (addList->GetType() != PT_List) {
//error
} else {
((TPairTypeList*)list.get())->GetValue()->InsertAfter(((TPairTypeList*)addList.get())->GetValue());
}
}
std::shared_ptr<TPairType> lType(GetPairType(expr.at(0).get()));
if (lType->GetType() == PT_List && ((TPairTypeList*)lType.get())->GetValue()->isList()) {
((TPairTypeList*)list.get())->GetValue()->InsertAfter(((TPairTypeList*)lType.get())->GetValue());
} else {
((TPairTypeList*)list.get())->GetValue()->ConvetToPair(lType.get());
}
return (new ListType(((TPairTypeList*)list.get())->GetValue()));
}
ExprType* plus(std::vector<std::shared_ptr<ExprType>>& expr) {
double result = 0;
if (!expr.size()) {
//error
} else {
for (int i = expr.size()-1; i >= 0; i--) {
if (expr.at(i)->Type == T_Int) {
result += dynamic_cast<NumberIntType*>(expr.at(i).get())->GetValue();
} else if (expr.at(i)->Type == T_Double) {
result += dynamic_cast<NumberDoubleType*>(expr.at(i).get())->GetValue();
} else {
//error
}
}
}
return (new NumberDoubleType(result));
}
ExprType* minus(std::vector<std::shared_ptr<ExprType>>& expr) {
double result = 0;
if (!expr.size()) {
//error
} else if (expr.size() > 1){
if (expr.at(expr.size()-1)->Type == T_Int) {
result = dynamic_cast<NumberIntType*>(expr.at(expr.size()-1).get())->GetValue();
} else if (expr.at(expr.size()-1)->Type == T_Double) {
result = dynamic_cast<NumberDoubleType*>(expr.at(expr.size()-1).get())->GetValue();
} else {
//error
}
for (int i = expr.size()-2; i >= 0; i--) {
if (expr.at(i)->Type == T_Int) {
result -= dynamic_cast<NumberIntType*>(expr.at(i).get())->GetValue();
} else if (expr.at(i)->Type == T_Double) {
result -= dynamic_cast<NumberDoubleType*>(expr.at(i).get())->GetValue();
} else {
//error
}
}
} else {
if (expr.at(0)->Type == T_Int) {
result -= dynamic_cast<NumberIntType*>(expr.at(0).get())->GetValue();
} else if (expr.at(0)->Type == T_Double) {
result -= dynamic_cast<NumberDoubleType*>(expr.at(0).get())->GetValue();
} else {
//error
}
}
return (new NumberDoubleType(result));
}
ExprType* mult(std::vector<std::shared_ptr<ExprType>>& expr) {
double result = 1;
if (!expr.size()) {
//error
} else {
for (int i = expr.size()-1; i >= 0; i--) {
if (expr.at(i)->Type == T_Int) {
result *= dynamic_cast<NumberIntType*>(expr.at(i).get())->GetValue();
} else if (expr.at(i)->Type == T_Double) {
result *= dynamic_cast<NumberDoubleType*>(expr.at(i).get())->GetValue();
} else {
//error
}
}
}
return (new NumberDoubleType(result));
}
ExprType* defineFun(std::vector<std::shared_ptr<ExprType>>& expr) {
if (expr.size() < 2) {
//error
} else if (expr.at(1)->Type == T_Ident && expr.size() == 2) {
dynamic_cast<IdentType*>(expr.at(1).get())->GetValue() = expr.at(0);
}
}
ExprType* division(std::vector<std::shared_ptr<ExprType>>& expr) {
double result = 1;
if (!expr.size()) {
//error
} else if (expr.size() > 1){
if (expr.at(expr.size()-1)->Type == T_Int) {
result = dynamic_cast<NumberIntType*>(expr.at(expr.size()-1).get())->GetValue();
} else if (expr.at(expr.size()-1)->Type == T_Double) {
result = dynamic_cast<NumberDoubleType*>(expr.at(expr.size()-1).get())->GetValue();
} else {
//error
}
for (int i = expr.size()-2; i >= 0; i--) {
if (expr.at(i)->Type == T_Int) {
result /= dynamic_cast<NumberIntType*>(expr.at(i).get())->GetValue();
} else if (expr.at(i)->Type == T_Double) {
result /= dynamic_cast<NumberDoubleType*>(expr.at(i).get())->GetValue();
} else {
//error
}
}
} else {
if (expr.at(0)->Type == T_Int) {
result = 1.0 / dynamic_cast<NumberIntType*>(expr.at(0).get())->GetValue();
} else if (expr.at(0)->Type == T_Double) {
result = 1.0 / dynamic_cast<NumberDoubleType*>(expr.at(0).get())->GetValue();
} else {
//error
}
}
return (new NumberDoubleType(result));
}
ExprType* equally(std::vector<std::shared_ptr<ExprType>>& expr) {
if (expr.size() < 2) {
//error
} else {
double result;
if (expr.at(expr.size()-1)->Type == T_Int) {
result = dynamic_cast<NumberIntType*>(expr.at(expr.size()-1).get())->GetValue();
} else if (expr.at(expr.size()-1)->Type == T_Double) {
result = dynamic_cast<NumberDoubleType*>(expr.at(expr.size()-1).get())->GetValue();
} else {
//error
}
for (int i = expr.size()-2; i >= 0; i--) {
if (expr.at(i)->Type == T_Int && dynamic_cast<NumberIntType*>(expr.at(i).get())->GetValue() != result) {
return (new BoolType(false));
} else if (expr.at(i)->Type == T_Double && dynamic_cast<NumberDoubleType*>(expr.at(i).get())->GetValue() != result) {
return (new BoolType(false));
} else {
//error
}
}
return (new BoolType(true));
}
}