This commit is contained in:
aozhiwei 2023-03-03 18:02:22 +08:00
parent 592661bae5
commit 4f3e7d31b9
2 changed files with 48 additions and 2 deletions

View File

@ -1,5 +1,7 @@
#include <a8/a8.h>
#include <ctype.h>
#include <a8/lisp.h>
namespace a8
@ -95,9 +97,52 @@ namespace a8
});
}
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> scope)
std::shared_ptr<Value> Expr::Compile(const std::string& script, std::shared_ptr<Scope> 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<Value> Expr::Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env)

View File

@ -66,10 +66,11 @@ namespace a8
{
public:
static std::shared_ptr<Value> Compile(const std::string& script, std::shared_ptr<Scope> scope);
static std::shared_ptr<Value> Compile(const std::string& script, std::shared_ptr<Scope> env);
static std::shared_ptr<Value> Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env);
private:
static int GetToken(const std::string& script, int& pos, std::string& token);
};
}