Refactoring.

pull/3/head
yhirose 9 years ago
parent ab3ade86d6
commit 8f74439798
  1. 58
      language/interpreter.cc
  2. 129
      language/interpreter.hpp

@ -82,7 +82,7 @@ private:
} }
static Value eval_function(const Ast& ast, shared_ptr<Environment> env) { 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) { for (auto node: ast.nodes[0]->nodes) {
auto mut = node->nodes[0]->token == "mut"; auto mut = node->nodes[0]->token == "mut";
const auto& name = node->nodes[1]->token; const auto& name = node->nodes[1]->token;
@ -91,7 +91,7 @@ private:
auto body = ast.nodes[1]; auto body = ast.nodes[1];
auto f = Value::FunctionValue( auto f = FunctionValue(
params, params,
[=](shared_ptr<Environment> callEnv) { [=](shared_ptr<Environment> callEnv) {
callEnv->append_outer(env); callEnv->append_outer(env);
@ -136,8 +136,8 @@ private:
// Array reference // Array reference
const auto& arr = val.to_array(); const auto& arr = val.to_array();
auto idx = eval(n, env).to_long(); auto idx = eval(n, env).to_long();
if (0 <= idx && idx < static_cast<long>(arr.data->values.size())) { if (0 <= idx && idx < static_cast<long>(arr.values->size())) {
val = arr.data->values.at(idx); val = arr.values->at(idx);
} }
} else if (n.original_tag == AstTag::Dot) { } else if (n.original_tag == AstTag::Dot) {
// Property // Property
@ -147,7 +147,7 @@ private:
if (prop.get_type() == Value::Function) { if (prop.get_type() == Value::Function) {
const auto& pf = prop.to_function(); const auto& pf = prop.to_function();
auto f = Value::FunctionValue( auto f = FunctionValue(
pf.data->params, pf.data->params,
[=](shared_ptr<Environment> callEnv) { [=](shared_ptr<Environment> callEnv) {
callEnv->initialize("this", val, false); callEnv->initialize("this", val, false);
@ -280,25 +280,25 @@ private:
}; };
static Value eval_object(const Ast& ast, shared_ptr<Environment> env) { 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++) { for (auto i = 0u; i < ast.nodes.size(); i++) {
const auto& prop = *ast.nodes[i]; const auto& prop = *ast.nodes[i];
const auto& name = prop.nodes[0]->token; const auto& name = prop.nodes[0]->token;
auto val = eval(*prop.nodes[1], env); auto val = eval(*prop.nodes[1], env);
obj.data->props.emplace(name, val); obj.properties->emplace(name, val);
} }
return Value(std::move(obj)); return Value(std::move(obj));
} }
static Value eval_array(const Ast& ast, shared_ptr<Environment> env) { 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++) { for (auto i = 0u; i < ast.nodes.size(); i++) {
auto expr = ast.nodes[i]; auto expr = ast.nodes[i];
auto val = eval(*expr, env); auto val = eval(*expr, env);
arr.data->values.push_back(val); arr.values->push_back(val);
} }
return Value(std::move(arr)); 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( bool run(
const string& path, const string& path,
shared_ptr<Environment> env, shared_ptr<Environment> env,

@ -2,12 +2,9 @@
#include <string> #include <string>
#include <peglib.h> #include <peglib.h>
struct Value;
struct Environment; struct Environment;
struct Value
{
enum Type { Undefined, Bool, Long, String, Object, Array, Function };
struct FunctionValue { struct FunctionValue {
struct Parameter { struct Parameter {
std::string name; std::string name;
@ -32,44 +29,24 @@ struct Value
}; };
struct ObjectValue { struct ObjectValue {
bool has_property(const std::string& name) const { bool has_property(const std::string& name) const;
if (data->props.find(name) == data->props.end()) { Value get_property(const std::string& name) const;
return prototypes.find(name) != prototypes.end(); virtual std::map<std::string, Value>& builtins();
}
return true;
}
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::shared_ptr<std::map<std::string, Value>> properties =
std::map<std::string, Value> props; std::make_shared<std::map<std::string, Value>>();
};
std::shared_ptr<Data> data = std::make_shared<Data>();
static std::map<std::string, Value> prototypes;
}; };
struct ArrayValue { struct ArrayValue : public ObjectValue {
Value get_property(const std::string& name) const { std::map<std::string, Value>& builtins() override;
if (data->props.find(name) == data->props.end()) {
return prototypes.at(name);
}
return data->props.at(name);
}
struct Data { std::shared_ptr<std::vector<Value>> values =
std::map<std::string, Value> props; std::make_shared<std::vector<Value>>();
std::vector<Value> values;
}; };
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) { Value() : type(Undefined) {
//std::cout << "Val::def ctor: " << std::endl; //std::cout << "Val::def ctor: " << std::endl;
@ -163,11 +140,11 @@ struct Value
} }
std::string str_object() const { std::string str_object() const {
const auto& props = to_object().data->props; const auto& properties = *to_object().properties;
std::string s = "{"; std::string s = "{";
auto it = props.begin(); auto it = properties.begin();
for (; it != props.end(); ++it) { for (; it != properties.end(); ++it) {
if (it != props.begin()) { if (it != properties.begin()) {
s += ", "; s += ", ";
} }
s += '"' + it->first + '"'; s += '"' + it->first + '"';
@ -179,7 +156,7 @@ struct Value
} }
std::string str_array() const { std::string str_array() const {
const auto& values = to_array().data->values; const auto& values = *to_array().values;
std::string s = "["; std::string s = "[";
for (auto i = 0u; i < values.size(); i++) { for (auto i = 0u; i < values.size(); i++) {
if (i != 0) { if (i != 0) {
@ -299,7 +276,7 @@ struct Environment
{ {
Environment() = default; Environment() = default;
void set_object(const Value::ObjectValue& obj) { void set_object(const ObjectValue& obj) {
obj_ = obj; obj_ = obj;
} }
@ -371,12 +348,74 @@ private:
std::shared_ptr<Environment> outer_; std::shared_ptr<Environment> outer_;
std::map<std::string, Symbol> dic_; 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) { inline void setup_built_in_functions(Environment& env) {
{ {
auto f = Value::FunctionValue( auto f = FunctionValue(
{ {"arg", true} }, { {"arg", true} },
[](std::shared_ptr<Environment> env) { [](std::shared_ptr<Environment> env) {
std::cout << env->get("arg").str() << std::endl; 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} }, { {"arg", true} },
[](std::shared_ptr<Environment> env) { [](std::shared_ptr<Environment> env) {
auto cond = env->get("arg").to_bool(); auto cond = env->get("arg").to_bool();

Loading…
Cancel
Save