1
This commit is contained in:
parent
8a1cc4a75a
commit
24625f05d1
32
a8/lisp.cc
32
a8/lisp.cc
@ -106,48 +106,48 @@ namespace a8
|
|||||||
"+",
|
"+",
|
||||||
[] (const List& params) -> std::shared_ptr<Value>
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
{
|
{
|
||||||
Atom result;
|
double result = 0;
|
||||||
for (auto param : *params) {
|
for (auto param : *params) {
|
||||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
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
|
RegisterCProc
|
||||||
(
|
(
|
||||||
"-",
|
"-",
|
||||||
[] (const List& params) -> std::shared_ptr<Value>
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
{
|
{
|
||||||
Atom result;
|
double result = 0;
|
||||||
for (auto param : *params) {
|
for (auto param : *params) {
|
||||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
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
|
RegisterCProc
|
||||||
(
|
(
|
||||||
"*",
|
"*",
|
||||||
[] (const List& params) -> std::shared_ptr<Value>
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
{
|
{
|
||||||
Atom result;
|
double result = 0;
|
||||||
for (auto param : *params) {
|
for (auto param : *params) {
|
||||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
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
|
RegisterCProc
|
||||||
(
|
(
|
||||||
"/",
|
"/",
|
||||||
[] (const List& params) -> std::shared_ptr<Value>
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
{
|
{
|
||||||
Atom result;
|
double result = 0;
|
||||||
for (auto param : *params) {
|
for (auto param : *params) {
|
||||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
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;
|
double val = 0.0f;
|
||||||
if (sscanf(token.c_str(), "%lf", &val) == 1) {
|
if (sscanf(token.c_str(), "%lf", &val) == 1) {
|
||||||
Atom atom;
|
stack.at(depth)->push_back(std::make_shared<Value>(Atom(val)));
|
||||||
atom.val = val;
|
|
||||||
stack.at(depth)->push_back(std::make_shared<Value>(atom));
|
|
||||||
} else {
|
} else {
|
||||||
auto symbol = env->Find(token);
|
auto symbol = env->Find(token);
|
||||||
if (symbol) {
|
if (symbol) {
|
||||||
@ -188,9 +186,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
stack.at(depth)->push_back(symbol);
|
stack.at(depth)->push_back(symbol);
|
||||||
} else {
|
} else {
|
||||||
Symbol symbol;
|
stack.at(depth)->push_back(std::make_shared<Value>(Symbol(token)));
|
||||||
symbol.name = token;
|
|
||||||
stack.at(depth)->push_back(std::make_shared<Value>(symbol));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,14 @@ namespace a8
|
|||||||
{
|
{
|
||||||
double val = 0;
|
double val = 0;
|
||||||
|
|
||||||
Atom() {};
|
|
||||||
Atom(double val) { this->val = val; }
|
Atom(double val) { this->val = val; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Symbol
|
struct Symbol
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
||||||
|
Symbol(const std::string& name) { this->name = name; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Value
|
struct Value
|
||||||
@ -57,6 +58,7 @@ namespace a8
|
|||||||
public:
|
public:
|
||||||
std::shared_ptr<Value> Find(const std::string& name);
|
std::shared_ptr<Value> Find(const std::string& name);
|
||||||
void RegisterCProc(const std::string& name, CProc proc);
|
void RegisterCProc(const std::string& name, CProc proc);
|
||||||
|
virtual ~Scope() {};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::shared_ptr<Value>> vars_;
|
std::map<std::string, std::shared_ptr<Value>> vars_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user