Skip to content

Commit dca84f6

Browse files
committed
Chapter 9 - High-Level Language
Jack for the PerfectlyBalanced program.
1 parent 83e94d7 commit dca84f6

File tree

14 files changed

+4660
-1
lines changed

14 files changed

+4660
-1
lines changed

09/PerfectlyBalanced/Array.vm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Array.new 0
2+
push argument 0
3+
push constant 0
4+
gt
5+
not
6+
if-goto IF_TRUE0
7+
goto IF_FALSE0
8+
label IF_TRUE0
9+
push constant 2
10+
call Sys.error 1
11+
pop temp 0
12+
label IF_FALSE0
13+
push argument 0
14+
call Memory.alloc 1
15+
return
16+
function Array.dispose 0
17+
push argument 0
18+
pop pointer 0
19+
push pointer 0
20+
call Memory.deAlloc 1
21+
pop temp 0
22+
push constant 0
23+
return

09/PerfectlyBalanced/Keyboard.vm

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
function Keyboard.init 0
2+
push constant 0
3+
return
4+
function Keyboard.keyPressed 0
5+
push constant 24576
6+
call Memory.peek 1
7+
return
8+
function Keyboard.readChar 2
9+
push constant 0
10+
call Output.printChar 1
11+
pop temp 0
12+
label WHILE_EXP0
13+
push local 1
14+
push constant 0
15+
eq
16+
push local 0
17+
push constant 0
18+
gt
19+
or
20+
not
21+
if-goto WHILE_END0
22+
call Keyboard.keyPressed 0
23+
pop local 0
24+
push local 0
25+
push constant 0
26+
gt
27+
if-goto IF_TRUE0
28+
goto IF_FALSE0
29+
label IF_TRUE0
30+
push local 0
31+
pop local 1
32+
label IF_FALSE0
33+
goto WHILE_EXP0
34+
label WHILE_END0
35+
call String.backSpace 0
36+
call Output.printChar 1
37+
pop temp 0
38+
push local 1
39+
call Output.printChar 1
40+
pop temp 0
41+
push local 1
42+
return
43+
function Keyboard.readLine 5
44+
push constant 80
45+
call String.new 1
46+
pop local 3
47+
push argument 0
48+
call Output.printString 1
49+
pop temp 0
50+
call String.newLine 0
51+
pop local 1
52+
call String.backSpace 0
53+
pop local 2
54+
label WHILE_EXP0
55+
push local 4
56+
not
57+
not
58+
if-goto WHILE_END0
59+
call Keyboard.readChar 0
60+
pop local 0
61+
push local 0
62+
push local 1
63+
eq
64+
pop local 4
65+
push local 4
66+
not
67+
if-goto IF_TRUE0
68+
goto IF_FALSE0
69+
label IF_TRUE0
70+
push local 0
71+
push local 2
72+
eq
73+
if-goto IF_TRUE1
74+
goto IF_FALSE1
75+
label IF_TRUE1
76+
push local 3
77+
call String.eraseLastChar 1
78+
pop temp 0
79+
goto IF_END1
80+
label IF_FALSE1
81+
push local 3
82+
push local 0
83+
call String.appendChar 2
84+
pop local 3
85+
label IF_END1
86+
label IF_FALSE0
87+
goto WHILE_EXP0
88+
label WHILE_END0
89+
push local 3
90+
return
91+
function Keyboard.readInt 2
92+
push argument 0
93+
call Keyboard.readLine 1
94+
pop local 0
95+
push local 0
96+
call String.intValue 1
97+
pop local 1
98+
push local 0
99+
call String.dispose 1
100+
pop temp 0
101+
push local 1
102+
return

09/PerfectlyBalanced/Main.jack

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Computes whether the same number of x's and y's appear in the input.
2+
class Main {
3+
function void main() {
4+
var PerfectlyBalanced balance;
5+
var String input;
6+
var char c;
7+
var boolean exit;
8+
9+
do Main.header();
10+
do Output.println();
11+
while(true) {
12+
let input = Keyboard.readLine("Enter the input: ");
13+
if (input.length() > 0) {
14+
if(input.charAt(0) = 81) {
15+
do Output.printString("<<<QUITTING>>>");
16+
do input.dispose();
17+
do Sys.halt();
18+
}
19+
}
20+
let balance = PerfectlyBalanced.new(input);
21+
do Screen.clearScreen();
22+
do Output.moveCursor(0, 0);
23+
do Main.header();
24+
do balance.output();
25+
do balance.dispose();
26+
}
27+
28+
return;
29+
}
30+
31+
function void header() {
32+
do Output.printString("<<<PROGRAM: PerfectlyBalanced>>>");
33+
do Output.println();
34+
do Output.printString("<<<DESC: Does input #ofx == #ofy?>>>");
35+
do Output.println();
36+
do Output.printString("<<<Press Q to quit execution>>>");
37+
do Output.println();
38+
do Output.println();
39+
40+
return;
41+
}
42+
}

0 commit comments

Comments
 (0)