Skip to content

Commit

Permalink
Fix ?? operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanblake4 committed Oct 2, 2023
1 parent 0d6b542 commit 34fab0b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/eval/compiler/expression/binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ Variable compileBinaryExpression(CompilerContext ctx, BinaryExpression e, [TypeR
var R = compileExpression(e.rightOperand, ctx, boundType);

if (e.operator.type == TokenType.QUESTION_QUESTION) {
final outVar = Variable.alloc(ctx, TypeRef.commonBaseType(ctx, {L.type.copyWith(nullable: false), R.type}));
final outType = TypeRef.commonBaseType(ctx, {L.type.copyWith(nullable: false), R.type}).copyWith(boxed: true);
var outVar = BuiltinValue().push(ctx).copyWith(type: outType);
L = L.boxIfNeeded(ctx);
ctx.pushOp(CopyValue.make(outVar.scopeFrameOffset, L.scopeFrameOffset), CopyValue.LEN);
macroBranch(ctx, null, condition: (_ctx) {
final $null = BuiltinValue().push(ctx);
final $null = BuiltinValue().push(ctx).boxIfNeeded(ctx);
ctx.pushOp(CheckEq.make(L.scopeFrameOffset, $null.scopeFrameOffset), CheckEq.LEN);
ctx.pushOp(PushReturnValue.make(), PushReturnValue.LEN);
return Variable.alloc(ctx, EvalTypes.boolType.copyWith(boxed: false));
}, thenBranch: (_ctx, rt) {
R = R.boxIfNeeded(ctx);
ctx.pushOp(CopyValue.make(outVar.scopeFrameOffset, R.scopeFrameOffset), CopyValue.LEN);
return StatementInfo(-1);
});

return outVar;
}

Expand Down
56 changes: 56 additions & 0 deletions test/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,43 @@ void main() {
}, prints('true\ntrue\ntrue\ntrue\nfalse\nfalse\ntrue\nfalse\n'));
});

test('Is num', () {
final runtime = compiler.compileWriteAndLoad({
'eval_test': {
'main.dart': '''
num main () {
var myfunc = ([dynamic a, dynamic b, dynamic c]) {
if(a is num && b is num){
return a + b;
}
return 0;
};
return myfunc(2, 4);
}
'''
}
});

expect(runtime.executeLib('package:eval_test/main.dart', 'main'), 6);
});

test('Null coalescing operator', () {
final runtime = compiler.compileWriteAndLoad({
'eval_test': {
'main.dart': '''
void main() {
print(null ?? 1);
print(2 ?? 1);
}
'''
}
});

expect(() {
runtime.executeLib('package:eval_test/main.dart', 'main');
}, prints('1\n2\n'));
});

test("Not expression", () {
final runtime = compiler.compileWriteAndLoad({
'eval_test': {
Expand All @@ -56,5 +93,24 @@ void main() {
runtime.executeLib('package:eval_test/main.dart', 'main');
}, prints('false\ntrue\n'));
});

test('Bitwise int operators', () {
final runtime = compiler.compileWriteAndLoad({
'eval_test': {
'main.dart': '''
void main() {
print(1 & 2);
print(1 | 2);
print(1 << 2);
print(1 >> 2);
}
'''
}
});

expect(() {
runtime.executeLib('package:eval_test/main.dart', 'main');
}, prints('0\n3\n4\n0\n'));
});
});
}

0 comments on commit 34fab0b

Please sign in to comment.