This commit is contained in:
aozhiwei 2023-03-04 12:00:41 +08:00
parent 8a1cc4a75a
commit 24625f05d1
2 changed files with 17 additions and 19 deletions

View File

@ -106,48 +106,48 @@ namespace a8
"+",
[] (const List& params) -> std::shared_ptr<Value>
{
Atom result;
double result = 0;
for (auto param : *params) {
Atom&& atom = std::any_cast<Atom>(param->value);
result.val += atom.val;
result += atom.val;
}
return std::make_shared<Value>(result);
return std::make_shared<Value>(Atom(result));
});
RegisterCProc
(
"-",
[] (const List& params) -> std::shared_ptr<Value>
{
Atom result;
double result = 0;
for (auto param : *params) {
Atom&& atom = std::any_cast<Atom>(param->value);
result.val -= atom.val;
result -= atom.val;
}
return std::make_shared<Value>(result);
return std::make_shared<Value>(Atom(result));
});
RegisterCProc
(
"*",
[] (const List& params) -> std::shared_ptr<Value>
{
Atom result;
double result = 0;
for (auto param : *params) {
Atom&& atom = std::any_cast<Atom>(param->value);
result.val *= atom.val;
result *= atom.val;
}
return std::make_shared<Value>(result);
return std::make_shared<Value>(Atom(result));
});
RegisterCProc
(
"/",
[] (const List& params) -> std::shared_ptr<Value>
{
Atom result;
double result = 0;
for (auto param : *params) {
Atom&& atom = std::any_cast<Atom>(param->value);
result.val /= atom.val;
result /= atom.val;
}
return std::make_shared<Value>(result);
return std::make_shared<Value>(Atom(result));
});
}
@ -177,9 +177,7 @@ namespace a8
}
double val = 0.0f;
if (sscanf(token.c_str(), "%lf", &val) == 1) {
Atom atom;
atom.val = val;
stack.at(depth)->push_back(std::make_shared<Value>(atom));
stack.at(depth)->push_back(std::make_shared<Value>(Atom(val)));
} else {
auto symbol = env->Find(token);
if (symbol) {
@ -188,9 +186,7 @@ namespace a8
}
stack.at(depth)->push_back(symbol);
} else {
Symbol symbol;
symbol.name = token;
stack.at(depth)->push_back(std::make_shared<Value>(symbol));
stack.at(depth)->push_back(std::make_shared<Value>(Symbol(token)));
}
}
}

View File

@ -27,13 +27,14 @@ namespace a8
{
double val = 0;
Atom() {};
Atom(double val) { this->val = val; }
};
struct Symbol
{
std::string name;
Symbol(const std::string& name) { this->name = name; }
};
struct Value
@ -57,6 +58,7 @@ namespace a8
public:
std::shared_ptr<Value> Find(const std::string& name);
void RegisterCProc(const std::string& name, CProc proc);
virtual ~Scope() {};
private:
std::map<std::string, std::shared_ptr<Value>> vars_;