The Elements of Computing Systems: Building a Modern Computer from First Principles (34 page)

BOOK: The Elements of Computing Systems: Building a Modern Computer from First Principles
3.06Mb size Format: txt, pdf, ePub
ads
Figure 9.7
Variable kinds in the Jack language (throughout the table,
subroutine
is either a
function, a method, or a constructor
).
 
Figure 9.7 gives a formal description of all the variable kinds supported by the Jack language. The scope of a variable is the region in the program in which the variable name is recognized.
Figure 9.8
Jack statements.
 
9.2.4 Statements
The Jack language features five generic statements. They are defined and described in figure 9.8.
9.2.5 Expressions
Jack expressions are defined recursively according to the rules given in figure 9.9.
Figure 9.9
Jack expressions.
 
Operator Priority and Order of Evaluation
Operator priority is not defined by the language, except that expressions in parentheses are evaluated first. Thus an expression like 2+3*4 may yield either 20 or 14, whereas 2+(3*4) is guaranteed to yield 14. The need to use parentheses in such expressions makes Jack programming a bit cumbersome. However, the lack of formal operator priority is intentional, since it simplifies the writing of Jack compilers. Of course, different language implementations (compilers) can specify an operator priority and add it to the language documentation, if so desired.
9.2.6 Subroutine Calls
Subroutine calls invoke methods, functions, and constructors for their effect, using the general syntax
subroutineName
(
argument-list
). The number and type of the arguments must match those of the subroutine’s parameters, as defined in its declaration. The parentheses must appear even if the argument list is empty. Each argument may be an expression of unlimited complexity. For example, the Math class, which is part of Jack’s standard library, contains a square root function whose declaration is function int sqrt(int n). Such a function can be invoked using calls like Math.sqrt (17), or Math.sqrt ((a * Math.sqrt (c-17) + 3), and so on.
Figure 9.10
Subroutine call examples.
 
Within a class, methods are called using the syntax
methodName(argument-list
), while functions and constructors must be called using their full names, namely,
className.subroutineName
(
argument-list
)
.
Outside a class, the class functions and constructors are also called using their full names, while methods are called using the syntax varName.methodName(argument-list), where varName is a previously defined object variable. Figure 9.10 gives some examples.
 
Object Construction and Disposal
Object construction is a two-stage affair. When a program declares a variable of some object type, only a reference (pointer) variable is created and allocated memory. To complete the object’s construction (if so desired), the program must call a constructor from the object’s class. Thus, a class that implements a type (e.g., Fraction from figure 9.3c) must contain at least one constructor. Constructors may have arbitrary names, but it is customary to call one of them new. Constructors are called just like any other class function using the format:
For example, let c = Circle.new(x,y,50) where x, y, and 50 are the screen location of the circle’s center and its radius. When a constructor is called, the compiler requests the operating system to allocate enough memory space to hold the new object in memory. The OS returns the base address of the allocated memory segment, and the compiler assigns it to this (in the circle example, the value of this is assigned to c). Next, the constructed object is typically initialized to some valid state, effected by the Jack commands found in the constructor’s body.
When an object is no longer needed in a program, it can be disposed. In particular, objects can be de-allocated from memory and their space reclaimed using the Memory.deAlloc (object) function from the standard library. Convention calls for every class to contain a dispose() method that properly encapsulates this de-allocation. For example, see figure 9.4.
9.2.7 The Jack Standard Library
The Jack language comes with a collection of built-in classes that extend the language’s capabilities. This standard library, which can also be viewed as a basic operating system, must be provided in every Jack language implementation. The standard library includes the following classes, all implemented in Jack:

Math:
provides basic mathematical operations;

String:
implements the String type and string-related operations;

Array:
implements the Array type and array-related operations;

Output:
handles text output to the screen;

Screen:
handles graphic output to the screen;

Keyboard:
handles user input from the keyboard;

Memory:
handles memory operations;

Sys:
provides some execution-related services.
Math
This class enables various mathematical operations.
• function void
init
(): for internal use only.
• function int
abs
(int x): returns the absolute value of x.
• function int
multiply
(int x, int y): returns the product of x and y.
• function int
divide
(int x, int y): returns the integer part of x/y.
• function int
min
(int x, int y): returns the minimum of x and y.
• function int
max
(int x, int y): returns the maximum of x and y.
• function int
sqrt
(int x): returns the integer part of the square root of x.
String
This class implements the String data type and various string-related operations.
 
■ constructor String
new
(int maxLength): constructs a new empty string (of length zero) that can contain at most maxLength characters;
■ method void
dispose
(): disposes this string;
■ method int
length
(): returns the length of this string;
■ method char
charAt
(int j): returns the character at location j of this string;
■ method void
setCharAt
(int j, char c): sets the j-th element of this string to c;
■ method String
appendChar
(char c): appends c to this string and returns this string;
■ method void
eraseLastChar
(): erases the last character from this string;
■ method int
intValue
(): returns the integer value of this string (or of the string prefix until a non-digit character is detected);
■ method void
setInt
(int j): sets this string to hold a representation of j;
■ function char
backSpace
(): returns the backspace character;
■ function char
doubleQuote
(): returns the double quote (“) character;
■ function char
newLine
(): returns the newline character.
 
Array
This class enables the construction and disposal of arrays.
■ function Array
new
(int size): constructs a new array of the given size;
■ method void
dispose
(): disposes this array.
 
Output
This class allows writing text on the screen.
■ function void
init
(): for internal use only;
■ function void
moveCursor
(int i, int j): moves the cursor to the j-th column of the i-th row, and erases the character displayed there;
■ function void
printChar
(char c): prints c at the cursor location and advances the cursor one column forward;
BOOK: The Elements of Computing Systems: Building a Modern Computer from First Principles
3.06Mb size Format: txt, pdf, ePub
ads

Other books

Americanah by Chimamanda Ngozi Adichie
The Steal by Rachel Shteir
Weaver of Dreams by Sparks, Brenda
Infinite Regress by Christopher G. Nuttall
Boy Minus Girl by Richard Uhlig
No Escape From Destiny by Dean, Kasey
Ten Thousand Charms by Allison Pittman
The Winner Stands Alone by Paulo Coelho
Vorpal Blade by Colin Forbes
In the Courts of the Sun by Brian D'Amato