The Jack Programming Language Compiler and Virtual MachineThe Jack Language Compilation Build (Linux Only)Compilation Compile from Jack to VMInterpret the VM code Hello, WorldFeaturesSyntaxArrays OutputObject Oriented features Compile && Run Output
Jack is an educational programming language created by Shimon Schocken and Noam Nisan for the course The Elements of Computing Systems, also known as Nand2Tetris, offered by The Hebrew University of Jerusalem. Students are asked to build their own compilers for this object-oriented language.
The Jack language is compiled in a two-tier process:
The code is first translated to an intermediate program (like Java's bytecode and C#'s Intermediate Language) and then a Virtual Machine Translator finishes the compilation process either by interpreting the VM code or by translating to the target's machine assembly language.
On the parent directory:
xxxxxxxxxx
$ sudo ./install.sh
xxxxxxxxxx
$ ./dcc <path>
xxxxxxxxxx
$./jack <path>
Hello world program in Jack
xxxxxxxxxx
class Main {
function int main() {
do Output.printf("Hello, World!\n");
return 0;
}
}
Save the file as Main.jack and run
xxxxxxxxxx
$ dcc .
This will compile all source files in the current folder. Run
xxxxxxxxxx
$ jack .
To interpret the VM files produced in the current folder
Output:
xxxxxxxxxx
Hello, World!
Jack is a Turing-complete, object-oriented language, with most of the features that any modern language has, such as branching, loops, assignments, arrays, classes, methods, arithmetic operations and more.
Jack has a C-like syntax. Reserved words in Jack are:
this | field |
static | constructor |
method | function |
int | char |
boolean | var |
let | do |
while | if |
else | return |
true | false |
null | void |
xxxxxxxxxx
class Main {
function int main() {
var Array a;
let a = Array.new(392, 32, 29, 3, 9);
do a.sort();
do Output.printf("%s is %i elements long.\n", a.to_s(), a.length());
return 0;
}
}
xxxxxxxxxx
[3,9,29,32,392] is 5 elements long
In Dog.jack:
xxxxxxxxxx
class Dog {
field int weight;
field String name;
constructor Dog new(int w, String n) {
let weight = w;
let name = n;
return this;
}
method void bark() {
var String bark;
if (weight < 20) {
let bark = "woof, woof";
} else {
let bark = "WOOF, WOOF";
}
do Output.printf("%s says %s!\n", name, bark);
return;
}
}
In Main.jack:
xxxxxxxxxx
class Main {
function void main() {
var Dog codie, fido;
let codie = Dog.new(24, "Codie");
let fido = Dog.new(12, "Fido");
do codie.bark();
do fido.bark();
return;
}
}
xxxxxxxxxx
$./dcc .
$./jack .
xxxxxxxxxx
Codie says WOOF, WOOF!
Fido says woof, woof!