Correctly implement negative array indices, other bugfixes

- check for negative string lengths
- handle negative values for emitInc/emitDec
- bugfix for parseInteger with leading minus
- fix set literals containing variables as elements
This commit is contained in:
slederer 2025-03-31 00:47:34 +02:00
parent bb602043d2
commit 6d08db2933
2 changed files with 24 additions and 7 deletions

View file

@ -1250,7 +1250,7 @@ begin
(* nothing to do *) (* nothing to do *)
end end
else else
if amount <= MaxTinyOffset then if (amount > 0) and (amount <= MaxTinyOffset) then
emitIns2Int('INC', amount) emitIns2Int('INC', amount)
else else
begin begin
@ -1266,7 +1266,7 @@ begin
(* nothing to do *) (* nothing to do *)
end end
else else
if amount <= MaxTinyOffset then if (amount > 0) and (amount <= MaxTinyOffset) then
emitIns2Int('DEC', amount) emitIns2Int('DEC', amount)
else else
begin begin

View file

@ -2456,10 +2456,7 @@ begin
begin begin
digits := curToken.tokenText; digits := curToken.tokenText;
if matchTokenOrNot(MinusToken) then if matchTokenOrNot(MinusToken) then
begin
readNextToken;
digits := digits + curToken.tokenText; digits := digits + curToken.tokenText;
end;
parseInteger := integerFromString(digits); parseInteger := integerFromString(digits);
end; end;
readNextToken; readNextToken;
@ -2812,6 +2809,8 @@ begin
if checkToken(CommaToken) then if checkToken(CommaToken) then
begin begin
(* TODO: handle comma syntax for indexing a char
from an array of strings *)
if elType.baseType <> ArrayType then if elType.baseType <> ArrayType then
errorExit2('invalid array subscript for', name) errorExit2('invalid array subscript for', name)
end; end;
@ -3231,10 +3230,22 @@ begin
readNextToken; readNextToken;
end; end;
(* Handle an array range part. Very similar to parseInteger
but additionally handles Boolean, Enum and Char types which
can be used for array indices *)
procedure getRangePart(var value:integer; var typeReturn: TypeSpec); procedure getRangePart(var value:integer; var typeReturn: TypeSpec);
var cnst: ConstRef; var cnst: ConstRef;
negate:boolean;
digits: string[12];
begin begin
setBaseType(typeReturn, NoType); setBaseType(typeReturn, NoType);
negate := false;
if checkToken(MinusToken) then
begin
negate := true;
readNextToken;
end;
if checkToken(IdentToken) then if checkToken(IdentToken) then
begin begin
@ -3268,7 +3279,11 @@ begin
else else
begin begin
setBaseType(typeReturn, IntegerType); setBaseType(typeReturn, IntegerType);
value := parseInteger; digits := curToken.tokenText;
if negate then
insert('-', digits, 1);
value := integerFromString(digits);
readNextToken;
end; end;
end; end;
@ -3433,9 +3448,9 @@ begin
if savedType.baseType = NoType then if savedType.baseType = NoType then
savedType := elementType; savedType := elementType;
emitLoadConstantInt(cnst^.intValue); emitLoadConstantInt(cnst^.intValue);
readNextToken;
end; end;
emitAddToSet; emitAddToSet;
readNextToken;
end end
else if checkToken(NumberToken) then else if checkToken(NumberToken) then
begin begin
@ -3999,6 +4014,8 @@ begin
begin begin
readNextToken; readNextToken;
length := parseInteger; length := parseInteger;
if length < 1 then
errorExit2('invalid string length', '');
matchToken(RBracketToken); matchToken(RBracketToken);
end; end;
typSpec.size := getStringMemSize(length); typSpec.size := getStringMemSize(length);