Nand2Tetris_Part2_09

Keep going.🎃

Unit 9.1 The Jack Language in a nutshell

About Jack language:

  • A simple, Java-like language
  • Object-based, no inheritance
  • Multi-purpose
  • Lends itself to interactive apps
  • Can be learned in about an hour

About Jack program:

  • A Jack program is a collection of one or more Jack classes, pone of which must be named Main
  • The Main class must have at least one function, named main
  • Program’s entry point: Main.main

Unit 9.2 Object-Based Programming

Abstraction - implementation:

  • Users of an abstraction need know nothing about it’s implementation
  • All they need is the class interface(API)

Jack subroutines: methods, constructors, functions. A subroutine must terminate with a return command.

About Garbage collection:

  • Jack has no garbage collection
  • Objects must be disposed explicity

Unit 9.3 List Processing

List definition(a highly recursive data structure):

  • the atom null, or
  • an atom, followed by a list
    Notation: (atom, list)

Who makes the magic work?(This is a interesting question.)

  • High-level: the constructor
  • Low-level: when compiling the constructor, the compiler plants calls to OS routines that find, and allocate, available memory space for the new object.

Unit 9.4 Jack Language Specification: Syntax

  • White space / comments

    1
    2
    3
    // xxx
    /* xxx */
    /** xxx */
  • keywords

keyword Introduction
class, constructor, method, function Program components
int, boolean, char, void Primitive types
var, static, field Variable declarations
let, do, if, else, while, return Statements
true, false, null Constant values
this Object reference
  • Symbols

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    keyword | Introduction
    ------- | -------------
    () | Used for grouping arithmetic expressions and for enclosing parameter-lists and arument-lists
    [] | Used for array indexing
    {} | Used for grouping program units and statements
    , | Variable list separator
    ; | Statement terminator
    = | Assignment and comparison operator
    . | Class membership
    +, -, *, /, &, |, ~, <, > | Operators
  • Constants

constant example
Integer constants 0, 256, …
String constants “Hello”
Boolean constants true, false
null null
  • Identifiers

Unit 9.5 Jack Language Specification: Data Types

  • Primitive types
type introduction
int Non-negative 2’s-complement 16-bit integer, i.e. an integer in the range 0, …, 32767
boolean true or false
char Unicode character
  • Class types
type example
OS types String, Array
User-defined types Fraction, List, …

🎈 About Type conversions: see the book. And Jack is a very weakly typed language.

Unit 9.6 Jack Language Specification: Classes

  • Class = basic compilation unit
  • Each class Foo is stored in a sparate Foo.jack file
  • The class name’s first character must be an uppercase letter(The file name is also)
  • Field variable declarations and static variable declarations must precede the subroutine declarations
  • Classes that provide functionality: Math class API(Contains functions only, no fields, constructors or methods, offers a “library of services”)
  • Classes that represent entities(objects, Fraction, List, String, …)
  • Don’st mix library functionality and object representation in the same class

Jack application:

  • A Jack program, or application, is a collection of one or more Jack classes, one of which must be named Main
  • The Main class must have at least one function, named main
  • Program’s entry point: Main.main

Jack’s standard class library/OS:

  • Closes gaps between high-level programs and the host hardware
  • Provides efficient implementations of commonly-used functions
  • Provides efficient implementations of commonly-used ADTs

Jack_standard_class

Unit 9.7 Jack Language Specification: Methods

Subroutine declaration:

1
2
3
4
constructor | method | function type subroutineName(parameter-list) {
local variable declarations
statements
}

Jack subroutines:

  • Constructors: create new objects
  • Methods: operate on the current object
  • Functions: static methods

Subroutine types and return values

  • Method and function type can be either void, a primitive data type, or a class name
  • Each subroutine must return a value

Constructors

Declarations:

1
2
3
4
constructor ClassName constructorName(parameter-list) {
local variable declarations
statements
}

Tips:

  • 0, 1 or more in a class
  • Common name: new
  • The constructor’s type must be the name of the constructor’s class
  • The constructor must return a reference to an object of the class type

Variables

Variable kinds:

  • static variables: class-level variables, can be manipulated by the class subroutines
  • field variables: object properties, can be manipulated by the class constructors and methods
  • local variables: used by the subroutines, for local computations
  • parameter variables: used to pass values to subroutines, behave like local variables

Variables must be:

  1. Declared before the are used
  2. Typed

Statements

let/if/while/do/return, only 4 statements and see the book for more informations.

Expressions

A Jack expression is one of the following:

  • A constant
  • A variable name in scope. The variable may be static, field, local, or parameter
  • The this keyword, denoting the current object(can not be used in functions)
  • An array element using the syntax Arr[expression], where Arr is a variable name of type Array in scope.
  • A subroutine call that returns a non-void type
  • An expression prefixed by one of the unary operator - or ~:
    • - expression: arithmetic negation
    • ~ expression: boolean negation(bit-wise for integers)
  • An expression of the form expression op expression where op is one of the following binary operators:
    • + - * /: Integer arithmetic operators
    • & |: Boolean And and Boolean Or(bit-wise for integers) operators
    • < > =: Comparison operators
  • (expression): An expression in parenthesis

Subroutine calls

Subroutine call syntax: subroutineName(argument-list)
Tips:

  • The number and type of arugments must agree with those of the subroutine’s parameters
  • Each argument can be an expression of unlimited complexity
  • Use do to call subroutines

String/Array

see the book for more informations.

Endnote: peculiar features of the Jack language

  • The keyword var: must be used declartaions -> var int x;(this statements can’t be placed under the let statement)
  • The keyword let: must be used in assignment -> let x = 0;
  • The keyword do: must be used for calling a method or a function outside an expression -> do reduce();
  • The body of a statement must be within curly brackets, even if it contains a single statement: if(a > 0) {reurn a} else {return -a};
  • All subroutine must end with a return
  • No operator priority: use parentheses to enforce priority of operations
  • The language is weakly typed

Unit 9.8 Developing Jack Apps: using the Jack language and OS

see the book and run the codes for more informations.

Unit 9.9 A Sample Jack App: Square Dance

see the video and run the codes for more informations.

Unit 9.10 Graphics Optimization

Sprite: A two-dimensional bitmap, typically integrated into a larger scene.

Challenges:

  • Drawing sprites quickly
  • Creating smooth animations

Solutions:

  • Use the standard OS graphics library
  • Use your own graphic functions

Unit 9.11 Project

Compiling and Running a Jack Program.

Unit 9.12 Perspective

  1. Can you sum up the differences between Jack and typical object oriented language?
  • Jack is a nice little language that leads much to be desired. At the same time, Jack has quite a few glaring limitations. To begin with, it’s primitive type system, consisting of three types only. And it has only two control structures, if and while. And as an object based language, it features no inheritance which is one of the most celebrated features of object oriented programming.

    The perfection is achieved not when there’s nothing more to add, but rather when there’s nothing left to take away.

  1. What is a weakly typed programming language?
  • Well, strongly typed languages are quite fussy about how you declare and use variables and data types. But in Jack, you can assign a value of any data type to a variable of any other type.
  1. Jack has some syntax conventions, like let and do that are not customary in other programming languages. Why did you introduce these syntax conventions to the language?
  • These prefix token were introduced into the language with one purpose that allowing the development of elegant, simple and minimal Jack compilers as you will do in the next two modules.

Buy me a coffee ? :)
0%