diff options
Diffstat (limited to 'src/op.cc')
-rw-r--r-- | src/op.cc | 90 |
1 files changed, 78 insertions, 12 deletions
@@ -36,7 +36,7 @@ namespace ledger { expr_t::ptr_op_t expr_t::op_t::compile(scope_t& scope) { - if (kind == IDENT) { + if (is_ident()) { DEBUG("expr.compile", "Looking up identifier '" << as_ident() << "'"); if (ptr_op_t def = scope.lookup(as_ident())) { @@ -58,6 +58,23 @@ expr_t::ptr_op_t expr_t::op_t::compile(scope_t& scope) if (kind < TERMINALS) return this; + if (kind == O_DEFINE) { + switch (left()->kind) { + case IDENT: + scope.define(left()->as_ident(), right()); + break; + case O_CALL: + if (left()->left()->is_ident()) + scope.define(left()->left()->as_ident(), this); + else + throw_(compile_error, "Invalid function definition"); + break; + default: + throw_(compile_error, "Invalid function definition"); + } + return wrap_value(value_t()); + } + ptr_op_t lhs(left()->compile(scope)); ptr_op_t rhs(kind > UNARY_OPERATORS && has_right() ? (kind == O_LOOKUP ? right() : right()->compile(scope)) : NULL); @@ -68,7 +85,7 @@ expr_t::ptr_op_t expr_t::op_t::compile(scope_t& scope) ptr_op_t intermediate(copy(lhs, rhs)); // Reduce constants immediately if possible - if (lhs->is_value() && (! rhs || rhs->is_value())) + if ((! lhs || lhs->is_value()) && (! rhs || rhs->is_value())) return wrap_value(intermediate->calc(scope)); return intermediate; @@ -87,11 +104,17 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context) result = as_value(); break; - case IDENT: + case IDENT: { if (! left()) throw_(calc_error, "Unknown identifier '" << as_ident() << "'"); - result = left()->calc(scope, context); + + // Evaluating an identifier is the same as calling its definition + // directly, so we create an empty call_scope_t to reflect the scope for + // this implicit call. + call_scope_t call_args(scope); + result = left()->calc(call_args, context); break; + } case FUNCTION: { // Evaluating a FUNCTION is the same as calling it directly; this happens @@ -102,9 +125,41 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context) break; } + case O_DEFINE: { + symbol_scope_t local_scope; + call_scope_t& call_args(downcast<call_scope_t>(scope)); + std::size_t args_count = call_args.size(); + std::size_t args_index = 0; + + assert(left()->kind == O_CALL); + + for (ptr_op_t sym = left()->right(); + sym; + sym = sym->has_right() ? sym->right() : NULL) { + ptr_op_t varname = sym; + if (sym->kind == O_COMMA) + varname = sym->left(); + + if (! varname->is_ident()) + throw_(calc_error, "Invalid function definition"); + else if (args_index == args_count) + local_scope.define(varname->as_ident(), wrap_value(false)); + else + local_scope.define(varname->as_ident(), + wrap_value(call_args[args_index++])); + } + + if (args_index < args_count) + throw_(calc_error, + "Too many arguments in function call (saw " << args_count << ")"); + + result = right()->compile(local_scope)->calc(local_scope, context); + break; + } + case O_LOOKUP: - if (left()->kind == IDENT && - left()->left() && left()->left()->kind == FUNCTION) { + if (left()->is_ident() && + left()->left() && left()->left()->is_function()) { call_scope_t call_args(scope); if (value_t obj = left()->left()->as_function()(call_args)) { if (obj.is_pointer()) { @@ -134,18 +189,20 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context) const string& name(func->as_ident()); func = func->left(); - if (! func || func->kind != FUNCTION) - throw_(calc_error, "Calling non-function '" << name << "'"); + if (! func) + throw_(calc_error, "Calling unknown function '" << name << "'"); - result = func->as_function()(call_args); + if (func->is_function()) + result = func->as_function()(call_args); + else + result = func->calc(call_args, context); break; } case O_MATCH: -#if 0 if (! right()->is_value() || ! right()->as_value().is_mask()) throw_(calc_error, "Right-hand argument to match operator must be a regex"); -#endif + result = (right()->calc(scope, context).as_mask() .match(left()->calc(scope, context).to_string())); break; @@ -417,6 +474,14 @@ bool expr_t::op_t::print(std::ostream& out, const context_t& context) const found = true; break; + case O_DEFINE: + if (left() && left()->print(out, context)) + found = true; + out << " := "; + if (has_right() && right()->print(out, context)) + found = true; + break; + case O_LOOKUP: if (left() && left()->print(out, context)) found = true; @@ -485,6 +550,7 @@ void expr_t::op_t::dump(std::ostream& out, const int depth) const out << "FUNCTION"; break; + case O_DEFINE: out << "O_DEFINE"; break; case O_LOOKUP: out << "O_LOOKUP"; break; case O_CALL: out << "O_CALL"; break; case O_MATCH: out << "O_MATCH"; break; @@ -521,7 +587,7 @@ void expr_t::op_t::dump(std::ostream& out, const int depth) const // An identifier is a special non-terminal, in that its left() can // hold the compiled definition of the identifier. - if (kind > TERMINALS || kind == IDENT) { + if (kind > TERMINALS || is_ident()) { if (left()) { left()->dump(out, depth + 1); if (kind > UNARY_OPERATORS && has_right()) |