1
This commit is contained in:
parent
0707a2b229
commit
f0b4b7aa16
38
a8/lisp.cc
38
a8/lisp.cc
@ -43,14 +43,46 @@ namespace a8
|
|||||||
vars_[name] = std::make_shared<Value>(proc);
|
vars_[name] = std::make_shared<Value>(proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<const Value>> Expr::Eval(std::shared_ptr<Scope> scope)
|
std::shared_ptr<Value> Expr::Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env)
|
||||||
{
|
{
|
||||||
|
while (true) {
|
||||||
|
switch (x->type) {
|
||||||
|
case ValueType::kAtom:
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ValueType::kSymbol:
|
||||||
|
{
|
||||||
|
Symbol&& symbol = std::any_cast<Symbol>(x->value);
|
||||||
|
return env->Find(symbol.name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ValueType::kList:
|
||||||
|
{
|
||||||
|
List&& list = std::any_cast<List>(x->value);
|
||||||
|
auto leader = list.at(0);
|
||||||
|
if (leader->type != ValueType::kCProc) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
CProc&& cproc = std::any_cast<CProc>(leader->value);
|
||||||
|
List exps = List(list.begin() + 1, list.end());
|
||||||
|
return cproc(exps);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
abort();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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<const List(const List)> CProc;
|
typedef std::function<std::shared_ptr<Value>(const List)> CProc;
|
||||||
|
|
||||||
struct Atom
|
struct Atom
|
||||||
{
|
{
|
||||||
@ -42,6 +42,7 @@ namespace a8
|
|||||||
Value(List list);
|
Value(List list);
|
||||||
Value(Symbol symbol);
|
Value(Symbol symbol);
|
||||||
Value(CProc cproc);
|
Value(CProc cproc);
|
||||||
|
bool IsType(ValueType t) { return t == type; };
|
||||||
};
|
};
|
||||||
|
|
||||||
class Scope
|
class Scope
|
||||||
@ -59,15 +60,10 @@ namespace a8
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
bool Compile(const std::string& script, std::shared_ptr<Scope> scope);
|
std::shared_ptr<Value> Compile(const std::string& script, std::shared_ptr<Scope> scope);
|
||||||
std::vector<std::shared_ptr<const Value>> Eval(std::shared_ptr<Scope> scope);
|
std::shared_ptr<Value> Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env);
|
||||||
|
|
||||||
List GetExprList() { return list_; };
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
List list_;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user