This commit is contained in:
aozhiwei 2023-03-03 21:55:56 +08:00
parent df13a1e451
commit 8cf0d04a93
4 changed files with 51 additions and 3 deletions

View File

@ -3,6 +3,7 @@
#include <ctype.h> #include <ctype.h>
#include <a8/lisp.h> #include <a8/lisp.h>
#include <a8/mutable_xobject.h>
namespace a8 namespace a8
{ {
@ -34,6 +35,49 @@ namespace a8
value = cproc; value = cproc;
} }
a8::XObject Value::ToXObject()
{
switch (type) {
case ValueType::kAtom:
{
Atom&& atom = std::any_cast<Atom>(value);
a8::XValue v(atom.val);
return a8::XObject(v);
}
break;
case ValueType::kList:
{
auto arr = a8::MutableXObject::CreateArray();
List list = std::any_cast<List>(value);
for (auto ele : *list) {
a8::XObject xobj = ele->ToXObject();
arr->Push(xobj);
}
return *arr;
}
break;
case ValueType::kSymbol:
{
Symbol&& symbol = std::any_cast<Symbol>(value);
auto obj = a8::MutableXObject::CreateObject();
obj->SetVal("name", symbol.name);
return *obj;
}
break;
case ValueType::kCProc:
{
auto obj = a8::MutableXObject::CreateObject();
return *obj;
}
break;
default:
{
abort();
}
break;
}
}
std::shared_ptr<Value> Scope::Find(const std::string& name) std::shared_ptr<Value> Scope::Find(const std::string& name)
{ {
auto itr = vars_.find(name); auto itr = vars_.find(name);
@ -170,7 +214,10 @@ namespace a8
token = ")"; token = ")";
return 1; return 1;
} }
while (pos < script.size() && !isspace(script[pos])) { while (pos < script.size() &&
!isspace(script[pos]) &&
script[pos] != ')' &&
script[pos] != '(') {
token += script[pos]; token += script[pos];
++pos; ++pos;
} }

View File

@ -43,6 +43,7 @@ namespace a8
Value(Symbol symbol); Value(Symbol symbol);
Value(CProc cproc); Value(CProc cproc);
bool IsType(ValueType t) { return t == type; }; bool IsType(ValueType t) { return t == type; };
a8::XObject ToXObject();
}; };
class Scope class Scope

View File

@ -31,7 +31,7 @@ namespace a8
return *this; return *this;
} }
a8::MutableXObject& MutableXObject::Push(a8::MutableXObject& val) a8::MutableXObject& MutableXObject::Push(a8::XObject& val)
{ {
if (type_ != XOT_ARRAY) { if (type_ != XOT_ARRAY) {
abort(); abort();

View File

@ -14,7 +14,7 @@ namespace a8
static std::shared_ptr<a8::MutableXObject> CreateArray(); static std::shared_ptr<a8::MutableXObject> CreateArray();
a8::MutableXObject& Push(a8::XValue val); a8::MutableXObject& Push(a8::XValue val);
a8::MutableXObject& Push(a8::MutableXObject& val); a8::MutableXObject& Push(a8::XObject& val);
a8::MutableXObject& SetVal(const std::string& key, a8::XValue val); a8::MutableXObject& SetVal(const std::string& key, a8::XValue val);
a8::MutableXObject& SetVal(const std::string& key, a8::XObject& val); a8::MutableXObject& SetVal(const std::string& key, a8::XObject& val);
a8::MutableXObject& SetVal(const std::string& key, a8::MutableXObject& val); a8::MutableXObject& SetVal(const std::string& key, a8::MutableXObject& val);