Fixed nested call and array reference problem.

This commit is contained in:
yhirose 2015-07-08 10:26:01 -04:00
parent 1ff6dc058b
commit cce6c20132
5 changed files with 111 additions and 94 deletions

View File

@ -3,41 +3,45 @@
STATEMENTS <- (EXPRESSION (';' _)?)*
EXPRESSION <- ASSIGNMENT / PRIMARY
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
WHILE <- 'while' _ EXPRESSION 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_AND <- CONDITION (CONDITION_OPERATOR CONDITION)*
CONDITION <- TERM (TERM_OPERATOR TERM)*
TERM <- UNARY_PLUS_OPERATOR? UNARY_PLUS
UNARY_PLUS <- UNARY_MINUS_OPERATOR? UNARY_MINUS
UNARY_MINUS <- UNARY_NOT_OPERATOR? UNARY_NOT
UNARY_NOT <- FACTOR (FACTOR_OPERATOR FACTOR)*
FACTOR <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
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
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
PARAMETER <- MUTABLE IDENTIFIER
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
BLOCK <- '{' _ STATEMENTS '}' _
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
TERM_OPERATOR <- < [-+] > _
ADDITIVE_OPERATOR <- < [-+] > _
UNARY_PLUS_OPERATOR <- < '+' > _
UNARY_MINUS_OPERATOR <- < '-' > _
UNARY_NOT_OPERATOR <- < '!' > _
FACTOR_OPERATOR <- < [*/%] > _
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _

View File

@ -13,9 +13,7 @@ struct Eval
case While: return eval_while(ast, env);
case If: return eval_if(ast, env);
case Function: return eval_function(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 Call: return eval_call(ast, env);
case Assignment: return eval_assignment(ast, env);
case LogicalOr: return eval_logical_or(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 BinExpresion: return eval_bin_expression(ast, env);
case Identifier: return eval_identifier(ast, env);
case Array: return eval_array(ast, env);
case Number: return eval_number(ast, env);
case Boolean: return eval_bool(ast, env);
case InterpolatedString: return eval_interpolated_string(ast, env);
@ -100,20 +99,23 @@ private:
});
};
static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) {
const auto& f = eval(*ast.nodes[0], env);
const auto& fv = f.to_function();
static Value eval_call(const Ast& ast, shared_ptr<Environment> env) {
Value val = eval(*ast.nodes[0], env);
const auto& args = ast.nodes[1]->nodes;
if (fv.params.size() <= args.size()) {
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>();
callEnv->initialize("self", f, false);
callEnv->initialize("self", val, false);
for (auto i = 0u; i < fv.params.size(); i++) {
auto param = fv.params[i];
auto arg = args[i];
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);
}
@ -121,36 +123,22 @@ private:
callEnv->initialize("__LINE__", Value((long)ast.line), false);
callEnv->initialize("__COLUMN__", Value((long)ast.column), false);
return fv.eval(callEnv);
}
val = f.eval(callEnv);
} else {
string msg = "arguments error...";
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);
} 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];
}
}
}
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();
return val;
}
static Value eval_logical_or(const Ast& ast, shared_ptr<Environment> env) {
@ -262,6 +250,20 @@ private:
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) {
return Value(stol(ast.token));
};

View File

@ -13,8 +13,6 @@ static auto g_grammar = R"(
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
WHILE <- 'while' _ EXPRESSION 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_AND <- CONDITION ('&&' _ CONDITION)*
@ -23,16 +21,21 @@ static auto g_grammar = R"(
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
MULTIPLICATIVE <- PRIMARY (MULTIPLICATIVE_OPERATOR PRIMARY)*
PRIMARY <- WHILE / IF / FUNCTION / FUNCTION_CALL / ARRAY / ARRAY_REFERENCE / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / IDENTIFIER / '(' _ EXPRESSION ')' _
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
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
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
PARAMETER <- MUTABLE IDENTIFIER
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
ARRAY_REFERENCE <- IDENTIFIER '[' _ EXPRESSION ']' _
BLOCK <- '{' _ STATEMENTS '}' _
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
@ -44,6 +47,7 @@ static auto g_grammar = R"(
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _
@ -87,10 +91,9 @@ peg& get_parser()
{ "IF", If, false },
{ "FUNCTION", Function, false },
{ "PARAMETERS", Default, false },
{ "FUNCTION_CALL", FunctionCall, false },
{ "ARRAY", Array, false },
{ "ARRAY_REFERENCE", ArrayReference, false },
{ "ARGUMENTS", Default, false },
{ "CALL", Call, true },
{ "ARGUMENTS", Arguments, false },
{ "INDEX", Index, false },
{ "LOGICAL_OR", LogicalOr, true },
{ "LOGICAL_AND", LogicalAnd, true },
{ "CONDITION", Condition, true },
@ -99,6 +102,7 @@ peg& get_parser()
{ "UNARY_MINUS", UnaryMinus, true },
{ "UNARY_NOT", UnaryNot, true },
{ "MULTIPLICATIVE", BinExpresion, true },
{ "ARRAY", Array, false },
{ "NUMBER", Number, false },
{ "BOOLEAN", Boolean, false },
{ "IDENTIFIER", Identifier, false },

View File

@ -3,7 +3,8 @@
enum AstTag
{
Default = peglib::AstDefaultTag,
Statements, While, If, FunctionCall, ArrayReference, Assignment,
Statements, While, If, Call, Assignment,
Arguments, Index,
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
Identifier, InterpolatedString,
Number, Boolean, Function, Array

View File

@ -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 () {
mut i = 1
mut ret = 0
@ -24,23 +46,7 @@ test_fib = fn () {
assert(ret == 610)
}
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_call()
test_closure()
test_sum()
test_fib()
test_closure()