Skip to content

Commit dc4d8de

Browse files
committed
Logical "!" operator.
1 parent 9dc4426 commit dc4d8de

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

packages/betwixt/lib/src/parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Parser {
237237
}
238238

239239
Expr _unary() {
240-
if (_match(TokenType.minus)) {
240+
if (_match(TokenType.minus) || _match(TokenType.bang)) {
241241
var op = _previous;
242242
return UnaryExpr(op, _unary());
243243
}

packages/betwixt/lib/src/renderer.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,20 @@ class Renderer
224224
Future<Object?> visitUnaryExpr(UnaryExpr expr) async {
225225
var operand = await expr.expression.accept(this);
226226
switch ((expr.op.type, operand)) {
227+
case (TokenType.bang, bool operand):
228+
return !operand;
229+
230+
case (TokenType.bang, _):
231+
_reporter.report(expr.op.span,
232+
'Operand to "!" must be a Boolean but was ${operand.runtimeType}.');
233+
return null;
234+
227235
case (TokenType.minus, num operand):
228236
return -operand;
229237

230238
case (TokenType.minus, _):
231239
_reporter.report(expr.op.span,
232-
'Cannot negate a value of type ${operand.runtimeType}.');
240+
'Operand to "-" must be a number but was ${operand.runtimeType}.');
233241
return null;
234242

235243
default:

packages/betwixt/lib/src/token.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ enum TokenType {
5252
comma,
5353
dot,
5454
equal,
55-
// TODO: Implement.
5655
bang,
5756
minus,
5857
plus,

packages/betwixt/test/cases/arithmetic/negate_wrong_type.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{ -"wat" }}
22
--- runtime errors
3-
line 1, column 4 of test: Cannot negate a value of type String.
3+
line 1, column 4 of test: Operand to "-" must be a number but was String.
44
55
1 │ {{ -"wat" }}
66
│ ^
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{ !yes }}
2+
{{ !no }}
3+
{{ !!yes }}
4+
{{ !!!no }}
5+
--- output
6+
false
7+
true
8+
true
9+
true
10+
--- data
11+
{"yes": true, "no": false}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{ !"wat" }}
2+
--- runtime errors
3+
line 1, column 4 of test: Operand to "!" must be a Boolean but was String.
4+
5+
1 │ {{ !"wat" }}
6+
│ ^
7+

0 commit comments

Comments
 (0)