summaryrefslogtreecommitdiff
path: root/t/trivial.pas
blob: fb97adc17389c67d61777fe3c9ecc00acd568b13 (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
program a;
const
    gb=true;
    gc='a';
    gi=5;
    gr=3.1415;
    gs='nagi';
var
    gv:integer;

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
    gv:=1;
    name_test_global:=gv+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
    gv:integer;
begin
    gv:=0;
    name_test_local:=gv+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;