1
This commit is contained in:
parent
4f3e7d31b9
commit
1037f6b4a7
58
a8/lisp.cc
58
a8/lisp.cc
@ -53,7 +53,7 @@ namespace a8
|
||||
[] (const List& params) -> std::shared_ptr<Value>
|
||||
{
|
||||
Atom result;
|
||||
for (auto param : params) {
|
||||
for (auto param : *params) {
|
||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||
result.val += atom.val;
|
||||
}
|
||||
@ -65,7 +65,7 @@ namespace a8
|
||||
[] (const List& params) -> std::shared_ptr<Value>
|
||||
{
|
||||
Atom result;
|
||||
for (auto param : params) {
|
||||
for (auto param : *params) {
|
||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||
result.val -= atom.val;
|
||||
}
|
||||
@ -77,7 +77,7 @@ namespace a8
|
||||
[] (const List& params) -> std::shared_ptr<Value>
|
||||
{
|
||||
Atom result;
|
||||
for (auto param : params) {
|
||||
for (auto param : *params) {
|
||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||
result.val *= atom.val;
|
||||
}
|
||||
@ -89,7 +89,7 @@ namespace a8
|
||||
[] (const List& params) -> std::shared_ptr<Value>
|
||||
{
|
||||
Atom result;
|
||||
for (auto param : params) {
|
||||
for (auto param : *params) {
|
||||
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||
result.val /= atom.val;
|
||||
}
|
||||
@ -99,14 +99,52 @@ namespace a8
|
||||
|
||||
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> env)
|
||||
{
|
||||
#if 0
|
||||
std::vector<List> lists;
|
||||
|
||||
int pos = 0;
|
||||
int depth = -1;
|
||||
while (true) {
|
||||
std::string token;
|
||||
int n = GetToken(script, pos, token);
|
||||
if (n != 1) {
|
||||
break;
|
||||
}
|
||||
if (token == "(") {
|
||||
++depth;
|
||||
lists.push_back(List());
|
||||
} else if (token == ")") {
|
||||
--depth;
|
||||
} else {
|
||||
if (depth < 0) {
|
||||
abort();
|
||||
}
|
||||
double val = 0.0f;
|
||||
if (sscanf(token.c_str(), "%lf", &val) == 1) {
|
||||
Atom atom;
|
||||
atom.val = val;
|
||||
lists.at(depth).push_back(std::make_shared<Value>(atom));
|
||||
} else {
|
||||
auto symbol = env->Find(token);
|
||||
if (symbol) {
|
||||
if (!symbol->IsType(ValueType::kCProc)) {
|
||||
abort();
|
||||
}
|
||||
lists.at(depth).push_back(symbol);
|
||||
} else {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (depth != -1) {
|
||||
abort();
|
||||
}
|
||||
if (lists.empty()) {
|
||||
abort();
|
||||
}
|
||||
//return std::make_shared<Value>(list);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
@ -163,19 +201,19 @@ namespace a8
|
||||
case ValueType::kList:
|
||||
{
|
||||
List&& list = std::any_cast<List>(x->value);
|
||||
auto leader = list.at(0);
|
||||
auto leader = list->at(0);
|
||||
if (leader->type == ValueType::kCProc) {
|
||||
CProc&& cproc = std::any_cast<CProc>(leader->value);
|
||||
List exps;
|
||||
for (auto itr = list.begin() + 1; itr != list.end(); ++itr) {
|
||||
exps.push_back(Eval(*itr, env));
|
||||
List exps = std::make_shared<std::vector<std::shared_ptr<Value>>>();
|
||||
for (auto itr = list->begin() + 1; itr != list->end(); ++itr) {
|
||||
exps->push_back(Eval(*itr, env));
|
||||
}
|
||||
return cproc(exps);
|
||||
} else {
|
||||
CProc&& cproc = std::any_cast<CProc>(Eval(leader, env)->value);
|
||||
List exps;
|
||||
for (auto itr = list.begin() + 1; itr != list.end(); ++itr) {
|
||||
exps.push_back(Eval(*itr, env));
|
||||
for (auto itr = list->begin() + 1; itr != list->end(); ++itr) {
|
||||
exps->push_back(Eval(*itr, env));
|
||||
}
|
||||
return cproc(exps);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user