Nand2Tetris — Chapter 9
The Jack High-Level Language
// Main.jack
//Simple Jack program
//movement:
//If you press any key while waiting for keyboard input, the number is increased by 1 and output.
//Press q to exit the program
class Main {
function void main() {
var int count;
var char key;
let count = 0;
do Output.printString("Nand2Tetris Jack Test");
do Output.println();
do Output.printString("Press any key. q is quit.");
do Output.println();
while (true) {
let key = Keyboard.keyPressed();
if (key = 113) {
return;
}
if (~(key = 0)) {
let count = count + 1;
do Output.printString("count = ");
do Output.printInt(count);
do Output.println();
while (~(Keyboard.keyPressed() = 0)) {
do Sys.wait(1);
}
}
do Sys.wait(1);
}
return;
}
}
// Counter.jack
//Example class with field values
//Create an object in the constructor and change the this value in the method.
class Counter {
field int value;
constructor Counter new() {
let value = 0;
return this;
}
method void inc() {
let value = value + 1;
return;
}
method int get() {
return value;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
}