From 24625f05d1636709680b03de74b60d2b4736d7d5 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 4 Mar 2023 12:00:41 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 32 ++++++++++++++------------------ a8/lisp.h | 4 +++- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/a8/lisp.cc b/a8/lisp.cc index 88d9661..7c1a6ae 100644 --- a/a8/lisp.cc +++ b/a8/lisp.cc @@ -106,48 +106,48 @@ namespace a8 "+", [] (const List& params) -> std::shared_ptr { - Atom result; + double result = 0; for (auto param : *params) { Atom&& atom = std::any_cast(param->value); - result.val += atom.val; + result += atom.val; } - return std::make_shared(result); + return std::make_shared(Atom(result)); }); RegisterCProc ( "-", [] (const List& params) -> std::shared_ptr { - Atom result; + double result = 0; for (auto param : *params) { Atom&& atom = std::any_cast(param->value); - result.val -= atom.val; + result -= atom.val; } - return std::make_shared(result); + return std::make_shared(Atom(result)); }); RegisterCProc ( "*", [] (const List& params) -> std::shared_ptr { - Atom result; + double result = 0; for (auto param : *params) { Atom&& atom = std::any_cast(param->value); - result.val *= atom.val; + result *= atom.val; } - return std::make_shared(result); + return std::make_shared(Atom(result)); }); RegisterCProc ( "/", [] (const List& params) -> std::shared_ptr { - Atom result; + double result = 0; for (auto param : *params) { Atom&& atom = std::any_cast(param->value); - result.val /= atom.val; + result /= atom.val; } - return std::make_shared(result); + return std::make_shared(Atom(result)); }); } @@ -177,9 +177,7 @@ namespace a8 } double val = 0.0f; if (sscanf(token.c_str(), "%lf", &val) == 1) { - Atom atom; - atom.val = val; - stack.at(depth)->push_back(std::make_shared(atom)); + stack.at(depth)->push_back(std::make_shared(Atom(val))); } else { auto symbol = env->Find(token); if (symbol) { @@ -188,9 +186,7 @@ namespace a8 } stack.at(depth)->push_back(symbol); } else { - Symbol symbol; - symbol.name = token; - stack.at(depth)->push_back(std::make_shared(symbol)); + stack.at(depth)->push_back(std::make_shared(Symbol(token))); } } } diff --git a/a8/lisp.h b/a8/lisp.h index 67805d8..82cc5f9 100644 --- a/a8/lisp.h +++ b/a8/lisp.h @@ -27,13 +27,14 @@ namespace a8 { double val = 0; - Atom() {}; Atom(double val) { this->val = val; } }; struct Symbol { std::string name; + + Symbol(const std::string& name) { this->name = name; } }; struct Value @@ -57,6 +58,7 @@ namespace a8 public: std::shared_ptr Find(const std::string& name); void RegisterCProc(const std::string& name, CProc proc); + virtual ~Scope() {}; private: std::map> vars_;