From 34fab0b1acb72afad759d39624a672a8b3a110b4 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 2 Oct 2023 00:06:21 -0600 Subject: [PATCH] Fix ?? operator --- lib/src/eval/compiler/expression/binary.dart | 9 +++- test/expression_test.dart | 56 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/src/eval/compiler/expression/binary.dart b/lib/src/eval/compiler/expression/binary.dart index f9bd196..c90ac6b 100644 --- a/lib/src/eval/compiler/expression/binary.dart +++ b/lib/src/eval/compiler/expression/binary.dart @@ -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; } diff --git a/test/expression_test.dart b/test/expression_test.dart index 2e2b249..e6eea57 100644 --- a/test/expression_test.dart +++ b/test/expression_test.dart @@ -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': { @@ -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')); + }); }); }