15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(compileE(e,env) @ cdr, env’’)<br />

end<br />

c○: Michael Kohlhase 304<br />

This completes the compiler for SW (except for the byte code generator which is trivial and an<br />

implementation of environments, which is available elsewhere). So, together with the virtual<br />

machine for L(VM) we discussed above, we can run SW programs on the register machine REMA.<br />

If we now use the REMA simulator from exercise 17 , then we can run SW programs on our com- EdNote:17<br />

puters outright.<br />

One thing that distinguishes SW from real programming languages is that it does not support<br />

procedure declarations. This does not make the language less expressive in principle, but makes<br />

structured programming much harder. The reason we did not introduce this is that our virtual<br />

machine does not have a good infrastructure that supports this. Therefore we will extend L(VM)<br />

with new operations next.<br />

Note that the compiler we have seen above produces L(VM) programs that have what is often<br />

called “memory leaks”. Variables that we declare in our SW program are not cleaned up before the<br />

program halts. In the current implementation we will not fix this (We would need an instruction<br />

for our VM that will “pop” a variable without storing it anywhere or that will simply decrease<br />

virtual stack pointer by a given value.), but we will get a better understanding for this when we<br />

talk about the static procedures next.<br />

Compiling the Extended Example: A while Loop<br />

Example 471 Consider the following program that computes (12)! and the corresponding<br />

L(VM) program:<br />

var n := 12; var a := 1; con 12 con 1<br />

while 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!