1 / 45

CS 476 – Programming Language Design

CS 476 – Programming Language Design. William Mansky. HW3 #3: Building a Proof Tree. HW3 #4: Writing Proof Rules. Adding Functions. With variables, assignment, declarations, and control flow, we have a simple imperative language

halmanza
Download Presentation

CS 476 – Programming Language Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 476 – Programming Language Design William Mansky

  2. HW3 #3: Building a Proof Tree

  3. HW3 #4: Writing Proof Rules

  4. Adding Functions • With variables, assignment, declarations, and control flow, we have a simple imperative language • But every imperative language in use has at least one more feature: functions

  5. Adding Functions • Main goal: code reuse • Requires: • Definitions and calls • Arguments and local state • Argument and return types • Ability to call a function from inside a function (even recursively!)

  6. Functions: Syntax of Definitions E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ ?} P ::= D; C C ::= …

  7. Functions: Syntax of Definitions E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C }| F; F P ::= D; F; C C ::= …

  8. Functions: Syntax of Definitions E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C; return E } | F; F P ::= D; F; C C ::= …

  9. Functions: Syntax of Calls E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C } | F; F P ::= D; F; C C ::= … | return E

  10. Functions: Syntax of Calls E ::= … | <ident>(E, …, E) T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C } | F; F P ::= D; F; C C ::= … | return E

  11. Functions: Syntax of Calls E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C } | F; F P ::= D; F; C C ::= … | return E | <ident> := <ident>(E, …, E)

  12. Functions: Types • is a declared function • Each of has the right type • The return type of matches the type of • We can store this information in

  13. Functions: Types int f(int x, int y){ return x + y } f is a function with return type int, and arguments int x and int y • We can store this information in

  14. Functions: Types • is a declared function • Each of has the right type • The return type of matches the type of

  15. Processing Function Declarations int f(int x, int y){ return x + y } function declaration new context old context

  16. Processing Function Declarations int f(int x, int y){ return x + y } • The function’s parameters should be available in the function body

  17. Processing Function Declarations int f(int x, int y){ return x + y } • The function’s parameters should be available in the function body • And so should the previously declared functions • (Mutually recursive functions would require an extra step)

  18. Processing Function Declarations int f(int x, int y){ return x + y } • The function’s parameters should be available in the function body • And so should the previously declared functions • We can use a fake variable to type any return commands

  19. Processing Function Declarations

  20. Functions: Semantics of Declarations

  21. Functions: Syntax of Calls E ::= … T ::= int | bool D ::= T <ident> | D; D F ::= T <ident>(T <ident>, …, T <ident>){ D; C } | F; F P ::= D; F; C C ::= … | return E | <ident> := <ident>(E, …, E)

  22. Functions: Types • is a declared function • Each of has the right type • The return type of matches the type of

  23. Functions: Semantics of Declarations

  24. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  25. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  26. Functions: Semantics of Calls

  27. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  28. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  29. Functions: Semantics of Calls • Evaluate the arguments • Look up in • Execute the body of and produce a return value • Assign the return value to

  30. Functions: Semantics of Calls • Small-step relation is now , where is a stack of states

  31. Functions: Semantics of Calls • Small-step relation is now , where is a stack of states

  32. Functions: Semantics of Calls • Small-step relation is now , where is a stack of states

  33. Functions: Semantics of Calls where ,

  34. Functions: What Does This Mean? • Big-step and small-step describe the same behavior • Small-step semantics now need a whole extra piece of state • And that state corresponds to a feature of real language implementations!

  35. Functions: Interpreter vs. Compiled let rec eval_cmd e s = match e with | Call (x, f, es) ->

  36. Functions: Interpreter vs. Compiled let rec eval_cmd e s = match e with | Call (x, f, es) -> match eval_exps es s with Some vs -> match s f with Some (xs, c) -> match eval_cmd c (merge (funs s) (make_statexs vs)) with | Some (Ret v) -> Some (State (update s x v))

  37. Functions: Interpreter vs. Compiled let rec eval_cmd e s = match e with | Call (x, f, es) -> match eval_exps es s with Some vs -> match s f with Some (xs, c) -> match eval_cmd c (merge (funs s) (make_statexs vs)) with | Some (Ret v) -> Some (State (update s x v)) push stackframe f: store args <compiled code for c> store ret addr pop stackframe jmp f jmp ret addr

  38. Imperative Languages • Arithmetic and boolean expressions • Variables and assignment • Control flow (conditionals, loops) • Variable declarations • Exceptions and exception handling • Function declarations and calls • There’s more, but we’ve covered the essentials! • Next up: object-oriented languages

More Related