-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide.pas
executable file
·165 lines (151 loc) · 8.98 KB
/
guide.pas
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
{java -cp classes Pascal execute guide.pas : Run this on terminal}
PROGRAM showcase (output);
BEGIN
dwriteln('+---------------------------------------------------+');
dwriteln('| Welcome to My Pascal Compiler |');
dwriteln('|---------------------------------------------------|');
dwriteln('| This program showcases the features of the custom |');
dwriteln('| Pascal compiler, including predefined functions, |');
dwriteln('| procedures, and various Pascal constructs. |');
dwriteln('| To run this program, run the following: |');
dwriteln('| java -cp classes Pascal execute guide.pas |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Hello World }
dwriteln('+---------------------------------------------------+');
dwriteln('|-----------------Hello World Example--------------|');
dwriteln('| PROGRAM hello (output); |');
dwriteln('| BEGIN {hello} |');
dwriteln('| dwriteln(''Hello, world.''); |');
dwriteln('| END; |');
dwriteln('| END {hello}. |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: If-Else }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------If-Else Example-----------------|');
dwriteln('| IF i = 1 THEN f := 10 |');
dwriteln('| ELSE IF i = 2 THEN f := 20 |');
dwriteln('| ELSE IF i = 3 THEN t := 30 |');
dwriteln('| ELSE IF i = 4 THEN f := 40 |');
dwriteln('| ELSE f := -1; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Arrays }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------Array Example-------------------|');
dwriteln('| Visit: arraytest.pas for the full array example. |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Record }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------Record Example------------------|');
dwriteln('| rec1 = RECORD |');
dwriteln('| i : integer; |');
dwriteln('| r : real; |');
dwriteln('| b1, b2 : boolean; |');
dwriteln('| c : char |');
dwriteln('| END; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: For Loops }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------For Loop Example-----------------|');
dwriteln('| FOR k := j TO 5 DO n := k; |');
dwriteln('| |');
dwriteln('| FOR k := n DOWNTO 1 DO j := k; |');
dwriteln('| |');
dwriteln('| FOR i := 1 TO 2 DO |');
dwriteln('| BEGIN |');
dwriteln('| FOR j := 1 TO 3 DO |');
dwriteln('| BEGIN |');
dwriteln('| k := i*j |');
dwriteln('| END |');
dwriteln('| END |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: While Loop }
dwriteln('+---------------------------------------------------+');
dwriteln('|-----------------While Loop Example---------------|');
dwriteln('| WHILE root*root - number > 0.000001 DO |');
dwriteln('| BEGIN |');
dwriteln('| root := (number/root + root)/2 |');
dwriteln('| END |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Switch Case }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------Case Statement------------------|');
dwriteln('| CASE i+1 OF |');
dwriteln('| 1: j := i; |');
dwriteln('| -8: j := 8*i; |');
dwriteln('| 5, 7, 4: j := 574*i; |');
dwriteln('| END; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Function and Procedure }
dwriteln('+---------------------------------------------------+');
dwriteln('|-------------Function and Procedure Example--------|');
dwriteln('| FUNCTION root(x : real) : real; |');
dwriteln('| VAR r : real; |');
dwriteln('| BEGIN |');
dwriteln('| r := 1; |');
dwriteln('| REPEAT |');
dwriteln('| r := (x/r + r)/2; |');
dwriteln('| UNTIL abs(x/sqr(r) - 1) < epsilon; |');
dwriteln('| root := r; |');
dwriteln('| END; |');
dwriteln('| |');
dwriteln('| PROCEDURE print(n : integer; root : real); |');
dwriteln('| BEGIN |');
dwriteln('| dwriteln(''The square root of '', |');
dwriteln('| number:4, '' is '', root:8:4); |');
dwriteln('| END; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Read and Write }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------Read and Write-------------------|');
dwriteln('| write(''Type an integer: ''); |');
dwriteln('| readln(i); |');
dwriteln('| writeln(''You typed: '', i); |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Repeat Statement }
dwriteln('+---------------------------------------------------+');
dwriteln('|-----------------Repeat Statement------------------|');
dwriteln('| REPEAT |');
dwriteln('| r := (n/r + r)/2 |');
dwriteln('| UNTIL r*r - n < epsilon; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Subrange }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------------Subrange Example-----------------|');
dwriteln('| TYPE |');
dwriteln('| subrange = 10..20; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Example: Nested Procedures }
dwriteln('+---------------------------------------------------+');
dwriteln('|----------------Nested Procedures------------------|');
dwriteln('| PROCEDURE nested(VAR n, m : integer); |');
dwriteln('| CONST ten = 10; |');
dwriteln('| TYPE subrange = five..ten; |');
dwriteln('| VAR a, b, c : integer; s : subrange; |');
dwriteln('| PROCEDURE deeply; |');
dwriteln('| VAR w : real; |');
dwriteln('| BEGIN |');
dwriteln('| w := i; |');
dwriteln('| nested(a, m); |');
dwriteln('| w := forwarded(b, w); |');
dwriteln('| END; |');
dwriteln('| END; |');
dwriteln('+---------------------------------------------------+');
dwriteln;
{ Final Message }
dwriteln('+---------------------------------------------------+');
dwriteln('|------------End of Showcase Program----------------|');
dwriteln('+---------------------------------------------------+');
dwriteln;
END.