From 0707a2b229d1bf9335a00025931962406c9e1cb6 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Mar 2023 11:22:32 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ a8/lisp.h | 35 ++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 a8/lisp.cc diff --git a/a8/lisp.cc b/a8/lisp.cc new file mode 100644 index 0000000..1f5cbc2 --- /dev/null +++ b/a8/lisp.cc @@ -0,0 +1,58 @@ +#include + +#include + +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 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(proc); + } + + bool Expr::Compile(const std::string& script, std::shared_ptr scope) + { + + } + + std::vector> Expr::Eval(std::shared_ptr scope) + { + + } + + } + +} diff --git a/a8/lisp.h b/a8/lisp.h index 2f6283c..929b094 100644 --- a/a8/lisp.h +++ b/a8/lisp.h @@ -19,11 +19,9 @@ namespace a8 kSymbol = 3, kCProc = 4, }; - - struct List - { - std::vector> elements; - }; + struct Value; + typedef std::vector> List; + typedef std::function CProc; struct Atom { @@ -38,30 +36,37 @@ namespace a8 struct Value { ValueType type; - std::shared_ptr symbol; - std::shared_ptr atom; - std::shared_ptr list; - } + std::any value; + + Value(Atom atom); + Value(List list); + Value(Symbol symbol); + Value(CProc cproc); + }; class Scope { + public: + std::shared_ptr Find(const std::string& name); + void RegisterCProc(const std::string& name, CProc proc); + private: - std::map vars_; - std::shared_ptr outer_; + std::map> vars_; + std::shared_ptr outer_; }; class Expr { public: - bool Compile(const std::string& script); - std::shared_ptr Eval(); + bool Compile(const std::string& script, std::shared_ptr scope); + std::vector> Eval(std::shared_ptr scope); - std::shared_ptr GetExprList() { return list_; }; + List GetExprList() { return list_; }; private: - std::shared_ptr list_; + List list_; };