implement newOrNil, changes to stdlib

- newOrNil works like new, but sets the variable to nil
  if the heap allocation failed
- change stdlib to use newOrNil in openfile and openvolumeid
- changes to programs that use openvolumeid
This commit is contained in:
slederer 2025-08-31 23:30:40 +02:00
parent 165517a9c8
commit 14d6de059d
10 changed files with 68 additions and 48 deletions

View file

@ -43,7 +43,7 @@ type TokenType = (
ArrayType, RecordType, PointerType, StringCharType, EnumType,
SetType, UnresolvedType );
SpecialProc = ( NoSP, NewSP, DisposeSP, ReadSP, WriteSP, ReadlnSP, WritelnSP,
SpecialProc = ( NoSP, NewSP, New0SP, DisposeSP, ReadSP, WriteSP, ReadlnSP, WritelnSP,
SetlengthSP, ValSP, StrSP, ExitSP );
SpecialFunc = ( NoSF, TruncSF, FracSF, IntSF, SqrSF, SuccSF, PredSF,
OddSF, ChrSF, OrdSF, AbsSF);
@ -291,7 +291,7 @@ var
'UNIT', 'IMPLEMENTATION', 'INTERFACE', 'USES',
'_' );
specialprocnames: array [SpecialProc] of string[12] = (
'_', 'NEW', 'DISPOSE', 'READ', 'WRITE', 'READLN', 'WRITELN', 'SETLENGTH',
'_', 'NEW', 'NEWORNIL', 'DISPOSE', 'READ', 'WRITE', 'READLN', 'WRITELN', 'SETLENGTH',
'VAL','STR', 'EXIT');
specialfuncnames: array [SpecialFunc] of string[8] = (
'_', 'TRUNC', 'FRAC', 'INT', 'SQR', 'SUCC', 'PRED', 'ODD',
@ -4500,7 +4500,7 @@ begin
isFunction := aProc^.returnType.baseType <> NoType;
end;
procedure parseNew;
procedure parseNew(checkNil:boolean);
var memLoc: MemLocation;
typeReturn: TypeSpec;
begin
@ -4526,17 +4526,17 @@ begin
emitLoadConstantInt(memLoc.typ.pointedType^.size);
emitMemAlloc;
(*We need to call CLEARMEM when the allocated type
contains strings.
INITSTRING checks if the header is non-zero to see if
the string is already initialized, and the allocated
chunk might contain random data so it would look
like an initialized string. *)
if typeContainsString(memLoc.typ.pointedType^) then
emitClearAlloc(memLoc.typ.pointedType);
end;
emitCheckAlloc;
(*We need to call CLEARMEM when the allocated type
contains strings.
INITSTRING checks if the header is non-zero to see if
the string is already initialized, and the allocated
chunk might contain random data so it would look
like an initialized string. *)
if checkNil then
emitCheckAlloc;
writeVariable(memLoc);
@ -5017,7 +5017,9 @@ begin
NoSP:
errorExit2('internal error in parseSpecialProcCall', lastToken.tokenText);
NewSP:
parseNew;
parseNew(true);
New0SP:
parseNew(false);
DisposeSP:
parseDispose;
ReadSP: