Skip to content

Commit

Permalink
Use L.type intead of resolving, remove L.name check, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wrbl606 committed Oct 18, 2023
1 parent cf0b309 commit 8aa1dd0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
10 changes: 4 additions & 6 deletions lib/src/eval/compiler/expression/postfix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ Variable compilePostfixExpression(PostfixExpression e, CompilerContext ctx) {
final L = V.getValue(ctx);
var out = L;

if (L.name != null) {
out = Variable.alloc(ctx, L.type);
ctx.pushOp(PushNull.make(), PushNull.LEN);
ctx.pushOp(CopyValue.make(out.scopeFrameOffset, L.scopeFrameOffset),
CopyValue.LEN);
}
out = Variable.alloc(ctx, L.type);
ctx.pushOp(PushNull.make(), PushNull.LEN);
ctx.pushOp(
CopyValue.make(out.scopeFrameOffset, L.scopeFrameOffset), CopyValue.LEN);

const opMap = {TokenType.PLUS_PLUS: '+', TokenType.MINUS_MINUS: '-'};

Expand Down
24 changes: 10 additions & 14 deletions lib/src/eval/compiler/expression/prefix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ Variable compilePrefixExpression(CompilerContext ctx, PrefixExpression e) {
if ([TokenType.PLUS_PLUS, TokenType.MINUS_MINUS].contains(e.operator.type)) {
final V = compileExpressionAsReference(e.operand, ctx);
final L = V.getValue(ctx);
final type = V.resolveType(ctx);
return _handleDoubleOperands(e, ctx, V, type, L);
return _handleDoubleOperands(e, ctx, V, L);
}

final V = compileExpression(e.operand, ctx);
Expand Down Expand Up @@ -63,24 +62,21 @@ Variable _handleDoubleOperands(
PrefixExpression e,
CompilerContext ctx,
Reference V,
TypeRef type,
Variable L,
) {
var out = L;
var l = L;

if (L.name != null) {
out = Variable.alloc(ctx, L.type);
ctx.pushOp(PushNull.make(), PushNull.LEN);
ctx.pushOp(
CopyValue.make(out.scopeFrameOffset, L.scopeFrameOffset),
CopyValue.LEN,
);
}
l = Variable.alloc(ctx, L.type);
ctx.pushOp(PushNull.make(), PushNull.LEN);
ctx.pushOp(
CopyValue.make(l.scopeFrameOffset, L.scopeFrameOffset),
CopyValue.LEN,
);

final result = out.invoke(
final result = l.invoke(
ctx,
_opMap[e.operator.type]!,
[_oneForType(type, ctx).push(ctx)],
[_oneForType(l.type, ctx).push(ctx)],
).result;

V.setValue(ctx, result);
Expand Down
9 changes: 7 additions & 2 deletions test/postfix_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ void main() {
int ii = i++;
print(ii);
print(i);
List<int> list = [0];
print(list[0]++);
print(list.first);
}
'''
}
Expand All @@ -32,7 +35,7 @@ void main() {
() {
runtime.executeLib('package:eval_test/main.dart', 'main');
},
prints('0.0\n1.0\n0\n1\n'),
prints('0.0\n1.0\n0\n1\n0\n1\n'),
);
});

Expand All @@ -49,6 +52,8 @@ void main() {
int ii = i--;
print(ii);
print(i);
List<int> list = [1];
print(list[0]--);
}
'''
}
Expand All @@ -58,7 +63,7 @@ void main() {
() {
runtime.executeLib('package:eval_test/main.dart', 'main');
},
prints('1.0\n0.0\n1\n0\n'),
prints('1.0\n0.0\n1\n0\n1\n'),
);
});
});
Expand Down
8 changes: 6 additions & 2 deletions test/prefix_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void main() {
int ii = ++i;
print(ii);
print(i);
List<int> list = [0];
print(++list[0]);
}
'''
}
Expand All @@ -32,7 +34,7 @@ void main() {
() {
runtime.executeLib('package:eval_test/main.dart', 'main');
},
prints('1.0\n1.0\n1\n1\n'),
prints('1.0\n1.0\n1\n1\n1\n'),
);
});

Expand All @@ -49,6 +51,8 @@ void main() {
int ii = --i;
print(ii);
print(i);
List<int> list = [1];
print(--list[0]);
}
'''
}
Expand All @@ -58,7 +62,7 @@ void main() {
() {
runtime.executeLib('package:eval_test/main.dart', 'main');
},
prints('0.0\n0.0\n0\n0\n'),
prints('0.0\n0.0\n0\n0\n0\n'),
);
});
});
Expand Down

0 comments on commit 8aa1dd0

Please sign in to comment.