-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal.html
More file actions
1674 lines (1660 loc) · 69.7 KB
/
pascal.html
File metadata and controls
1674 lines (1660 loc) · 69.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="icon" type="image/gif" href="/favicon.gif"/>
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png" />
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png" />
<title>Pascal Style Languages: Pascal, Ada, PL/pgSQL - Hyperpolyglot</title>
<style type="text/css" id="internal-style">
@import url(hyperpolyglot.css);
</style>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="content-language" content="en"/>
</head>
<body>
<div id="container-wrap-wrap">
<div id="container-wrap">
<div id="container">
<div id="header">
<h1><a href="/"><span>Hyperpolyglot</span></a></h1>
</div>
<div id="content-wrap">
<div id="main-content">
<div id="page-title">
Pascal Style Languages: Pascal, Ada, PL/pgSQL
</div>
<div id="page-content">
<p><em>a side-by-side reference sheet</em></p>
<p><a name="top" id="top"></a><a href="#grammar-invocation">grammar and invocation</a> | <a href="#variables-expressions">variables and expressions</a> | <a href="#arithmetic-logic">arithmetic and logic</a> | <a href="#strings">strings</a> | <a href="#regexes">regexes</a> | <a href="#dates-time">dates and time</a> | <a href="#arrays">arrays</a> | <a href="#user-defined-types">user-defined types</a> | <a href="#generic-types">generic types</a> | <a href="#functions">functions</a> | <a href="#execution-control">execution control</a> | <a href="#exceptions">exceptions</a> | <a href="#file-handles">file handles</a> | <a href="#files">files</a> | <a href="#directories">directories</a> | <a href="#processes-environment">processes and environment</a> | <a href="#libraries-namespaces">libraries and namespaces</a></p>
<table class="wiki-content-table">
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="version-used" id="version-used"></a><a href="#version-used-note">version used</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>Free Pascal 2.4</em></span></td>
<td><span style="color: gray"><em>GNAT GCC 4.6</em></span><br />
<span style="color: gray"><em>Ada 2012</em></span></td>
<td><span style="color: gray"><em>Postgres 9.1</em></span></td>
</tr>
<tr>
<td><a name="show-version" id="show-version"></a><a href="#show-version-note">show version</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>$ fpc -v</td>
<td>$ gnatgcc -gnat12 <span style="white-space: pre-wrap;">--</span>version</td>
<td>$ psql <span style="white-space: pre-wrap;">--</span>version</td>
</tr>
<tr>
<th colspan="4"><a name="grammar-invocation" id="grammar-invocation"></a><a href="#grammar-invocation-note">grammar and invocation</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="hello-world" id="hello-world"></a><a href="#hello-world-note">hello word</a></td>
<td>$ cat hello.pas<br />
program Hello;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>WriteLn('hello world!');<br />
end.<br />
<br />
$ fpc hello.pas<br />
<br />
$ ./hello</td>
<td>$ cat hello.adb<br />
with Text_IO;<br />
use Text_IO;<br />
<br />
procedure Hello is<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>Put_Line("Hello World!");<br />
end Hello;<br />
<br />
$ gnatmake -gnat12 hello.adb<br />
<br />
$ ./hello<br />
Hello World!</td>
<td><span style="color: gray"><em>at psql prompt:</em></span><br />
<span style="white-space: pre-wrap;">></span> create or replace function hello()<br />
returns varchar as $$<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return 'Hello World!';<br />
end;<br />
$$ language plpgsql;<br />
CREATE FUNCTION<br />
<br />
<span style="white-space: pre-wrap;">></span> select hello();<br />
hello<br />
<span style="white-space: pre-wrap;">--------------</span><br />
Hello World!<br />
(1 row)</td>
</tr>
<tr>
<td><a name="file-suffixes" id="file-suffixes"></a><a href="#file-suffixes-note">file suffixes</a></td>
<td>foo.pas<br />
foo.o<br />
foo</td>
<td><span style="color: gray"><em>source:</em></span><br />
foo.ads: specification<br />
foo.adb: body<br />
<br />
<span style="color: gray"><em>compiler generated:</em></span><br />
foo.adi: compilation information<br />
foo.o: object<br />
foo</td>
<td></td>
</tr>
<tr>
<td><a name="source-code-encoding" id="source-code-encoding"></a><a href="#source-code-encoding-note">source code encoding</a></td>
<td></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> default is ISO 8859-1:</span><br />
pragma Wide_Character_Encoding(UTF8)</td>
<td></td>
</tr>
<tr>
<td><a name="eol-comment" id="eol-comment"></a><a href="#eol-comment-note">end-of-line comment</a></td>
<td><span style="color: gray"><em>not ISO Pascal:</em></span><br />
<span style="white-space: pre-wrap;">//</span> <span style="color: gray"><em>comment line</em></span><br />
<span style="white-space: pre-wrap;">//</span> <span style="color: gray"><em>another line</em></span></td>
<td><span style="white-space: pre-wrap;">--</span> <span style="color: gray"><em>comment line</em></span><br />
<span style="white-space: pre-wrap;">--</span> <span style="color: gray"><em>another line</em></span></td>
<td><span style="white-space: pre-wrap;">--</span> <span style="color: gray"><em>comment line</em></span><br />
<span style="white-space: pre-wrap;">--</span> <span style="color: gray"><em>another line</em></span></td>
</tr>
<tr>
<td><a name="multiple-line-comment" id="multiple-line-comment"></a><a href="#multiple-line-comment-note">multiple line comment</a></td>
<td>(* <span style="color: gray"><em>comment line</em></span><br />
<span style="color: gray"><em>another line</em></span> *)<br />
<br />
{ <span style="color: gray"><em>comment line</em></span><br />
<span style="color: gray"><em>another line</em></span> }</td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<th colspan="4"><a name="variables-expressions" id="variables-expressions"></a><a href="#variables-expressions-note">variables and expressions</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="case-sensitive-id" id="case-sensitive-id"></a><a href="#case-sensitive-id-note">are identifiers case sensitive</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>no</em></span></td>
<td><span style="color: gray"><em>no</em></span></td>
<td><span style="color: gray"><em>no</em></span></td>
</tr>
<tr>
<td><a name="declarations" id="declarations"></a><a href="#declarations-note">declare constant, type, and variable</a></td>
<td>program Foo;<br />
const<br />
<span style="white-space: pre-wrap;"> </span>PI: Real = 3.14159;<br />
type<br />
<span style="white-space: pre-wrap;"> </span>Customer = record<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Id: Integer;<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Name: String;<br />
<span style="white-space: pre-wrap;"> </span>end;<br />
var<br />
<span style="white-space: pre-wrap;"> </span>I: Integer;<br />
<span style="white-space: pre-wrap;"> </span>C: Customer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>body of program</em></span></td>
<td>procedure Foo is<br />
<span style="white-space: pre-wrap;"> </span>Pi: constant Float := 3.14;<br />
<span style="white-space: pre-wrap;"> </span>N: Integer;<br />
<span style="white-space: pre-wrap;"> </span>type Customer is Record<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Id: Integer;<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Name: String(1..4);<br />
<span style="white-space: pre-wrap;"> </span>end Record;<br />
<span style="white-space: pre-wrap;"> </span>C: Customer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>body of program</em></span></td>
<td>create type customer as (<br />
<span style="white-space: pre-wrap;"> </span>id integer,<br />
<span style="white-space: pre-wrap;"> </span>name text<br />
);<br />
<br />
create or replace function foo() returns void as $$<br />
declare<br />
<span style="white-space: pre-wrap;"> </span>pi numeric(10,4) = 3.14;<br />
<span style="white-space: pre-wrap;"> </span>i integer = 42;<br />
<span style="white-space: pre-wrap;"> </span>c customer%rowtype;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return;<br />
end $$ language plpgsql;</td>
</tr>
<tr>
<td><a href="#block">block with local scope</a></td>
<td></td>
<td>declare<br />
<span style="white-space: pre-wrap;"> </span>N: Integer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>N := 7;<br />
end;</td>
<td>declare<br />
<span style="white-space: pre-wrap;"> </span>i integer := 3;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>raise notice 'i is %', i;<br />
end;</td>
</tr>
<tr>
<td><a name="assignment" id="assignment"></a><a href="#assignment-note">assignment</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>X := 1;</td>
<td>X := 1;</td>
<td>x = 1;</td>
</tr>
<tr>
<td><a name="pointer-declaration" id="pointer-declaration"></a><a href="#pointer-declaration-note">pointer declaration</a></td>
<td>IP: ^Integer;</td>
<td>type Integer_Ptr is access Integer;<br />
Ip: Integer_Ptr;</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="allocate-heap" id="allocate-heap"></a><a href="#allocate-heap-note">allocate heap</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>new(IP);</td>
<td>Ip := new Integer;</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="free-heap" id="free-heap"></a><a href="#free-heap-note">free heap</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>dispose(IP);</td>
<td>with Ada.Unchecked_Deallocation;<br />
<br />
procedure Free_Example is<br />
<span style="white-space: pre-wrap;"> </span>type Integer_Ptr is access Integer;<br />
<span style="white-space: pre-wrap;"> </span>procedure Free is new Ada.Unchecked_Deallocation(<br />
<span style="white-space: pre-wrap;"> </span><span style="white-space: pre-wrap;"> </span>Integer, Integer_Ptr);<br />
<span style="white-space: pre-wrap;"> </span>Ip: Integer_Ptr;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>Ip := new Integer;<br />
<span style="white-space: pre-wrap;"> </span>Ip.all := 7;<br />
<span style="white-space: pre-wrap;"> </span>Free(Ip);<br />
end Hello;</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="deference-pointer" id="deference-pointer"></a><a href="#dereference-pointer-note">dereference pointer</a></td>
<td>IP^ := 7;<br />
Ans := 6 * IP^;</td>
<td>Ip.all := 7;<br />
ans := 6 * Ip.all;</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="null" id="null"></a><a href="#null-note">null literal</a></td>
<td><span style="color: gray"><em>can only be assigned to pointers:</em></span><br />
nil</td>
<td><span style="color: gray"><em>can only be assigned to access types:</em></span><br />
null</td>
<td>NULL</td>
</tr>
<tr>
<td><a name="null-test" id="null-test"></a><a href="#null-test-note">null test</a></td>
<td>X = nil</td>
<td>X = null</td>
<td>x is NULL<br />
<span style="color: gray">x = NULL <em>is always false</em></span></td>
</tr>
<tr>
<td><a name="coalesce" id="coalesce"></a><a href="#coalesce-note">coalesce</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td></td>
<td>7 + coalesce(x, 0)</td>
</tr>
<tr>
<td><a name="nullif" id="nullif"></a><a href="#nullif-note">nullif</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td></td>
<td>nullif(x, 0)</td>
</tr>
<tr>
<td><a name="conditional-expr" id="conditional-expr"></a><a href="#conditional-expr-note">conditional expression</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> Ada 2012:</span><br />
(if X > 0 then X else -X)</td>
<td>case when x > 0 then x else -x end</td>
</tr>
<tr>
<th colspan="4"><a name="arithmetic-logic" id="arithmetic-logic"></a><a href="#arithmetic-logic-note">arithmetic and logic</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="boolean-type" id="boolean-type"></a><a href="#boolean-type-note">boolean type</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>Boolean</td>
<td>Boolean</td>
<td>BOOL BOOLEAN</td>
</tr>
<tr>
<td><a name="true-false" id="true-false"></a><a href="#true-false-note">true and false</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>True False</td>
<td>True False</td>
<td>TRUE FALSE</td>
</tr>
<tr>
<td><a name="falsehoods" id="falsehoods"></a><a href="#falsehoods-note">falsehoods</a></td>
<td>False<br />
<span style="color: gray"><em>non booleans cause error in boolean context</em></span></td>
<td>False<br />
<span style="color: gray"><em>non booleans cause error in boolean context</em></span></td>
<td>FALSE NULL 0<br />
<span style="color: gray"><em>strings and floats cause error in boolean context</em></span></td>
</tr>
<tr>
<td><a name="logical-op" id="logical-op"></a><a href="#logical-op-note">logical operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>and or xor not</td>
<td>and or xor not</td>
<td>AND OR <span style="color: gray"><em>none</em></span> NOT</td>
</tr>
<tr>
<td><a name="short-circuit-op" id="short-circuit-op"></a><a href="#short-circuit-op-note">short circuit operators</a></td>
<td><span style="color: gray">{ in Free Pascal 'and' and 'or' also short circuit }</span><br />
and_then<br />
or_else</td>
<td>and then<br />
or else</td>
<td>AND<br />
OR</td>
</tr>
<tr>
<td><a name="int-type" id="int-type"></a><a href="#int-type-note">integer type</a></td>
<td>Integer</td>
<td>Integer</td>
<td>smallint: <span style="color: gray"><em>2 bytes</em></span><br />
integer: <span style="color: gray"><em>4 bytes</em></span><br />
bigint: <span style="color: gray"><em>8 bytes</em></span></td>
</tr>
<tr>
<td><a name="float-type" id="float-type"></a><a href="#float-type-note">float type</a></td>
<td>Real</td>
<td>Float</td>
<td>real: <span style="color: gray"><em>4 bytes</em></span><br />
double precision: <span style="color: gray"><em>8 bytes</em></span></td>
</tr>
<tr>
<td><a name="fixed-type" id="fixed-type"></a><a href="#fixed-type-note">fixed type</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>four fractional digits:</em></span><br />
Currency</td>
<td>type Currency is delta 0.01 digits 18;<br />
Amt: Currency;<br />
<br />
Amt := 3.99;</td>
<td>numeric(<span style="color: gray"><em>digits</em></span>, <span style="color: gray"><em>fractional_digits</em></span>)</td>
</tr>
<tr>
<td><a name="relational-op" id="relational-op"></a><a href="#relational-op-note">relational operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="white-space: pre-wrap;">=</span> <> < > <= >=</td>
<td><span style="white-space: pre-wrap;">=</span> /= < > <= >=</td>
<td><span style="white-space: pre-wrap;">=</span> != <span style="color: gray"><em>also:</em></span> <> < > <= >=</td>
</tr>
<tr>
<td><a name="min-max" id="min-max"></a><a href="#min-max-note">min and max</a></td>
<td>uses Math;<br />
<br />
Min(1, 2)<br />
Max(1, 2)<br />
MinIntValue([1, 2, 3])<br />
MaxIntValue([1, 2, 3])<br />
MinValue([1.0, 2.0, 3.0])<br />
MaxValue([1.0, 2.0, 3.0])</td>
<td>Integer'Min(1, 2)<br />
Integer'Max(1, 2)<br />
Integer'Min(1, Integer'Min(2, 3))<br />
Integer'Max(1, Integer'Max(2, 3))<br />
Float'Min(1.0, Float'Min(2.0, 3.0))<br />
Float'Max(1.0, Float'Max(2.0, 3.0))</td>
<td>least(1,2,3)<br />
greatest(1,2,3)</td>
</tr>
<tr>
<td><a name="arith-op" id="arith-op"></a><a href="#arith-op-note">arithmetic operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>+ - * / div mod</td>
<td>+ - * <span style="color: gray"><em>none</em></span> / mod <span style="color: gray"><em>or</em></span> rem</td>
<td>+ - * <span style="color: gray"><em>none</em></span> / %</td>
</tr>
<tr>
<td><a name="int-div" id="int-div"></a><a href="#int-div-note">integer division</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>7 div 3</td>
<td>7 / 3</td>
<td>7 / 3</td>
</tr>
<tr>
<td><a name="int-div-zero" id="int-div-zero"></a><a href="#int-div-zero-note">integer division by zero</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>raises</em></span> EDivByZero</td>
<td><span style="color: gray"><em>raises</em></span> Constraint_Error</td>
<td><span style="color: gray"><em>raises</em></span> division_by_zero</td>
</tr>
<tr>
<td><a name="float-div" id="float-div"></a><a href="#float-div-note">float division</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>7 / 3</td>
<td>Float(7) / Float(3)</td>
<td>7 * 1.0 / 3</td>
</tr>
<tr>
<td><a name="float-div-zero" id="float-div-zero"></a><a href="#float-div-zero-note">float division by zero</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>+Inf</td>
<td><span style="color: gray"><em>raises</em></span> Constraint_Error</td>
<td><span style="color: gray"><em>raises</em></span> division_by_zero</td>
</tr>
<tr>
<td><a name="power" id="power"></a><a href="#power-note">power</a></td>
<td>uses Math;<br />
<br />
Power(2, 16);</td>
<td>2 ** 16</td>
<td>2 ^ 16</td>
</tr>
<tr>
<td><a name="sqrt" id="sqrt"></a><a href="#sqrt-note">sqrt</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>Sqrt(2)</td>
<td>with Ada.Numerics.Elementary_Functions;<br />
use Ada.Numerics.Elementary_Functions;<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">--</span> no implicit type conversion of Integer to Float:</span><br />
Sqrt(2.0)</td>
<td>sqrt(2)</td>
</tr>
<tr>
<td><a name="sqrt-negative-one" id="sqrt-negative-one"></a><a href="#sqrt-negative-one-note">sqrt -1</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>raises</em></span> EInvalidOp</td>
<td><span style="color: gray"><em>raises</em></span> Ada.Numerics.Argument_Error</td>
<td><span style="color: gray"><em>raises</em></span> invalid_argument_for_power_function</td>
</tr>
<tr>
<td><a name="transcendental-func" id="transcendental-func"></a><a href="#transcendental-func-note">transcendental functions</a></td>
<td>uses Math;<br />
<br />
Exp Ln Sin Cos Tan ArcSin ArcCos ArcTan ArcTan2</td>
<td>with Ada.Numerics.Elementary_Functions;<br />
use Ada.Numerics.Elementary_Functions;<br />
<br />
Exp Log Sin Cos Tan Arcsin Arccos <span style="color: gray"><em>none</em></span> Arctan</td>
<td>exp ln sin cos tan asin acos atan atan2</td>
</tr>
<tr>
<td><a name="float-truncation" id="float-truncation"></a><a href="#float-truncation-note">float truncation</a><br />
<span style="color: gray"><em>towards zero, to nearest integer, down, up</em></span></td>
<td>uses Math;<br />
<br />
Trunc Round Floor Ceil</td>
<td><span style="color: gray"><em>return Float:</em></span><br />
<span style="color: gray"><em>??</em></span> Float'Rounding Float'Floor Float'Ceiling</td>
<td>trunc round floor ceil</td>
</tr>
<tr>
<td><a name="absolute-val" id="absolute-val"></a><a href="#absolute-val-note">absolute value</a><br />
<span style="color: gray"><em>and sign</em></span></td>
<td>Abs(-7)<br />
<span style="color: gray"><em>none</em></span></td>
<td>Abs(-7)<br />
<span style="color: gray"><em>none</em></span></td>
<td>abs(-7)<br />
sign(-7)</td>
</tr>
<tr>
<td><a name="int-overflow" id="int-overflow"></a><a href="#int-overflow-note">integer overflow</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>modular arithmetic</em></span></td>
<td><span style="color: gray"><em>modular arithmetic</em></span></td>
<td><span style="color: gray"><em>raises</em></span> numeric_value_out_of_range</td>
</tr>
<tr>
<td><a name="float-overflow" id="float-overflow"></a><a href="#float-overflow-note">float overflow</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td><span style="color: gray"><em>raises</em></span> EOverflow</td>
<td>+Inf<span style="white-space: pre-wrap;">*******</span></td>
<td><span style="color: gray"><em>raises</em></span> numeric_value_out_of_range</td>
</tr>
<tr>
<td><a name="random-num" id="random-num"></a><a href="#random-num-note">random number</a><br />
<span style="color: gray"><em>integer, float</em></span></td>
<td>Random(100)<br />
Random</td>
<td>with Ada.Numerics.Float_Random;<br />
with Ada.Numerics.Discrete_Random;<br />
use Ada.Numerics;<br />
<br />
procedure Foo is<br />
<span style="white-space: pre-wrap;"> </span>type Rand_Range is range 0..99;<br />
<span style="white-space: pre-wrap;"> </span>package Rand_Int is new Discrete_Random(Rand_Range);<br />
<span style="white-space: pre-wrap;"> </span>IG: Rand_Int.Generator;<br />
<span style="white-space: pre-wrap;"> </span>FG: Float_Random.Generator;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use</em></span> Rand_Int.Random(IG)<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>use</em></span> Float_Random.Random(FG)</td>
<td>floor(100 * random())<br />
random()</td>
</tr>
<tr>
<td><a name="bit-op" id="bit-op"></a><a href="#bit-op-note">bit operators</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>shl shr and or xor not</td>
<td></td>
<td><span style="white-space: pre-wrap;"> << >> </span> & | ^ ~</td>
</tr>
<tr>
<th colspan="4"><a name="strings" id="strings"></a><a href="#strings-note">strings</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a href="#string-literal">string literal</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>'Don''t say "foo"'</td>
<td>"Don't say ""foo"""</td>
<td>'Don''t say "foo"'</td>
</tr>
<tr>
<td><a href="#fixed-string-type">fixed length string type</a></td>
<td></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> error unless string length is 6</span><br />
String(1..6)<br />
Wide_Wide_String(1..6)</td>
<td><span style="color: gray"><em>pads length to</em> n <em>with spaces:</em></span><br />
char(<span style="color: gray"><em>n</em></span>)</td>
</tr>
<tr>
<td><a href="#bounded-string-type">bounded length string type</a></td>
<td></td>
<td></td>
<td><span style="color: gray"><em>error if</em> n <em>exceeded:</em></span><br />
varchar(<span style="color: gray"><em>n</em></span>)</td>
</tr>
<tr>
<td><a href="#unbounded-string-type">unbounded length string type</a></td>
<td></td>
<td>with Ada.Strings.Unbounded;<br />
<br />
Ada.Strings.Unbounded.Unbounded_String</td>
<td>text</td>
</tr>
<tr>
<td><a href="#character-type">character type</a></td>
<td></td>
<td>Character <span style="color: gray"><em>1 byte</em></span><br />
Wide_Wide_Character <span style="color: gray"><em>4 bytes</em></span></td>
<td>char(1)</td>
</tr>
<tr>
<td><a href="#chr-ord">chr and ord</a></td>
<td>Chr(65)<br />
Ord('A')</td>
<td></td>
<td>chr(65)<br />
ascii('A')</td>
</tr>
<tr>
<td><a href="#string-concatenate">concatenate</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>'hello' + ' world'</td>
<td>"hello" & " world"</td>
<td>'hello' <span style="white-space: pre-wrap;">||</span> ' world'</td>
</tr>
<tr>
<td><a href="#string-length">length</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>Length('hello')</td>
<td></td>
<td>length('hello')</td>
</tr>
<tr>
<td><a href="#extract-substring">extract substring</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>Copy(S, 1, 4)</td>
<td></td>
<td>substr('hello', 1, 4)</td>
</tr>
<tr>
<td><a href="#index-substring">index of substring</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td>Pos('hell', 'hello')</td>
<td></td>
<td>strpos('hello', 'hell')</td>
</tr>
<tr>
<td><a href="#case-manipulation">case manipulation</a></td>
<td>uses SysUtils;<br />
<br />
UpperCase('hello')<br />
LowerCase('HELLO')</td>
<td></td>
<td>upper('hello')<br />
lower('HELLO')</td>
</tr>
<tr>
<td><a href="#strip">strip</a></td>
<td>Trim(' foo ')<br />
TrimLeft(' foo')<br />
TrimRight('foo ')</td>
<td></td>
<td>trim(' foo ')<br />
ltrim(' foo')<br />
rtrim('foo ')</td>
</tr>
<tr>
<td><a href="#pad">pad on left, pad on right</a></td>
<td></td>
<td></td>
<td>lpad('foo', 10)<br />
rpad('foo', 10)</td>
</tr>
<tr>
<td><a href="#string-number-conversion">convert string to number</a></td>
<td>uses SysUtils;<br />
<br />
7 + StrToInt('12')<br />
73.9 + StrToFloat('.037')</td>
<td></td>
<td><span style="color: gray"><em>arithmetic operators automatically convert strings to numbers</em></span><br />
cast('12' as int)<br />
cast('3.14') as real)</td>
</tr>
<tr>
<td><a href="#number-string-conversion">convert number to string</a></td>
<td>uses SysUtils;<br />
<br />
'value: ' + IntToStr(8)<br />
'value: ' + FloatToStr(3.14)</td>
<td>Integer'Image(8)<br />
Float'Image(3.14)</td>
<td><span style="color: gray"><em>double pipe operator</em> <span style="white-space: pre-wrap;">||</span> <em>converts numbers operands to strings</em></span><br />
cast(8 to text)<br />
cast(3.14 to text)</td>
</tr>
<tr>
<th colspan="4"><a name="regexes" id="regexes"></a><a href="#regexes-note">regular expressions</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="match" id="match"></a><a href="#match-note">match</a></td>
<td></td>
<td></td>
<td>select *<br />
from pwt<br />
where name similar to 'r[a-z]+';</td>
</tr>
<tr>
<td><a name="substitute" id="substitute"></a><a href="#substitute-note">substitute</a><br />
<span style="white-space: pre-wrap;"> </span></td>
<td></td>
<td></td>
<td>select regexp_replace('foo bar', 'bar$', 'baz');</td>
</tr>
<tr>
<td><a name="extract-subgroup" id="extract-subgroup"></a><a href="#extract-subgroup-note">extract subgroup</a></td>
<td></td>
<td></td>
<td>select (regexp_matches('foobar', '(f..)bar'))[1];</td>
</tr>
<tr>
<th colspan="4"><a name="dates-time" id="dates-time"></a><a href="#dates-time-note">dates and time</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="current-datetime" id="current-datetime"></a><a href="#current-datetime-note">current datetime</a></td>
<td></td>
<td>with Ada.Calendar;<br />
<br />
Now: Ada.Calendar.Time;<br />
<br />
Now := Ada.Calendar.Clock;</td>
<td>now()</td>
</tr>
<tr>
<td><a name="date-time-to-str" id="date-time-to-str"></a><a href="#date-time-to-str-note">datetime to string</a></td>
<td></td>
<td>with Ada.Calendar.Formating;<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">--</span> "2015-03-29 17:46:35":</span><br />
Ada.Calendar.Formatting.Image(Now);</td>
<td>to_char(now(), 'YYYY-MM-DD HH24:MI:SS')</td>
</tr>
<tr>
<td><a name="str-to-date-time" id="str-to-date-time"></a><a href="#str-to-date-time-note">string to datetime</a></td>
<td></td>
<td></td>
<td>to_timestamp('2011-09-26 00:00:47', 'YYYY-MM-DD HH24:MI:SS')</td>
</tr>
<tr>
<th colspan="4"><a name="arrays" id="arrays"></a><a href="#arrays-note">arrays</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a href="#declare-array">declare array</a></td>
<td>A: array[1..5] of Integer;</td>
<td>A: array(1..5) of Integer;</td>
<td>a int[];</td>
</tr>
<tr>
<td><a href="#array-length">array length</a></td>
<td></td>
<td>A'Last</td>
<td>array_length(a, 1)</td>
</tr>
<tr>
<td><a href="#array-access">array element access</a></td>
<td>A[1] := 3;</td>
<td>A(1) := 3;</td>
<td>a[1] = 3;</td>
</tr>
<tr>
<td><a href="#array-initialization">array initialization</a></td>
<td></td>
<td>A: array(1..5) of Integer := (1, 3, 5, 2, 4);</td>
<td>a int[] = array[1, 3, 5, 2, 4];</td>
</tr>
<tr>
<td><a href="#array-slice">array slice</a></td>
<td></td>
<td>A(3..4) := A(1..2);</td>
<td>a[1:2]<br />
<span style="color: gray"><em>can assign to slice in</em> UPDATE <em>statement but not in assignment</em></span></td>
</tr>
<tr>
<td><a href="#array-out-of-bounds">array out of bounds behavior</a></td>
<td><span style="color: gray"><em>undefined; free pascal permits out of bounds memory access</em></span></td>
<td><span style="color: gray"><em>compiler warning; raises</em></span> Constraint_Error <span style="color: gray"><em>at runtime</em></span></td>
<td>NULL</td>
</tr>
<tr>
<td><a href="#declare-multidimensional-array">declare multidimensional array</a></td>
<td></td>
<td></td>
<td>a integer[][];</td>
</tr>
<tr>
<td><a href="#multidimensional-array-access">multidimensional array access</a></td>
<td></td>
<td></td>
<td>a[2][3] = 7;</td>
</tr>
<tr>
<th colspan="4"><a name="user-defined-types" id="user-defined-types"></a><a href="#user-defined-types-note">user-defined types</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="type-synonym" id="type-synonym"></a><a href="#type-synonym-note">type synonym</a></td>
<td>type<br />
<span style="white-space: pre-wrap;"> </span>CustomerId = Integer;</td>
<td></td>
<td></td>
</tr>
<tr>
<td><a name="enumerated-type" id="enumerated-type"></a><a href="#enumerated-type-note">enumerated type</a></td>
<td>type<br />
<span style="white-space: pre-wrap;"> </span>Direction = (North, South, East, West);<br />
var<br />
<span style="white-space: pre-wrap;"> </span>Wind: Direction = North;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>WriteLn(Wind); <span style="color: gray">{ prints 'North' }</span></td>
<td></td>
<td>create type direction as enum ( 'north', 'south', 'east', 'west');<br />
<br />
create table wind ( origin direction, speed_mph real );<br />
<br />
insert into wind values ( 'north', 12 );</td>
</tr>
<tr>
<td><a href="#define-record-type">define record type</a></td>
<td><span style="color: gray"><em>in type section:</em></span><br />
Customer = record<br />
<span style="white-space: pre-wrap;"> </span>Id: Integer;<br />
<span style="white-space: pre-wrap;"> </span>Name: String;<br />
end;</td>
<td>type Customer is record<br />
<span style="white-space: pre-wrap;"> </span>Id: Integer;<br />
<span style="white-space: pre-wrap;"> </span>Name: String(1..4);<br />
end record;</td>
<td>create type customer as (<br />
<span style="white-space: pre-wrap;"> </span>id integer,<br />
<span style="white-space: pre-wrap;"> </span>name text<br />
);</td>
</tr>
<tr>
<td><a href="#declare-record">declare record</a></td>
<td>C: Customer;</td>
<td>C: Customer := (17, "John");</td>
<td>declare<br />
<span style="white-space: pre-wrap;"> </span>c customer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>c = (17,'John');<br />
<span style="white-space: pre-wrap;"> </span><span style="color: gray"><em>code which uses</em> c</span></td>
</tr>
<tr>
<td><a href="#record-member-access">record member access</a></td>
<td>C.Name := 'Fred';</td>
<td>C.Name := 'Fred';</td>
<td>c.name = 'Fred'</td>
</tr>
<tr>
<td><a href="#record-block">record block</a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<th colspan="4"><a name="generic-types" id="generic-types"></a><a href="#generic-types-note">generic types</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<th colspan="4"><a name="functions" id="functions"></a><a href="#functions-note">functions</a></th>
</tr>
<tr>
<th></th>
<th><a href="#pascal">pascal</a></th>
<th><a href="#ada">ada</a></th>
<th><a href="#plpgsql">plpgsql</a></th>
</tr>
<tr>
<td><a name="decl-func" id="decl-func"></a><a href="#decl-func-note">declare</a></td>
<td></td>
<td>function Add(M: Integer; N: Integer) return Integer;</td>
<td></td>
</tr>
<tr>
<td><a name="def-func" id="def-func"></a><a href="#def-func-note">define</a></td>
<td>function Add(M: Integer; N: Integer): Integer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>Result := M + N;<br />
end;</td>
<td>function Add(M: Integer; N: Integer) return Integer is<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return M + N;<br />
end Add;</td>
<td>create function add(i int, j int)<br />
returns int as $$<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return i + j;<br />
end;<br />
$$ language plpgsql;</td>
</tr>
<tr>
<td><a name="call-func" id="call-func"></a><a href="#call-func-note">call</a></td>
<td>Sum := Add(7, 3);</td>
<td>Sum := Add(7, 3);</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> in select clause:</span><br />
select add(1, 2);<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">--</span> in where clause:</span><br />
select * from cust where id = add(1, 2);<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">--</span> inside PL/pgSQL functions can be used wherever<br />
<span style="white-space: pre-wrap;">--</span> expressions are permitted. Can be used as a statement<br />
<span style="white-space: pre-wrap;">--</span> with perform:</span><br />
perform add(1, 2);</td>
</tr>
<tr>
<td><a name="undef-func" id="undef-func"></a><a href="#undef-func-note">undefine function</a></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><em>none</em></span></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> parameter types are required:</span><br />
drop function foo(int, int);</td>
</tr>
<tr>
<td><a name="no-retval" id="no-retval"></a><a href="#no-retval-note">no return value</a></td>
<td>procedure Message(Msg: String);<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>WriteLn(Msg);<br />
end;</td>
<td>procedure Message(Msg: String) is<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>Text_IO.Put_Line(Msg);<br />
end Message;</td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span> declare return type as void:</span><br />
create or replace function message(msg text)<br />
returns void as $$<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>raise notice '%', msg;<br />
end;<br />
$$ language plpgsql;</td>
</tr>
<tr>
<td><a name="pass-by-ref" id="pass-by-ref"></a><a href="#pass-by-ref-note">pass by reference</a></td>
<td><span style="color: gray">{ declare parameter with var }</span><br />
procedure Incr(var N: Integer);<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>N := N + 1;<br />
end;<br />
<br />
var<br />
<span style="white-space: pre-wrap;"> </span>I: Integer;<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>I := 3;<br />
<span style="white-space: pre-wrap;"> </span>Incr(I);<br />
<span style="white-space: pre-wrap;"> </span>WriteLn(I); <span style="color: gray">{ prints 4 }</span><br />
end.</td>
<td>procedure Incr(N: in out Integer) is<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>N := N + 1;<br />
end Incr;<br />
<br />
I := 3;<br />
Incr(3);</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="pass-uninitialized-by-ref" id="pass-uninitialized-by-ref"></a><a href="#pass-uninitialized-by-ref-note">pass uninitialized variable by reference</a></td>
<td></td>
<td>procedure Get_Pi(Pi: out Float) is<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>Pi := 3.14;<br />
end Get_Pi;</td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="nested-func" id="nested-func"></a><a href="#nested-func-note">nested function</a></td>
<td></td>
<td></td>
<td><span style="color: gray"><em>none</em></span></td>
</tr>
<tr>
<td><a name="overloaded-func" id="overloaded-func"></a><a href="#overloaded-func-note">overloaded function</a></td>
<td></td>
<td></td>
<td><span style="color: gray"><span style="white-space: pre-wrap;">--</span>integer version:</span><br />
create or replace function add(m int, n int)<br />
returns int as $$<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return m + n;<br />
end;<br />
$$ language plpgsql;<br />
<br />
<span style="color: gray"><span style="white-space: pre-wrap;">--</span>float version:</span><br />
create or replace function add(x real, y real)<br />
returns real as $$<br />
begin<br />
<span style="white-space: pre-wrap;"> </span>return x + y;<br />
end;<br />
$$ language plpgsql;</td>
</tr>
<tr>
<td><a name="forward-decl" id="forward-decl"></a><a href="#forward-decl-note">forward declaration</a></td>
<td>function Odd(N: Integer): Boolean;<br />
Forward;<br />
<br />
function Even(N: Integer): Boolean;<br />