mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Added array. (working...)
This commit is contained in:
parent
6795c56c47
commit
d2d396c351
@ -7,9 +7,6 @@
|
|||||||
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
||||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
|
||||||
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
||||||
|
|
||||||
@ -21,7 +18,14 @@
|
|||||||
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
|
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
|
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
|
||||||
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
||||||
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
||||||
|
|
||||||
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
|
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
|
|
||||||
|
@ -14,6 +14,8 @@ struct Eval
|
|||||||
case If: return eval_if(ast, env);
|
case If: return eval_if(ast, env);
|
||||||
case Function: return eval_function(ast, env);
|
case Function: return eval_function(ast, env);
|
||||||
case FunctionCall: return eval_function_call(ast, env);
|
case FunctionCall: return eval_function_call(ast, env);
|
||||||
|
case Array: return eval_array(ast, env);
|
||||||
|
case ArrayReference: return eval_array_reference(ast, env);
|
||||||
case Assignment: return eval_assignment(ast, env);
|
case Assignment: return eval_assignment(ast, env);
|
||||||
case LogicalOr: return eval_logical_or(ast, env);
|
case LogicalOr: return eval_logical_or(ast, env);
|
||||||
case LogicalAnd: return eval_logical_and(ast, env);
|
case LogicalAnd: return eval_logical_and(ast, env);
|
||||||
@ -100,11 +102,11 @@ private:
|
|||||||
|
|
||||||
static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) {
|
static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
const auto& var = ast.nodes[0]->token;
|
const auto& var = ast.nodes[0]->token;
|
||||||
const auto& args = ast.nodes[1]->nodes;
|
|
||||||
|
|
||||||
const auto& f = env->get(var);
|
const auto& f = env->get(var);
|
||||||
const auto& fv = f.to_function();
|
const auto& fv = f.to_function();
|
||||||
|
|
||||||
|
const auto& args = ast.nodes[1]->nodes;
|
||||||
|
|
||||||
if (fv.params.size() <= args.size()) {
|
if (fv.params.size() <= args.size()) {
|
||||||
auto callEnv = make_shared<Environment>();
|
auto callEnv = make_shared<Environment>();
|
||||||
|
|
||||||
@ -124,6 +126,35 @@ private:
|
|||||||
throw runtime_error(msg);
|
throw runtime_error(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Value eval_array(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
|
vector<Value> values;
|
||||||
|
|
||||||
|
for (auto i = 0u; i < ast.nodes.size(); i++) {
|
||||||
|
auto expr = ast.nodes[i];
|
||||||
|
auto val = eval(*expr, env);
|
||||||
|
values.push_back(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Value(Value::ArrayValue {
|
||||||
|
values
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static Value eval_array_reference(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
|
const auto& var = ast.nodes[0]->token;
|
||||||
|
|
||||||
|
const auto& a = env->get(var);
|
||||||
|
const auto& av = a.to_array();
|
||||||
|
|
||||||
|
const auto& idx = eval(*ast.nodes[1], env).to_long();
|
||||||
|
|
||||||
|
if (0 <= idx && idx < av.values.size()) {
|
||||||
|
return av.values[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Value();
|
||||||
|
}
|
||||||
|
|
||||||
static Value eval_logical_or(const Ast& ast, shared_ptr<Environment> env) {
|
static Value eval_logical_or(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
if (ast.nodes.size() == 1) {
|
if (ast.nodes.size() == 1) {
|
||||||
return eval(*ast.nodes[0], env);
|
return eval(*ast.nodes[0], env);
|
||||||
@ -251,7 +282,14 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
bool run(const string& path, shared_ptr<Environment> env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast)
|
bool run(
|
||||||
|
const string& path,
|
||||||
|
shared_ptr<Environment> env,
|
||||||
|
const char* expr,
|
||||||
|
size_t len,
|
||||||
|
Value& val,
|
||||||
|
string& msg,
|
||||||
|
bool print_ast)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
shared_ptr<Ast> ast;
|
shared_ptr<Ast> ast;
|
||||||
|
@ -270,4 +270,11 @@ private:
|
|||||||
std::map<std::string, Symbol> dic_;
|
std::map<std::string, Symbol> dic_;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool run(const std::string& path, std::shared_ptr<Environment> env, const char* expr, size_t len, Value& val, std::string& msg, bool print_ast);
|
bool run(
|
||||||
|
const std::string& path,
|
||||||
|
std::shared_ptr<Environment> env,
|
||||||
|
const char* expr,
|
||||||
|
size_t len,
|
||||||
|
Value& val,
|
||||||
|
std::string& msg,
|
||||||
|
bool print_ast);
|
||||||
|
@ -13,9 +13,6 @@ static auto g_grammar = R"(
|
|||||||
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
||||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
|
||||||
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
||||||
|
|
||||||
@ -27,7 +24,14 @@ static auto g_grammar = R"(
|
|||||||
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
|
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
|
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
|
||||||
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
||||||
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
||||||
|
|
||||||
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
|
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
|
|
||||||
@ -84,6 +88,8 @@ peg& get_parser()
|
|||||||
{ "FUNCTION", Function, false },
|
{ "FUNCTION", Function, false },
|
||||||
{ "PARAMETERS", Default, false },
|
{ "PARAMETERS", Default, false },
|
||||||
{ "FUNCTION_CALL", FunctionCall, false },
|
{ "FUNCTION_CALL", FunctionCall, false },
|
||||||
|
{ "ARRAY", Array, false },
|
||||||
|
{ "ARRAY_REFERENCE", ArrayReference, false },
|
||||||
{ "ARGUMENTS", Default, false },
|
{ "ARGUMENTS", Default, false },
|
||||||
{ "PRIMARY", LogicalOr, true },
|
{ "PRIMARY", LogicalOr, true },
|
||||||
{ "LOGICAL_OR", LogicalAnd, true },
|
{ "LOGICAL_OR", LogicalAnd, true },
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
enum AstTag
|
enum AstTag
|
||||||
{
|
{
|
||||||
Default = peglib::AstDefaultTag,
|
Default = peglib::AstDefaultTag,
|
||||||
Statements, While, If, FunctionCall, Assignment,
|
Statements, While, If, FunctionCall, ArrayReference, Assignment,
|
||||||
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
|
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
|
||||||
Identifier, InterpolatedString,
|
Identifier, InterpolatedString,
|
||||||
Number, Boolean, Function
|
Number, Boolean, Function, Array
|
||||||
};
|
};
|
||||||
|
|
||||||
peglib::peg& get_parser();
|
peglib::peg& get_parser();
|
||||||
|
Loading…
Reference in New Issue
Block a user