Chapter 9
by Arpon Sarker
The Elements of Computing Systems Chapter 9 - High-Level Language
Introduction
We are now at the high-level language portions of the book. This chapter just gets us acquainted with the Jack language so we can easily create the compiler for it in the next two chapters. The Jack language is capable of procedural programming, array handling, and abstract data types.
Syntactic Elements
A Jack program is a sequence of tokens separated by an arbitrary amount of white space and comments. Tokens an be symbols (syntax symbols such as (), [], ;, +, -, etc.), reserved words, constants (literals), and identifiers (variables).
Program Structure
The basic programming unit in Jack is a class. Each class is in a separate file and can be compiled separately. Each class declarations specifies a name which is globally accessible, a sequence of field and static variables, and then a sequence of subroutine declarations such as methods (object-accessible or this), constructors, and functions (both constructors and functions are class-accessible or static). Each subroutine returns a value which is stated in it subroutine declaration alongside its name.
Variables
Variables must be explicitly declared before they are used (no assignment with initialisation). There are four kinds of variables: field (private class field), static (class static variable), local (subroutine variable where it is deallocated after subroutine returns), and parameter (variables passed to subroutine call). The data types of these variables are either primitive (int, char, boolean) or an object type which is in the name of a class which is either in the Jack standard library or in any other class residing in its program directory.
For object types, the declaration of an object variable only causes the creation of a pointer or reference variable (same in Java). When the programmer actually constructs the object, memory is then allocated for the object where the pointer before, points to the base segment of the object. Once an object is no longer needed, it is disposed by de-allocating it from memory by a standard library function.
The Jack language is weakly typed, meaning the results of attempted assignment or conversion from one type to another is not explicitly defined (unregulated conversions allowed). They are expected to cover these conversions: characters and integers are automatically converted, an integer is converted to a reference variable, and an object variable can be converted into an Array variable, and vice versa (the object fields are array entries).
Statements
Jack features five statements: let (assignment), if, while, do (used to call a function, ignoring returned value), and return.
Expressions
Expressions are defined recursively: a constant, variable name, this, array element, subroutine call, - + expression, ~ + expression, expression op expression, and (expression).
Jack Standard Library (OS)
This standard library can be viewed as a basic operating system as it handles memory management , I/O device management, allows high-levle functions and bridges the gap between the software and the underlying hardware for high-level programmers to use Jack without having to implement these subroutines themselves. However, this does not do file and disk management, provides user interface, time-sharing, batch processing, networking, multi-threading, security and privacy. Since this standard library is for the Hack computer, device drivers are not needed here for the operating system to allow programmers special access to hardware components.
The standard library includes the following classes:
- Math
- String
- Array
- Output (text output)
- Screen (graphic output)
- Keyboard
- Memory (memory operations)
- Sys (execution-related services)
Extensions
The extensions that could be made here which was not touched on by this book in pursuit of a functional and minimalistic compiler are: object-oriented language but doesn’t support inheritance, lacking in primitive type system, weakly-typed, keywords such as let and do are a hassle for programmers but is easy for implementing the compiler, curly brackets needed for single statements, and no formal operator priority (must use parantheses).
tags: compsci - computer_architecture - programming