Lec13 Jack
Lec13 Jack
Lec13 Jack
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 1
Where we are at:
Assembly
Language
Assembler
Chapter 6
abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 2
Some milestones in the evolution of programming languages
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 3
Programming languages
Procedural programming (e.g. C, Fortran, Pascal)
Object-oriented programming (e.g. C++, Java, Python)
Functional programming (e.g. Lisp, ML, Haskell)
Logic programming (e.g. Prolog)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 4
ML
fun fac(x) = if x=0 then 1
else x*fac(x-1);
fun length(L) =
if (L=nil) then 0
else 1+length(tl(L));
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 5
Prolog
Facts
human(kate).
human(bill).
likes(bill,kate).
likes(kate,john).
likes(john,kate).
Rules
friend(X,Y) :- likes(X,Y),likes(Y,X).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 6
Prolog
Absolute value
abs(X, X) :- X>=0, !.
abs(X, Y) :- Y is –X.
?- abs(-9,R).
R=9
?- abs(-9,8).
No
Length of a list
my_length([], 0).
my_length([_|T],R) :- my_length(T, R1), R is R1+1.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 9
Object oriented programming
message
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 10
The Jack programming language
Jack: a simple, object-based, high-level language with a Java-like syntax
Some sample applications written in Jack:
procedural
programming
Pong game
Space Invaders
Tetris
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 11
Disclaimer
Although Jack is a real programming language, we don’t
view it as an end.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 12
Roadmap for learning Jack
Start with examples
Hello World
Procedure and array
Abstract data types
Linked list
...
Formal Jack Spec.
More complex examples
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 13
Hello world
/** Hello World program. */
class Main {
function void main () {
// Prints some text using the standard library
do Output.printString("Hello World");
do Output.println(); // New line
return;
}
}
Some observations:
Java-like syntax Class_name.method_name
Classes Standard library a set of OS
Entry point: Main.main services (methods and functions)
organized in 8 supplied classes:
Typical comments format
Math, String. Array, Output,
do for function calls Keyboard, Screen, Memory, Sys
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 14
Jack standard library aka language extensions aka Jack OS
class Math {
function void init()
function int abs(int x)
function int multiply(int x, int y)
function int divide(int x, int y)
function int min(int x, int y)
function int max(int x, int y)
function int sqrt(int x)
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 15
Jack standard library aka language extensions aka Jack OS
Class String {
constructor String new(int maxLength)
method void dispose()
method int length()
method char charAt(int j)
method void setCharAt(int j, char c)
method String appendChar(char c)
method void eraseLastChar()
method int intValue()
method void setInt(int j)
function char backSpace()
function char doubleQuote()
function char newLine()
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 16
Jack standard library aka language extensions aka Jack OS
Class Array {
class Memory {
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 17
Jack standard library aka language extensions aka Jack OS
class Output {
function void moveCursor(int i, int j)
function void printChar(char c)
function void printString(String s)
function void printInt(int i)
function void println()
function void backSpace()
}
Class Screen {
function void clearScreen()
function void setColor(boolean b)
function void drawPixel(int x, int y)
function void drawLine(int x1, int y1, int x2, int y2)
function void drawRectangle(int x1, int y1, int x2, int y2)
function void drawCircle(int x, int y, int r)
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 18
Jack standard library aka language extensions aka Jack OS
Class Keyboard {
function char keyPressed()
function char readChar()
function String readLine(String message)
function int readInt(String message)
}
Class Sys {
function void halt():
function void error(int errorCode)
function void wait(int duration)
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 19
Typical programming tasks in Jack
Jack can be used to develop any app that comes to my mind, for example:
These insights will serve us in the next lectures, when we build the Jack
compiler.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 20
Array example
class Main { var: variable declaration
function void main () { type: int, Array
var Array a;
var int length; let: assignment
var int i, sum; Array: provided by OS.
let length = Keyboard.readInt(“#number:”)
No type for an array.
let a = Array.new(length); Actually, it can contain
let i = 0; any type and even
different types in an
while (i < length) { array.
let a[i] = Keyboard.readInt("next: ");
let sum = sum + a[i]; Primitive types: int,
let i = i+1; boolean, char.
}
All types in Jack occupy
do Output.printString("The average: "); one word. When declaring
do Output.printInt(sum / length); a variable of primitive
do Output.println(); types, the space is
return; reserved. For other
}
types, a reference is
}
reserved.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 21
Procedural programming example
class Main { Jack program = a collection of
one or more classes
/** Sums up 1 + 2 + 3 + ... + n */
function int sum (int n) { Jack class = a collection of one
var int sum, i; or more subroutines
let sum = 0;
let i = 1; Execution order: when we
while (~(i > n)) { execute a Jack program,
let sum = sum + i; Main.main() starts running.
let i = i + 1;
} Jack subroutine:
return sum;
} method
constructor
function void main () {
var int n; function (static method)
let n = Keyboard.readInt("Enter n: "); (the example on the left
do Output.printString("The result is: "); has functions only, as it is
do Output.printInt(sum(n)); “object-less”)
return;
}
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 22
Object-oriented programming example
The BankAccount class (skeletal)
/** Represents a bank account.
A bank account has an owner, an id, and a balance.
The id values start at 0 and increment by 1 each
time a new account is created. */
class BankAccount {
/** Constructs a new bank account with a 0 balance. */
constructor BankAccount new(String owner)
/** Deposits the given amount in this account. */
method void deposit(int amount)
/** Withdraws the given amount from this account. */
method void withdraw(int amount)
/** Prints the data of this account. */
method void printInfo()
/** Disposes this account. */
method void dispose()
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 23
Object-oriented programming example (continues)
/** Represents a bank account. */ // Code in any other class:
class BankAccount { var int x;
// class‐level variable var BankAccount b; 1
static int newAcctId; 3 let b = BankAccount.new("joe");
Explain b = BankAccount.new("joe")
// Private variables(fields/properties)
field int id; Calls the constructor (which creates a
field String owner; new BankAccount object)
field int balance; Explain return this
The constructor returns the RAM base
/** Constructs a new bank account */
address of the memory block that
constructor BankAccount new (String stores the data of the newly created
owner) { BankAccount object
let id = newAcctId;
let newAcctId = newAcctId + 1; Explain b = BankAccount.new("joe")
let this.owner = owner; stores in variable b a pointer to the
let balance = 0; object’s base memory address
return this; 2
}
// More BankAccount methods.
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 24
Object-oriented programming example (continues)
/** Represents a bank account. */ // Code in any other class:
class BankAccount { var int x;
// class‐level variable var BankAccount b; 1
static int newAcctId; 3 let b = BankAccount.new("joe");
Behind the scene (following compilation):
// Private variables(fields/properties)
field int id; // b = BankAccount.new("joe")
field String owner; push "joe"
field int balance; call BankAccount.new
pop b
/** Constructs a new bank account */ Explanation: the calling code pushes an
constructor BankAccount new (String argument and calls the constructor; the
owner) { constructor’s code (not shown above; the
let id = newAcctId; compiler generates Memory.alloc(n)
let newAcctId = newAcctId + 1; for constructors) creates a new object,
let this.owner = owner; pushes its base address onto the stack,
let balance = 0; and returns;
return this; 2
The calling code then pops the base
}
address into a variable that will now
// More BankAccount methods.
point to the new object.
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 25
Object-oriented programming example (continues)
class BankAccount { ...
static int nAccounts; var BankAccount b1, b2;
...
field int id; let b1 = BankAccount.new("joe");
field String owner; let b2 = BankAccount.new("jane");
field int balance; do b1.deposit(5000);
do b1.withdraw(1000);
// Constructor ... (omitted) ...
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 26
Object-oriented programming example (continues)
class BankAccount { // Code in any other class:
static int nAccounts; ...
var int x;
field int id; var BankAccount b;
field String owner;
field int balance; let b = BankAccount.new("joe");
// Manipulates b...
// Constructor ... (omitted) do b.printInfo();
do b.dispose();
/** Prints information about this account. */
method void printInfo () { Explain
do Output.printInt(id);
do Output.printString(owner); do Memory.deAlloc(this)
do Output.printInt(balance);
return; This is a call to an OS
} function that knows how to
recycle the memory block
/** Disposes this account. */ whose base-address is this.
method void dispose () { We will write this function
do Memory.deAlloc(this); when we develop the OS
return; (project 12).
}
// More BankAccount methods.
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 27
Object-oriented programming example (continues)
class BankAccount { // Code in any other class:
static int nAccounts; ...
var int x;
field int id; var BankAccount b;
field String owner;
field int balance; let b = BankAccount.new("joe");
// Manipulates b...
// Constructor ... (omitted) do b.printInfo();
do b.dispose();
/** Prints information about this account. */
method void printInfo () { Explain
do Output.printInt(id);
do Output.printString(owner); do b.dispose()
do Output.printInt(balance);
Jack has no garbage
return;
}
collection; The programmer
is responsible for explicitly
/** Disposes this account. */
recycling memory resources
method void dispose () {
of objects that are no
do Memory.deAlloc(this); longer needed. If you don’t
return; do so, you may run out of
} memory.
// More BankAccount methods.
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 28
Abstract data type example
The Fraction class API (method signatures)
/** A fraction consists of a numerator and a denominator, both int values */
class Fraction {
/** Constructs a fraction from the given data */
constructor Fraction new(int numerator, int denominator)
/** Reduces this fraction, e.g. changes 20/100 to 1/5. */
method void reduce()
/** Accessors
method int getNumerator()
method int getDenominator()
/** Returns the sum of this fraction and the other one */
method Fraction plus(Fraction other)
/** Returns the product of this fraction and the other one */
method Fraction product(Fraction other)
/** Prints this fraction */
method void print()
/** Disposes this fraction */
method void dispose()
}
}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 29
Abstract data type example (continues)
class Fraction {
field int numerator, denominator;
/** Reduces this fraction */
method void reduce () { // Code omitted }
// A static method computing the greatest common denominator of a and b.
function int gcd (int a, int b) { // Code omitted }
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 30
Abstract data type example (continues)
...
// Constructor and previously defined methods omitted
/** Returns the sum of this fraction the other one */
method Fraction plus (Fraction other) {
var int sum;
let sum = (numerator * other.getDenominator()) +
(other.getNumerator() * denominator());
return Fraction.new(sum , denominator * other.getDenominator());
}
// Similar fraction arithmetic methods follow, code omitted.
/** Prints this fraction */
method void print () { // Code in any other class:
do Output.printInt(numerator); var Fraction a, b, c;
do Output.printString("/");
let a = Fraction.new(2,3);
do Output.printInt(denominator);
let b = Fraction.new(1,5);
return
} // computes c = a + b
let c = a.plus(b);
} do c.print(); // prints "13/15"
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 31
Data structure example
/** Represents a sequence of int values, implemented as a linked list.
The list consists of an atom, which is an int value,
and a tail, which is either a list or a null value. */ v 5
class List {
field int data;
v 2 3 5
field List next;
/* Creates a new list */
constructor List new (int car, List cdr) {
let data = car;
let next = cdr;
return this;
}
/* Disposes this list by recursively disposing its tail. */
method void dispose() {
if (~(next = null)) { // Code in any other class:
do next.dispose(); ...
} // Creates a list holding 2,3, and 5:
do Memory.deAlloc(this);
return; var List v;
} let v = List.new(5 , null);
... let v = List.new(2 , List.new(3,v));
...
} // class List.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 32
Jack language specification
Syntax
Program structure
Data types
Variable kinds
Expressions
Statements
Subroutine calling
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 33
Jack syntactic elements
A jack program is a sequence of tokens separated by an
arbitrary amount of white space and comments.
Tokens can be symbols, reserved words, constants and
identifiers.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 34
Jack syntactic elements
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 35
Jack syntactic elements
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 36
Jack program structure
About this spec:
class ClassName {
Every part in this spec can appear 0
field variable declarations; or more times
static variable declarations;
The order of the field / static
constructor type { parameterList ) {
declarations is arbitrary
local variable declarations;
statements The order of the subroutine
} declarations is arbitrary
method type { parameterList ) {
Each type is either int, boolean,
local variable declarations;
statements char, or a class name.
}
A Jack program:
function type { parameterList ) {
local variable declarations; Each class is written in a separate
statements file (compilation unit)
}
} Jack program = collection of one or
more classes, one of which must be
named Main
The Main class must contain at least
one method, named main()
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 37
Jack data types
Primitive types (Part of the language; Realized by the compiler):
int 16-bit 2’s complement (from ‐32768 to 32767)
boolean 0 and –1, standing for true and false
char unicode character (‘a’, ‘x’, ‘+’, ‘%’, ...)
var Array a;
Let a = 5000;
Let a[100] = 77; // RAM[5100]=77
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 39
Jack variable kinds and scope
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 40
Jack Statements (five types)
let varName = expression;
or
let varName[expression] = expression;
if (expression) {
statements
}
else {
statements
}
while (expression) {
statements
}
do function-or-method-call;
return expression;
or
return;
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 41
Jack expressions
A Jack expression is any one of the following:
A constant
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 45
Perspective
Jack is an object-based language: no inheritance
Standard library
OS (project 12).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 46
Principles of object-oriented programming
encapsulation (information hiding) polymorphism
inheritance
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 47
Which language should you learn?
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 48
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 49
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 50
Programming languages
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 51
Most popular PLs (2014/4)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 52
Most popular PL trends
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 53
Final project
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language slide 54