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,
|
kSymbol = 3,
|
||||||
kCProc = 4,
|
kCProc = 4,
|
||||||
};
|
};
|
||||||
|
struct Value;
|
||||||
struct List
|
typedef std::vector<std::shared_ptr<Value>> List;
|
||||||
{
|
typedef std::function<const List(const List)> CProc;
|
||||||
std::vector<std::shared_ptr<Value>> elements;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Atom
|
struct Atom
|
||||||
{
|
{
|
||||||
@ -38,30 +36,37 @@ namespace a8
|
|||||||
struct Value
|
struct Value
|
||||||
{
|
{
|
||||||
ValueType type;
|
ValueType type;
|
||||||
std::shared_ptr<Symbol> symbol;
|
std::any value;
|
||||||
std::shared_ptr<Atom> atom;
|
|
||||||
std::shared_ptr<List> list;
|
Value(Atom atom);
|
||||||
}
|
Value(List list);
|
||||||
|
Value(Symbol symbol);
|
||||||
|
Value(CProc cproc);
|
||||||
|
};
|
||||||
|
|
||||||
class Scope
|
class Scope
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
std::shared_ptr<Value> Find(const std::string& name);
|
||||||
|
void RegisterCProc(const std::string& name, CProc proc);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, Value> vars_;
|
std::map<std::string, std::shared_ptr<Value>> vars_;
|
||||||
std::shared_ptr<scope> outer_;
|
std::shared_ptr<Scope> outer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Expr
|
class Expr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
bool Compile(const std::string& script);
|
bool Compile(const std::string& script, std::shared_ptr<Scope> scope);
|
||||||
std::shared_ptr<List> Eval();
|
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:
|
private:
|
||||||
|
|
||||||
std::shared_ptr<List> list_;
|
List list_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user