1
This commit is contained in:
parent
7bd1467401
commit
0707a2b229
58
a8/lisp.cc
Normal file
58
a8/lisp.cc
Normal file
@ -0,0 +1,58 @@
|
||||
#include <a8/a8.h>
|
||||
|
||||
#include <a8/lisp.h>
|
||||
|
||||
namespace a8
|
||||
{
|
||||
|
||||
namespace lisp
|
||||
{
|
||||
|
||||
Value::Value(Atom atom)
|
||||
{
|
||||
type = ValueType::kAtom;
|
||||
value = atom;
|
||||
}
|
||||
|
||||
Value::Value(List list)
|
||||
{
|
||||
type = ValueType::kList;
|
||||
value = list;
|
||||
}
|
||||
|
||||
Value::Value(Symbol symbol)
|
||||
{
|
||||
type = ValueType::kSymbol;
|
||||
value = symbol;
|
||||
}
|
||||
|
||||
Value::Value(CProc cproc)
|
||||
{
|
||||
type = ValueType::kCProc;
|
||||
value = cproc;
|
||||
}
|
||||
|
||||
std::shared_ptr<Value> Scope::Find(const std::string& name)
|
||||
{
|
||||
auto itr = vars_.find(name);
|
||||
return itr != vars_.end() ? itr->second : nullptr;
|
||||
}
|
||||
|
||||
void Scope::RegisterCProc(const std::string& name, CProc proc)
|
||||
{
|
||||
vars_[name] = std::make_shared<Value>(proc);
|
||||
}
|
||||
|
||||
bool 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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
35
a8/lisp.h
35
a8/lisp.h
@ -19,11 +19,9 @@ namespace a8
|
||||
kSymbol = 3,
|
||||
kCProc = 4,
|
||||
};
|
||||
|
||||
struct List
|
||||
{
|
||||
std::vector<std::shared_ptr<Value>> elements;
|
||||
};
|
||||
struct Value;
|
||||
typedef std::vector<std::shared_ptr<Value>> List;
|
||||
typedef std::function<const List(const List)> CProc;
|
||||
|
||||
struct Atom
|
||||
{
|
||||
@ -38,30 +36,37 @@ namespace a8
|
||||
struct Value
|
||||
{
|
||||
ValueType type;
|
||||
std::shared_ptr<Symbol> symbol;
|
||||
std::shared_ptr<Atom> atom;
|
||||
std::shared_ptr<List> list;
|
||||
}
|
||||
std::any value;
|
||||
|
||||
Value(Atom atom);
|
||||
Value(List list);
|
||||
Value(Symbol symbol);
|
||||
Value(CProc cproc);
|
||||
};
|
||||
|
||||
class Scope
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<Value> Find(const std::string& name);
|
||||
void RegisterCProc(const std::string& name, CProc proc);
|
||||
|
||||
private:
|
||||
std::map<std::string, Value> vars_;
|
||||
std::shared_ptr<scope> outer_;
|
||||
std::map<std::string, std::shared_ptr<Value>> vars_;
|
||||
std::shared_ptr<Scope> outer_;
|
||||
};
|
||||
|
||||
class Expr
|
||||
{
|
||||
public:
|
||||
|
||||
bool Compile(const std::string& script);
|
||||
std::shared_ptr<List> Eval();
|
||||
bool 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<List> GetExprList() { return list_; };
|
||||
List GetExprList() { return list_; };
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<List> list_;
|
||||
List list_;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user