Semantics (Final Pieces)
Closures (in practice)
IMP vs JS
Add Closures: Grammar
(create closure)
(call closure)
Block scope in IMP
Block scope in JS
IMP vs. MITScript vs. JS vs. Python
Examples
Python:
x = 7
def g():
x = 8
def h():
print(x)
x = x + 1
h()
h()
return h
print(x)
hh=g()
hh()
print(x)
print(x) is an error. The assignment in the scope adds automatically adds x to the scope
Records
Objects for Free
var Point = fun(x, y) {
var this = {
x : x;
y : y;
print : fun(){
log("Point(" + this.x + ", " + this.y + ")");
};
};
return this;
};
var p = Point(5,5);
p.print();
Add Records: Grammar
(update record)
(create record)
Records
var a = { x : 1; y : 2};
var b = a;
b.x = 3;
print(a.x)
What’s the output?
Objects for Free
var Point = fun(x, y) {
var this = {
x : x;
y : y;
print : fun(){
log("Point(" + this.x + ", " + this.y + ")");
};
};
return this;
};
var p = Point(5,5);
p.print();
Records
Objects for Free
var Point = fun(x, y) {
var this = {
x : x;
y : y;
print : fun(){
log("Point(" + this.x + ", " + this.y + ")");
};
};
return this;
};
var p = Point(5,5);
p.print();
We just built a whole programming language!