From df13a1e451f81040e9859739e74d21e43a90bef0 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Mar 2023 19:22:11 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/a8/lisp.cc b/a8/lisp.cc index 8581093..850c659 100644 --- a/a8/lisp.cc +++ b/a8/lisp.cc @@ -99,9 +99,7 @@ namespace a8 std::shared_ptr Expr::Compile(const std::string& script, std::shared_ptr env) { - #if 0 - std::vector lists; - + std::vector stack; int pos = 0; int depth = -1; while (true) { @@ -111,8 +109,9 @@ namespace a8 break; } if (token == "(") { + List new_list = std::make_shared>>(); + stack.push_back(new_list); ++depth; - lists.push_back(List()); } else if (token == ")") { --depth; } else { @@ -123,14 +122,14 @@ namespace a8 if (sscanf(token.c_str(), "%lf", &val) == 1) { Atom atom; atom.val = val; - lists.at(depth).push_back(std::make_shared(atom)); + stack.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); + stack.at(depth)->push_back(symbol); } else { abort(); } @@ -140,11 +139,10 @@ namespace a8 if (depth != -1) { abort(); } - if (lists.empty()) { + if (stack.empty()) { abort(); } - //return std::make_shared(list); - #endif + return std::make_shared(stack[0]); } /* @@ -211,7 +209,7 @@ namespace a8 return cproc(exps); } else { CProc&& cproc = std::any_cast(Eval(leader, env)->value); - List exps; + List exps = std::make_shared>>(); for (auto itr = list->begin() + 1; itr != list->end(); ++itr) { exps->push_back(Eval(*itr, env)); }