This commit is contained in:
aozhiwei 2023-03-03 19:07:40 +08:00
parent 4f3e7d31b9
commit 1037f6b4a7
2 changed files with 49 additions and 11 deletions

View File

@ -53,7 +53,7 @@ namespace a8
[] (const List& params) -> std::shared_ptr<Value> [] (const List& params) -> std::shared_ptr<Value>
{ {
Atom result; Atom result;
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.val += atom.val;
} }
@ -65,7 +65,7 @@ namespace a8
[] (const List& params) -> std::shared_ptr<Value> [] (const List& params) -> std::shared_ptr<Value>
{ {
Atom result; Atom result;
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.val -= atom.val;
} }
@ -77,7 +77,7 @@ namespace a8
[] (const List& params) -> std::shared_ptr<Value> [] (const List& params) -> std::shared_ptr<Value>
{ {
Atom result; Atom result;
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.val *= atom.val;
} }
@ -89,7 +89,7 @@ namespace a8
[] (const List& params) -> std::shared_ptr<Value> [] (const List& params) -> std::shared_ptr<Value>
{ {
Atom result; Atom result;
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.val /= atom.val;
} }
@ -99,14 +99,52 @@ namespace a8
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> env) 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 pos = 0;
int depth = -1;
while (true) { while (true) {
std::string token; std::string token;
int n = GetToken(script, pos, token); int n = GetToken(script, pos, token);
if (n != 1) { if (n != 1) {
break; 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: case ValueType::kList:
{ {
List&& list = std::any_cast<List>(x->value); List&& list = std::any_cast<List>(x->value);
auto leader = list.at(0); auto leader = list->at(0);
if (leader->type == ValueType::kCProc) { if (leader->type == ValueType::kCProc) {
CProc&& cproc = std::any_cast<CProc>(leader->value); CProc&& cproc = std::any_cast<CProc>(leader->value);
List exps; List exps = std::make_shared<std::vector<std::shared_ptr<Value>>>();
for (auto itr = list.begin() + 1; itr != list.end(); ++itr) { for (auto itr = list->begin() + 1; itr != list->end(); ++itr) {
exps.push_back(Eval(*itr, env)); exps->push_back(Eval(*itr, env));
} }
return cproc(exps); return cproc(exps);
} else { } else {
CProc&& cproc = std::any_cast<CProc>(Eval(leader, env)->value); CProc&& cproc = std::any_cast<CProc>(Eval(leader, env)->value);
List exps; List exps;
for (auto itr = list.begin() + 1; itr != list.end(); ++itr) { for (auto itr = list->begin() + 1; itr != list->end(); ++itr) {
exps.push_back(Eval(*itr, env)); exps->push_back(Eval(*itr, env));
} }
return cproc(exps); return cproc(exps);
} }

View File

@ -20,7 +20,7 @@ namespace a8
kCProc = 4, kCProc = 4,
}; };
struct Value; struct Value;
typedef std::vector<std::shared_ptr<Value>> List; typedef std::shared_ptr<std::vector<std::shared_ptr<Value>>> List;
typedef std::function<std::shared_ptr<Value>(const List&)> CProc; typedef std::function<std::shared_ptr<Value>(const List&)> CProc;
struct Atom struct Atom