-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm code now outputs correctly for arrays
- Loading branch information
sc17ljth
committed
Apr 15, 2019
1 parent
ca6607b
commit 3c71370
Showing
2 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,70 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/09/List/Main.jack | ||
// File name: projects/11/ComplexArrays/Main.jack | ||
|
||
/** Demonstrates the use of the List abstraction. */ | ||
/** | ||
* Performs several complex array processing tests. | ||
* For each test, the expected result is printed, along with the | ||
* actual result. In each test, the two results should be equal. | ||
*/ | ||
class Main { | ||
function void main() { | ||
// Creates and uses the list (2,3,5). | ||
var List v; | ||
let v = List.new(5,null); | ||
let v = List.new(2,List.new(3,v)); | ||
do v.print(); // prints 2 3 5 | ||
do v.dispose(); // disposes the list | ||
return; | ||
} | ||
|
||
function void main() { | ||
var Array a, b, c; | ||
|
||
let a = Array.new(10); | ||
let b = Array.new(5); | ||
let c = Array.new(1); | ||
|
||
let a[3] = 2; | ||
let a[4] = 8; | ||
let a[5] = 4; | ||
let b[a[3]] = a[3] + 3; // b[2] = 5 | ||
let a[b[a[3]]] = a[a[5]] * b[((7 - a[3]) - Main.double(2)) + 1]; // a[5] = 8 * 5 = 40 | ||
let c[0] = null; | ||
let c = c[0]; | ||
|
||
do Output.printString("Test 1: expected result: 5; actual result: "); | ||
do Output.printInt(b[2]); | ||
do Output.println(); | ||
do Output.printString("Test 2: expected result: 40; actual result: "); | ||
do Output.printInt(a[5]); | ||
do Output.println(); | ||
do Output.printString("Test 3: expected result: 0; actual result: "); | ||
do Output.printInt(c); | ||
do Output.println(); | ||
|
||
let c = null; | ||
|
||
if (c = null) { | ||
do Main.fill(a, 10); | ||
let c = a[3]; | ||
let c[1] = 33; | ||
let c = a[7]; | ||
let c[1] = 77; | ||
let b = a[3]; | ||
let b[1] = b[1] + c[1]; // b[1] = 33 + 77 = 110; | ||
} | ||
|
||
do Output.printString("Test 4: expected result: 77; actual result: "); | ||
do Output.printInt(c[1]); | ||
do Output.println(); | ||
do Output.printString("Test 5: expected result: 110; actual result: "); | ||
do Output.printInt(b[1]); | ||
do Output.println(); | ||
return; | ||
} | ||
|
||
function int double(int a) { | ||
return a * 2; | ||
} | ||
|
||
function void fill(Array a, int size) { | ||
while (size > 0) { | ||
let size = size - 1; | ||
let a[size] = Array.new(3); | ||
} | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters