parseUnary method
- and !. A negated constant is folded.
Implementation
Expression parseUnary() {
if (current.kind == '-') {
final token = expectToken('-', '"-"');
final operand = parseUnary();
if (!operand.type.isNumeric) {
fail('a ${operand.type} cannot be negated.', where: token);
}
if (operand is ExpressionConstant) {
return ExpressionConstant(<double>[
for (final component in operand.value) -component,
], operand.type);
}
return ExpressionNegate(operand);
}
if (syntax.logic && current.kind == '!') {
final token = expectToken('!', '"!"');
final operand = parseUnary();
if (!operand.type.isBoolean) {
fail('"!" takes a bool, and this is a ${operand.type}.', where: token);
}
return ExpressionNot(operand);
}
return parsePostfix();
}