@@ -1286,15 +1286,50 @@ const ast::Expression *Analyzer::analyze(const pt::SubscriptExpression *expr)
1286
1286
}
1287
1287
type = arraytype->elementtype ;
1288
1288
const ast::ReferenceExpression *ref = dynamic_cast <const ast::ReferenceExpression *>(base);
1289
- // This check for ArrayReferenceRangeExpression is a stupendous hack that
1290
- // allows expressions like a[LAST] (which is compiled as a[LAST TO LAST][0])
1291
- // to work. Without this, the subscript [0] on the range expression tries
1292
- // to generate the address of the base so it can apply an INDEXAR opcode.
1293
- // Since the range expression is really a function call, it has no address.
1294
- if (ref != nullptr && dynamic_cast <const ast::ArrayReferenceRangeExpression *>(ref) == nullptr ) {
1295
- return new ast::ArrayReferenceIndexExpression (type, ref, index , false );
1289
+ if (ref != nullptr ) {
1290
+ if (expr->from_last ) {
1291
+ return new ast::ArrayReferenceIndexExpression (
1292
+ type,
1293
+ ref,
1294
+ new ast::AdditionExpression (
1295
+ new ast::SubtractionExpression (
1296
+ new ast::FunctionCall (
1297
+ new ast::VariableExpression (
1298
+ ref->type ->methods .at (" size" )
1299
+ ),
1300
+ { ref }
1301
+ ),
1302
+ new ast::ConstantNumberExpression (number_from_sint32 (1 ))
1303
+ ),
1304
+ index
1305
+ ),
1306
+ false
1307
+ );
1308
+ } else {
1309
+ return new ast::ArrayReferenceIndexExpression (type, ref, index , false );
1310
+ }
1296
1311
} else {
1297
- return new ast::ArrayValueIndexExpression (type, base, index , false );
1312
+ if (expr->from_last ) {
1313
+ return new ast::ArrayValueIndexExpression (
1314
+ type,
1315
+ base,
1316
+ new ast::AdditionExpression (
1317
+ new ast::SubtractionExpression (
1318
+ new ast::FunctionCall (
1319
+ new ast::VariableExpression (
1320
+ base->type ->methods .at (" size" )
1321
+ ),
1322
+ { base }
1323
+ ),
1324
+ new ast::ConstantNumberExpression (number_from_sint32 (1 ))
1325
+ ),
1326
+ index
1327
+ ),
1328
+ false
1329
+ );
1330
+ } else {
1331
+ return new ast::ArrayValueIndexExpression (type, base, index , false );
1332
+ }
1298
1333
}
1299
1334
} else if (dicttype != nullptr ) {
1300
1335
if (not index ->type ->is_assignment_compatible (ast::TYPE_STRING)) {
0 commit comments