mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Code cleanup.
This commit is contained in:
parent
f3e44ae16c
commit
0e0e3f6f24
@ -101,21 +101,14 @@ struct FunctionValue {
|
|||||||
bool mut;
|
bool mut;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Data {
|
|
||||||
std::vector<Parameter> params;
|
|
||||||
std::function<Value (std::shared_ptr<Environment> env)> eval;
|
|
||||||
};
|
|
||||||
|
|
||||||
FunctionValue(
|
FunctionValue(
|
||||||
const std::vector<Parameter>& params,
|
const std::vector<Parameter>& params,
|
||||||
const std::function<Value (std::shared_ptr<Environment> env)>& eval) {
|
const std::function<Value (std::shared_ptr<Environment> env)>& eval)
|
||||||
|
: params(std::make_shared<std::vector<Parameter>>(params))
|
||||||
|
, eval(eval) {}
|
||||||
|
|
||||||
data = std::make_shared<Data>();
|
std::shared_ptr<std::vector<Parameter>> params;
|
||||||
data->params = params;
|
std::function<Value (std::shared_ptr<Environment> env)> eval;
|
||||||
data->eval = eval;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Data> data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ObjectValue {
|
struct ObjectValue {
|
||||||
@ -173,10 +166,6 @@ struct Value
|
|||||||
explicit Value(ArrayValue&& a) : type(Array), v(a) {}
|
explicit Value(ArrayValue&& a) : type(Array), v(a) {}
|
||||||
explicit Value(FunctionValue&& f) : type(Function), v(f) {}
|
explicit Value(FunctionValue&& f) : type(Function), v(f) {}
|
||||||
|
|
||||||
Type get_type() const {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool to_bool() const {
|
bool to_bool() const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Bool: return v.get<bool>();
|
case Bool: return v.get<bool>();
|
||||||
@ -200,6 +189,13 @@ struct Value
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FunctionValue to_function() const {
|
||||||
|
switch (type) {
|
||||||
|
case Function: return v.get<FunctionValue>();
|
||||||
|
default: throw std::runtime_error("type error.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ObjectValue to_object() const {
|
ObjectValue to_object() const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Object: return v.get<ObjectValue>();
|
case Object: return v.get<ObjectValue>();
|
||||||
@ -214,13 +210,6 @@ struct Value
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionValue to_function() const {
|
|
||||||
switch (type) {
|
|
||||||
case Function: return v.get<FunctionValue>();
|
|
||||||
default: throw std::runtime_error("type error.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Value get_property(const std::string& name) const {
|
Value get_property(const std::string& name) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Object: return to_object().get_property(name);
|
case Object: return to_object().get_property(name);
|
||||||
@ -290,15 +279,7 @@ struct Value
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const Value& rhs) const {
|
bool operator!=(const Value& rhs) const {
|
||||||
switch (type) {
|
return !operator==(rhs);
|
||||||
case Undefined: return rhs.type != Undefined;
|
|
||||||
case Bool: return to_bool() != rhs.to_bool();
|
|
||||||
case Long: return to_long() != rhs.to_long();
|
|
||||||
case String: return to_string() != rhs.to_string();
|
|
||||||
// TODO: Object and Array support
|
|
||||||
default: throw std::logic_error("invalid internal condition.");
|
|
||||||
}
|
|
||||||
// NOTREACHED
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<=(const Value& rhs) const {
|
bool operator<=(const Value& rhs) const {
|
||||||
@ -349,12 +330,8 @@ struct Value
|
|||||||
// NOTREACHED
|
// NOTREACHED
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
friend std::ostream& operator<<(std::ostream&, const Value&);
|
|
||||||
|
|
||||||
Type type;
|
Type type;
|
||||||
peglib::any v;
|
peglib::any v;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& os, const Value& val)
|
inline std::ostream& operator<<(std::ostream& os, const Value& val)
|
||||||
@ -366,19 +343,11 @@ struct Environment
|
|||||||
{
|
{
|
||||||
Environment() = default;
|
Environment() = default;
|
||||||
|
|
||||||
void set_object(const ObjectValue& obj) {
|
|
||||||
obj_ = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_outer(std::shared_ptr<Environment> outer) {
|
|
||||||
outer_ = outer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void append_outer(std::shared_ptr<Environment> outer) {
|
void append_outer(std::shared_ptr<Environment> outer) {
|
||||||
if (outer_) {
|
if (this->outer) {
|
||||||
outer_->append_outer(outer);
|
this->outer->append_outer(outer);
|
||||||
} else {
|
} else {
|
||||||
outer_ = outer;
|
this->outer = outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,21 +355,21 @@ struct Environment
|
|||||||
if (dic_.find(s) != dic_.end()) {
|
if (dic_.find(s) != dic_.end()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (obj_.has_property(s)) {
|
if (object.has_property(s)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return outer_ && outer_->has(s);
|
return outer && outer->has(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value get(const std::string& s) const {
|
Value get(const std::string& s) const {
|
||||||
if (dic_.find(s) != dic_.end()) {
|
if (dic_.find(s) != dic_.end()) {
|
||||||
return dic_.at(s).val;
|
return dic_.at(s).val;
|
||||||
}
|
}
|
||||||
if (obj_.has_property(s)) {
|
if (object.has_property(s)) {
|
||||||
return obj_.get_property(s);
|
return object.get_property(s);
|
||||||
}
|
}
|
||||||
if (outer_) {
|
if (outer) {
|
||||||
return outer_->get(s);
|
return outer->get(s);
|
||||||
}
|
}
|
||||||
std::string msg = "undefined variable '" + s + "'...";
|
std::string msg = "undefined variable '" + s + "'...";
|
||||||
throw std::runtime_error(msg);
|
throw std::runtime_error(msg);
|
||||||
@ -417,8 +386,8 @@ struct Environment
|
|||||||
sym.val = val;
|
sym.val = val;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (outer_ && outer_->has(s)) {
|
if (outer && outer->has(s)) {
|
||||||
outer_->assign(s, val);
|
outer->assign(s, val);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// NOTREACHED
|
// NOTREACHED
|
||||||
@ -430,35 +399,35 @@ struct Environment
|
|||||||
dic_[s] = Symbol{val, mut};
|
dic_[s] = Symbol{val, mut};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Environment> outer;
|
||||||
|
ObjectValue object;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Symbol {
|
struct Symbol {
|
||||||
Value val;
|
Value val;
|
||||||
bool mut;
|
bool mut;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::shared_ptr<Environment> outer_;
|
|
||||||
std::map<std::string, Symbol> dic_;
|
std::map<std::string, Symbol> dic_;
|
||||||
ObjectValue obj_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool ObjectValue::has_property(const std::string& name) const {
|
inline bool ObjectValue::has_property(const std::string& name) const {
|
||||||
if (properties->find(name) == properties->end()) {
|
if (properties->find(name) == properties->end()) {
|
||||||
const auto& proto = const_cast<ObjectValue*>(this)->builtins();
|
const auto& props = const_cast<ObjectValue*>(this)->builtins();
|
||||||
return proto.find(name) != proto.end();
|
return props.find(name) != props.end();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value ObjectValue::get_property(const std::string& name) const {
|
inline Value ObjectValue::get_property(const std::string& name) const {
|
||||||
if (properties->find(name) == properties->end()) {
|
if (properties->find(name) == properties->end()) {
|
||||||
const auto& proto = const_cast<ObjectValue*>(this)->builtins();
|
const auto& props = const_cast<ObjectValue*>(this)->builtins();
|
||||||
return proto.at(name);
|
return props.at(name);
|
||||||
}
|
}
|
||||||
return properties->at(name);
|
return properties->at(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::map<std::string, Value>& ObjectValue::builtins() {
|
inline std::map<std::string, Value>& ObjectValue::builtins() {
|
||||||
static std::map<std::string, Value> proto_ = {
|
static std::map<std::string, Value> props_ = {
|
||||||
{
|
{
|
||||||
"size",
|
"size",
|
||||||
Value(FunctionValue(
|
Value(FunctionValue(
|
||||||
@ -471,11 +440,11 @@ inline std::map<std::string, Value>& ObjectValue::builtins() {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return proto_;
|
return props_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::map<std::string, Value>& ArrayValue::builtins() {
|
inline std::map<std::string, Value>& ArrayValue::builtins() {
|
||||||
static std::map<std::string, Value> proto_ = {
|
static std::map<std::string, Value> props_ = {
|
||||||
{
|
{
|
||||||
"size",
|
"size",
|
||||||
Value(FunctionValue(
|
Value(FunctionValue(
|
||||||
@ -500,23 +469,24 @@ inline std::map<std::string, Value>& ArrayValue::builtins() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return proto_;
|
return props_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void setup_built_in_functions(Environment& env) {
|
inline void setup_built_in_functions(Environment& env) {
|
||||||
{
|
env.initialize(
|
||||||
auto f = FunctionValue(
|
"puts",
|
||||||
|
Value(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;
|
||||||
return Value();
|
return Value();
|
||||||
}
|
}
|
||||||
);
|
)),
|
||||||
env.initialize("puts", Value(std::move(f)), false);
|
false);
|
||||||
}
|
|
||||||
|
|
||||||
{
|
env.initialize(
|
||||||
auto f = FunctionValue(
|
"assert",
|
||||||
|
Value(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();
|
||||||
@ -528,9 +498,8 @@ inline void setup_built_in_functions(Environment& env) {
|
|||||||
}
|
}
|
||||||
return Value();
|
return Value();
|
||||||
}
|
}
|
||||||
);
|
)),
|
||||||
env.initialize("assert", Value(std::move(f)), false);
|
false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Eval
|
struct Eval
|
||||||
@ -623,15 +592,13 @@ private:
|
|||||||
|
|
||||||
auto body = ast.nodes[1];
|
auto body = ast.nodes[1];
|
||||||
|
|
||||||
auto f = FunctionValue(
|
return Value(FunctionValue(
|
||||||
params,
|
params,
|
||||||
[=](std::shared_ptr<Environment> callEnv) {
|
[=](std::shared_ptr<Environment> callEnv) {
|
||||||
callEnv->append_outer(env);
|
callEnv->append_outer(env);
|
||||||
return eval(*body, callEnv);
|
return eval(*body, callEnv);
|
||||||
}
|
}
|
||||||
);
|
));
|
||||||
|
|
||||||
return Value(std::move(f));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static Value eval_call(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_call(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
@ -644,7 +611,7 @@ private:
|
|||||||
if (n.original_tag == "ARGUMENTS"_) {
|
if (n.original_tag == "ARGUMENTS"_) {
|
||||||
// Function call
|
// Function call
|
||||||
const auto& f = val.to_function();
|
const auto& f = val.to_function();
|
||||||
const auto& params = f.data->params;
|
const auto& params = *f.params;
|
||||||
const auto& args = n.nodes;
|
const auto& args = n.nodes;
|
||||||
if (params.size() <= args.size()) {
|
if (params.size() <= args.size()) {
|
||||||
auto callEnv = std::make_shared<Environment>();
|
auto callEnv = std::make_shared<Environment>();
|
||||||
@ -661,7 +628,7 @@ private:
|
|||||||
callEnv->initialize("__LINE__", Value((long)ast.line), false);
|
callEnv->initialize("__LINE__", Value((long)ast.line), false);
|
||||||
callEnv->initialize("__COLUMN__", Value((long)ast.column), false);
|
callEnv->initialize("__COLUMN__", Value((long)ast.column), false);
|
||||||
|
|
||||||
val = f.data->eval(callEnv);
|
val = f.eval(callEnv);
|
||||||
} else {
|
} else {
|
||||||
std::string msg = "arguments error...";
|
std::string msg = "arguments error...";
|
||||||
throw std::runtime_error(msg);
|
throw std::runtime_error(msg);
|
||||||
@ -677,22 +644,18 @@ private:
|
|||||||
// Property
|
// Property
|
||||||
auto name = n.token;
|
auto name = n.token;
|
||||||
auto prop = val.get_property(name);
|
auto prop = val.get_property(name);
|
||||||
|
if (prop.type == Value::Function) {
|
||||||
if (prop.get_type() == Value::Function) {
|
|
||||||
const auto& pf = prop.to_function();
|
const auto& pf = prop.to_function();
|
||||||
|
val = Value(FunctionValue(
|
||||||
auto f = FunctionValue(
|
*pf.params,
|
||||||
pf.data->params,
|
|
||||||
[=](std::shared_ptr<Environment> callEnv) {
|
[=](std::shared_ptr<Environment> callEnv) {
|
||||||
callEnv->initialize("this", val, false);
|
callEnv->initialize("this", val, false);
|
||||||
if (val.get_type() == Value::Object) {
|
if (val.type == Value::Object) {
|
||||||
callEnv->set_object(val.to_object());
|
callEnv->object = val.to_object();
|
||||||
}
|
}
|
||||||
return pf.data->eval(callEnv);
|
return pf.eval(callEnv);
|
||||||
}
|
}
|
||||||
);
|
));
|
||||||
|
|
||||||
val = Value(std::move(f));
|
|
||||||
} else {
|
} else {
|
||||||
val = prop;
|
val = prop;
|
||||||
}
|
}
|
||||||
@ -701,83 +664,61 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return val;
|
return std::move(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_logical_or(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_logical_or(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
assert(ast.nodes.size() > 1); // if the size is 1, thes node will be hoisted.
|
||||||
return eval(*ast.nodes[0], env);
|
Value val;
|
||||||
} else {
|
for (auto node: ast.nodes) {
|
||||||
Value ret;
|
val = eval(*node, env);
|
||||||
for (auto node: ast.nodes) {
|
if (val.to_bool()) {
|
||||||
ret = eval(*node, env);
|
return std::move(val);
|
||||||
if (ret.to_bool()) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
return std::move(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_logical_and(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_logical_and(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
Value ret;
|
Value val;
|
||||||
for (auto node: ast.nodes) {
|
for (auto node: ast.nodes) {
|
||||||
ret = eval(*node, env);
|
val = eval(*node, env);
|
||||||
if (!ret.to_bool()) {
|
if (!val.to_bool()) {
|
||||||
return ret;
|
return std::move(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return std::move(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_condition(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_condition(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
assert(ast.nodes.size() == 3); // if the size is 1, thes node will be hoisted.
|
||||||
return eval(*ast.nodes[0], env);
|
|
||||||
} else {
|
|
||||||
auto lhs = eval(*ast.nodes[0], env);
|
|
||||||
auto ope = eval(*ast.nodes[1], env).to_string();
|
|
||||||
auto rhs = eval(*ast.nodes[2], env);
|
|
||||||
|
|
||||||
if (ope == "==") {
|
auto lhs = eval(*ast.nodes[0], env);
|
||||||
return Value(lhs == rhs);
|
auto ope = eval(*ast.nodes[1], env).to_string();
|
||||||
} else if (ope == "!=") {
|
auto rhs = eval(*ast.nodes[2], env);
|
||||||
return Value(lhs != rhs);
|
|
||||||
} else if (ope == "<=") {
|
if (ope == "==") { return Value(lhs == rhs); }
|
||||||
return Value(lhs <= rhs);
|
else if (ope == "!=") { return Value(lhs != rhs); }
|
||||||
} else if (ope == "<") {
|
else if (ope == "<=") { return Value(lhs <= rhs); }
|
||||||
return Value(lhs < rhs);
|
else if (ope == "<") { return Value(lhs < rhs); }
|
||||||
} else if (ope == ">=") {
|
else if (ope == ">=") { return Value(lhs >= rhs); }
|
||||||
return Value(lhs >= rhs);
|
else if (ope == ">") { return Value(lhs > rhs); }
|
||||||
} else if (ope == ">") {
|
else { throw std::logic_error("invalid internal condition."); }
|
||||||
return Value(lhs > rhs);
|
|
||||||
} else {
|
|
||||||
throw std::logic_error("invalid internal condition.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_unary_plus(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_unary_plus(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
assert(ast.nodes.size() == 2); // if the size is 1, thes node will be hoisted.
|
||||||
return eval(*ast.nodes[0], env);
|
return eval(*ast.nodes[1], env);
|
||||||
} else {
|
|
||||||
return eval(*ast.nodes[1], env);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_unary_minus(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_unary_minus(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
assert(ast.nodes.size() == 2); // if the size is 1, thes node will be hoisted.
|
||||||
return eval(*ast.nodes[0], env);
|
return Value(eval(*ast.nodes[1], env).to_long() * -1);
|
||||||
} else {
|
|
||||||
return Value(eval(*ast.nodes[1], env).to_long() * -1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_unary_not(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_unary_not(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
assert(ast.nodes.size() == 2); // if the size is 1, thes node will be hoisted.
|
||||||
return eval(*ast.nodes[0], env);
|
return Value(!eval(*ast.nodes[1], env).to_bool());
|
||||||
} else {
|
|
||||||
return Value(!eval(*ast.nodes[1], env).to_bool());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_bin_expression(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_bin_expression(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
@ -797,44 +738,39 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_assignment(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_assignment(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
const auto& mut = ast.nodes[0]->token;
|
|
||||||
const auto& var = ast.nodes[1]->token;
|
const auto& var = ast.nodes[1]->token;
|
||||||
auto val = eval(*ast.nodes[2], env);
|
auto val = eval(*ast.nodes[2], env);
|
||||||
if (env->has(var)) {
|
if (env->has(var)) {
|
||||||
env->assign(var, val);
|
env->assign(var, val);
|
||||||
} else {
|
} else {
|
||||||
|
const auto& mut = ast.nodes[0]->token;
|
||||||
env->initialize(var, val, mut == "mut");
|
env->initialize(var, val, mut == "mut");
|
||||||
}
|
}
|
||||||
return val;
|
return std::move(val);
|
||||||
};
|
};
|
||||||
|
|
||||||
static Value eval_identifier(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_identifier(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
const auto& var = ast.token;
|
return env->get(ast.token);
|
||||||
return env->get(var);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static Value eval_object(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_object(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
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.properties->emplace(name, val);
|
obj.properties->emplace(name, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Value(std::move(obj));
|
return Value(std::move(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value eval_array(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
static Value eval_array(const peglib::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
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.values->push_back(val);
|
arr.values->push_back(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Value(std::move(arr));
|
return Value(std::move(arr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -870,8 +806,6 @@ inline bool run(
|
|||||||
bool print_ast)
|
bool print_ast)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
std::shared_ptr<peglib::Ast> ast;
|
|
||||||
|
|
||||||
auto& parser = get_parser();
|
auto& parser = get_parser();
|
||||||
|
|
||||||
parser.log = [&](size_t ln, size_t col, const std::string& err_msg) {
|
parser.log = [&](size_t ln, size_t col, const std::string& err_msg) {
|
||||||
@ -880,6 +814,8 @@ inline bool run(
|
|||||||
msg = ss.str();
|
msg = ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::shared_ptr<peglib::Ast> ast;
|
||||||
|
|
||||||
if (parser.parse_n(expr, len, ast)) {
|
if (parser.parse_n(expr, len, ast)) {
|
||||||
if (print_ast) {
|
if (print_ast) {
|
||||||
ast->print();
|
ast->print();
|
||||||
|
30
peglib.h
30
peglib.h
@ -159,8 +159,8 @@ struct SemanticValue
|
|||||||
SemanticValue()
|
SemanticValue()
|
||||||
: s(nullptr), n(0) {}
|
: s(nullptr), n(0) {}
|
||||||
|
|
||||||
SemanticValue(const any& _val, const char* _name, const char* _s, size_t _n)
|
SemanticValue(const any& val, const char* name, const char* s, size_t n)
|
||||||
: val(_val), name(_name), s(_s), n(_n) {}
|
: val(val), name(name), s(s), n(n) {}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& get() {
|
T& get() {
|
||||||
@ -423,12 +423,12 @@ struct Context
|
|||||||
|
|
||||||
std::map<std::pair<size_t, size_t>, std::tuple<size_t, any>> cache_result;
|
std::map<std::pair<size_t, size_t>, std::tuple<size_t, any>> cache_result;
|
||||||
|
|
||||||
Context(const char* _s, size_t _l, size_t _def_count, bool enablePackratParsing)
|
Context(const char* s, size_t l, size_t def_count, bool enablePackratParsing)
|
||||||
: s(_s)
|
: s(s)
|
||||||
, l(_l)
|
, l(l)
|
||||||
, error_pos(nullptr)
|
, error_pos(nullptr)
|
||||||
, message_pos(nullptr)
|
, message_pos(nullptr)
|
||||||
, def_count(_def_count)
|
, def_count(def_count)
|
||||||
, cache_register(enablePackratParsing ? def_count * (l + 1) : 0)
|
, cache_register(enablePackratParsing ? def_count * (l + 1) : 0)
|
||||||
, cache_success(enablePackratParsing ? def_count * (l + 1) : 0)
|
, cache_success(enablePackratParsing ? def_count * (l + 1) : 0)
|
||||||
, stack_size(0)
|
, stack_size(0)
|
||||||
@ -1953,25 +1953,25 @@ inline constexpr unsigned int operator "" _(const char* s, size_t) {
|
|||||||
|
|
||||||
struct Ast
|
struct Ast
|
||||||
{
|
{
|
||||||
Ast(size_t _line, size_t _column, const char* _name, const std::vector<std::shared_ptr<Ast>>& _nodes)
|
Ast(size_t line, size_t column, const char* name, const std::vector<std::shared_ptr<Ast>>& nodes)
|
||||||
: line(_line), column(_column), name(_name), original_name(name), is_token(false), nodes(_nodes)
|
: line(line), column(column), name(name), original_name(name), is_token(false), nodes(nodes)
|
||||||
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
||||||
, tag(str2tag(_name)), original_tag(tag)
|
, tag(str2tag(name)), original_tag(tag)
|
||||||
#endif
|
#endif
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Ast(size_t _line, size_t _column, const char* _name, const std::string& _token)
|
Ast(size_t line, size_t column, const char* name, const std::string& token)
|
||||||
: line(_line), column(_column), name(_name), original_name(name), is_token(true), token(_token)
|
: line(line), column(column), name(name), original_name(name), is_token(true), token(token)
|
||||||
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
||||||
, tag(str2tag(_name)), original_tag(tag)
|
, tag(str2tag(name)), original_tag(tag)
|
||||||
#endif
|
#endif
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Ast(const Ast& ast, const char* _original_name)
|
Ast(const Ast& ast, const char* original_name)
|
||||||
: line(ast.line), column(ast.column), name(ast.name), original_name(_original_name)
|
: line(ast.line), column(ast.column), name(ast.name), original_name(original_name)
|
||||||
, is_token(ast.is_token), token(ast.token), nodes(ast.nodes)
|
, is_token(ast.is_token), token(ast.token), nodes(ast.nodes)
|
||||||
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
#ifdef PEGLIB_HAS_CONSTEXPR_SUPPORT
|
||||||
, tag(ast.tag), original_tag(str2tag(_original_name))
|
, tag(ast.tag), original_tag(str2tag(original_name))
|
||||||
#endif
|
#endif
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user