[toc]
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:
$ sudo ./install.sh
$ ./dcc <path>
$./jack <path>
Hello world program in Jack
class Main {
function int main() {
do Output.printf("Hello, World!\n");
return 0;
}
}
Save the file as Main.jack and run
$ dcc .
This will compile all source files in the current folder. Run
$ jack .
To interpret the VM files produced in the current folder
Output:
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 |
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;
}
}
[3,9,29,32,392] is 5 elements long
In Dog.jack:
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:
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;
}
}
$./dcc .
$./jack .
Codie says WOOF, WOOF!
Fido says woof, woof!