From 4f3e7d31b979a0cf10dad609832392bd9469d2ea Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 3 Mar 2023 18:02:22 +0800 Subject: [PATCH] 1 --- a8/lisp.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++++- a8/lisp.h | 3 ++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/a8/lisp.cc b/a8/lisp.cc index 5059a23..f879e1e 100644 --- a/a8/lisp.cc +++ b/a8/lisp.cc @@ -1,5 +1,7 @@ #include +#include + #include namespace a8 @@ -95,9 +97,52 @@ namespace a8 }); } - std::shared_ptr Expr::Compile(const std::string& script, std::shared_ptr scope) + std::shared_ptr Expr::Compile(const std::string& script, std::shared_ptr env) { + int pos = 0; + while (true) { + std::string token; + int n = GetToken(script, pos, token); + if (n != 1) { + break; + } + } + } + /* + 1: token + -1: end + */ + int Expr::GetToken(const std::string& script, int& pos, std::string& token) + { + if (pos >= script.size()) { + return -1; + } + while (pos < script.size() && isspace(script[pos])) { + ++pos; + } + if (pos >= script.size()) { + return -1; + } + if (script[pos] == '(') { + ++pos; + token = "("; + return 1; + } + if (script[pos] == ')') { + ++pos; + token = ")"; + return 1; + } + while (pos < script.size() && !isspace(script[pos])) { + token += script[pos]; + ++pos; + } + if (token.empty()) { + return -1; + } else { + return 1; + } } std::shared_ptr Expr::Eval(std::shared_ptr x, std::shared_ptr env) diff --git a/a8/lisp.h b/a8/lisp.h index c462e7e..bb2b7bf 100644 --- a/a8/lisp.h +++ b/a8/lisp.h @@ -66,10 +66,11 @@ namespace a8 { public: - static std::shared_ptr Compile(const std::string& script, std::shared_ptr scope); + static std::shared_ptr Compile(const std::string& script, std::shared_ptr env); static std::shared_ptr Eval(std::shared_ptr x, std::shared_ptr env); private: + static int GetToken(const std::string& script, int& pos, std::string& token); }; }