1
This commit is contained in:
parent
592661bae5
commit
4f3e7d31b9
47
a8/lisp.cc
47
a8/lisp.cc
@ -1,5 +1,7 @@
|
|||||||
#include <a8/a8.h>
|
#include <a8/a8.h>
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include <a8/lisp.h>
|
#include <a8/lisp.h>
|
||||||
|
|
||||||
namespace a8
|
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)
|
std::shared_ptr<Value> Expr::Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env)
|
||||||
|
@ -66,10 +66,11 @@ namespace a8
|
|||||||
{
|
{
|
||||||
public:
|
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);
|
static std::shared_ptr<Value> Eval(std::shared_ptr<Value> x, std::shared_ptr<Scope> env);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static int GetToken(const std::string& script, int& pos, std::string& token);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user