Download Language development case study (Pdf, 133 pages)

Transcript
68 A SINGLE PASS COMPILER FOR MINILOOP
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:
(*******************************************************************************
*
* RDP release 1.50 by Adrian Johnstone ([email protected]) 20 December 1997
*
* testloop.m - a piece of Miniloop source to test the Miniloop compiler
*
* This file may be freely distributed. Please mail improvements to the author.
*
*******************************************************************************)
int a=3+4, b=1;
print("a is ", a, "\n");
b=a*2;
print("b is ", b, ", -b is ", -b, "\n");
print(a, " cubed is ", a**3, "\n");
int z = a;
if z==a then print ("z equals a\n") else print("z does not equal a\n");
z=a - 3;
if z==a then print ("z equals a\n") else print("z does not equal a\n");
a = 3;
while a > 0 do
begin
print("a is ", a, "\n");
a = a - 1
end;
(* End of testloop.m *)
Figure 7.1 An example miniloop program (testloop.m)
The output produced when this is run through the miniloop compiler and
then assembled and simulated by mvmasm and mvmsim, is shown in Figure 7.2.
The assembler code produced by miniloop is shown in Figures 7.6{7.8 and
discussed in section 7.8.
7.1.1 The begin end block (compound statement)
It is useful to be able to group statements together into blocks so that a single
if statement can control the execution of a list of statements. In minicond
only a single statement could be placed within the then or else clause of an
if statement. The begin end brackets allow statements to be grouped and
treated as a single, compound, statement. It is worth noting that miniloop
is strict about the placement of semicolons which are statement separators not
statement terminators as they are in ANSI-C. The last statement in a begin end