From 592661bae5547a0c1675816595b803f902d83041 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Mar 2023 13:49:50 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++---- a8/lisp.h | 12 ++++++--- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/a8/lisp.cc b/a8/lisp.cc index 055fe13..5059a23 100644 --- a/a8/lisp.cc +++ b/a8/lisp.cc @@ -43,6 +43,58 @@ namespace a8 vars_[name] = std::make_shared(proc); } + GlobalScope::GlobalScope() + { + RegisterCProc + ( + "+", + [] (const List& params) -> std::shared_ptr + { + Atom result; + for (auto param : params) { + Atom&& atom = std::any_cast(param->value); + result.val += atom.val; + } + return std::make_shared(result); + }); + RegisterCProc + ( + "-", + [] (const List& params) -> std::shared_ptr + { + Atom result; + for (auto param : params) { + Atom&& atom = std::any_cast(param->value); + result.val -= atom.val; + } + return std::make_shared(result); + }); + RegisterCProc + ( + "*", + [] (const List& params) -> std::shared_ptr + { + Atom result; + for (auto param : params) { + Atom&& atom = std::any_cast(param->value); + result.val *= atom.val; + } + return std::make_shared(result); + }); + RegisterCProc + ( + "/", + [] (const List& params) -> std::shared_ptr + { + Atom result; + for (auto param : params) { + Atom&& atom = std::any_cast(param->value); + result.val /= atom.val; + } + return std::make_shared(result); + }); + } + std::shared_ptr Expr::Compile(const std::string& script, std::shared_ptr scope) { @@ -67,12 +119,21 @@ namespace a8 { List&& list = std::any_cast(x->value); auto leader = list.at(0); - if (leader->type != ValueType::kCProc) { - abort(); + if (leader->type == ValueType::kCProc) { + CProc&& cproc = std::any_cast(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(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(leader->value); - List exps = List(list.begin() + 1, list.end()); - return cproc(exps); } break; default: diff --git a/a8/lisp.h b/a8/lisp.h index 16a8f08..c462e7e 100644 --- a/a8/lisp.h +++ b/a8/lisp.h @@ -21,7 +21,7 @@ namespace a8 }; struct Value; typedef std::vector> List; - typedef std::function(const List)> CProc; + typedef std::function(const List&)> CProc; struct Atom { @@ -56,12 +56,18 @@ namespace a8 std::shared_ptr outer_; }; + class GlobalScope : public Scope + { + public: + GlobalScope(); + }; + class Expr { public: - std::shared_ptr Compile(const std::string& script, std::shared_ptr scope); - std::shared_ptr Eval(std::shared_ptr x, std::shared_ptr env); + static std::shared_ptr Compile(const std::string& script, std::shared_ptr scope); + static std::shared_ptr Eval(std::shared_ptr x, std::shared_ptr env); private: };