Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Language Principles

Similar presentations


Presentation on theme: "Programming Language Principles"— Presentation transcript:

1 Programming Language Principles
Extending Tiny Programming Language Principles Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

2 Extending Tiny Let's add a few more constructs to Tiny.
The repeat statement (a la Pascal). Code: n: E1 E2 iffalse n

3 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"

4

5 Extending Tiny (cont’d)
The one-armed "if". Code: E1 iffalse n E2 n:

6 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”

7

8 Extending Tiny (cont’d)
Conditional Expression Code: E1 iffalse n E2 goto m n: E3 m:

9 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")

10 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) ???

11

12 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)

13 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))

14 Prefix Auto-Increment (cont’d)
error () = if lookup(x) !=0 then error () else gen(error (), "var undefined") next () = next () + 5 top () = top () + 1 ??? type () = ???

15

16 A Sample "Compilation" Using Tiny's AG
To save time (and space), we'll track only code, next, top

17 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.

18

19 Programming Language Principles
Extending Tiny Programming Language Principles Lecture 19 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida


Download ppt "Programming Language Principles"

Similar presentations


Ads by Google