Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs...

46
1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture 3: Scoping and Iterators Lexical Scoping and its Interpreter Simple Objects Iterators

Transcript of Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs...

Page 1: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

1

Ras BodikAlvin CheungMaaz Ahmad

Talia RingerBen Tebbs

Hack Your Language!CSE401 Winter 2016Introduction to Compiler Construction

Lecture 3: Scoping and IteratorsLexical Scoping and its Interpreter Simple ObjectsIterators

Page 2: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Announcements

PA1 will be due this Sunday at 11PMHow did HW1 go?

Please sign up on Piazza!– https://piazza.com/class#winter2016/cse401– From now on, post on piazza instead of staff mailing list

and catalyst discussion board– Questions can be marked as private on piazza

2

Page 3: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Announcements

Final quiz– Time TBD (either last lecture or last section)– Only covers second half of the class– You will be well prepared if you work on the

assignments, the project, and attend lectures

Next Monday is a holiday– We will give an extra lecture this week on Thurs or Fri– Look out for piazza poll email

3

Page 4: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Outline for today

• More scoping• Adding objects to our language• Iterators

4

Page 5: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

An interpreter for the unit language

Now we want to evaluate (ft +m)*3*ft

def eval(e): if type(e) == type(1): return (e,{}) if type(e) == type(1.1): return (e,{})if type(e) == type('m'): return lookupUnit(e)

def lookupUnit(u): return {

'm' : (1, {'m':1}), 'ft' : (0.3048, {'m':1}), 's' : (1, {'s':1}), 'year' : (31556926, {'s':1}), 'kg' : (1, {'kg':1}), 'lb' : (0.45359237, {'kg':1}) }[u];

Rest of code at : http://bitbucket.org/bodi k/cs164 fa09/s rc/c73c51cfce 36/L3-Conve rsionC alculator/P rep-for-lectu re/C onve rsion Calculator.p y 5

Page 6: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

A simple core language

All constructs will be desugared to this language.Key constructs in the language:

– first-class functions (i.e., they can be passed as values)– definitions of local variables (variable binding)– objects aside, these are sufficient to build a DSL like d3

The grammar

E := n | id // reference to a variable| E+E | E-E | E/E | E*E | function (id,…,id) { E } // (anon.) function value| E(E,…,E) // application (call)| var id=E // var introduction

6

Page 7: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Rewrite rules

7

if (C) E1 E2is rewritten into

_ifelse_(C, function(){E1}, function(){E2})

is an example of a rewrite rule

if

C

condition

consequent

E1

E2

alternate

Desugaring happens between parsing and interpretation.

We will introduce a DSL to express these rules in PA

call

_ifelse_ list

fun args

C fun

E1

fun

E2

Page 8: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Environment: maps symbols (var names) to values– Consists of (symbol, value) pairs– env.lookup(sym) returns the value of the first sym in env– the first variable shadows the pairs with the same name

What “first” means matters!

How to look up values of variables?

8

Page 9: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Example

looking up _ifelse_ in the environment returns the (anonymous) function that is bound to _ifelse_

9

fun

call

_ifelse_ list

fun args

C

function

argsbody

E1

fun

E2

body body

thencond else

list

(_ifelse_, )

envlookup

Page 10: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Scoping

A question of x’s:

var x=1function f(callback) {

var x=2 callback()

}f( function(){ x } )

Note: f is a high-order function:it accepts other functions as arguments

10

Which x should this return?

Page 11: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Scope

We must define where a variable is visible (its scope)

Dynamic scoping:Return the value that was last declared during execution

Static (lexical) scoping:Return the value that is the most local in terms of scope

11

Page 12: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Scope

We must define where a variable is visible (its scope)

Dynamic scoping:Variable is visible globally until the end of its lifetime.The environment is a stack. New bindings are pushed.The lookup will proceed from the top of stack.

Static (lexical) scoping:A function carries its own env (fun+env is called closure).vars defined by different functions are kept separate. Env is a tree; lookup proceeds from a leaf towards the root.

12

Page 13: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Interpreter for dynamic scoping

13

Page 14: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The dynamic-scoping interpreter

14

var env = […] // env is global; initially an empty stackfunction eval(n) {

