summaryrefslogtreecommitdiff
path: root/t/trivial.pas
blob: e71d0371e5c8c907d67fb459242233a5862f2282 (plain)
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
program a;
const
    gb=true;
    gc='a';
    gi=5;
    gr=3.1415;
    gs='nagi';
var
    gvi:integer;
    gvf:real;
    gvp:^char;
    gva:array[0..4] of integer;

(**)function {}comment_test({}):integer{};(**)
{comment}
begin{
begin(* this is in a comment *)
end;
}
comment_test:=1
(*
{begin end; also in a comment}
*)(**)(****)(* *)(***)
end;

function arithmetic_test(a:integer;b:char;c:real):integer;
var
    d:integer;
    e:char;
    f:real;
begin
    a:=1;   d:=-2;
    b:='B'; e:='\x46';
    c:=1.5; f:=-3.14;

    a:=a+b-c+d;
    b:=b*c-d*e;
    c:=c+d*e+f;

    b:=1; e:=2;
    a:=1/2+b/e;
    d:=1 div 2 + b div e;

    a:=a and e or b xor d;
    d:=(a shl 3) xor (not a shr b);
    arithmetic_test:=a+d
end;

function string_test():char;
var 
    s:string;
    a,b:char;
begin
    s:='hello world';
    a:=s[3];
    b:=s[6];
    string_test:=s[b-a]
end;

function array_test():integer;
var
    ai:array['a'..'z'] of integer;
    ac:array[0..25] of char;
    i:integer;
    c:char;
begin
    i:=0;
    c:='z';
    repeat
    begin
        ac[i]:=c;
        ai[c]:=i;
        i:=i+1;
        c:=c-1
    end until i>25;
    array_test:=ai['f']-ac[5];
end;

function pointer_test():integer;
var
    a,b:integer;
    c:array[0..100] of integer;
    p,q:^integer;
    s:string;
    ps:^char;
begin
    a:=1;
    p:=@c[0];
    q:=@c[4];
    b:=q-p;
    p[0]:=a;
    q[b]:=a;
    (p+1)[0]:=a+1;
    b:=p[0]+p[1];
    s:='testabcd';
    ps:=s+2;
    pointer_test:=b+ps[3];
end;

function recursion_test(x:integer):integer;
begin
    if (x=0) or (x=1) then
        recursion_test:=1
    else
        recursion_test:=recursion_test(x-1)+recursion_test(x-2)
end;

function branch_test(a:integer):integer;
var
    b,c,d:integer;
begin
    b:=3;
    c:=0;
    d:=4;
    if a<b then
        c:=-1
    else
        if a>b then
            c:=1;
    while b>0 do
    begin
        while d>0 do
        begin
            d:=d-1;
            if d=3 then
            begin
                d:=2;
                break
            end;
            a:=a*d+b;
        end;
        b:=b-1
    end;
    repeat
    begin
        repeat
        begin
            d:=d+1;
            if d=3 then
            begin
                d:=4;
                continue;
            end;
            a:=a*c+d;
        end
        until d>5;
        a:=a*c;
        b:=b+1
    end
    until b>5;
    branch_test:=a;
end;

function name_test_global():integer;
begin
    gvi:=1;
    name_test_global:=gvi+gb+gc+gi+gr+gs[3];
end;

function name_test_local():integer;
const
    gb=false;
    gc='A';
    gi=10;
    gr=2.71828;
    gs='Hanjie Wu';
var
    gvi:integer;
begin
    gvi:=0;
    name_test_local:=gvi+gb+gc+gi+gr+gs[3];
end;

function name_test():boolean;
begin
    name_test:=name_test_global()<>name_test_local()
end;

function void_test():void;
var
    a,b:^void;
begin
end;

function alias_test():integer;
type
    a1=integer;
    a2=array[0..9] of a1;
    a3=array[0..9] of a2;
    rt=record
        m1:integer;
        m2:array[1..10] of integer;
        m3:real
    end;
var
    i:a1;
    arr:a3;
    p:^a2;
    arr2:array[0..9] of array[0..99] of array[0..9] of integer;
    r:rt;
begin
    i:=4;
    arr[i][i]:=i;
    p:=@arr[i];
    p[0][i]:=p[0][i]+1;
    arr2[0][1][2]:=123;
    alias_test:=arr2[0][1][2]+arr[i][i]*i;
end;

function record_test():integer;
type
    t1=record
        m1:integer;
        m2:char;
        m3:array[2..6] of real;
        m4: array[1..2] of record
            m1:array[0..2] of integer;
            m2:boolean
        end
    end;
var
    a:t1;
    pa:^t1;
begin
    a.m1:=2;
    a.m2:='t';
    a.m3[2]:=6.66;
    pa:=@a;
    pa[0].m3[6]:=7.77;
    pa^.m4[1].m1[2]:=1234;
    record_test:=a.m3[6]+pa^.m3[a.m1]+a.m4[1].m1[2];
end;

function case_test(a:integer):integer;
begin
    case a of
    1,2,3:case_test:=111;
    4,5,6:case a of
            4:case_test:=222;
            5,6:case a of
                5:case_test:=333;
                6:case_test:=555;
            end;
        end;
    else
        case_test:=666;
    end
end;