Wiki: Pascal

General

Types

Numbers and keywords

Example I (for-to)
for i:=1 to 100 do doSomething(i);
for i:=1to 100do doSomething(i);
Example II (repeat-until)
i:=0;repeat doSomething(i);i+=1;until i=100;
i:=0;repeat doSomething(i);i+=1until i=100;
Example III (end)
begin i:=0;repeat i+=1;doSomething(i)until i=100;end.
begin i:=0;repeat i+=1;doSomething(i)until i=100end.
{❌ ERROR, parsed as 100e and expecting the exponent}
begin i:=0;repeat i+=1;doSomething(i)until i=1e2end.
{✅ NO ERROR}

HEX and OCT integers

Example I (hex superiority)
begin doSomething(x);x:=x div 10;end;
begin doSomething(x);x:=x div$a;end;
Example II (oct superiority)
begin doSomething(x);x:=x div 500;end;
begin doSomething(x);x:=x div$1f4;end;
begin doSomething(x);x:=x div$1f4end;
{❌ ERROR, FPC is trying to parse "$1f4e" as a hex integer}
begin doSomething(x);x:=x div&764;end;
begin doSomething(x);x:=x div&764end;
{✅ NO ERROR, oct values don't have 'e' in the range of digits and can't be represented in scientific notation}

Loops

Example I (one operation)
for i:=1to 100do doSomething(i);
i:=0;repeat i+=1;doSomething(i)until i=100;
Example II (two operations)
for i:=1to 100do begin doSomething(i);doSomethingElse(i)end;
i:=0;repeat i+=1;doSomething(i);doSomethingElse(i)until i=100;
Example III (two operations with a variable implicitly set to 0)
var i:word;{...}for i:=1to 100do begin doSomething(i);doSomethingElse(i)end;
var i:word;{...}repeat i+=1;doSomething(i);doSomethingElse(i)until i=100;

Arguments number

Example
var i:word;begin for i:=1 to 100 do doSomething(i);end.
begin for argc:=1 to 100 do doSomething(argc);end.

String to Number parsing

Example
var x:int32;s:string='90210';begin val(s,x);writeln(x+1)end.
uses sysutils;var s:string='90210';begin writeln(strtoint(s)+2)end.
uses strutils;var s:string='90210';begin writeln(numb2dec(s,10)+3)end.
uses{$H+}sysutils;var s:string='90210';begin writeln(s.toint64()+4)end.
uses sysutils;var x:int32;s:string='90210';begin sscanf(s,'%d',[@x]);writeln(x+5)end.

CTRL-format for characters

Example (^` vs ' ')
{$H+}
uses sysutils;begin writeln('code golf'.split(' ')[1]);end.
{$H+}
uses sysutils;begin writeln('code golf'.split(^`)[1]);end.
Most useful replacements
^@ --> '\0'
^i --> '\t'
^j --> '\n'
^m --> '\r'

^` --> ' '
^< --> '|'
^> --> '~'

^a..^z --> char(1)..char(26)
^0..^9 --> 'p'..'y'

Variant

Example (FizzBuzz mock)
{n bytes}
var i:word;s:string;
begin
 for i:=1 to 100 do begin
  s:=getFizzBuzzString(i);
  if s=''then write(i);
  writeln(s);
 end;
end.
{n-3 bytes}
var i:word;s:variant;
begin
 for i:=1 to 100 do begin
  s:=getFizzBuzzString(i);
  if s=''then s:=i;
  writeln(s);
 end;
end.