switch (n.op) {case “int”: return n.valcase “id”: return lookup(env,n.name)case “+”: return eval(n.arg1) + eval(n.arg2)...// function (id) { E }case “function”: return { “ast_node”: n } // this dict is our fun value// E(E)case “call”: var f = eval(n.fun) var a = eval(n.arg)

check if f is a function value. If not, exit with errorenv.push(f.ast_node.param.name, a)var ret = eval(f.ast_node.body)env.pop() // end the life time of the parameterreturn ret }}

Page 15: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Problems with dynamic scoping

15

Page 16: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Dynamic scoping

In dynamic scoping, env.lookup(“x”) returns the last x added to envthat is still live.

Problem with dynamic scoping:

var x=1f( function(){ x } ) function f(callback) {

var x=2 callback()

}

Note: hof is a high-order function:It accepts other functions as arguments 16

2 is returned!! Why?

Page 17: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Dynamic scoping illustration

var x=1 function f(callback) {

var x=2 callback()

}f( function(){ x } )

17

(x, 1)

env (stack)

var x=1function f(callback) {

var x=2 callback()

}f( function(){ x } )

(x, 1)

env

top

(x, 2) top

var x=1function f(callback) {

var x=2 callback()

}f( function(){ x } )

current program counter

What value of x is returned by f?

(f, …)

(x, 1)

env

(x, 2) top

(f, …)

Page 18: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Learn more

• Find a language with dynamic scoping

• Study its tutorial and find useful applications of dynamic scoping

• Efficiency of name lookup in dynamic scoping: Our lookup must traverse the entire stack. Can you think of a constant-time algorithm for finding a variable in env.

18

Page 19: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Static scoping with closures

19

Page 20: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Why static scoping?

We want to look up var values in the environment where the function was defined

Not where it is called, as we saw in dynamic scoping

To do so, we need functions to “remember” where they were defined

– i.e., they need to “carry around” their env with them– this is done using closures

20

Page 21: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Closures

Closure: a pair (function, environment)the representation of function value in modern languages

the function:– “pointer” to function code – Includes parameter names and the code of the body– may have free variables, ex: y in function(x) { x+y }

• these are resolved (looked up) using the function’s environment

the environment:– the environment in which the function was created– where the function finds vars from its enclosing scope

21

Page 22: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Example

var x = 1;var y = 2;var z = 3;var f = function(x) {

x = 4;y = 5;var z = 6;

}// execution point 1f(x)

22

sym valueparent null

x 1y 2z 3f AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

closure

frame

Page 23: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Example

var x = 1;var y = 2;var z = 3;var f = function(x) {

x = 4;y = 5;var z = 6;

// execution point 2}

f(x)

23

sym valueparent null

x 1y 2 5z 3f AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

closure

sym valueparent

x 4z 3

Page 24: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Example with higher order functions

From the book Programming in Lua

names = { "Peter", "Paul", "Mary" }grades = { Mary: 10, Paul: 7, Paul: 8 }sort(names, function(n1,n2) {

grades[n1] > grades[n2]})

Sorts the list names based on grades.grades not passed to sort via parameters but via closure

24

Page 25: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

A cool closure

function derivative(f,delta)function(x) {

(f(x+delta) – f(x))/delta}

}

var c = derivative(sin, 0.001)

print(cos(10), c(10))--> -0.83907, -0.83907

25

Page 26: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Summary of key concepts

• Idea: allow nested functions + allow access only to nonlocals in parent (ie statically outer) functions

• The environment: frames on the parent chain• Name resolution for x: first x from on parent chain• Solves modularity problems of dynamic scoping• Functions are now represented as closures, a pair

of (function code, function environment) • Frames created for a function’s locals survive after

the function returns• This allows creating data on the heap, accessed via

functions (eg a closure that increments its counter)26

Page 27: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The interpreter for static scoping

27

Page 28: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Static-scoping interpreter

This part is the same as in dynamic scopingexcept that env is passed into recursive calls to eval,which is cleaner than updating the global env

function eval(n, env) { switch (n.op) {case “int”: return n.arg1case “id”: return env.lookup(n.arg1)case “+”: return eval(n.arg1, env) + eval(n.arg2, env)…

}}

28

Page 29: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

29

eval(program, { “parent”: null }) // env with an empty framefunction eval(n, env) { switch (n.op) {...case “function”: // construct and return the closure

return { “ast_node”: n, “env”: env }

sym valueparent null

x 1y 2z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

closure

var x = 1;var y = 2;var z = 3;var f = function(x) {

x = 4;y = 5;var z = 6;

}

f(x)

Example code

pc

Page 30: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

30

case “call”:var callee = eval(n.fun, env)

var x = 1;var y = 2;var z = 3;var f = function(x) {

...}f(x)

Example code

pc

sym valueparent null

x 1y 2z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

callee

Page 31: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

31

case “call”:var callee = eval(n.fun, env)var arg = eval(n.arg, env)

check if f is a function value. If not, exit with error!var new_frame = Frame()

var x = 1;var y = 2;var z = 3;var f = function(x) {

...}f(x)

Example code

pc

sym valueparent null

x 1y 2z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

sym value

new_frame

callee

Page 32: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

32

case “call”:var callee = eval(n.fun, env)var arg = eval(n.arg, env)

check if f is a function value. If not, exit with error!var new_frame = Frame()new_frame.parent = callee.env

var x = 1;var y = 2;var z = 3;var f = function(x) {

...}f(x)

Example code

pc

sym valueparent null

x 1y 2z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

sym valueparent

new_frame

callee

Page 33: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

33

case “call”:var callee = eval(n.fun, env)var arg = eval(n.arg, env)

check if f is a function value. If not, exit with error!var new_frame = Frame()new_frame.parent = callee.envnew_frame[callee.ast_node.param.name] = arg

var x = 1;var y = 2;var z = 3;var f = function(x) {

...}f(x)

Example code

pc

sym valueparent null

x 1y 2z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

sym valueparent

x 1

new_frame

callee

Page 34: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

The lexical-scoping interpreter

34

case “call”:var callee = eval(n.fun, env)var arg = eval(n.arg, env)

check if f is a function value. If not, exit with error!var new_frame = Frame()new_frame.parent = callee.envnew_frame[callee.ast_node.param.name] = argreturn eval(callee.ast_node.body, new_frame)

var x = 1;var y = 2;var z = 3;var f = function(x) {

...}f(x)

Example code

pc

sym valueparent null

x 1y 2 5z 3f

AST env

params: [x]Body: x = 4;

y = 5;var z = 6;

sym valueparent

x 4

z 6

new_frame

callee

Page 35: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Intermission

35

Page 36: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Introduction to Objects

36

Page 37: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Recall from CSE 143

What are objects- state (attributes) and - code (methods)- objects belong to classes, and classes can be inherited

Why objects?abstraction: hide implementation using encapsulation

Why inheritance?reuse: specialization of an object’s behavior reuses its code

37

Page 38: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

For now, let’s add simple objects – dicts!

Three operations:{}obj[k]obj[k]=v

What about arrays?Objects whose keys are numeric indices!This is actually how it’s done in Lua.

38

Page 39: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Iterators

39

Page 40: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Iterators

Whenever a language includes collections or allows you to build one

we also want constructs for iterating over them

Example: d3 selections (sets of DOM nodes)

The each operator in aSelection.each(aFunction)

is an iterator (implemented as a function)

40

Page 41: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Let’s design a for iterator

We need to worry about two things:– What data can for iterate over?– What can be in the body?

41

Page 42: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Let’s design a for iterator (behavior)

Desired behavior: say want to iterate from 1 to 10:for x in iter(10) { print x }

Q1: Is iter a keyword in the language? No, a function.Q2: What does it return? An iterator function.

function iter(n) {var i = 0function () {

if (i < n) { i = i + 1; i } else { null }

} } 42

Page 43: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Let’s design a for iterator (generality)

Q3: In general, what constructs to permit in __ ?

for x in __ { print x }

A: Any expression that returns an iterator function.

– the syntax of for is thus: for ID in E { S }– these are all legal programs:

for x in myIter { S } for x in myIterArray[2] { S } for x in myIterFactory() { S } for x in myIterFactoryFactory()() { S } // J

43

Page 44: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Let’s design a for iterator (scoping)

Q4: What is the scope x?

for x in E { S }

Q5: In what environment should E be evaluated?In particular, should the environment include x?

E should be evaluated in e, the environment of for.

S should be evaluated in e extended with the binding for x.

44

Page 45: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Implementing the for iterator

We are done with the design of behavior (semantics).Now to implementation. We’ll desugar it, of course.

for ID in E { S }

{ // a block to introduce new scope var t1 = Evar ID = t1()while (ID != null) {

SID = t1()

}} 45

Page 46: Hack Your Language! · 2016-01-12 · 1 Ras Bodik Alvin Cheung Maaz Ahmad Talia Ringer Ben Tebbs Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Lecture

Side note: the block scope

A new scope can be introduced by desugaring, too:

{ S } à ( function(){ S } )()

This trick is used in JS programs to restrict symbol visibility, i.e. to implement a simple module construct.

46