mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Fixed nested call and array reference problem.
This commit is contained in:
parent
1ff6dc058b
commit
cce6c20132
@ -3,41 +3,45 @@
|
|||||||
|
|
||||||
STATEMENTS <- (EXPRESSION (';' _)?)*
|
STATEMENTS <- (EXPRESSION (';' _)?)*
|
||||||
|
|
||||||
EXPRESSION <- ASSIGNMENT / PRIMARY
|
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
||||||
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_CALL <- IDENTIFIER ARGUMENTS
|
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
|
||||||
|
|
||||||
PRIMARY <- LOGICAL_OR ('||' _ LOGICAL_OR)*
|
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
||||||
LOGICAL_OR <- LOGICAL_AND ('&&' _ LOGICAL_AND)*
|
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
||||||
LOGICAL_AND <- CONDITION (CONDITION_OPERATOR CONDITION)*
|
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
||||||
CONDITION <- TERM (TERM_OPERATOR TERM)*
|
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
||||||
TERM <- UNARY_PLUS_OPERATOR? UNARY_PLUS
|
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
|
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
||||||
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
|
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
||||||
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
|
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
||||||
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
|
||||||
|
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
||||||
|
|
||||||
|
#FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
||||||
|
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
||||||
|
#ARRAY_REFERENCE <- IDENTIFIER INDEX
|
||||||
|
INDEX <- '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
|
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||||
|
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
|
||||||
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
|
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
|
|
||||||
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
||||||
TERM_OPERATOR <- < [-+] > _
|
ADDITIVE_OPERATOR <- < [-+] > _
|
||||||
UNARY_PLUS_OPERATOR <- < '+' > _
|
UNARY_PLUS_OPERATOR <- < '+' > _
|
||||||
UNARY_MINUS_OPERATOR <- < '-' > _
|
UNARY_MINUS_OPERATOR <- < '-' > _
|
||||||
UNARY_NOT_OPERATOR <- < '!' > _
|
UNARY_NOT_OPERATOR <- < '!' > _
|
||||||
FACTOR_OPERATOR <- < [*/%] > _
|
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
||||||
|
|
||||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||||
|
|
||||||
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
NUMBER <- < [0-9]+ > _
|
NUMBER <- < [0-9]+ > _
|
||||||
BOOLEAN <- < ('true' / 'false') > _
|
BOOLEAN <- < ('true' / 'false') > _
|
||||||
STRING <- ['] < (!['] .)* > ['] _
|
STRING <- ['] < (!['] .)* > ['] _
|
||||||
|
@ -13,9 +13,7 @@ struct Eval
|
|||||||
case While: return eval_while(ast, env);
|
case While: return eval_while(ast, env);
|
||||||
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 Call: return eval_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);
|
||||||
@ -25,6 +23,7 @@ struct Eval
|
|||||||
case UnaryNot: return eval_unary_not(ast, env);
|
case UnaryNot: return eval_unary_not(ast, env);
|
||||||
case BinExpresion: return eval_bin_expression(ast, env);
|
case BinExpresion: return eval_bin_expression(ast, env);
|
||||||
case Identifier: return eval_identifier(ast, env);
|
case Identifier: return eval_identifier(ast, env);
|
||||||
|
case Array: return eval_array(ast, env);
|
||||||
case Number: return eval_number(ast, env);
|
case Number: return eval_number(ast, env);
|
||||||
case Boolean: return eval_bool(ast, env);
|
case Boolean: return eval_bool(ast, env);
|
||||||
case InterpolatedString: return eval_interpolated_string(ast, env);
|
case InterpolatedString: return eval_interpolated_string(ast, env);
|
||||||
@ -100,57 +99,46 @@ private:
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) {
|
static Value eval_call(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
const auto& f = eval(*ast.nodes[0], env);
|
Value val = eval(*ast.nodes[0], env);
|
||||||
const auto& fv = f.to_function();
|
|
||||||
|
|
||||||
const auto& args = ast.nodes[1]->nodes;
|
for (auto i = 1u; i < ast.nodes.size(); i++) {
|
||||||
|
const auto& n = *ast.nodes[i];
|
||||||
|
if (n.tag == AstTag::Arguments) {
|
||||||
|
// Function call
|
||||||
|
const auto& f = val.to_function();
|
||||||
|
const auto& args = n.nodes;
|
||||||
|
if (f.params.size() <= args.size()) {
|
||||||
|
auto callEnv = make_shared<Environment>();
|
||||||
|
|
||||||
if (fv.params.size() <= args.size()) {
|
callEnv->initialize("self", val, false);
|
||||||
auto callEnv = make_shared<Environment>();
|
|
||||||
|
|
||||||
callEnv->initialize("self", f, false);
|
for (auto iprm = 0u; iprm < f.params.size(); iprm++) {
|
||||||
|
auto param = f.params[iprm];
|
||||||
|
auto arg = args[iprm];
|
||||||
|
auto val = eval(*arg, env);
|
||||||
|
callEnv->initialize(param.name, val, param.mut);
|
||||||
|
}
|
||||||
|
|
||||||
for (auto i = 0u; i < fv.params.size(); i++) {
|
callEnv->initialize("__LINE__", Value((long)ast.line), false);
|
||||||
auto param = fv.params[i];
|
callEnv->initialize("__COLUMN__", Value((long)ast.column), false);
|
||||||
auto arg = args[i];
|
|
||||||
auto val = eval(*arg, env);
|
val = f.eval(callEnv);
|
||||||
callEnv->initialize(param.name, val, param.mut);
|
} else {
|
||||||
|
string msg = "arguments error...";
|
||||||
|
throw runtime_error(msg);
|
||||||
|
}
|
||||||
|
} else { // n.tag == AstTag::Index
|
||||||
|
// Array reference
|
||||||
|
const auto& a = val.to_array();
|
||||||
|
const auto& idx = eval(*n.nodes[0], env).to_long();
|
||||||
|
if (0 <= idx && idx < static_cast<long>(a.values.size())) {
|
||||||
|
val = a.values[idx];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callEnv->initialize("__LINE__", Value((long)ast.line), false);
|
|
||||||
callEnv->initialize("__COLUMN__", Value((long)ast.column), false);
|
|
||||||
|
|
||||||
return fv.eval(callEnv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string msg = "arguments error...";
|
return val;
|
||||||
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& a = eval(*ast.nodes[0], env).to_array();
|
|
||||||
const auto& i = eval(*ast.nodes[1], env).to_long();
|
|
||||||
|
|
||||||
if (0 <= i && i < static_cast<long>(a.values.size())) {
|
|
||||||
return a.values[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
@ -262,6 +250,20 @@ private:
|
|||||||
return env->get(var);
|
return env->get(var);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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_number(const Ast& ast, shared_ptr<Environment> env) {
|
static Value eval_number(const Ast& ast, shared_ptr<Environment> env) {
|
||||||
return Value(stol(ast.token));
|
return Value(stol(ast.token));
|
||||||
};
|
};
|
||||||
|
@ -13,8 +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_CALL <- IDENTIFIER ARGUMENTS
|
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (', ' _ EXPRESSION)*)? ')' _
|
|
||||||
|
|
||||||
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
||||||
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
||||||
@ -23,16 +21,21 @@ static auto g_grammar = R"(
|
|||||||
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
||||||
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
||||||
MULTIPLICATIVE <- PRIMARY (MULTIPLICATIVE_OPERATOR PRIMARY)*
|
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
||||||
PRIMARY <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
|
|
||||||
|
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
||||||
|
|
||||||
|
#FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
||||||
|
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
||||||
|
#ARRAY_REFERENCE <- IDENTIFIER INDEX
|
||||||
|
INDEX <- '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
|
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||||
|
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
|
||||||
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
|
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
|
|
||||||
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
||||||
@ -44,6 +47,7 @@ static auto g_grammar = R"(
|
|||||||
|
|
||||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||||
|
|
||||||
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
NUMBER <- < [0-9]+ > _
|
NUMBER <- < [0-9]+ > _
|
||||||
BOOLEAN <- < ('true' / 'false') > _
|
BOOLEAN <- < ('true' / 'false') > _
|
||||||
STRING <- ['] < (!['] .)* > ['] _
|
STRING <- ['] < (!['] .)* > ['] _
|
||||||
@ -87,10 +91,9 @@ peg& get_parser()
|
|||||||
{ "IF", If, false },
|
{ "IF", If, false },
|
||||||
{ "FUNCTION", Function, false },
|
{ "FUNCTION", Function, false },
|
||||||
{ "PARAMETERS", Default, false },
|
{ "PARAMETERS", Default, false },
|
||||||
{ "FUNCTION_CALL", FunctionCall, false },
|
{ "CALL", Call, true },
|
||||||
{ "ARRAY", Array, false },
|
{ "ARGUMENTS", Arguments, false },
|
||||||
{ "ARRAY_REFERENCE", ArrayReference, false },
|
{ "INDEX", Index, false },
|
||||||
{ "ARGUMENTS", Default, false },
|
|
||||||
{ "LOGICAL_OR", LogicalOr, true },
|
{ "LOGICAL_OR", LogicalOr, true },
|
||||||
{ "LOGICAL_AND", LogicalAnd, true },
|
{ "LOGICAL_AND", LogicalAnd, true },
|
||||||
{ "CONDITION", Condition, true },
|
{ "CONDITION", Condition, true },
|
||||||
@ -99,6 +102,7 @@ peg& get_parser()
|
|||||||
{ "UNARY_MINUS", UnaryMinus, true },
|
{ "UNARY_MINUS", UnaryMinus, true },
|
||||||
{ "UNARY_NOT", UnaryNot, true },
|
{ "UNARY_NOT", UnaryNot, true },
|
||||||
{ "MULTIPLICATIVE", BinExpresion, true },
|
{ "MULTIPLICATIVE", BinExpresion, true },
|
||||||
|
{ "ARRAY", Array, false },
|
||||||
{ "NUMBER", Number, false },
|
{ "NUMBER", Number, false },
|
||||||
{ "BOOLEAN", Boolean, false },
|
{ "BOOLEAN", Boolean, false },
|
||||||
{ "IDENTIFIER", Identifier, false },
|
{ "IDENTIFIER", Identifier, false },
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
enum AstTag
|
enum AstTag
|
||||||
{
|
{
|
||||||
Default = peglib::AstDefaultTag,
|
Default = peglib::AstDefaultTag,
|
||||||
Statements, While, If, FunctionCall, ArrayReference, Assignment,
|
Statements, While, If, Call, Assignment,
|
||||||
|
Arguments, Index,
|
||||||
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
|
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
|
||||||
Identifier, InterpolatedString,
|
Identifier, InterpolatedString,
|
||||||
Number, Boolean, Function, Array
|
Number, Boolean, Function, Array
|
||||||
|
@ -1,4 +1,26 @@
|
|||||||
|
|
||||||
|
test_call = fn () {
|
||||||
|
ret = fn(){[1,fn(){[4,5,6]},3]}()[1]()[1]
|
||||||
|
assert(ret == 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
test_closure = fn () {
|
||||||
|
make_func = fn (mut x) {
|
||||||
|
mut n = 100
|
||||||
|
fn () {
|
||||||
|
n = n + 1
|
||||||
|
x = x + 1 + n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f = make_func(10)
|
||||||
|
f()
|
||||||
|
f()
|
||||||
|
ret = f()
|
||||||
|
|
||||||
|
assert(ret == 319)
|
||||||
|
}
|
||||||
|
|
||||||
test_sum = fn () {
|
test_sum = fn () {
|
||||||
mut i = 1
|
mut i = 1
|
||||||
mut ret = 0
|
mut ret = 0
|
||||||
@ -24,23 +46,7 @@ test_fib = fn () {
|
|||||||
assert(ret == 610)
|
assert(ret == 610)
|
||||||
}
|
}
|
||||||
|
|
||||||
test_closure = fn () {
|
test_call()
|
||||||
make_func = fn (mut x) {
|
test_closure()
|
||||||
mut n = 100
|
|
||||||
fn () {
|
|
||||||
n = n + 1
|
|
||||||
x = x + 1 + n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f = make_func(10)
|
|
||||||
f()
|
|
||||||
f()
|
|
||||||
ret = f()
|
|
||||||
|
|
||||||
assert(ret == 319)
|
|
||||||
}
|
|
||||||
|
|
||||||
test_sum()
|
test_sum()
|
||||||
test_fib()
|
test_fib()
|
||||||
test_closure()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user