1
This commit is contained in:
parent
f0b4b7aa16
commit
592661bae5
71
a8/lisp.cc
71
a8/lisp.cc
@ -43,6 +43,58 @@ namespace a8
|
|||||||
vars_[name] = std::make_shared<Value>(proc);
|
vars_[name] = std::make_shared<Value>(proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GlobalScope::GlobalScope()
|
||||||
|
{
|
||||||
|
RegisterCProc
|
||||||
|
(
|
||||||
|
"+",
|
||||||
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
|
{
|
||||||
|
Atom result;
|
||||||
|
for (auto param : params) {
|
||||||
|
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||||
|
result.val += atom.val;
|
||||||
|
}
|
||||||
|
return std::make_shared<Value>(result);
|
||||||
|
});
|
||||||
|
RegisterCProc
|
||||||
|
(
|
||||||
|
"-",
|
||||||
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
|
{
|
||||||
|
Atom result;
|
||||||
|
for (auto param : params) {
|
||||||
|
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||||
|
result.val -= atom.val;
|
||||||
|
}
|
||||||
|
return std::make_shared<Value>(result);
|
||||||
|
});
|
||||||
|
RegisterCProc
|
||||||
|
(
|
||||||
|
"*",
|
||||||
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
|
{
|
||||||
|
Atom result;
|
||||||
|
for (auto param : params) {
|
||||||
|
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||||
|
result.val *= atom.val;
|
||||||
|
}
|
||||||
|
return std::make_shared<Value>(result);
|
||||||
|
});
|
||||||
|
RegisterCProc
|
||||||
|
(
|
||||||
|
"/",
|
||||||
|
[] (const List& params) -> std::shared_ptr<Value>
|
||||||
|
{
|
||||||
|
Atom result;
|
||||||
|
for (auto param : params) {
|
||||||
|
Atom&& atom = std::any_cast<Atom>(param->value);
|
||||||
|
result.val /= atom.val;
|
||||||
|
}
|
||||||
|
return std::make_shared<Value>(result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> scope)
|
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> scope)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -67,12 +119,21 @@ namespace a8
|
|||||||
{
|
{
|
||||||
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) {
|
||||||
abort();
|
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));
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
return cproc(exps);
|
||||||
}
|
}
|
||||||
CProc&& cproc = std::any_cast<CProc>(leader->value);
|
|
||||||
List exps = List(list.begin() + 1, list.end());
|
|
||||||
return cproc(exps);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
12
a8/lisp.h
12
a8/lisp.h
@ -21,7 +21,7 @@ namespace a8
|
|||||||
};
|
};
|
||||||
struct Value;
|
struct Value;
|
||||||
typedef std::vector<std::shared_ptr<Value>> List;
|
typedef 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
|
||||||
{
|
{
|
||||||
@ -56,12 +56,18 @@ namespace a8
|
|||||||
std::shared_ptr<Scope> outer_;
|
std::shared_ptr<Scope> outer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class GlobalScope : public Scope
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GlobalScope();
|
||||||
|
};
|
||||||
|
|
||||||
class Expr
|
class Expr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
std::shared_ptr<Value> Compile(const std::string& script, std::shared_ptr<Scope> scope);
|
static std::shared_ptr<Value> Compile(const std::string& script, std::shared_ptr<Scope> scope);
|
||||||
std::shared_ptr<Value> Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env);
|
static std::shared_ptr<Value> Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user