mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Refactoring.
This commit is contained in:
parent
ab3ade86d6
commit
8f74439798
@ -82,7 +82,7 @@ private:
|
||||
}
|
||||
|
||||
static Value eval_function(const Ast& ast, shared_ptr<Environment> env) {
|
||||
std::vector<Value::FunctionValue::Parameter> params;
|
||||
std::vector<FunctionValue::Parameter> params;
|
||||
for (auto node: ast.nodes[0]->nodes) {
|
||||
auto mut = node->nodes[0]->token == "mut";
|
||||
const auto& name = node->nodes[1]->token;
|
||||
@ -91,7 +91,7 @@ private:
|
||||
|
||||
auto body = ast.nodes[1];
|
||||
|
||||
auto f = Value::FunctionValue(
|
||||
auto f = FunctionValue(
|
||||
params,
|
||||
[=](shared_ptr<Environment> callEnv) {
|
||||
callEnv->append_outer(env);
|
||||
@ -136,8 +136,8 @@ private:
|
||||
// Array reference
|
||||
const auto& arr = val.to_array();
|
||||
auto idx = eval(n, env).to_long();
|
||||
if (0 <= idx && idx < static_cast<long>(arr.data->values.size())) {
|
||||
val = arr.data->values.at(idx);
|
||||
if (0 <= idx && idx < static_cast<long>(arr.values->size())) {
|
||||
val = arr.values->at(idx);
|
||||
}
|
||||
} else if (n.original_tag == AstTag::Dot) {
|
||||
// Property
|
||||
@ -147,7 +147,7 @@ private:
|
||||
if (prop.get_type() == Value::Function) {
|
||||
const auto& pf = prop.to_function();
|
||||
|
||||
auto f = Value::FunctionValue(
|
||||
auto f = FunctionValue(
|
||||
pf.data->params,
|
||||
[=](shared_ptr<Environment> callEnv) {
|
||||
callEnv->initialize("this", val, false);
|
||||
@ -280,25 +280,25 @@ private:
|
||||
};
|
||||
|
||||
static Value eval_object(const Ast& ast, shared_ptr<Environment> env) {
|
||||
Value::ObjectValue obj;
|
||||
ObjectValue obj;
|
||||
|
||||
for (auto i = 0u; i < ast.nodes.size(); i++) {
|
||||
const auto& prop = *ast.nodes[i];
|
||||
const auto& name = prop.nodes[0]->token;
|
||||
auto val = eval(*prop.nodes[1], env);
|
||||
obj.data->props.emplace(name, val);
|
||||
obj.properties->emplace(name, val);
|
||||
}
|
||||
|
||||
return Value(std::move(obj));
|
||||
}
|
||||
|
||||
static Value eval_array(const Ast& ast, shared_ptr<Environment> env) {
|
||||
Value::ArrayValue arr;
|
||||
ArrayValue arr;
|
||||
|
||||
for (auto i = 0u; i < ast.nodes.size(); i++) {
|
||||
auto expr = ast.nodes[i];
|
||||
auto val = eval(*expr, env);
|
||||
arr.data->values.push_back(val);
|
||||
arr.values->push_back(val);
|
||||
}
|
||||
|
||||
return Value(std::move(arr));
|
||||
@ -322,46 +322,6 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
std::map<std::string, Value> Value::ObjectValue::prototypes = {
|
||||
{
|
||||
"size",
|
||||
Value(FunctionValue(
|
||||
{},
|
||||
[](shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
long n = val.to_object().data->props.size();
|
||||
return Value(n);
|
||||
}
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
std::map<std::string, Value> Value::ArrayValue::prototypes = {
|
||||
{
|
||||
"size",
|
||||
Value(FunctionValue(
|
||||
{},
|
||||
[](shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
long n = val.to_array().data->values.size();
|
||||
return Value(n);
|
||||
}
|
||||
))
|
||||
},
|
||||
{
|
||||
"push",
|
||||
Value(FunctionValue {
|
||||
{ {"arg", false} },
|
||||
[](shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
const auto& arg = callEnv->get("arg");
|
||||
val.to_array().data->values.push_back(arg);
|
||||
return Value();
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
bool run(
|
||||
const string& path,
|
||||
shared_ptr<Environment> env,
|
||||
|
@ -2,12 +2,9 @@
|
||||
#include <string>
|
||||
#include <peglib.h>
|
||||
|
||||
struct Value;
|
||||
struct Environment;
|
||||
|
||||
struct Value
|
||||
{
|
||||
enum Type { Undefined, Bool, Long, String, Object, Array, Function };
|
||||
|
||||
struct FunctionValue {
|
||||
struct Parameter {
|
||||
std::string name;
|
||||
@ -32,44 +29,24 @@ struct Value
|
||||
};
|
||||
|
||||
struct ObjectValue {
|
||||
bool has_property(const std::string& name) const {
|
||||
if (data->props.find(name) == data->props.end()) {
|
||||
return prototypes.find(name) != prototypes.end();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool has_property(const std::string& name) const;
|
||||
Value get_property(const std::string& name) const;
|
||||
virtual std::map<std::string, Value>& builtins();
|
||||
|
||||
Value get_property(const std::string& name) const {
|
||||
if (data->props.find(name) == data->props.end()) {
|
||||
return prototypes.at(name);
|
||||
}
|
||||
return data->props.at(name);
|
||||
}
|
||||
|
||||
struct Data {
|
||||
std::map<std::string, Value> props;
|
||||
};
|
||||
std::shared_ptr<Data> data = std::make_shared<Data>();
|
||||
|
||||
static std::map<std::string, Value> prototypes;
|
||||
std::shared_ptr<std::map<std::string, Value>> properties =
|
||||
std::make_shared<std::map<std::string, Value>>();
|
||||
};
|
||||
|
||||
struct ArrayValue {
|
||||
Value get_property(const std::string& name) const {
|
||||
if (data->props.find(name) == data->props.end()) {
|
||||
return prototypes.at(name);
|
||||
}
|
||||
return data->props.at(name);
|
||||
}
|
||||
struct ArrayValue : public ObjectValue {
|
||||
std::map<std::string, Value>& builtins() override;
|
||||
|
||||
struct Data {
|
||||
std::map<std::string, Value> props;
|
||||
std::vector<Value> values;
|
||||
std::shared_ptr<std::vector<Value>> values =
|
||||
std::make_shared<std::vector<Value>>();
|
||||
};
|
||||
std::shared_ptr<Data> data = std::make_shared<Data>();
|
||||
|
||||
static std::map<std::string, Value> prototypes;
|
||||
};
|
||||
struct Value
|
||||
{
|
||||
enum Type { Undefined, Bool, Long, String, Object, Array, Function };
|
||||
|
||||
Value() : type(Undefined) {
|
||||
//std::cout << "Val::def ctor: " << std::endl;
|
||||
@ -163,11 +140,11 @@ struct Value
|
||||
}
|
||||
|
||||
std::string str_object() const {
|
||||
const auto& props = to_object().data->props;
|
||||
const auto& properties = *to_object().properties;
|
||||
std::string s = "{";
|
||||
auto it = props.begin();
|
||||
for (; it != props.end(); ++it) {
|
||||
if (it != props.begin()) {
|
||||
auto it = properties.begin();
|
||||
for (; it != properties.end(); ++it) {
|
||||
if (it != properties.begin()) {
|
||||
s += ", ";
|
||||
}
|
||||
s += '"' + it->first + '"';
|
||||
@ -179,7 +156,7 @@ struct Value
|
||||
}
|
||||
|
||||
std::string str_array() const {
|
||||
const auto& values = to_array().data->values;
|
||||
const auto& values = *to_array().values;
|
||||
std::string s = "[";
|
||||
for (auto i = 0u; i < values.size(); i++) {
|
||||
if (i != 0) {
|
||||
@ -299,7 +276,7 @@ struct Environment
|
||||
{
|
||||
Environment() = default;
|
||||
|
||||
void set_object(const Value::ObjectValue& obj) {
|
||||
void set_object(const ObjectValue& obj) {
|
||||
obj_ = obj;
|
||||
}
|
||||
|
||||
@ -371,12 +348,74 @@ private:
|
||||
|
||||
std::shared_ptr<Environment> outer_;
|
||||
std::map<std::string, Symbol> dic_;
|
||||
Value::ObjectValue obj_;
|
||||
ObjectValue obj_;
|
||||
};
|
||||
|
||||
inline bool ObjectValue::has_property(const std::string& name) const {
|
||||
if (properties->find(name) == properties->end()) {
|
||||
const auto& proto = const_cast<ObjectValue*>(this)->builtins();
|
||||
return proto.find(name) != proto.end();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline Value ObjectValue::get_property(const std::string& name) const {
|
||||
if (properties->find(name) == properties->end()) {
|
||||
const auto& proto = const_cast<ObjectValue*>(this)->builtins();
|
||||
return proto.at(name);
|
||||
}
|
||||
return properties->at(name);
|
||||
}
|
||||
|
||||
inline std::map<std::string, Value>& ObjectValue::builtins() {
|
||||
static std::map<std::string, Value> proto_ = {
|
||||
{
|
||||
"size",
|
||||
Value(FunctionValue(
|
||||
{},
|
||||
[](std::shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
long n = val.to_object().properties->size();
|
||||
return Value(n);
|
||||
}
|
||||
))
|
||||
}
|
||||
};
|
||||
return proto_;
|
||||
}
|
||||
|
||||
inline std::map<std::string, Value>& ArrayValue::builtins() {
|
||||
static std::map<std::string, Value> proto_ = {
|
||||
{
|
||||
"size",
|
||||
Value(FunctionValue(
|
||||
{},
|
||||
[](std::shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
long n = val.to_array().values->size();
|
||||
return Value(n);
|
||||
}
|
||||
))
|
||||
},
|
||||
{
|
||||
"push",
|
||||
Value(FunctionValue {
|
||||
{ {"arg", false} },
|
||||
[](std::shared_ptr<Environment> callEnv) {
|
||||
const auto& val = callEnv->get("this");
|
||||
const auto& arg = callEnv->get("arg");
|
||||
val.to_array().values->push_back(arg);
|
||||
return Value();
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
return proto_;
|
||||
}
|
||||
|
||||
inline void setup_built_in_functions(Environment& env) {
|
||||
{
|
||||
auto f = Value::FunctionValue(
|
||||
auto f = FunctionValue(
|
||||
{ {"arg", true} },
|
||||
[](std::shared_ptr<Environment> env) {
|
||||
std::cout << env->get("arg").str() << std::endl;
|
||||
@ -387,7 +426,7 @@ inline void setup_built_in_functions(Environment& env) {
|
||||
}
|
||||
|
||||
{
|
||||
auto f = Value::FunctionValue(
|
||||
auto f = FunctionValue(
|
||||
{ {"arg", true} },
|
||||
[](std::shared_ptr<Environment> env) {
|
||||
auto cond = env->get("arg").to_bool();
|
||||
|
Loading…
Reference in New Issue
Block a user