From 1037f6b4a7b25073615b03c001dba52c2c952d32 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Mar 2023 19:07:40 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++---------- a8/lisp.h | 2 +- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/a8/lisp.cc b/a8/lisp.cc index f879e1e..d767580 100644 --- a/a8/lisp.cc +++ b/a8/lisp.cc @@ -53,7 +53,7 @@ namespace a8 [] (const List& params) -> std::shared_ptr { Atom result; - for (auto param : params) { + for (auto param : *params) { Atom&& atom = std::any_cast(param->value); result.val += atom.val; } @@ -65,7 +65,7 @@ namespace a8 [] (const List& params) -> std::shared_ptr { Atom result; - for (auto param : params) { + for (auto param : *params) { Atom&& atom = std::any_cast(param->value); result.val -= atom.val; } @@ -77,7 +77,7 @@ namespace a8 [] (const List& params) -> std::shared_ptr { Atom result; - for (auto param : params) { + for (auto param : *params) { Atom&& atom = std::any_cast(param->value); result.val *= atom.val; } @@ -89,7 +89,7 @@ namespace a8 [] (const List& params) -> std::shared_ptr { Atom result; - for (auto param : params) { + for (auto param : *params) { Atom&& atom = std::any_cast(param->value); result.val /= atom.val; } @@ -99,14 +99,52 @@ namespace a8 std::shared_ptr Expr::Compile(const std::string& script, std::shared_ptr env) { + #if 0 + std::vector lists; + int pos = 0; + int depth = -1; while (true) { std::string token; int n = GetToken(script, pos, token); if (n != 1) { break; } + if (token == "(") { + ++depth; + lists.push_back(List()); + } else if (token == ")") { + --depth; + } else { + if (depth < 0) { + abort(); + } + double val = 0.0f; + if (sscanf(token.c_str(), "%lf", &val) == 1) { + Atom atom; + atom.val = val; + lists.at(depth).push_back(std::make_shared(atom)); + } else { + auto symbol = env->Find(token); + if (symbol) { + if (!symbol->IsType(ValueType::kCProc)) { + abort(); + } + lists.at(depth).push_back(symbol); + } else { + abort(); + } + } + } } + if (depth != -1) { + abort(); + } + if (lists.empty()) { + abort(); + } + //return std::make_shared(list); + #endif } /* @@ -163,19 +201,19 @@ namespace a8 case ValueType::kList: { List&& list = std::any_cast(x->value); - auto leader = list.at(0); + auto leader = list->at(0); 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)); + List exps = std::make_shared>>(); + 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)); + for (auto itr = list->begin() + 1; itr != list->end(); ++itr) { + exps->push_back(Eval(*itr, env)); } return cproc(exps); } diff --git a/a8/lisp.h b/a8/lisp.h index bb2b7bf..6704386 100644 --- a/a8/lisp.h +++ b/a8/lisp.h @@ -20,7 +20,7 @@ namespace a8 kCProc = 4, }; struct Value; - typedef std::vector> List; + typedef std::shared_ptr>> List; typedef std::function(const List&)> CProc; struct Atom