Programming Language Principles Extending Tiny Programming Language Principles Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida
Extending Tiny Let's add a few more constructs to Tiny. The repeat statement (a la Pascal). Code: n: E1 E2 iffalse n
The Repeat Statement (a la Pascal) E → <repeat E E > code () = gen(code (2), "iffalse",next ()) next () = next (2) + 1 top () = top (2) - 1 error () = if type (2)="bool" then error (2) else gen(error (2), "non-bool exp") type () = "statement"
Extending Tiny (cont’d) The one-armed "if". Code: E1 iffalse n E2 n:
The One-Armed "if" E → < if E E > code (2) = gen(code (1), "iffalse",next (2)) next (2) = next (1) + 1 top (2) = top (1) - 1 error (2) = if type (1)="bool" then error (1) else gen(error (1), "non-bool exp") type () = "statement”
Extending Tiny (cont’d) Conditional Expression Code: E1 iffalse n E2 goto m n: E3 m:
Conditional Expression E → < cond E E E > code (2) = gen(code (1), "iffalse",next (2)+1) next (2) = next (1) + 1 top (2) = top (1) - 1 error (2) = if type (1)="bool" then error (1) else gen(error (1), "non-bool exp")
Conditional Expression (cont’d) code (3) = gen(code (2), “goto",next (3)) next (3) = next (2) + 1 error () = if type (2)= type (3) then error (3) else gen(error (3), “type mismatch") top () = top (3) - 1 type () = type (2) ??? type (3) ???
Extending Tiny Prefix auto-increment. Problematic. If x has not been defined, what location do we reserve for it on the stack ? Code: load n lit 1 add save n load n n = lookup(x)
Prefix Auto-Increment E → < ++p ‘<identifier:x>’ > code () = if lookup(x)=0 then code () else gen( gen( gen(code (), "load", lookup(x)), "lit", 1), "add"), "save", lookup(x)), "load", lookup(x))
Prefix Auto-Increment (cont’d) error () = if lookup(x) !=0 then error () else gen(error (), "var undefined") next () = next () + 5 top () = top () + 1 ??? type () = ???
A Sample "Compilation" Using Tiny's AG To save time (and space), we'll track only code, next, top
A Sample "Compilation" Using Tiny's AG (cont’d) Tiny program: program teeny: assign x := read; if x=1 then assign x:=0 else output x end teeny. Code file: read load 1 lit 1 equal iffalse 9 lit 0 save 1 goto 11 print stop.
Programming Language Principles Extending Tiny Programming Language Principles Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida