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>
|
[] (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);
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user