Renamed O_COMMA to O_CONS, and changed semantics
In the old scheme, nested values would simply flatten and concatenate, so that '((1, 2), 3) = (1, 2, 3)'. Now sublists are preserved, so that sequences may be passed as arguments to functions.
This commit is contained in:
parent
9a44b8a547
commit
e919f53c99
4 changed files with 52 additions and 30 deletions
|
|
@ -39,7 +39,7 @@ namespace {
|
||||||
void push_sort_value(std::list<sort_value_t>& sort_values,
|
void push_sort_value(std::list<sort_value_t>& sort_values,
|
||||||
expr_t::ptr_op_t node, T * scope)
|
expr_t::ptr_op_t node, T * scope)
|
||||||
{
|
{
|
||||||
if (node->kind == expr_t::op_t::O_COMMA) {
|
if (node->kind == expr_t::op_t::O_CONS) {
|
||||||
push_sort_value(sort_values, node->left(), scope);
|
push_sort_value(sort_values, node->left(), scope);
|
||||||
push_sort_value(sort_values, node->right(), scope);
|
push_sort_value(sort_values, node->right(), scope);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
src/op.cc
37
src/op.cc
|
|
@ -137,7 +137,7 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * locus)
|
||||||
sym;
|
sym;
|
||||||
sym = sym->has_right() ? sym->right() : NULL) {
|
sym = sym->has_right() ? sym->right() : NULL) {
|
||||||
ptr_op_t varname = sym;
|
ptr_op_t varname = sym;
|
||||||
if (sym->kind == O_COMMA)
|
if (sym->kind == O_CONS)
|
||||||
varname = sym->left();
|
varname = sym->left();
|
||||||
|
|
||||||
if (! varname->is_ident())
|
if (! varname->is_ident())
|
||||||
|
|
@ -269,25 +269,30 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * locus)
|
||||||
assert(! "We should never calculate an O_COLON operator");
|
assert(! "We should never calculate an O_COLON operator");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_COMMA: {
|
case O_CONS:
|
||||||
value_t temp(left()->calc(scope, locus));
|
result = left()->calc(scope, locus);
|
||||||
|
DEBUG("op.cons", "car = " << result);
|
||||||
|
|
||||||
|
if (has_right()) {
|
||||||
|
value_t temp;
|
||||||
|
temp.push_back(result);
|
||||||
|
|
||||||
ptr_op_t next = right();
|
ptr_op_t next = right();
|
||||||
while (next) {
|
while (next) {
|
||||||
ptr_op_t value_op;
|
ptr_op_t value_op;
|
||||||
if (next->kind == O_COMMA) {
|
if (next->kind == O_CONS) {
|
||||||
value_op = next->left();
|
value_op = next->left();
|
||||||
next = next->right();
|
next = next->right();
|
||||||
} else {
|
} else {
|
||||||
value_op = next;
|
value_op = next;
|
||||||
next = NULL;
|
next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
temp.push_back(value_op->calc(scope, locus));
|
temp.push_back(value_op->calc(scope, locus));
|
||||||
|
DEBUG("op.cons", "temp now = " << temp);
|
||||||
}
|
}
|
||||||
result = temp;
|
result = temp;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case LAST:
|
case LAST:
|
||||||
default:
|
default:
|
||||||
|
|
@ -463,12 +468,19 @@ bool expr_t::op_t::print(std::ostream& out, const context_t& context) const
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_COMMA:
|
case O_CONS:
|
||||||
|
if (has_right()) {
|
||||||
|
out << "(";
|
||||||
if (left() && left()->print(out, context))
|
if (left() && left()->print(out, context))
|
||||||
found = true;
|
found = true;
|
||||||
out << ", ";
|
out << ", ";
|
||||||
if (has_right() && right()->print(out, context))
|
if (has_right() && right()->print(out, context))
|
||||||
found = true;
|
found = true;
|
||||||
|
out << ")";
|
||||||
|
}
|
||||||
|
else if (left() && left()->print(out, context)) {
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_DEFINE:
|
case O_DEFINE:
|
||||||
|
|
@ -490,10 +502,19 @@ bool expr_t::op_t::print(std::ostream& out, const context_t& context) const
|
||||||
case O_CALL:
|
case O_CALL:
|
||||||
if (left() && left()->print(out, context))
|
if (left() && left()->print(out, context))
|
||||||
found = true;
|
found = true;
|
||||||
|
if (has_right()) {
|
||||||
|
if (right()->kind == O_CONS) {
|
||||||
|
if (right()->print(out, context))
|
||||||
|
found = true;
|
||||||
|
} else {
|
||||||
out << "(";
|
out << "(";
|
||||||
if (has_right() && right()->print(out, context))
|
if (has_right() && right()->print(out, context))
|
||||||
found = true;
|
found = true;
|
||||||
out << ")";
|
out << ")";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out << "()";
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_MATCH:
|
case O_MATCH:
|
||||||
|
|
@ -572,7 +593,7 @@ void expr_t::op_t::dump(std::ostream& out, const int depth) const
|
||||||
case O_QUERY: out << "O_QUERY"; break;
|
case O_QUERY: out << "O_QUERY"; break;
|
||||||
case O_COLON: out << "O_COLON"; break;
|
case O_COLON: out << "O_COLON"; break;
|
||||||
|
|
||||||
case O_COMMA: out << "O_COMMA"; break;
|
case O_CONS: out << "O_CONS"; break;
|
||||||
|
|
||||||
case LAST:
|
case LAST:
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
2
src/op.h
2
src/op.h
|
|
@ -108,7 +108,7 @@ public:
|
||||||
O_QUERY,
|
O_QUERY,
|
||||||
O_COLON,
|
O_COLON,
|
||||||
|
|
||||||
O_COMMA,
|
O_CONS,
|
||||||
|
|
||||||
O_DEFINE,
|
O_DEFINE,
|
||||||
O_LOOKUP,
|
O_LOOKUP,
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,8 @@ expr_t::parser_t::parse_value_term(std::istream& in,
|
||||||
}
|
}
|
||||||
|
|
||||||
case token_t::LPAREN:
|
case token_t::LPAREN:
|
||||||
node = parse_value_expr(in, tflags.plus_flags(PARSE_PARTIAL).minus_flags(PARSE_SINGLE));
|
node = parse_value_expr(in, tflags.plus_flags(PARSE_PARTIAL)
|
||||||
|
.minus_flags(PARSE_SINGLE));
|
||||||
tok = next_token(in, tflags);
|
tok = next_token(in, tflags);
|
||||||
if (tok.kind != token_t::RPAREN)
|
if (tok.kind != token_t::RPAREN)
|
||||||
tok.expected(')');
|
tok.expected(')');
|
||||||
|
|
@ -399,7 +400,7 @@ expr_t::parser_t::parse_value_expr(std::istream& in,
|
||||||
|
|
||||||
if (tok.kind == token_t::COMMA) {
|
if (tok.kind == token_t::COMMA) {
|
||||||
ptr_op_t prev(node);
|
ptr_op_t prev(node);
|
||||||
node = new op_t(op_t::O_COMMA);
|
node = new op_t(op_t::O_CONS);
|
||||||
node->set_left(prev);
|
node->set_left(prev);
|
||||||
node->set_right(parse_value_expr(in, tflags));
|
node->set_right(parse_value_expr(in, tflags));
|
||||||
if (! node->right())
|
if (! node->right())
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue