From d3a0cc3a8ba6dfeb64d3faeffdeb6845b60e5840 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 20 Jan 2018 15:41:49 +0100 Subject: rearranged the dir for github - removed tools and pdfs - rearranged the projects dirs - added md files - other minor changes --- projects/11/Average/Main.jack | 29 - projects/11/Average/Main.vm | 147 ----- projects/11/ComplexArrays/Main.jack | 70 --- projects/11/ComplexArrays/Main.vm | 681 -------------------- projects/11/ConvertToBin/Main.jack | 84 --- projects/11/ConvertToBin/Main.vm | 110 ---- projects/11/ConvertToBin/setram.tst | 3 - projects/11/JackCompiler | Bin 284520 -> 0 bytes projects/11/JackCompiler.hi | Bin 10319 -> 0 bytes projects/11/JackCompiler.hs | 538 ---------------- projects/11/JackCompiler.o | Bin 409680 -> 0 bytes projects/11/K/Board.jack | 305 --------- projects/11/K/Board.vm | 1183 ----------------------------------- projects/11/K/K.txt | 25 - projects/11/K/KGame.jack | 34 - projects/11/K/KGame.vm | 74 --- projects/11/K/Main.jack | 9 - projects/11/K/Main.vm | 11 - projects/11/Pong/Ball.jack | 203 ------ projects/11/Pong/Ball.vm | 427 ------------- projects/11/Pong/Bat.jack | 103 --- projects/11/Pong/Bat.vm | 204 ------ projects/11/Pong/Main.jack | 20 - projects/11/Pong/Main.vm | 13 - projects/11/Pong/PongGame.jack | 137 ---- projects/11/Pong/PongGame.vm | 309 --------- projects/11/Seven/Main.jack | 17 - projects/11/Seven/Main.vm | 10 - projects/11/Square/Main.jack | 17 - projects/11/Square/Main.vm | 11 - projects/11/Square/Square.jack | 110 ---- projects/11/Square/Square.vm | 298 --------- projects/11/Square/SquareGame.jack | 81 --- projects/11/Square/SquareGame.vm | 168 ----- projects/11/Test.jack | 7 - projects/11/systemsub.txt | 59 -- projects/11/systemsub1.txt | 42 -- 37 files changed, 5539 deletions(-) delete mode 100644 projects/11/Average/Main.jack delete mode 100644 projects/11/Average/Main.vm delete mode 100644 projects/11/ComplexArrays/Main.jack delete mode 100644 projects/11/ComplexArrays/Main.vm delete mode 100644 projects/11/ConvertToBin/Main.jack delete mode 100644 projects/11/ConvertToBin/Main.vm delete mode 100644 projects/11/ConvertToBin/setram.tst delete mode 100755 projects/11/JackCompiler delete mode 100644 projects/11/JackCompiler.hi delete mode 100644 projects/11/JackCompiler.hs delete mode 100644 projects/11/JackCompiler.o delete mode 100644 projects/11/K/Board.jack delete mode 100644 projects/11/K/Board.vm delete mode 100644 projects/11/K/K.txt delete mode 100644 projects/11/K/KGame.jack delete mode 100644 projects/11/K/KGame.vm delete mode 100644 projects/11/K/Main.jack delete mode 100644 projects/11/K/Main.vm delete mode 100644 projects/11/Pong/Ball.jack delete mode 100644 projects/11/Pong/Ball.vm delete mode 100644 projects/11/Pong/Bat.jack delete mode 100644 projects/11/Pong/Bat.vm delete mode 100644 projects/11/Pong/Main.jack delete mode 100644 projects/11/Pong/Main.vm delete mode 100644 projects/11/Pong/PongGame.jack delete mode 100644 projects/11/Pong/PongGame.vm delete mode 100644 projects/11/Seven/Main.jack delete mode 100644 projects/11/Seven/Main.vm delete mode 100644 projects/11/Square/Main.jack delete mode 100644 projects/11/Square/Main.vm delete mode 100644 projects/11/Square/Square.jack delete mode 100644 projects/11/Square/Square.vm delete mode 100644 projects/11/Square/SquareGame.jack delete mode 100644 projects/11/Square/SquareGame.vm delete mode 100644 projects/11/Test.jack delete mode 100644 projects/11/systemsub.txt delete mode 100644 projects/11/systemsub1.txt (limited to 'projects/11') diff --git a/projects/11/Average/Main.jack b/projects/11/Average/Main.jack deleted file mode 100644 index d560bf1..0000000 --- a/projects/11/Average/Main.jack +++ /dev/null @@ -1,29 +0,0 @@ -// 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/11/Average/Main.jack - -// (Same as projects/09/Average/Main.jack) - -// Inputs some numbers and computes their average -class Main { - function void main() { - var Array a; - var int length; - var int i, sum; - - let length = Keyboard.readInt("How many numbers? "); - let a = Array.new(length); // constructs the array - - let i = 0; - while (i < length) { - let a[i] = Keyboard.readInt("Enter a number: "); - let sum = sum + a[i]; - let i = i + 1; - } - - do Output.printString("The average is "); - do Output.printInt(sum / length); - return; - } -} diff --git a/projects/11/Average/Main.vm b/projects/11/Average/Main.vm deleted file mode 100644 index 91772b9..0000000 --- a/projects/11/Average/Main.vm +++ /dev/null @@ -1,147 +0,0 @@ -function Main.main 4 -push constant 18 -call String.new 1 -push constant 72 -call String.appendChar 2 -push constant 111 -call String.appendChar 2 -push constant 119 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 109 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 121 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 109 -call String.appendChar 2 -push constant 98 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 63 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Keyboard.readInt 1 -pop local 1 -push local 1 -call Array.new 1 -pop local 0 -push constant 0 -pop local 2 -label main.While0 -push local 2 -push local 1 -lt -not -if-goto main.EndWhile0 -push constant 16 -call String.new 1 -push constant 69 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 109 -call String.appendChar 2 -push constant 98 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Keyboard.readInt 1 -push local 0 -push local 2 -add -pop pointer 1 -pop that 0 -push local 3 -push local 0 -push local 2 -add -pop pointer 1 -push that 0 -add -pop local 3 -push local 2 -push constant 1 -add -pop local 2 -goto main.While0 -label main.EndWhile0 -push constant 15 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 104 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 118 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 103 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 105 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 3 -push local 1 -call Math.divide 2 -call Output.printInt 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/ComplexArrays/Main.jack b/projects/11/ComplexArrays/Main.jack deleted file mode 100644 index fe51c20..0000000 --- a/projects/11/ComplexArrays/Main.jack +++ /dev/null @@ -1,70 +0,0 @@ -// 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/11/ComplexArrays/Main.jack - -/** - * 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() { - 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; - } -} diff --git a/projects/11/ComplexArrays/Main.vm b/projects/11/ComplexArrays/Main.vm deleted file mode 100644 index dc83733..0000000 --- a/projects/11/ComplexArrays/Main.vm +++ /dev/null @@ -1,681 +0,0 @@ -function Main.main 3 -push constant 10 -call Array.new 1 -pop local 0 -push constant 5 -call Array.new 1 -pop local 1 -push constant 1 -call Array.new 1 -pop local 2 -push constant 2 -push local 0 -push constant 3 -add -pop pointer 1 -pop that 0 -push constant 8 -push local 0 -push constant 4 -add -pop pointer 1 -pop that 0 -push constant 4 -push local 0 -push constant 5 -add -pop pointer 1 -pop that 0 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -push constant 3 -add -push local 1 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -add -pop pointer 1 -pop that 0 -push local 0 -push local 0 -push constant 5 -add -pop pointer 1 -push that 0 -add -pop pointer 1 -push that 0 -push local 1 -push constant 7 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -sub -push constant 2 -call Main.double 1 -sub -push constant 1 -add -add -pop pointer 1 -push that 0 -call Math.multiply 2 -push local 0 -push local 1 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -add -pop pointer 1 -push that 0 -add -pop pointer 1 -pop that 0 -push constant 0 -push local 2 -push constant 0 -add -pop pointer 1 -pop that 0 -push local 2 -push constant 0 -add -pop pointer 1 -push that 0 -pop local 2 -push constant 43 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 49 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 120 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 53 -call String.appendChar 2 -push constant 59 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 1 -push constant 2 -add -pop pointer 1 -push that 0 -call Output.printInt 1 -pop temp 0 -call Output.println 0 -pop temp 0 -push constant 44 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 50 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 120 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 52 -call String.appendChar 2 -push constant 48 -call String.appendChar 2 -push constant 59 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 0 -push constant 5 -add -pop pointer 1 -push that 0 -call Output.printInt 1 -pop temp 0 -call Output.println 0 -pop temp 0 -push constant 43 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 51 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 120 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 48 -call String.appendChar 2 -push constant 59 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 2 -call Output.printInt 1 -pop temp 0 -call Output.println 0 -pop temp 0 -push constant 0 -pop local 2 -push local 2 -push constant 0 -eq -not -if-goto main.Else0 -push local 0 -push constant 10 -call Main.fill 2 -pop temp 0 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -pop local 2 -push constant 33 -push local 2 -push constant 1 -add -pop pointer 1 -pop that 0 -push local 0 -push constant 7 -add -pop pointer 1 -push that 0 -pop local 2 -push constant 77 -push local 2 -push constant 1 -add -pop pointer 1 -pop that 0 -push local 0 -push constant 3 -add -pop pointer 1 -push that 0 -pop local 1 -push local 1 -push constant 1 -add -pop pointer 1 -push that 0 -push local 2 -push constant 1 -add -pop pointer 1 -push that 0 -add -push local 1 -push constant 1 -add -pop pointer 1 -pop that 0 -label main.Else0 -push constant 44 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 52 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 120 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 55 -call String.appendChar 2 -push constant 55 -call String.appendChar 2 -push constant 59 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 2 -push constant 1 -add -pop pointer 1 -push that 0 -call Output.printInt 1 -pop temp 0 -call Output.println 0 -pop temp 0 -push constant 45 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 53 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 120 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 49 -call String.appendChar 2 -push constant 49 -call String.appendChar 2 -push constant 48 -call String.appendChar 2 -push constant 59 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 116 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 1 -push constant 1 -add -pop pointer 1 -push that 0 -call Output.printInt 1 -pop temp 0 -call Output.println 0 -pop temp 0 -push constant 0 -return -function Main.double 0 -push argument 0 -push constant 2 -call Math.multiply 2 -return -function Main.fill 0 -label fill.While0 -push argument 1 -push constant 0 -gt -not -if-goto fill.EndWhile0 -push argument 1 -push constant 1 -sub -pop argument 1 -push constant 3 -call Array.new 1 -push argument 0 -push argument 1 -add -pop pointer 1 -pop that 0 -goto fill.While0 -label fill.EndWhile0 -push constant 0 -return diff --git a/projects/11/ConvertToBin/Main.jack b/projects/11/ConvertToBin/Main.jack deleted file mode 100644 index e1ab941..0000000 --- a/projects/11/ConvertToBin/Main.jack +++ /dev/null @@ -1,84 +0,0 @@ -// 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/11/ConvertToBin/Main.jack - -/** - * Unpacks a 16-bit number into its binary representation: - * Takes the 16-bit number stored in RAM[8000] and stores its individual - * bits in RAM[8001..8016] (each location will contain 0 or 1). - * Before the conversion, RAM[8001]..RAM[8016] are initialized to -1. - * - * The program should be tested as follows: - * 1) Load the program into the supplied VM emulator - * 2) Put some value in RAM[8000] - * 3) Switch to "no animation" - * 4) Run the program (give it enough time to run) - * 5) Stop the program - * 6) Check that RAM[8001]..RAM[8016] contains the correct binary result, and - * that none of these memory locations contains -1. - */ -class Main { - - /** - * Initializes RAM[8001]..RAM[8016] to -1, - * and converts the value in RAM[8000] to binary. - */ - function void main() { - var int value; - do Memory.poke(8000, 3827); - //do Memory.poke(8000, -7); - do Main.fillMemory(8001, 16, -1); // sets RAM[8001]..RAM[8016] to -1 - let value = Memory.peek(8000); // reads a value from RAM[8000] - do Main.convert(value); // performs the conversion - return; - } - - /** Converts the given decimal value to binary, and puts - * the resulting bits in RAM[8001]..RAM[8016]. */ - function void convert(int value) { - var int mask, position; - var boolean loop; - - let loop = true; - while (loop) { - let position = position + 1; - let mask = Main.nextMask(mask); - - if (~(position > 16)) { - - if (~((value & mask) = 0)) { - do Memory.poke(8000 + position, 1); - } - else { - do Memory.poke(8000 + position, 0); - } - } - else { - let loop = false; - } - } - return; - } - - /** Returns the next mask (the mask that should follow the given mask). */ - function int nextMask(int mask) { - if (mask = 0) { - return 1; - } - else { - return mask * 2; - } - } - - /** Fills 'length' consecutive memory locations with 'value', - * starting at 'startAddress'. */ - function void fillMemory(int startAddress, int length, int value) { - while (length > 0) { - do Memory.poke(startAddress, value); - let length = length - 1; - let startAddress = startAddress + 1; - } - return; - } -} diff --git a/projects/11/ConvertToBin/Main.vm b/projects/11/ConvertToBin/Main.vm deleted file mode 100644 index 15d63c0..0000000 --- a/projects/11/ConvertToBin/Main.vm +++ /dev/null @@ -1,110 +0,0 @@ -function Main.main 1 -push constant 8000 -push constant 3827 -call Memory.poke 2 -pop temp 0 -push constant 8001 -push constant 16 -push constant 1 -neg -call Main.fillMemory 3 -pop temp 0 -push constant 8000 -call Memory.peek 1 -pop local 0 -push local 0 -call Main.convert 1 -pop temp 0 -push constant 0 -return -function Main.convert 3 -push constant 1 -neg -pop local 2 -label convert.While0 -push local 2 -not -if-goto convert.EndWhile0 -push local 1 -push constant 1 -add -pop local 1 -push local 0 -call Main.nextMask 1 -pop local 0 -push local 1 -push constant 16 -gt -not -not -if-goto convert.While0.Else0 -push argument 0 -push local 0 -and -push constant 0 -eq -not -not -if-goto convert.While0.Else0.Else0 -push constant 8000 -push local 1 -add -push constant 1 -call Memory.poke 2 -pop temp 0 -goto convert.While0.Else0.Endif0 -label convert.While0.Else0.Else0 -push constant 8000 -push local 1 -add -push constant 0 -call Memory.poke 2 -pop temp 0 -label convert.While0.Else0.Endif0 -goto convert.While0.Endif0 -label convert.While0.Else0 -push constant 0 -pop local 2 -label convert.While0.Endif0 -goto convert.While0 -label convert.EndWhile0 -push constant 0 -return -function Main.nextMask 0 -push argument 0 -push constant 0 -eq -not -if-goto nextMask.Else0 -push constant 1 -return -goto nextMask.Endif0 -label nextMask.Else0 -push argument 0 -push constant 2 -call Math.multiply 2 -return -label nextMask.Endif0 -function Main.fillMemory 0 -label fillMemory.While0 -push argument 1 -push constant 0 -gt -not -if-goto fillMemory.EndWhile0 -push argument 0 -push argument 2 -call Memory.poke 2 -pop temp 0 -push argument 1 -push constant 1 -sub -pop argument 1 -push argument 0 -push constant 1 -add -pop argument 0 -goto fillMemory.While0 -label fillMemory.EndWhile0 -push constant 0 -return diff --git a/projects/11/ConvertToBin/setram.tst b/projects/11/ConvertToBin/setram.tst deleted file mode 100644 index a33eece..0000000 --- a/projects/11/ConvertToBin/setram.tst +++ /dev/null @@ -1,3 +0,0 @@ -load Main.vm -set RAM[8000] 1023; -vmstep; diff --git a/projects/11/JackCompiler b/projects/11/JackCompiler deleted file mode 100755 index 2dc9b98..0000000 Binary files a/projects/11/JackCompiler and /dev/null differ diff --git a/projects/11/JackCompiler.hi b/projects/11/JackCompiler.hi deleted file mode 100644 index 50c8c9c..0000000 Binary files a/projects/11/JackCompiler.hi and /dev/null differ diff --git a/projects/11/JackCompiler.hs b/projects/11/JackCompiler.hs deleted file mode 100644 index bf3acfe..0000000 --- a/projects/11/JackCompiler.hs +++ /dev/null @@ -1,538 +0,0 @@ --- Jack Compiler, as the coursework of Project 11 of Nand2Tetris course. --- Author: Yuchen Pei (me@ypei.me) --- Date: 2018-01-15 -{-# LANGUAGE FlexibleContexts #-} -import Text.Parsec.Prim -import Text.Parsec.Char -import Text.Parsec.Combinator -import Data.Functor.Identity -import Data.Either -import Data.Maybe -import Data.List -import System.Environment -import System.Directory -import Control.Monad -import Data.Char -import Data.Map (Map) -import qualified Data.Map as Map -import Debug.Trace - -data JClass = JClass JIdentifier [JClassVarDec] [JSubroutineDec] deriving (Show, Eq) -data JClassVarDec = JClassVarDec JClassVarScope JTypeAndId deriving (Show, Eq) -data JSubroutineDec = JSubroutineDec JSubroutineHeader JSubroutineBody deriving (Show, Eq) -data JSubroutineHeader = JSubroutineHeader JSubroutineType JTypeAndId [JTypeAndId] deriving (Show, Eq) -data JSubroutineBody = JSubroutineBody [JTypeAndId] [JStatement] deriving (Show, Eq) -data JStatement = JLetStatment JVarId JExpression - | JIfStatement JExpression [JStatement] (Maybe [JStatement]) - | JWhileStatment JExpression [JStatement] - | JDoStatement JSubroutineCall - | JReturnStatement (Maybe JExpression) - deriving (Show, Eq) -data JExpression = JIntConst Int - | JStrConst [Char] - | JKeywordConst [Char] -- can only be true, false, null or this - | JExpVar JVarId - | JExpCall JSubroutineCall - | JExpUna JUOp JExpression -- JOp can only be - or ~ here - | JExpBin JExpression [(JBOp, JExpression)] - deriving (Show, Eq) -data JVarId = JVarId JIdentifier (Maybe JExpression) deriving (Show, Eq) -data JSubroutineCall = JSubroutineCall JIdentifier (Maybe JIdentifier) [JExpression] deriving (Show, Eq) - ---data ClassTable = C [Char] Table Int deriving (Show, Eq) -type JBOp = Char -type JUOp = Char -type JIdentifier = [Char] -type JTypeAndId = (JType, JIdentifier) -type JClassVarScope = [Char] -type JType = [Char] -type JSubroutineType = [Char] -type JackParser = Parsec [Char] () --- For variable tables: map name (type, (seg, n)) --- For the subroutine table: map name (kind, (type, nArgs)) -type Table = Map [Char] ([Char], ([Char], Int)) - - -binaryOpChars = "+-*/&|<>=" -unaryOpChars = "-~" -keywordConstStrs = ["true", "false", "null", "this"] -primTypeStrs = ["int", "char", "boolean"] -primTypeStrs' = primTypeStrs ++ ["void"] -classVarScopeStrs = ["static", "field"] -subroutineTypeStrs = ["constructor", "function", "method"] -alphaUnderscore = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['_'] -alphaNumUnderscore = alphaUnderscore ++ ['0'..'9'] -alphaNumUnderscoredot = '.':alphaNumUnderscore - -parse' parser = parse parser "" - -jackReader = parse' (many jSpace >> jClass) - -jClass :: JackParser JClass -jClass = do - string "class" >> many1 jSpace - id <- jIdentifier - many jSpace >> char '{' - classVarDecs <- many $ try (many jSpace >> jClassVarDecs) - subroutineDecs <- many $ try (many jSpace >> jSubroutineDec) - many jSpace >> char '}' - return $ JClass id (mconcat classVarDecs) subroutineDecs - -jClassType :: JackParser [Char] -jClassType = do - x <- oneOf ['A' .. 'Z'] - xs <- many $ oneOf alphaNumUnderscore - return $ x:xs - -jBOp :: JackParser JBOp -jBOp = oneOf binaryOpChars - -jUOp :: JackParser JUOp -jUOp = oneOf unaryOpChars - -jComment :: JackParser () -jComment = jInlineComment <|> jBlockComment - -jInlineComment :: JackParser () -jInlineComment = return () <* - try (string "//" >> manyTill (noneOf "\n\r") endOfLine) - --try (string "//" >> manyTill (noneOf "\n") newline) - -jSpace :: JackParser () -jSpace = (return () <* space) <|> jComment - -jBlockComment :: JackParser () -jBlockComment = return () <* - try (string "/*" >> manyTill anyChar (try $ string "*/")) - -jPrimType :: JackParser JType -jPrimType = choice $ string <$> primTypeStrs - -jType :: JackParser JType -jType = choice [jPrimType, jClassType] - -jPrimType' :: JackParser JType -jPrimType' = choice $ string <$> primTypeStrs' - -jType' :: JackParser JType -jType' = choice [jPrimType', jClassType] - -jClassVarScope :: JackParser JClassVarScope -jClassVarScope = choice $ string <$> classVarScopeStrs - -jSubroutineType :: JackParser JSubroutineType -jSubroutineType = choice $ string <$> subroutineTypeStrs - -jIdentifier :: JackParser [Char] -jIdentifier = do - x <- oneOf alphaUnderscore - xs <- many $ oneOf alphaNumUnderscore - return $ x:xs - -jClassVarDecs :: JackParser [JClassVarDec] -jClassVarDecs = do - scope <- jClassVarScope - many1 jSpace - typeAndIds <- jTypeAndIds - many jSpace >> char ';' - return $ JClassVarDec scope <$> typeAndIds - -jVarDecs :: JackParser [JTypeAndId] -jVarDecs = - (string "var" >> many1 jSpace) >> jTypeAndIds <* (many jSpace >> char ';') - -jTypeAndId :: JackParser JType -> JackParser JTypeAndId -jTypeAndId p = do - type_ <- p - many1 jSpace - id <- jIdentifier - return (type_, id) - -jTypeAndIds :: JackParser [JTypeAndId] -jTypeAndIds = do - type_ <- jType - many1 jSpace - ids <- sepBy jIdentifier (try $ many jSpace >> char ',' >> many jSpace) - return $ (\x -> (type_, x)) <$> ids - -jParameters :: JackParser [JTypeAndId] -jParameters = do - char '(' >> many jSpace - params <- sepBy (jTypeAndId jType) (try $ many jSpace >> char ',' >> many jSpace) - many jSpace >> char ')' - return params - -jSubroutineHeader :: JackParser JSubroutineHeader -jSubroutineHeader = do - subtype <- jSubroutineType - many1 jSpace - typeAndId <- jTypeAndId jType' - params <- jParameters - return $ JSubroutineHeader subtype typeAndId params - -jExpression :: JackParser JExpression -jExpression = jExpBin <|> jTerm - -jTerm :: JackParser JExpression -jTerm = choice [jIntConst, jStrConst, jKeywordConst, jExpCall, jExpVar, jExpUna, jExpInBraces] - -jIntConst :: JackParser JExpression -jIntConst = JIntConst <$> (read <$> many1 digit) - -jStrConst :: JackParser JExpression -jStrConst = JStrConst <$> between (char '"') (char '"') (many $ noneOf "\"") - -jKeywordConst :: JackParser JExpression -jKeywordConst = JKeywordConst <$> (choice $ try <$> string <$> keywordConstStrs) - -jExpVar :: JackParser JExpression -jExpVar = JExpVar <$> jVarId - -jVarId :: JackParser JVarId -jVarId = do - id <- jIdentifier - maybeArray <- optionMaybe $ try (char '[' >> jExpression <* char ']') - return $ JVarId id maybeArray - -jExpCall :: JackParser JExpression -jExpCall = JExpCall <$> jSubroutineCall - -jExpUna :: JackParser JExpression -jExpUna = do - op <- oneOf unaryOpChars - many jSpace - x <- jTerm - return $ JExpUna op x - -jExpInBraces :: JackParser JExpression -jExpInBraces = between (char '(') (char ')') jExpression -- expressions like a + () is not allowed - -jExpBin :: JackParser JExpression -jExpBin = try $ do - x <- jTerm - xs <- many1 jOpAndTerm - return $ JExpBin x xs - -jOpAndTerm :: JackParser (JBOp, JExpression) -jOpAndTerm = do - op <- many jSpace >> jBOp - x <- many jSpace >> jTerm - return (op, x) - -jSubroutineDec :: JackParser JSubroutineDec -jSubroutineDec = do - header <- jSubroutineHeader - many jSpace - body <- jSubroutineBody - return $ JSubroutineDec header body - -jSubroutineBody :: JackParser JSubroutineBody -jSubroutineBody = do - char '{' - varDecs <- many $ try (many jSpace >> jVarDecs) - stmts <- many $ try (many jSpace >> jStatement) - many jSpace >> char '}' - return $ JSubroutineBody (mconcat varDecs) stmts - -jStatement :: JackParser JStatement -jStatement = choice [jLetStatement, jIfStatement, jWhileStatement, jDoStatement, jReturnStatement] - -jLetStatement :: JackParser JStatement -jLetStatement = do - string "let" >> many1 jSpace - leftVarId <- jVarId - many jSpace >> char '=' >> many jSpace - exp <- jExpression - many jSpace >> char ';' - return $ JLetStatment leftVarId exp - -jIfStatement = do - string "if" >> many jSpace >> char '(' >> many jSpace - exp <- jExpression - many jSpace >> char ')' >> many jSpace >> char '{' >> many jSpace - stmts <- many (try $ many jSpace >> jStatement) - many jSpace >> char '}' - stmts' <- optionMaybe $ try jElseBlock - return $ JIfStatement exp stmts stmts' - -jElseBlock :: JackParser [JStatement] -jElseBlock = do - many jSpace >> string "else" >> many jSpace >> char '{' >> many jSpace - stmts <- many (try $ many jSpace >> jStatement) - many jSpace >> char '}' - return stmts - -jWhileStatement :: JackParser JStatement -jWhileStatement = do - string "while" >> many jSpace >> char '(' >> many jSpace - exp <- jExpression - many jSpace >> char ')' >> many jSpace >> char '{' - stmts <- many (try $ many jSpace >> jStatement) - many jSpace >> char '}' - return $ JWhileStatment exp stmts - -jDoStatement :: JackParser JStatement -jDoStatement = do - jCall <- string "do" >> many jSpace >> jSubroutineCall - many jSpace >> char ';' - return $ JDoStatement jCall - -jReturnStatement :: JackParser JStatement -jReturnStatement = do - string "return" >> many jSpace - res <- optionMaybe $ try jExpression - many jSpace >> char ';' - return $ JReturnStatement res - -jSubroutineCall :: JackParser JSubroutineCall -jSubroutineCall = try $ do - callee <- jIdentifier - method <- optionMaybe $ try (char '.' >> jIdentifier) - args <- emptyArgs <|> someArgs - return $ JSubroutineCall callee method args - -emptyArgs :: JackParser [JExpression] -emptyArgs = return [] <* (try $ char '(' >> many jSpace >> char ')') - -someArgs :: JackParser [JExpression] -someArgs = do - char '(' >> many jSpace - exps <- sepBy jExpression (many jSpace >> char ',' >> many jSpace) - many jSpace >> char ')' - return exps - - --- vm writer starts from here - -buildCTable :: [JClassVarDec] -> (Table, Int) -buildCTable xs = go 0 0 Map.empty xs - where go n _ t [] = (t, n) - go nField nStatic t ((JClassVarDec "field" (ty, jId)):ys) = - go (nField + 1) nStatic (Map.insert jId (ty, ("this", nField)) t) ys - go nField nStatic t ((JClassVarDec "static" (ty, jId)):ys) = - go nField (nStatic + 1) (Map.insert jId (ty, ("static", nStatic)) t) ys - - ---data JClass = JClass JIdentifier [JClassVarDec] [JSubroutineDec] deriving (Show, Eq) -buildSRTable :: [JClass] -> Table -buildSRTable xs = Map.fromList $ mconcat $ go <$> xs where - go (JClass cName _ subs) = go' <$> subs where - go' (JSubroutineDec (JSubroutineHeader kind (ty, sName) args) _) = - (cName ++ "." ++ sName, (kind, (ty, nArgs))) where - nArgs = length args + if kind == "method" then 1 else 0 - -buildLTable :: Bool -> [JTypeAndId] -> [JTypeAndId] -> Table -buildLTable isMethod args lcls = - (go "argument" args (if isMethod then 1 else 0) Map.empty) `Map.union` (go "local" lcls 0 Map.empty) where - go _ [] _ t = t - go kind ((ty, name):xs) n t = go kind xs (n + 1) $ Map.insert name (ty, (kind, n)) t - ---data JSubroutineDec = JSubroutineDec JSubroutineHeader JSubroutineBody deriving (Show, Eq) ---data JSubroutineHeader = JSubroutineHeader JSubroutineType JTypeAndId [JTypeAndId] deriving (Show, Eq) ---data JSubroutineBody = JSubroutineBody [JTypeAndId] [JStatement] deriving (Show, Eq) - -jackCompiler :: Table -> [[Char]] -> [[Char]] -jackCompiler initTable = vmWriter initTable . fmap (fromRight (JClass "" [] []) . jackReader) - -vmWriter :: Table -> [JClass] -> [[Char]] -vmWriter initTable xs = vClass (buildSRTable xs `Map.union` initTable) <$> xs - -vClass :: Table -> JClass -> [Char] -vClass u (JClass name vars subs) = mconcat $ vSubroutineDec name t u n <$> subs where - (t, n) = buildCTable vars - -vSubroutineDec :: [Char] -> Table -> Table -> Int -> JSubroutineDec -> [Char] -vSubroutineDec cName t u n sub = - vFunction (cName ++ "." ++ sName) nLcls ++ kindSpec ++ - vStatement t s u cName sName 0 stmts where - JSubroutineDec (JSubroutineHeader _ (_, sName) args) (JSubroutineBody lcls stmts) = sub - nLcls = length lcls - kind = fst $ u Map.! (cName ++ "." ++ sName) - s = buildLTable (kind == "method") args lcls - kindSpec = if kind == "constructor" - then vNew n - else if kind == "method" - then vPush "argument" 0 ++ vPop "pointer" 0 - else "" - --- data JStatement = JLetStatment JVarId JExpression - -- | JIfStatement JExpression [JStatement] (Maybe [JStatement]) - -- | JWhileStatment JExpression [JStatement] - -- | JDoStatement JSubroutineCall - -- | JReturnStatement (Maybe JExpression) - -vStatement _ _ _ _ _ _ [] = "" - --- vExpression: push the result of exp; vPopToVar: pop to the var addr - -vStatement t s u cName name n ((JLetStatment var exp):stmts) = - vExpression t s u cName exp ++ vPopToVar t s u cName var ++ vStatement t s u cName name n stmts - -vStatement t s u cName name n ((JIfStatement cond thenStmts elseStmts):stmts) = - vExpression t s u cName cond ++ vNot ++ vThen thenStmts ++ - maybe (vLabel labelElse) vElse elseStmts ++ vStatement t s u cName name (n + 1) stmts where - labelElse = name ++ ".Else" ++ show n - labelEndIf = name ++ ".Endif" ++ show n - labelIf = name ++ ".If" ++ show n - vThen xs = vIfGoto labelElse ++ vStatement t s u cName labelElse 0 xs - vElse xs = vGoto labelEndIf ++ vLabel labelElse ++ - vStatement t s u cName labelIf 0 xs ++ vLabel labelEndIf - -vStatement t s u cName name n ((JWhileStatment cond loopStmts):stmts) = - vLabel labelWhile ++ vExpression t s u cName cond ++ vNot ++ vIfGoto labelEndWhile ++ - vStatement t s u cName labelWhile 0 loopStmts ++ vGoto labelWhile ++ - vLabel labelEndWhile ++ vStatement t s u cName name (n + 1) stmts where - labelWhile = name ++ ".While" ++ show n - labelEndWhile = name ++ ".EndWhile" ++ show n - -vStatement t s u cName name n ((JDoStatement subCall):stmts) = - vSubroutineCall t s u cName subCall ++ vPop "temp" 0 ++ vStatement t s u cName name n stmts - -vStatement t s u cName name n ((JReturnStatement ret):stmts) = - maybe (vPush "constant" 0) (vExpression t s u cName) ret ++ vReturn ++ - vStatement t s u cName name n stmts - --- data JExpression = JIntConst Int - -- | JStrConst [Char] - -- | JKeywordConst [Char] -- can only be true, false, null or this - -- | JExpVar JVarId - -- | JExpCall JSubroutineCall - -- | JExpUna JUOp JExpression -- JOp can only be - or ~ here - -- | JExpBin JExpression [(JBOp, JExpression)] - -vExpression :: Table -> Table -> Table -> [Char] -> JExpression -> [Char] - -vExpression t s _ _ (JIntConst n) = vPush "constant" n - -vExpression t s _ _ (JStrConst xs) = - vPush "constant" (length xs) ++ vCall "String.new" 1 ++ mconcat (go <$> xs) where - go x = vPush "constant" (ord x) ++ vCall "String.appendChar" 2 - -vExpression t s _ _ (JKeywordConst x) - | x == "null" || x == "false" = vPush "constant" 0 - | x == "true" = vPush "constant" 1 ++ vNeg - | x == "this" = vPush "pointer" 0 - -vExpression t s u cName (JExpVar (JVarId name idx)) = vVar t s u cName name idx vPush - -vExpression t s u cName (JExpCall subCall) = vSubroutineCall t s u cName subCall - -vExpression t s u cName (JExpUna op exp) = vExpression t s u cName exp ++ vOp op - where vOp '~' = vNot; vOp '-' = vNeg; - -vExpression t s u cName (JExpBin exp xs) = - vExpression t s u cName exp ++ mconcat (go <$> xs) - where go (op, exp) = vExpression t s u cName exp ++ vOp op - vOp '+' = vAdd; vOp '-' = vSub; vOp '*' = vMul; vOp '/' = vDiv; - vOp '&' = vAnd; vOp '|' = vOr; vOp '<' = vLt; vOp '>' = vGt; - vOp '=' = vEq; - -vPopToVar t s u cName (JVarId name idx) = vVar t s u cName name idx vPop - -vVar :: Table -> Table -> Table -> [Char] -> [Char] -> Maybe JExpression -> ([Char] -> Int -> [Char]) -> [Char] -vVar t s u cName name idx vPushOrPop = - let (seg, n) = getSegN t s name in - case idx of - Nothing -> vPushOrPop seg n - Just exp -> vPush seg n ++ vExpression t s u cName exp ++ vAdd - ++ vPop "pointer" 1 ++ vPushOrPop "that" 0 - - -getRecord :: Table -> Table -> [Char] -> ([Char], ([Char], Int)) -getRecord t s name = case Map.lookup name s of - Just x -> x - Nothing -> t Map.! name - -getSegN :: Table -> Table -> [Char] -> ([Char], Int) -getSegN t s name = snd $ getRecord t s name - -getType :: Table -> Table -> [Char] -> [Char] -getType t s name = fst $ getRecord t s name - --- data JSubroutineCall = JSubroutineCall JIdentifier (Maybe JIdentifier) [JExpression] deriving (Show, Eq) -vSubroutineCall t s u cName (JSubroutineCall name name' args) = - method ++ mconcat (vExpression t s u cName <$> args) ++ vCall name'' nArgs - where - name'' = if name' == Nothing - then cName ++ "." ++ name - else if head name `elem` ['A' .. 'Z'] - then name ++ "." ++ fromJust name' - else getType t s name ++ "." ++ fromJust name' - -- u is the SRTable - (method, nArgs) = - if fst (u Map.! name'') == "method" - then if name' == Nothing - then (vPush "pointer" 0, length args + 1) - else let (seg, n) = getSegN t s name in (vPush seg n, length args + 1) - else ("", length args) - -vNew n = vPush "constant" n ++ vCall "Memory.alloc" 1 ++ vPop "pointer" 0 -vAdd = "add\n" -vNot = "not\n" -vNeg = "neg\n" -vSub = "sub\n" -vMul = "call Math.multiply 2\n" -vDiv = "call Math.divide 2\n" -vAnd = "and\n" -vOr = "or\n" -vLt = "lt\n" -vGt = "gt\n" -vEq = "eq\n" -vReturn = "return\n" -vLabel xs = "label " ++ xs ++ "\n" -vGoto xs = "goto " ++ xs ++ "\n" -vIfGoto xs = "if-goto " ++ xs ++ "\n" -vFunction xs n = "function " ++ xs ++ " " ++ show n ++ "\n" -vCall xs n = "call " ++ xs ++ " " ++ show n ++ "\n" -vPush xs n = "push " ++ xs ++ " " ++ show n ++ "\n" -vPop xs n = "pop " ++ xs ++ " " ++ show n ++ "\n" - - --- IO - --- reader of system subroutines' headers -jClasses :: JackParser [JClass] -jClasses = many (try $ many jSpace >> jClass) - -sysSubroutineTable :: IO Table -sysSubroutineTable = do - x <- readFile "./systemsub.txt" - return $ buildSRTable $ head $ rights [parse' jClasses x] - -main = do - dir <- head <$> getArgs - filesWODir <- filter isJackFile <$> listDirectory dir - let jackFiles = (dir++) <$> filesWODir - codes <- sequence $ readFile <$> jackFiles - initTable <- sysSubroutineTable - zipWithM writeFile (chExt <$> jackFiles) (jackCompiler initTable codes) - where isJackFile xs = drop (length xs - 5) xs == ".jack" - chExt xs = take (length xs - 4) xs ++ "vm" - --} - - ---testing - -testCompiler :: [[Char]] -> IO () -testCompiler xs = do - initTable <- sysSubroutineTable - print $ jackCompiler initTable xs - -testCompiler' :: IO () -testCompiler' = do - --xs <- readFile "./Square/Square.jack" - xs <- readFile "./Pong/Ball.jack" - testCompiler [xs] - -testReader :: IO () -testReader = do - --xs <- readFile "./Square/Square.jack" - xs <- readFile "./Pong/Ball.jack" - --print $ buildSRTable $ rights $ [jackReader $ xs] - print $ jackReader $ xs - - ---test reader writer x = let Right y = parse' reader x in writer y ---test' x = let Right y = parse' jStatement x in vStatement Map.empty Map.empty Map.empty "" 0 [y] - - diff --git a/projects/11/JackCompiler.o b/projects/11/JackCompiler.o deleted file mode 100644 index 49a6ac5..0000000 Binary files a/projects/11/JackCompiler.o and /dev/null differ diff --git a/projects/11/K/Board.jack b/projects/11/K/Board.jack deleted file mode 100644 index 49d42d5..0000000 --- a/projects/11/K/Board.jack +++ /dev/null @@ -1,305 +0,0 @@ -class Board { - field Array grid; - field int nTurn; - field int seed; - - constructor Board new() { - var int i, j; - var Array t; - let grid = Array.new(4); - let i = 0; - while (i < 4) { - let j = 0; - let grid[i] = Array.new(4); - let t = grid[i]; - while (j < 4) { - let t[j] = 32; - let j = j + 1; - } - let i = i + 1; - } - let t = grid[0]; - let t[0] = 65; - let nTurn = 0; - let seed = 0; - return this; - } - - method void arrange(Array xs, boolean isHorizontal) { - if (isHorizontal) { - do deepcopy(xs, grid); - } else { - do transpose(xs, grid); - } - do dispose4(xs); - return; - } - - - method void copy(Array xs, Array ys) { - var int i; - let i = 0; - while (i < 4) { - let ys[i] = xs[i]; - let i = i + 1; - } - return; - } - - method void deepcopy(Array xs, Array ys) { - var int i; - let i = 0; - while (i < 4) { - do copy(xs[i], ys[i]); - let i = i + 1; - } - return; - } - - method void transpose(Array xs, Array ys) { - var int i, j; - var Array t, s; - let i = 0; - while (i < 4) { - let j = 0; - let t = ys[i]; - while (j < 4) { - let s = xs[j]; - let t[j] = s[i]; - let j = j + 1; - } - let i = i + 1; - } - return; - } - - method void dispose4(Array xs){ - var int i; - var Array t; - let i = 0; - while (i < 4) { - let t = xs[i]; - do t.dispose(); - let i = i + 1; - } - do xs.dispose(); - return; - } - - method Array new4(){ - var Array xs; - var int i; - let xs = Array.new(4); - let i = 0; - while (i < 4) { - let xs[i] = Array.new(4); - let i = i + 1; - } - return xs; - } - - method Array getStrips(boolean isHorizontal){ - var Array xs; - let xs = new4(); - if (isHorizontal) { - do deepcopy(grid, xs); - } else { - do transpose(grid, xs); - } - return xs; - } - - method Array align(Array xs, boolean left){ - var int i, j; - let i = 0; - let j = 0; - if (left) { - while (i < 4) { - if (xs[i] > 64) { - let xs[j] = xs[i]; - let j = j + 1; - } - let i = i + 1; - } - while (j < 4) { - let xs[j] = 32; - let j = j + 1; - } - } else { - while (i < 4) { - if (xs[3 - i] > 64) { - let xs[3 - j] = xs[3 - i]; - let j = j + 1; - } - let i = i + 1; - } - while (j < 4) { - let xs[3 - j] = 32; - let j = j + 1; - } - } - return xs; - } - - method Array reduce(Array xs, boolean left){ - if ((xs[0] = xs[1]) & (xs[2] = xs[3]) & (xs[0] > 64) & (xs[2] > 64)) { - if (left) { - let xs[0] = xs[0] + 1; - let xs[1] = xs[2] + 1; - let xs[2] = 32; - let xs[3] = 32; - } else { - let xs[3] = xs[3] + 1; - let xs[2] = xs[1] + 1; - let xs[1] = 32; - let xs[0] = 32; - } - return xs; - } - if ((xs[0] = xs[1]) & (xs[0] > 64)) { - if (left) { - let xs[0] = xs[0] + 1; - let xs[1] = xs[2]; - let xs[2] = xs[3]; - let xs[3] = 32; - } else { - let xs[1] = xs[1] + 1; - let xs[0] = 32; - } - return xs; - } - if ((xs[2] = xs[3]) & (xs[2] > 64)) { - if (left) { - let xs[2] = xs[2] + 1; - let xs[3] = 32; - } else { - let xs[3] = xs[3] + 1; - let xs[2] = xs[1]; - let xs[1] = xs[0]; - let xs[0] = 32; - } - return xs; - } - if ((xs[1] = xs[2]) & (xs[1] > 64)) { - if (left) { - let xs[1] = xs[1] + 1; - let xs[2] = xs[3]; - let xs[3] = 32; - } else { - let xs[2] = xs[2] + 1; - let xs[1] = xs[0]; - let xs[0] = 32; - } - return xs; - } - return xs; - } - - method void addTile(){ - var Array t; - var int r, c, parity, newTile; - /* - let t = grid[1]; - if (t[1] = 32) { - let t[1] = 65; - } - */ - let seed = seed * 25173 + 13849; - if (seed < 0) { - let seed = - seed; - } - - if (seed - (seed / 2 * 2) = 0) { - let parity = 1; - } else { - let parity = -1; - } - - let seed = seed - (seed / 16 * 16); - let r = seed / 4; - let c = seed - (4 * r); - let t = grid[r]; - let newTile = 65; - - while (t[c] > 64){ - let seed = seed + parity; - if (seed < 0) { - let seed = 15; - } - let seed = seed - (seed / 16 * 16); - let r = seed / 4; - let c = seed - (4 * r); - let t = grid[r]; - let newTile = 131 - newTile; - } - let t[c] = newTile; - return; - } - - method void transform(char dir){ - var boolean isHorizontal, left; - var Array xs; - var int i; - if ((dir = 0) | (dir = 1)) { - let left = true; - } else { - let left = false; - } - if ((dir = 0) | (dir = 2)) { - let isHorizontal = true; - } else { - let isHorizontal = false; - } - let xs = getStrips(isHorizontal); - let i = 0; - while (i < 4) { - let xs[i] = reduce(align(xs[i], left), left); - let i = i + 1; - } - do arrange(xs, isHorizontal); - return; - } - - method void next(char dir){ - let nTurn = nTurn + 1; - do transform(dir); - do addTile(); - return; - } - - method void draw(){ - var int r, c, i, j; - var Array t; - let r = 9; - let c = 30; - - do Output.moveCursor(r - 1, c - 1); - do Output.printString("+----+"); - do Output.moveCursor(r + 4, c - 1); - do Output.printString("+----+"); - - let i = 0; - while (i < 4) { - let j = 0; - do Output.moveCursor(r + i, c - 1); - do Output.printString("|"); - let t = grid[i]; - while (j < 4) { - do Output.printChar(t[j]); - let j = j + 1; - } - do Output.printString("|"); - let i = i + 1; - } - - do Output.moveCursor(r + 6, c - 2); - do Output.printString("Turn: "); - do Output.printInt(nTurn); - return; - } - - method void dispose() { - do Memory.deAlloc(this); - return; - } -} diff --git a/projects/11/K/Board.vm b/projects/11/K/Board.vm deleted file mode 100644 index 272b52d..0000000 --- a/projects/11/K/Board.vm +++ /dev/null @@ -1,1183 +0,0 @@ -function Board.new 3 -push constant 3 -call Memory.alloc 1 -pop pointer 0 -push constant 4 -call Array.new 1 -pop this 0 -push constant 0 -pop local 0 -label new.While0 -push local 0 -push constant 4 -lt -not -if-goto new.EndWhile0 -push constant 0 -pop local 1 -push constant 4 -call Array.new 1 -push this 0 -push local 0 -add -pop pointer 1 -pop that 0 -push this 0 -push local 0 -add -pop pointer 1 -push that 0 -pop local 2 -label new.While0.While0 -push local 1 -push constant 4 -lt -not -if-goto new.While0.EndWhile0 -push constant 32 -push local 2 -push local 1 -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -goto new.While0.While0 -label new.While0.EndWhile0 -push local 0 -push constant 1 -add -pop local 0 -goto new.While0 -label new.EndWhile0 -push this 0 -push constant 0 -add -pop pointer 1 -push that 0 -pop local 2 -push constant 65 -push local 2 -push constant 0 -add -pop pointer 1 -pop that 0 -push constant 0 -pop this 1 -push constant 0 -pop this 2 -push pointer 0 -return -function Board.arrange 0 -push argument 0 -pop pointer 0 -push argument 2 -not -if-goto arrange.Else0 -push pointer 0 -push argument 1 -push this 0 -call Board.deepcopy 3 -pop temp 0 -goto arrange.Endif0 -label arrange.Else0 -push pointer 0 -push argument 1 -push this 0 -call Board.transpose 3 -pop temp 0 -label arrange.Endif0 -push pointer 0 -push argument 1 -call Board.dispose4 2 -pop temp 0 -push constant 0 -return -function Board.copy 1 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -label copy.While0 -push local 0 -push constant 4 -lt -not -if-goto copy.EndWhile0 -push argument 1 -push local 0 -add -pop pointer 1 -push that 0 -push argument 2 -push local 0 -add -pop pointer 1 -pop that 0 -push local 0 -push constant 1 -add -pop local 0 -goto copy.While0 -label copy.EndWhile0 -push constant 0 -return -function Board.deepcopy 1 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -label deepcopy.While0 -push local 0 -push constant 4 -lt -not -if-goto deepcopy.EndWhile0 -push pointer 0 -push argument 1 -push local 0 -add -pop pointer 1 -push that 0 -push argument 2 -push local 0 -add -pop pointer 1 -push that 0 -call Board.copy 3 -pop temp 0 -push local 0 -push constant 1 -add -pop local 0 -goto deepcopy.While0 -label deepcopy.EndWhile0 -push constant 0 -return -function Board.transpose 4 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -label transpose.While0 -push local 0 -push constant 4 -lt -not -if-goto transpose.EndWhile0 -push constant 0 -pop local 1 -push argument 2 -push local 0 -add -pop pointer 1 -push that 0 -pop local 2 -label transpose.While0.While0 -push local 1 -push constant 4 -lt -not -if-goto transpose.While0.EndWhile0 -push argument 1 -push local 1 -add -pop pointer 1 -push that 0 -pop local 3 -push local 3 -push local 0 -add -pop pointer 1 -push that 0 -push local 2 -push local 1 -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -goto transpose.While0.While0 -label transpose.While0.EndWhile0 -push local 0 -push constant 1 -add -pop local 0 -goto transpose.While0 -label transpose.EndWhile0 -push constant 0 -return -function Board.dispose4 2 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -label dispose4.While0 -push local 0 -push constant 4 -lt -not -if-goto dispose4.EndWhile0 -push argument 1 -push local 0 -add -pop pointer 1 -push that 0 -pop local 1 -push local 1 -call Array.dispose 1 -pop temp 0 -push local 0 -push constant 1 -add -pop local 0 -goto dispose4.While0 -label dispose4.EndWhile0 -push argument 1 -call Array.dispose 1 -pop temp 0 -push constant 0 -return -function Board.new4 2 -push argument 0 -pop pointer 0 -push constant 4 -call Array.new 1 -pop local 0 -push constant 0 -pop local 1 -label new4.While0 -push local 1 -push constant 4 -lt -not -if-goto new4.EndWhile0 -push constant 4 -call Array.new 1 -push local 0 -push local 1 -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -goto new4.While0 -label new4.EndWhile0 -push local 0 -return -function Board.getStrips 1 -push argument 0 -pop pointer 0 -push pointer 0 -call Board.new4 1 -pop local 0 -push argument 1 -not -if-goto getStrips.Else0 -push pointer 0 -push this 0 -push local 0 -call Board.deepcopy 3 -pop temp 0 -goto getStrips.Endif0 -label getStrips.Else0 -push pointer 0 -push this 0 -push local 0 -call Board.transpose 3 -pop temp 0 -label getStrips.Endif0 -push local 0 -return -function Board.align 2 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -push constant 0 -pop local 1 -push argument 2 -not -if-goto align.Else0 -label align.Else0.While0 -push local 0 -push constant 4 -lt -not -if-goto align.Else0.EndWhile0 -push argument 1 -push local 0 -add -pop pointer 1 -push that 0 -push constant 64 -gt -not -if-goto align.Else0.While0.Else0 -push argument 1 -push local 0 -add -pop pointer 1 -push that 0 -push argument 1 -push local 1 -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -label align.Else0.While0.Else0 -push local 0 -push constant 1 -add -pop local 0 -goto align.Else0.While0 -label align.Else0.EndWhile0 -label align.Else0.While1 -push local 1 -push constant 4 -lt -not -if-goto align.Else0.EndWhile1 -push constant 32 -push argument 1 -push local 1 -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -goto align.Else0.While1 -label align.Else0.EndWhile1 -goto align.Endif0 -label align.Else0 -label align.If0.While0 -push local 0 -push constant 4 -lt -not -if-goto align.If0.EndWhile0 -push argument 1 -push constant 3 -push local 0 -sub -add -pop pointer 1 -push that 0 -push constant 64 -gt -not -if-goto align.If0.While0.Else0 -push argument 1 -push constant 3 -push local 0 -sub -add -pop pointer 1 -push that 0 -push argument 1 -push constant 3 -push local 1 -sub -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -label align.If0.While0.Else0 -push local 0 -push constant 1 -add -pop local 0 -goto align.If0.While0 -label align.If0.EndWhile0 -label align.If0.While1 -push local 1 -push constant 4 -lt -not -if-goto align.If0.EndWhile1 -push constant 32 -push argument 1 -push constant 3 -push local 1 -sub -add -pop pointer 1 -pop that 0 -push local 1 -push constant 1 -add -pop local 1 -goto align.If0.While1 -label align.If0.EndWhile1 -label align.Endif0 -push argument 1 -return -function Board.reduce 0 -push argument 0 -pop pointer 0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -eq -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -eq -and -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push constant 64 -gt -and -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push constant 64 -gt -and -not -if-goto reduce.Else0 -push argument 2 -not -if-goto reduce.Else0.Else0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -goto reduce.Else0.Endif0 -label reduce.Else0.Else0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -label reduce.Else0.Endif0 -push argument 1 -return -label reduce.Else0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -eq -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push constant 64 -gt -and -not -if-goto reduce.Else1 -push argument 2 -not -if-goto reduce.Else1.Else0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -goto reduce.Else1.Endif0 -label reduce.Else1.Else0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -label reduce.Else1.Endif0 -push argument 1 -return -label reduce.Else1 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -eq -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push constant 64 -gt -and -not -if-goto reduce.Else2 -push argument 2 -not -if-goto reduce.Else2.Else0 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -goto reduce.Else2.Endif0 -label reduce.Else2.Else0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -label reduce.Else2.Endif0 -push argument 1 -return -label reduce.Else2 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -eq -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push constant 64 -gt -and -not -if-goto reduce.Else3 -push argument 2 -not -if-goto reduce.Else3.Else0 -push argument 1 -push constant 1 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 3 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 3 -add -pop pointer 1 -pop that 0 -goto reduce.Else3.Endif0 -label reduce.Else3.Else0 -push argument 1 -push constant 2 -add -pop pointer 1 -push that 0 -push constant 1 -add -push argument 1 -push constant 2 -add -pop pointer 1 -pop that 0 -push argument 1 -push constant 0 -add -pop pointer 1 -push that 0 -push argument 1 -push constant 1 -add -pop pointer 1 -pop that 0 -push constant 32 -push argument 1 -push constant 0 -add -pop pointer 1 -pop that 0 -label reduce.Else3.Endif0 -push argument 1 -return -label reduce.Else3 -push argument 1 -return -function Board.addTile 5 -push argument 0 -pop pointer 0 -push this 2 -push constant 25173 -call Math.multiply 2 -push constant 13849 -add -pop this 2 -push this 2 -push constant 0 -lt -not -if-goto addTile.Else0 -push this 2 -neg -pop this 2 -label addTile.Else0 -push this 2 -push this 2 -push constant 2 -call Math.divide 2 -push constant 2 -call Math.multiply 2 -sub -push constant 0 -eq -not -if-goto addTile.Else1 -push constant 1 -pop local 3 -goto addTile.Endif1 -label addTile.Else1 -push constant 1 -neg -pop local 3 -label addTile.Endif1 -push this 2 -push this 2 -push constant 16 -call Math.divide 2 -push constant 16 -call Math.multiply 2 -sub -pop this 2 -push this 2 -push constant 4 -call Math.divide 2 -pop local 1 -push this 2 -push constant 4 -push local 1 -call Math.multiply 2 -sub -pop local 2 -push this 0 -push local 1 -add -pop pointer 1 -push that 0 -pop local 0 -push constant 65 -pop local 4 -label addTile.While2 -push local 0 -push local 2 -add -pop pointer 1 -push that 0 -push constant 64 -gt -not -if-goto addTile.EndWhile2 -push this 2 -push local 3 -add -pop this 2 -push this 2 -push constant 0 -lt -not -if-goto addTile.While2.Else0 -push constant 15 -pop this 2 -label addTile.While2.Else0 -push this 2 -push this 2 -push constant 16 -call Math.divide 2 -push constant 16 -call Math.multiply 2 -sub -pop this 2 -push this 2 -push constant 4 -call Math.divide 2 -pop local 1 -push this 2 -push constant 4 -push local 1 -call Math.multiply 2 -sub -pop local 2 -push this 0 -push local 1 -add -pop pointer 1 -push that 0 -pop local 0 -push constant 131 -push local 4 -sub -pop local 4 -goto addTile.While2 -label addTile.EndWhile2 -push local 4 -push local 0 -push local 2 -add -pop pointer 1 -pop that 0 -push constant 0 -return -function Board.transform 4 -push argument 0 -pop pointer 0 -push argument 1 -push constant 0 -eq -push argument 1 -push constant 1 -eq -or -not -if-goto transform.Else0 -push constant 1 -neg -pop local 1 -goto transform.Endif0 -label transform.Else0 -push constant 0 -pop local 1 -label transform.Endif0 -push argument 1 -push constant 0 -eq -push argument 1 -push constant 2 -eq -or -not -if-goto transform.Else1 -push constant 1 -neg -pop local 0 -goto transform.Endif1 -label transform.Else1 -push constant 0 -pop local 0 -label transform.Endif1 -push pointer 0 -push local 0 -call Board.getStrips 2 -pop local 2 -push constant 0 -pop local 3 -label transform.While2 -push local 3 -push constant 4 -lt -not -if-goto transform.EndWhile2 -push pointer 0 -push pointer 0 -push local 2 -push local 3 -add -pop pointer 1 -push that 0 -push local 1 -call Board.align 3 -push local 1 -call Board.reduce 3 -push local 2 -push local 3 -add -pop pointer 1 -pop that 0 -push local 3 -push constant 1 -add -pop local 3 -goto transform.While2 -label transform.EndWhile2 -push pointer 0 -push local 2 -push local 0 -call Board.arrange 3 -pop temp 0 -push constant 0 -return -function Board.next 0 -push argument 0 -pop pointer 0 -push this 1 -push constant 1 -add -pop this 1 -push pointer 0 -push argument 1 -call Board.transform 2 -pop temp 0 -push pointer 0 -call Board.addTile 1 -pop temp 0 -push constant 0 -return -function Board.draw 5 -push argument 0 -pop pointer 0 -push constant 9 -pop local 0 -push constant 30 -pop local 1 -push local 0 -push constant 1 -sub -push local 1 -push constant 1 -sub -call Output.moveCursor 2 -pop temp 0 -push constant 6 -call String.new 1 -push constant 43 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 43 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 0 -push constant 4 -add -push local 1 -push constant 1 -sub -call Output.moveCursor 2 -pop temp 0 -push constant 6 -call String.new 1 -push constant 43 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 45 -call String.appendChar 2 -push constant 43 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push constant 0 -pop local 2 -label draw.While0 -push local 2 -push constant 4 -lt -not -if-goto draw.EndWhile0 -push constant 0 -pop local 3 -push local 0 -push local 2 -add -push local 1 -push constant 1 -sub -call Output.moveCursor 2 -pop temp 0 -push constant 1 -call String.new 1 -push constant 124 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push this 0 -push local 2 -add -pop pointer 1 -push that 0 -pop local 4 -label draw.While0.While0 -push local 3 -push constant 4 -lt -not -if-goto draw.While0.EndWhile0 -push local 4 -push local 3 -add -pop pointer 1 -push that 0 -call Output.printChar 1 -pop temp 0 -push local 3 -push constant 1 -add -pop local 3 -goto draw.While0.While0 -label draw.While0.EndWhile0 -push constant 1 -call String.new 1 -push constant 124 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push local 2 -push constant 1 -add -pop local 2 -goto draw.While0 -label draw.EndWhile0 -push local 0 -push constant 6 -add -push local 1 -push constant 2 -sub -call Output.moveCursor 2 -pop temp 0 -push constant 6 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push this 1 -call Output.printInt 1 -pop temp 0 -push constant 0 -return -function Board.dispose 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/K/K.txt b/projects/11/K/K.txt deleted file mode 100644 index dc75c82..0000000 --- a/projects/11/K/K.txt +++ /dev/null @@ -1,25 +0,0 @@ -from a to k - -- a title frame showing game name and instructions. space to continue. -- a main frame showing the game running -- a endofgame frame showing game results: win or lose - -- a Board class with cell being a 4 by 4 two-d array. -- arrange strings according to an orientation: arrange(4 strings, dir)=cells - - dir: 0: align to left, 1: align to right, 2: align to top, 3:align to bottom - - e.g.: arrange({"abc", "bc", "dc", "efeh", 0}) gives the following board: - abc_ - bc__ - dc__ - efeh - - arrange({"abc", "bc", "dc", "efeh", 0}, 3) gives the following - ___e - a__f - bbde - ccch -- a new tile of 'a' or 'b' appears each turn somewhere, using some quasirandomisation e.g. (23 * n^2 + 79) mod 16. - - addtile(char, x, y) -- the board transformation: board.trans(dir) = arrange(dir) . (fmap reduce) . getstring(dir). calls getstrings, then reduce on each row / column -- board.getstrings(dir)= the strings according to direction dir. Inverse of arrange. -- reduce(string)=string: reduce("baac")=="bbc"; reduce("aabb")=="bc"; and reduce("cbb")== "cc" instead of "d". -- when one of the hjkl keys is pressed get the direction dir. diff --git a/projects/11/K/KGame.jack b/projects/11/K/KGame.jack deleted file mode 100644 index 796f5b5..0000000 --- a/projects/11/K/KGame.jack +++ /dev/null @@ -1,34 +0,0 @@ -class KGame{ - field Board board; - - constructor KGame new() { - let board = Board.new(); - do board.draw(); - return this; - } - - method void dispose() { - do board.dispose(); - do Memory.deAlloc(this); - return; - } - - method void run() { - var int key, dir; - var boolean exit; - let key = 0; - let exit = false; - while (~exit) { - while ((key < 130) | (key > 133)) { - let key = Keyboard.keyPressed(); - } - let dir = key - 130; - while (~(key = 0)) { - let key = Keyboard.keyPressed(); - } - do board.next(dir); - do board.draw(); - } - return; - } -} diff --git a/projects/11/K/KGame.vm b/projects/11/K/KGame.vm deleted file mode 100644 index eef2df0..0000000 --- a/projects/11/K/KGame.vm +++ /dev/null @@ -1,74 +0,0 @@ -function KGame.new 0 -push constant 1 -call Memory.alloc 1 -pop pointer 0 -call Board.new 0 -pop this 0 -push this 0 -call Board.draw 1 -pop temp 0 -push pointer 0 -return -function KGame.dispose 0 -push argument 0 -pop pointer 0 -push this 0 -call Board.dispose 1 -pop temp 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function KGame.run 3 -push argument 0 -pop pointer 0 -push constant 0 -pop local 0 -push constant 0 -pop local 2 -label run.While0 -push local 2 -not -not -if-goto run.EndWhile0 -label run.While0.While0 -push local 0 -push constant 130 -lt -push local 0 -push constant 133 -gt -or -not -if-goto run.While0.EndWhile0 -call Keyboard.keyPressed 0 -pop local 0 -goto run.While0.While0 -label run.While0.EndWhile0 -push local 0 -push constant 130 -sub -pop local 1 -label run.While0.While1 -push local 0 -push constant 0 -eq -not -not -if-goto run.While0.EndWhile1 -call Keyboard.keyPressed 0 -pop local 0 -goto run.While0.While1 -label run.While0.EndWhile1 -push this 0 -push local 1 -call Board.next 2 -pop temp 0 -push this 0 -call Board.draw 1 -pop temp 0 -goto run.While0 -label run.EndWhile0 -push constant 0 -return diff --git a/projects/11/K/Main.jack b/projects/11/K/Main.jack deleted file mode 100644 index 4136e80..0000000 --- a/projects/11/K/Main.jack +++ /dev/null @@ -1,9 +0,0 @@ -class Main { - function void main() { - var KGame game; - let game = KGame.new(); - do game.run(); - do game.dispose(); - return; - } -} diff --git a/projects/11/K/Main.vm b/projects/11/K/Main.vm deleted file mode 100644 index 0a56ae9..0000000 --- a/projects/11/K/Main.vm +++ /dev/null @@ -1,11 +0,0 @@ -function Main.main 1 -call KGame.new 0 -pop local 0 -push local 0 -call KGame.run 1 -pop temp 0 -push local 0 -call KGame.dispose 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Pong/Ball.jack b/projects/11/Pong/Ball.jack deleted file mode 100644 index 02e47f9..0000000 --- a/projects/11/Pong/Ball.jack +++ /dev/null @@ -1,203 +0,0 @@ -// 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/11/Pong/Ball.jack - -/** - * A graphical ball. Characterized by a screen location and distance of - * last destination. Has methods for drawing, erasing and moving on the screen. - * The ball is displayed as a filled, 6-by-6 pixles rectangle. - */ -class Ball { - - field int x, y; // the ball's screen location (in pixels) - field int lengthx, lengthy; // distance of last destination (in pixels) - - field int d, straightD, diagonalD; // used for straight line movement computation - field boolean invert, positivex, positivey; // (same) - - field int leftWall, rightWall, topWall, bottomWall; // wall locations - - field int wall; // last wall that the ball was bounced off of - - /** Constructs a new ball with the given initial location and wall locations. */ - constructor Ball new(int Ax, int Ay, - int AleftWall, int ArightWall, int AtopWall, int AbottomWall) { - let x = Ax; - let y = Ay; - let leftWall = AleftWall; - let rightWall = ArightWall - 6; // -6 for ball size - let topWall = AtopWall; - let bottomWall = AbottomWall - 6; // -6 for ball size - let wall = 0; - do show(); - return this; - } - - /** Deallocates the Ball's memory. */ - method void dispose() { - do Memory.deAlloc(this); - return; - } - - /** Shows the ball. */ - method void show() { - do Screen.setColor(true); - do draw(); - return; - } - - /** Hides the ball. */ - method void hide() { - do Screen.setColor(false); - do draw(); - return; - } - - /** Draws the ball. */ - method void draw() { - do Screen.drawRectangle(x, y, x + 5, y + 5); - return; - } - - /** Returns the ball's left edge. */ - method int getLeft() { - return x; - } - - /** Returns the ball's right edge. */ - method int getRight() { - return x + 5; - } - - /** Computes and sets the ball's destination. */ - method void setDestination(int destx, int desty) { - var int dx, dy, temp; - let lengthx = destx - x; - let lengthy = desty - y; - let dx = Math.abs(lengthx); - let dy = Math.abs(lengthy); - let invert = (dx < dy); - - if (invert) { - let temp = dx; // swap dx, dy - let dx = dy; - let dy = temp; - let positivex = (y < desty); - let positivey = (x < destx); - } - else { - let positivex = (x < destx); - let positivey = (y < desty); - } - - let d = (2 * dy) - dx; - let straightD = 2 * dy; - let diagonalD = 2 * (dy - dx); - - return; - } - - /** - * Moves the ball one unit towards its destination. - * If the ball has reached a wall, returns 0. - * Else, returns a value according to the wall: - * 1 (left wall), 2 (right wall), 3 (top wall), 4 (bottom wall). - */ - method int move() { - - do hide(); - - if (d < 0) { let d = d + straightD; } - else { - let d = d + diagonalD; - - if (positivey) { - if (invert) { let x = x + 4; } - else { let y = y + 4; } - } - else { - if (invert) { let x = x - 4; } - else { let y = y - 4; } - } - } - - if (positivex) { - if (invert) { let y = y + 4; } - else { let x = x + 4; } - } - else { - if (invert) { let y = y - 4; } - else { let x = x - 4; } - } - - if (~(x > leftWall)) { - let wall = 1; - let x = leftWall; - } - if (~(x < rightWall)) { - let wall = 2; - let x = rightWall; - } - if (~(y > topWall)) { - let wall = 3; - let y = topWall; - } - if (~(y < bottomWall)) { - let wall = 4; - let y = bottomWall; - } - - do show(); - - return wall; - } - - /** - * Bounces off the current wall: sets the new destination - * of the ball according to the ball's angle and the given - * bouncing direction (-1/0/1=left/center/right or up/center/down). - */ - method void bounce(int bouncingDirection) { - var int newx, newy, divLengthx, divLengthy, factor; - - // dividing by 10 first since results are too big - let divLengthx = lengthx / 10; - let divLengthy = lengthy / 10; - if (bouncingDirection = 0) { let factor = 10; } - else { - if (((~(lengthx < 0)) & (bouncingDirection = 1)) | ((lengthx < 0) & (bouncingDirection = (-1)))) { - let factor = 20; // bounce direction is in ball direction - } - else { let factor = 5; } // bounce direction is against ball direction - } - - if (wall = 1) { - let newx = 506; - let newy = (divLengthy * (-50)) / divLengthx; - let newy = y + (newy * factor); - } - else { - if (wall = 2) { - let newx = 0; - let newy = (divLengthy * 50) / divLengthx; - let newy = y + (newy * factor); - } - else { - if (wall = 3) { - let newy = 250; - let newx = (divLengthx * (-25)) / divLengthy; - let newx = x + (newx * factor); - } - else { // assumes wall = 4 - let newy = 0; - let newx = (divLengthx * 25) / divLengthy; - let newx = x + (newx * factor); - } - } - } - - do setDestination(newx, newy); - return; - } -} diff --git a/projects/11/Pong/Ball.vm b/projects/11/Pong/Ball.vm deleted file mode 100644 index 9d7b984..0000000 --- a/projects/11/Pong/Ball.vm +++ /dev/null @@ -1,427 +0,0 @@ -function Ball.new 0 -push constant 15 -call Memory.alloc 1 -pop pointer 0 -push argument 0 -pop this 0 -push argument 1 -pop this 1 -push argument 2 -pop this 10 -push argument 3 -push constant 6 -sub -pop this 11 -push argument 4 -pop this 12 -push argument 5 -push constant 6 -sub -pop this 13 -push constant 0 -pop this 14 -push pointer 0 -call Ball.show 1 -pop temp 0 -push pointer 0 -return -function Ball.dispose 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function Ball.show 0 -push argument 0 -pop pointer 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push pointer 0 -call Ball.draw 1 -pop temp 0 -push constant 0 -return -function Ball.hide 0 -push argument 0 -pop pointer 0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push pointer 0 -call Ball.draw 1 -pop temp 0 -push constant 0 -return -function Ball.draw 0 -push argument 0 -pop pointer 0 -push this 0 -push this 1 -push this 0 -push constant 5 -add -push this 1 -push constant 5 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Ball.getLeft 0 -push argument 0 -pop pointer 0 -push this 0 -return -function Ball.getRight 0 -push argument 0 -pop pointer 0 -push this 0 -push constant 5 -add -return -function Ball.setDestination 3 -push argument 0 -pop pointer 0 -push argument 1 -push this 0 -sub -pop this 2 -push argument 2 -push this 1 -sub -pop this 3 -push this 2 -call Math.abs 1 -pop local 0 -push this 3 -call Math.abs 1 -pop local 1 -push local 0 -push local 1 -lt -pop this 7 -push this 7 -not -if-goto setDestination.Else0 -push local 0 -pop local 2 -push local 1 -pop local 0 -push local 2 -pop local 1 -push this 1 -push argument 2 -lt -pop this 8 -push this 0 -push argument 1 -lt -pop this 9 -goto setDestination.Endif0 -label setDestination.Else0 -push this 0 -push argument 1 -lt -pop this 8 -push this 1 -push argument 2 -lt -pop this 9 -label setDestination.Endif0 -push constant 2 -push local 1 -call Math.multiply 2 -push local 0 -sub -pop this 4 -push constant 2 -push local 1 -call Math.multiply 2 -pop this 5 -push constant 2 -push local 1 -push local 0 -sub -call Math.multiply 2 -pop this 6 -push constant 0 -return -function Ball.move 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Ball.hide 1 -pop temp 0 -push this 4 -push constant 0 -lt -not -if-goto move.Else0 -push this 4 -push this 5 -add -pop this 4 -goto move.Endif0 -label move.Else0 -push this 4 -push this 6 -add -pop this 4 -push this 9 -not -if-goto move.If0.Else0 -push this 7 -not -if-goto move.If0.Else0.Else0 -push this 0 -push constant 4 -add -pop this 0 -goto move.If0.Else0.Endif0 -label move.If0.Else0.Else0 -push this 1 -push constant 4 -add -pop this 1 -label move.If0.Else0.Endif0 -goto move.If0.Endif0 -label move.If0.Else0 -push this 7 -not -if-goto move.If0.If0.Else0 -push this 0 -push constant 4 -sub -pop this 0 -goto move.If0.If0.Endif0 -label move.If0.If0.Else0 -push this 1 -push constant 4 -sub -pop this 1 -label move.If0.If0.Endif0 -label move.If0.Endif0 -label move.Endif0 -push this 8 -not -if-goto move.Else1 -push this 7 -not -if-goto move.Else1.Else0 -push this 1 -push constant 4 -add -pop this 1 -goto move.Else1.Endif0 -label move.Else1.Else0 -push this 0 -push constant 4 -add -pop this 0 -label move.Else1.Endif0 -goto move.Endif1 -label move.Else1 -push this 7 -not -if-goto move.If1.Else0 -push this 1 -push constant 4 -sub -pop this 1 -goto move.If1.Endif0 -label move.If1.Else0 -push this 0 -push constant 4 -sub -pop this 0 -label move.If1.Endif0 -label move.Endif1 -push this 0 -push this 10 -gt -not -not -if-goto move.Else2 -push constant 1 -pop this 14 -push this 10 -pop this 0 -label move.Else2 -push this 0 -push this 11 -lt -not -not -if-goto move.Else3 -push constant 2 -pop this 14 -push this 11 -pop this 0 -label move.Else3 -push this 1 -push this 12 -gt -not -not -if-goto move.Else4 -push constant 3 -pop this 14 -push this 12 -pop this 1 -label move.Else4 -push this 1 -push this 13 -lt -not -not -if-goto move.Else5 -push constant 4 -pop this 14 -push this 13 -pop this 1 -label move.Else5 -push pointer 0 -call Ball.show 1 -pop temp 0 -push this 14 -return -function Ball.bounce 5 -push argument 0 -pop pointer 0 -push this 2 -push constant 10 -call Math.divide 2 -pop local 2 -push this 3 -push constant 10 -call Math.divide 2 -pop local 3 -push argument 1 -push constant 0 -eq -not -if-goto bounce.Else0 -push constant 10 -pop local 4 -goto bounce.Endif0 -label bounce.Else0 -push this 2 -push constant 0 -lt -not -push argument 1 -push constant 1 -eq -and -push this 2 -push constant 0 -lt -push argument 1 -push constant 1 -neg -eq -and -or -not -if-goto bounce.If0.Else0 -push constant 20 -pop local 4 -goto bounce.If0.Endif0 -label bounce.If0.Else0 -push constant 5 -pop local 4 -label bounce.If0.Endif0 -label bounce.Endif0 -push this 14 -push constant 1 -eq -not -if-goto bounce.Else1 -push constant 506 -pop local 0 -push local 3 -push constant 50 -neg -call Math.multiply 2 -push local 2 -call Math.divide 2 -pop local 1 -push this 1 -push local 1 -push local 4 -call Math.multiply 2 -add -pop local 1 -goto bounce.Endif1 -label bounce.Else1 -push this 14 -push constant 2 -eq -not -if-goto bounce.If1.Else0 -push constant 0 -pop local 0 -push local 3 -push constant 50 -call Math.multiply 2 -push local 2 -call Math.divide 2 -pop local 1 -push this 1 -push local 1 -push local 4 -call Math.multiply 2 -add -pop local 1 -goto bounce.If1.Endif0 -label bounce.If1.Else0 -push this 14 -push constant 3 -eq -not -if-goto bounce.If1.If0.Else0 -push constant 250 -pop local 1 -push local 2 -push constant 25 -neg -call Math.multiply 2 -push local 3 -call Math.divide 2 -pop local 0 -push this 0 -push local 0 -push local 4 -call Math.multiply 2 -add -pop local 0 -goto bounce.If1.If0.Endif0 -label bounce.If1.If0.Else0 -push constant 0 -pop local 1 -push local 2 -push constant 25 -call Math.multiply 2 -push local 3 -call Math.divide 2 -pop local 0 -push this 0 -push local 0 -push local 4 -call Math.multiply 2 -add -pop local 0 -label bounce.If1.If0.Endif0 -label bounce.If1.Endif0 -label bounce.Endif1 -push pointer 0 -push local 0 -push local 1 -call Ball.setDestination 3 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Pong/Bat.jack b/projects/11/Pong/Bat.jack deleted file mode 100644 index 340760f..0000000 --- a/projects/11/Pong/Bat.jack +++ /dev/null @@ -1,103 +0,0 @@ -// 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/11/Pong/Bat.jack - -/** - * A graphical Pong bat. - * Displayed as a filled horizontal rectangle that has - * a screen location, a width and a height. - * Has methods for drawing, erasing, moving left and right, - * and changing its width (to make the hitting action more challenging). - * This class should have been called "paddle", following the - * standard Pong terminology. But, unaware of this terminology, - * we called it "bat", and then decided to stick to it. - */ -class Bat { - - field int x, y; // the bat's screen location - field int width, height; // the bat's width and height - field int direction; // direction of the bat's movement (1 = left, 2 = right) - - /** Constructs a new bat with the given location and width. */ - constructor Bat new(int Ax, int Ay, int Awidth, int Aheight) { - let x = Ax; - let y = Ay; - let width = Awidth; - let height = Aheight; - let direction = 2; - do show(); - return this; - } - - /** Deallocates the object's memory. */ - method void dispose() { - do Memory.deAlloc(this); - return; - } - - /** Shows the bat. */ - method void show() { - do Screen.setColor(true); - do draw(); - return; - } - - /** Hides the bat. */ - method void hide() { - do Screen.setColor(false); - do draw(); - return; - } - - /** Draws the bat. */ - method void draw() { - do Screen.drawRectangle(x, y, x + width, y + height); - return; - } - - /** Sets the bat's direction (0=stop, 1=left, 2=right). */ - method void setDirection(int Adirection) { - let direction = Adirection; - return; - } - - /** Returns the bat's left edge. */ - method int getLeft() { - return x; - } - - /** Returns the bat's right edge. */ - method int getRight() { - return x + width; - } - - /** Sets the bat's width. */ - method void setWidth(int Awidth) { - do hide(); - let width = Awidth; - do show(); - return; - } - - /** Moves the bat one step in the bat's direction. */ - method void move() { - if (direction = 1) { - let x = x - 4; - if (x < 0) { let x = 0; } - do Screen.setColor(false); - do Screen.drawRectangle((x + width) + 1, y, (x + width) + 4, y + height); - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + 3, y + height); - } - else { - let x = x + 4; - if ((x + width) > 511) { let x = 511 - width; } - do Screen.setColor(false); - do Screen.drawRectangle(x - 4, y, x - 1, y + height); - do Screen.setColor(true); - do Screen.drawRectangle((x + width) - 3, y, x + width, y + height); - } - return; - } -} diff --git a/projects/11/Pong/Bat.vm b/projects/11/Pong/Bat.vm deleted file mode 100644 index 77acfe4..0000000 --- a/projects/11/Pong/Bat.vm +++ /dev/null @@ -1,204 +0,0 @@ -function Bat.new 0 -push constant 5 -call Memory.alloc 1 -pop pointer 0 -push argument 0 -pop this 0 -push argument 1 -pop this 1 -push argument 2 -pop this 2 -push argument 3 -pop this 3 -push constant 2 -pop this 4 -push pointer 0 -call Bat.show 1 -pop temp 0 -push pointer 0 -return -function Bat.dispose 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function Bat.show 0 -push argument 0 -pop pointer 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push pointer 0 -call Bat.draw 1 -pop temp 0 -push constant 0 -return -function Bat.hide 0 -push argument 0 -pop pointer 0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push pointer 0 -call Bat.draw 1 -pop temp 0 -push constant 0 -return -function Bat.draw 0 -push argument 0 -pop pointer 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 3 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Bat.setDirection 0 -push argument 0 -pop pointer 0 -push argument 1 -pop this 4 -push constant 0 -return -function Bat.getLeft 0 -push argument 0 -pop pointer 0 -push this 0 -return -function Bat.getRight 0 -push argument 0 -pop pointer 0 -push this 0 -push this 2 -add -return -function Bat.setWidth 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Bat.hide 1 -pop temp 0 -push argument 1 -pop this 2 -push pointer 0 -call Bat.show 1 -pop temp 0 -push constant 0 -return -function Bat.move 0 -push argument 0 -pop pointer 0 -push this 4 -push constant 1 -eq -not -if-goto move.Else0 -push this 0 -push constant 4 -sub -pop this 0 -push this 0 -push constant 0 -lt -not -if-goto move.Else0.Else0 -push constant 0 -pop this 0 -label move.Else0.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 1 -add -push this 1 -push this 0 -push this 2 -add -push constant 4 -add -push this 1 -push this 3 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push constant 3 -add -push this 1 -push this 3 -add -call Screen.drawRectangle 4 -pop temp 0 -goto move.Endif0 -label move.Else0 -push this 0 -push constant 4 -add -pop this 0 -push this 0 -push this 2 -add -push constant 511 -gt -not -if-goto move.If0.Else0 -push constant 511 -push this 2 -sub -pop this 0 -label move.If0.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push constant 4 -sub -push this 1 -push this 0 -push constant 1 -sub -push this 1 -push this 3 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 3 -sub -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 3 -add -call Screen.drawRectangle 4 -pop temp 0 -label move.Endif0 -push constant 0 -return diff --git a/projects/11/Pong/Main.jack b/projects/11/Pong/Main.jack deleted file mode 100644 index 45e2dd2..0000000 --- a/projects/11/Pong/Main.jack +++ /dev/null @@ -1,20 +0,0 @@ -// 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/11/Pong/Main.jack - -/** - * The main class of the Pong game. - */ -class Main { - - /** Initializes a Pong game and starts running it. */ - function void main() { - var PongGame game; - do PongGame.newInstance(); - let game = PongGame.getInstance(); - do game.run(); - do game.dispose(); - return; - } -} diff --git a/projects/11/Pong/Main.vm b/projects/11/Pong/Main.vm deleted file mode 100644 index 94d76d7..0000000 --- a/projects/11/Pong/Main.vm +++ /dev/null @@ -1,13 +0,0 @@ -function Main.main 1 -call PongGame.newInstance 0 -pop temp 0 -call PongGame.getInstance 0 -pop local 0 -push local 0 -call PongGame.run 1 -pop temp 0 -push local 0 -call PongGame.dispose 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Pong/PongGame.jack b/projects/11/Pong/PongGame.jack deleted file mode 100644 index 7e1ae4c..0000000 --- a/projects/11/Pong/PongGame.jack +++ /dev/null @@ -1,137 +0,0 @@ -// 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/11/Pong/PongGame.jack - -/** - * Represents a Pong game. - */ -class PongGame { - - static PongGame instance; // the singelton, a Pong game instance - field Bat bat; // the bat - field Ball ball; // the ball - field int wall; // the current wall that the ball is bouncing off of. - field boolean exit; // true when the game is over - field int score; // the current score. - field int lastWall; // the last wall that the ball bounced off of. - - // The current width of the bat - field int batWidth; - - /** Constructs a new Pong game. */ - constructor PongGame new() { - do Screen.clearScreen(); - let batWidth = 50; // initial bat size - let bat = Bat.new(230, 229, batWidth, 7); - let ball = Ball.new(253, 222, 0, 511, 0, 229); - do ball.setDestination(400,0); - do Screen.drawRectangle(0, 238, 511, 240); - do Output.moveCursor(22,0); - do Output.printString("Score: 0"); - - let exit = false; - let score = 0; - let wall = 0; - let lastWall = 0; - - return this; - } - - /** Deallocates the object's memory. */ - method void dispose() { - do bat.dispose(); - do ball.dispose(); - do Memory.deAlloc(this); - return; - } - - /** Creates an instance of Pong game, and stores it. */ - function void newInstance() { - let instance = PongGame.new(); - return; - } - - /** Returns the single instance of this Pong game. */ - function PongGame getInstance() { - return instance; - } - - /** Starts the game, and andles inputs from the user that control - * the bat's movement direction. */ - method void run() { - var char key; - - while (~exit) { - // waits for a key to be pressed. - while ((key = 0) & (~exit)) { - let key = Keyboard.keyPressed(); - do bat.move(); - do moveBall(); - do Sys.wait(50); - } - - if (key = 130) { do bat.setDirection(1); } - else { - if (key = 132) { do bat.setDirection(2); } - else { - if (key = 140) { let exit = true; } - } - } - - // Waits for the key to be released. - while ((~(key = 0)) & (~exit)) { - let key = Keyboard.keyPressed(); - do bat.move(); - do moveBall(); - do Sys.wait(50); - } - } - - if (exit) { - do Output.moveCursor(10,27); - do Output.printString("Game Over"); - } - - return; - } - - /** - * Handles ball movement, including bouncing. - * If the ball bounces off a wall, finds its new direction. - * If the ball bounces off the bat, increases the score by one - * and shrinks the bat's size, to make the game more challenging. - */ - method void moveBall() { - var int bouncingDirection, batLeft, batRight, ballLeft, ballRight; - - let wall = ball.move(); - - if ((wall > 0) & (~(wall = lastWall))) { - let lastWall = wall; - let bouncingDirection = 0; - let batLeft = bat.getLeft(); - let batRight = bat.getRight(); - let ballLeft = ball.getLeft(); - let ballRight = ball.getRight(); - - if (wall = 4) { - let exit = (batLeft > ballRight) | (batRight < ballLeft); - if (~exit) { - if (ballRight < (batLeft + 10)) { let bouncingDirection = -1; } - else { - if (ballLeft > (batRight - 10)) { let bouncingDirection = 1; } - } - - let batWidth = batWidth - 2; - do bat.setWidth(batWidth); - let score = score + 1; - do Output.moveCursor(22,7); - do Output.printInt(score); - } - } - do ball.bounce(bouncingDirection); - } - return; - } -} \ No newline at end of file diff --git a/projects/11/Pong/PongGame.vm b/projects/11/Pong/PongGame.vm deleted file mode 100644 index 5521919..0000000 --- a/projects/11/Pong/PongGame.vm +++ /dev/null @@ -1,309 +0,0 @@ -function PongGame.new 0 -push constant 7 -call Memory.alloc 1 -pop pointer 0 -call Screen.clearScreen 0 -pop temp 0 -push constant 50 -pop this 6 -push constant 230 -push constant 229 -push this 6 -push constant 7 -call Bat.new 4 -pop this 0 -push constant 253 -push constant 222 -push constant 0 -push constant 511 -push constant 0 -push constant 229 -call Ball.new 6 -pop this 1 -push this 1 -push constant 400 -push constant 0 -call Ball.setDestination 3 -pop temp 0 -push constant 0 -push constant 238 -push constant 511 -push constant 240 -call Screen.drawRectangle 4 -pop temp 0 -push constant 22 -push constant 0 -call Output.moveCursor 2 -pop temp 0 -push constant 8 -call String.new 1 -push constant 83 -call String.appendChar 2 -push constant 99 -call String.appendChar 2 -push constant 111 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 48 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -push constant 0 -pop this 3 -push constant 0 -pop this 4 -push constant 0 -pop this 2 -push constant 0 -pop this 5 -push pointer 0 -return -function PongGame.dispose 0 -push argument 0 -pop pointer 0 -push this 0 -call Bat.dispose 1 -pop temp 0 -push this 1 -call Ball.dispose 1 -pop temp 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function PongGame.newInstance 0 -call PongGame.new 0 -pop static 0 -push constant 0 -return -function PongGame.getInstance 0 -push static 0 -return -function PongGame.run 1 -push argument 0 -pop pointer 0 -label run.While0 -push this 3 -not -not -if-goto run.EndWhile0 -label run.While0.While0 -push local 0 -push constant 0 -eq -push this 3 -not -and -not -if-goto run.While0.EndWhile0 -call Keyboard.keyPressed 0 -pop local 0 -push this 0 -call Bat.move 1 -pop temp 0 -push pointer 0 -call PongGame.moveBall 1 -pop temp 0 -push constant 50 -call Sys.wait 1 -pop temp 0 -goto run.While0.While0 -label run.While0.EndWhile0 -push local 0 -push constant 130 -eq -not -if-goto run.While0.Else1 -push this 0 -push constant 1 -call Bat.setDirection 2 -pop temp 0 -goto run.While0.Endif1 -label run.While0.Else1 -push local 0 -push constant 132 -eq -not -if-goto run.While0.If1.Else0 -push this 0 -push constant 2 -call Bat.setDirection 2 -pop temp 0 -goto run.While0.If1.Endif0 -label run.While0.If1.Else0 -push local 0 -push constant 140 -eq -not -if-goto run.While0.If1.If0.Else0 -push constant 1 -neg -pop this 3 -label run.While0.If1.If0.Else0 -label run.While0.If1.Endif0 -label run.While0.Endif1 -label run.While0.While2 -push local 0 -push constant 0 -eq -not -push this 3 -not -and -not -if-goto run.While0.EndWhile2 -call Keyboard.keyPressed 0 -pop local 0 -push this 0 -call Bat.move 1 -pop temp 0 -push pointer 0 -call PongGame.moveBall 1 -pop temp 0 -push constant 50 -call Sys.wait 1 -pop temp 0 -goto run.While0.While2 -label run.While0.EndWhile2 -goto run.While0 -label run.EndWhile0 -push this 3 -not -if-goto run.Else1 -push constant 10 -push constant 27 -call Output.moveCursor 2 -pop temp 0 -push constant 9 -call String.new 1 -push constant 71 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 109 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 79 -call String.appendChar 2 -push constant 118 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -call Output.printString 1 -pop temp 0 -label run.Else1 -push constant 0 -return -function PongGame.moveBall 5 -push argument 0 -pop pointer 0 -push this 1 -call Ball.move 1 -pop this 2 -push this 2 -push constant 0 -gt -push this 2 -push this 5 -eq -not -and -not -if-goto moveBall.Else0 -push this 2 -pop this 5 -push constant 0 -pop local 0 -push this 0 -call Bat.getLeft 1 -pop local 1 -push this 0 -call Bat.getRight 1 -pop local 2 -push this 1 -call Ball.getLeft 1 -pop local 3 -push this 1 -call Ball.getRight 1 -pop local 4 -push this 2 -push constant 4 -eq -not -if-goto moveBall.Else0.Else0 -push local 1 -push local 4 -gt -push local 2 -push local 3 -lt -or -pop this 3 -push this 3 -not -not -if-goto moveBall.Else0.Else0.Else0 -push local 4 -push local 1 -push constant 10 -add -lt -not -if-goto moveBall.Else0.Else0.Else0.Else0 -push constant 1 -neg -pop local 0 -goto moveBall.Else0.Else0.Else0.Endif0 -label moveBall.Else0.Else0.Else0.Else0 -push local 3 -push local 2 -push constant 10 -sub -gt -not -if-goto moveBall.Else0.Else0.Else0.If0.Else0 -push constant 1 -pop local 0 -label moveBall.Else0.Else0.Else0.If0.Else0 -label moveBall.Else0.Else0.Else0.Endif0 -push this 6 -push constant 2 -sub -pop this 6 -push this 0 -push this 6 -call Bat.setWidth 2 -pop temp 0 -push this 4 -push constant 1 -add -pop this 4 -push constant 22 -push constant 7 -call Output.moveCursor 2 -pop temp 0 -push this 4 -call Output.printInt 1 -pop temp 0 -label moveBall.Else0.Else0.Else0 -label moveBall.Else0.Else0 -push this 1 -push local 0 -call Ball.bounce 2 -pop temp 0 -label moveBall.Else0 -push constant 0 -return diff --git a/projects/11/Seven/Main.jack b/projects/11/Seven/Main.jack deleted file mode 100644 index 067402e..0000000 --- a/projects/11/Seven/Main.jack +++ /dev/null @@ -1,17 +0,0 @@ -// 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/11/Seven/Main.jack - -/** - * Computes the value of 1 + (2 * 3) and prints the result - * at the top-left of the screen. - */ -class Main { - - function void main() { - do Output.printInt(1 + (2 * 3)); - return; - } - -} diff --git a/projects/11/Seven/Main.vm b/projects/11/Seven/Main.vm deleted file mode 100644 index a209c18..0000000 --- a/projects/11/Seven/Main.vm +++ /dev/null @@ -1,10 +0,0 @@ -function Main.main 0 -push constant 1 -push constant 2 -push constant 3 -call Math.multiply 2 -add -call Output.printInt 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Square/Main.jack b/projects/11/Square/Main.jack deleted file mode 100644 index 0753893..0000000 --- a/projects/11/Square/Main.jack +++ /dev/null @@ -1,17 +0,0 @@ -// 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/11/Square/Main.jack - -// (same as projects/09/Square/Main.jack) - -/** Initializes a new Square Dance game and starts running it. */ -class Main { - function void main() { - var SquareGame game; - let game = SquareGame.new(); - do game.run(); - do game.dispose(); - return; - } -} diff --git a/projects/11/Square/Main.vm b/projects/11/Square/Main.vm deleted file mode 100644 index a3b355b..0000000 --- a/projects/11/Square/Main.vm +++ /dev/null @@ -1,11 +0,0 @@ -function Main.main 1 -call SquareGame.new 0 -pop local 0 -push local 0 -call SquareGame.run 1 -pop temp 0 -push local 0 -call SquareGame.dispose 1 -pop temp 0 -push constant 0 -return diff --git a/projects/11/Square/Square.jack b/projects/11/Square/Square.jack deleted file mode 100644 index 5a92838..0000000 --- a/projects/11/Square/Square.jack +++ /dev/null @@ -1,110 +0,0 @@ -// 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/11/Square/Square.jack - -// (same as projects/09/Square/Square.jack) - -/** Implements a graphical square. */ -class Square { - - field int x, y; // screen location of the square's top-left corner - field int size; // length of this square, in pixels - - /** Constructs a new square with a given location and size. */ - constructor Square new(int Ax, int Ay, int Asize) { - let x = Ax; - let y = Ay; - let size = Asize; - do draw(); - return this; - } - - /** Disposes this square. */ - method void dispose() { - do Memory.deAlloc(this); - return; - } - - /** Draws the square on the screen. */ - method void draw() { - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + size, y + size); - return; - } - - /** Erases the square from the screen. */ - method void erase() { - do Screen.setColor(false); - do Screen.drawRectangle(x, y, x + size, y + size); - return; - } - - /** Increments the square size by 2 pixels. */ - method void incSize() { - if (((y + size) < 254) & ((x + size) < 510)) { - do erase(); - let size = size + 2; - do draw(); - } - return; - } - - /** Decrements the square size by 2 pixels. */ - method void decSize() { - if (size > 2) { - do erase(); - let size = size - 2; - do draw(); - } - return; - } - - /** Moves the square up by 2 pixels. */ - method void moveUp() { - if (y > 1) { - do Screen.setColor(false); - do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); - let y = y - 2; - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + size, y + 1); - } - return; - } - - /** Moves the square down by 2 pixels. */ - method void moveDown() { - if ((y + size) < 254) { - do Screen.setColor(false); - do Screen.drawRectangle(x, y, x + size, y + 1); - let y = y + 2; - do Screen.setColor(true); - do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); - } - return; - } - - /** Moves the square left by 2 pixels. */ - method void moveLeft() { - if (x > 1) { - do Screen.setColor(false); - do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); - let x = x - 2; - do Screen.setColor(true); - do Screen.drawRectangle(x, y, x + 1, y + size); - } - return; - } - - /** Moves the square right by 2 pixels. */ - method void moveRight() { - if ((x + size) < 510) { - do Screen.setColor(false); - do Screen.drawRectangle(x, y, x + 1, y + size); - let x = x + 2; - do Screen.setColor(true); - do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); - } - return; - } -} diff --git a/projects/11/Square/Square.vm b/projects/11/Square/Square.vm deleted file mode 100644 index 871ccf7..0000000 --- a/projects/11/Square/Square.vm +++ /dev/null @@ -1,298 +0,0 @@ -function Square.new 0 -push constant 3 -call Memory.alloc 1 -pop pointer 0 -push argument 0 -pop this 0 -push argument 1 -pop this 1 -push argument 2 -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -push pointer 0 -return -function Square.dispose 0 -push argument 0 -pop pointer 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function Square.draw 0 -push argument 0 -pop pointer 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Square.erase 0 -push argument 0 -pop pointer 0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push constant 0 -return -function Square.incSize 0 -push argument 0 -pop pointer 0 -push this 1 -push this 2 -add -push constant 254 -lt -push this 0 -push this 2 -add -push constant 510 -lt -and -not -if-goto incSize.Else0 -push pointer 0 -call Square.erase 1 -pop temp 0 -push this 2 -push constant 2 -add -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -label incSize.Else0 -push constant 0 -return -function Square.decSize 0 -push argument 0 -pop pointer 0 -push this 2 -push constant 2 -gt -not -if-goto decSize.Else0 -push pointer 0 -call Square.erase 1 -pop temp 0 -push this 2 -push constant 2 -sub -pop this 2 -push pointer 0 -call Square.draw 1 -pop temp 0 -label decSize.Else0 -push constant 0 -return -function Square.moveUp 0 -push argument 0 -pop pointer 0 -push this 1 -push constant 1 -gt -not -if-goto moveUp.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 2 -add -push constant 1 -sub -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 1 -push constant 2 -sub -pop this 1 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push constant 1 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveUp.Else0 -push constant 0 -return -function Square.moveDown 0 -push argument 0 -pop pointer 0 -push this 1 -push this 2 -add -push constant 254 -lt -not -if-goto moveDown.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push this 2 -add -push this 1 -push constant 1 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 1 -push constant 2 -add -pop this 1 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 2 -add -push constant 1 -sub -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveDown.Else0 -push constant 0 -return -function Square.moveLeft 0 -push argument 0 -pop pointer 0 -push this 0 -push constant 1 -gt -not -if-goto moveLeft.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 1 -sub -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 0 -push constant 2 -sub -pop this 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push constant 1 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveLeft.Else0 -push constant 0 -return -function Square.moveRight 0 -push argument 0 -pop pointer 0 -push this 0 -push this 2 -add -push constant 510 -lt -not -if-goto moveRight.Else0 -push constant 0 -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 1 -push this 0 -push constant 1 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -push this 0 -push constant 2 -add -pop this 0 -push constant 1 -neg -call Screen.setColor 1 -pop temp 0 -push this 0 -push this 2 -add -push constant 1 -sub -push this 1 -push this 0 -push this 2 -add -push this 1 -push this 2 -add -call Screen.drawRectangle 4 -pop temp 0 -label moveRight.Else0 -push constant 0 -return diff --git a/projects/11/Square/SquareGame.jack b/projects/11/Square/SquareGame.jack deleted file mode 100644 index 4fe7e39..0000000 --- a/projects/11/Square/SquareGame.jack +++ /dev/null @@ -1,81 +0,0 @@ -// 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/11/Square/SquareGame.jack - -// (same as projects/09/Square/SquareGame.jack) - -/** - * Implements the Square Dance game. - * This simple game allows the user to move a black square around - * the screen, and change the square's size during the movement. - * When the game starts, a square of 30 by 30 pixels is shown at the - * top-left corner of the screen. The user controls the square as follows. - * The 4 arrow keys are used to move the square up, down, left, and right. - * The 'z' and 'x' keys are used, respectively, to decrement and increment - * the square's size. The 'q' key is used to quit the game. - */ - -class SquareGame { - field Square square; // the square of this game - field int direction; // the square's current direction: - // 0=none, 1=up, 2=down, 3=left, 4=right - - /** Constructs a new Square Game. */ - constructor SquareGame new() { - // Creates a 30 by 30 pixels square and positions it at the top-left - // of the screen. - let square = Square.new(0, 0, 30); - let direction = 0; // initial state is no movement - return this; - } - - /** Disposes this game. */ - method void dispose() { - do square.dispose(); - do Memory.deAlloc(this); - return; - } - - /** Moves the square in the current direction. */ - method void moveSquare() { - if (direction = 1) { do square.moveUp(); } - if (direction = 2) { do square.moveDown(); } - if (direction = 3) { do square.moveLeft(); } - if (direction = 4) { do square.moveRight(); } - do Sys.wait(5); // delays the next movement - return; - } - - /** Runs the game: handles the user's inputs and moves the square accordingly */ - method void run() { - var char key; // the key currently pressed by the user - var boolean exit; - let exit = false; - - while (~exit) { - // waits for a key to be pressed - while (key = 0) { - let key = Keyboard.keyPressed(); - do moveSquare(); - } - if (key = 81) { let exit = true; } // q key - if (key = 90) { do square.decSize(); } // z key - if (key = 88) { do square.incSize(); } // x key - if (key = 131) { let direction = 1; } // up arrow - if (key = 133) { let direction = 2; } // down arrow - if (key = 130) { let direction = 3; } // left arrow - if (key = 132) { let direction = 4; } // right arrow - - // waits for the key to be released - while (~(key = 0)) { - let key = Keyboard.keyPressed(); - do moveSquare(); - } - } // while - return; - } -} - - - diff --git a/projects/11/Square/SquareGame.vm b/projects/11/Square/SquareGame.vm deleted file mode 100644 index f2dd542..0000000 --- a/projects/11/Square/SquareGame.vm +++ /dev/null @@ -1,168 +0,0 @@ -function SquareGame.new 0 -push constant 2 -call Memory.alloc 1 -pop pointer 0 -push constant 0 -push constant 0 -push constant 30 -call Square.new 3 -pop this 0 -push constant 0 -pop this 1 -push pointer 0 -return -function SquareGame.dispose 0 -push argument 0 -pop pointer 0 -push this 0 -call Square.dispose 1 -pop temp 0 -push pointer 0 -call Memory.deAlloc 1 -pop temp 0 -push constant 0 -return -function SquareGame.moveSquare 0 -push argument 0 -pop pointer 0 -push this 1 -push constant 1 -eq -not -if-goto moveSquare.Else0 -push this 0 -call Square.moveUp 1 -pop temp 0 -label moveSquare.Else0 -push this 1 -push constant 2 -eq -not -if-goto moveSquare.Else1 -push this 0 -call Square.moveDown 1 -pop temp 0 -label moveSquare.Else1 -push this 1 -push constant 3 -eq -not -if-goto moveSquare.Else2 -push this 0 -call Square.moveLeft 1 -pop temp 0 -label moveSquare.Else2 -push this 1 -push constant 4 -eq -not -if-goto moveSquare.Else3 -push this 0 -call Square.moveRight 1 -pop temp 0 -label moveSquare.Else3 -push constant 5 -call Sys.wait 1 -pop temp 0 -push constant 0 -return -function SquareGame.run 2 -push argument 0 -pop pointer 0 -push constant 0 -pop local 1 -label run.While0 -push local 1 -not -not -if-goto run.EndWhile0 -label run.While0.While0 -push local 0 -push constant 0 -eq -not -if-goto run.While0.EndWhile0 -call Keyboard.keyPressed 0 -pop local 0 -push pointer 0 -call SquareGame.moveSquare 1 -pop temp 0 -goto run.While0.While0 -label run.While0.EndWhile0 -push local 0 -push constant 81 -eq -not -if-goto run.While0.Else1 -push constant 1 -neg -pop local 1 -label run.While0.Else1 -push local 0 -push constant 90 -eq -not -if-goto run.While0.Else2 -push this 0 -call Square.decSize 1 -pop temp 0 -label run.While0.Else2 -push local 0 -push constant 88 -eq -not -if-goto run.While0.Else3 -push this 0 -call Square.incSize 1 -pop temp 0 -label run.While0.Else3 -push local 0 -push constant 131 -eq -not -if-goto run.While0.Else4 -push constant 1 -pop this 1 -label run.While0.Else4 -push local 0 -push constant 133 -eq -not -if-goto run.While0.Else5 -push constant 2 -pop this 1 -label run.While0.Else5 -push local 0 -push constant 130 -eq -not -if-goto run.While0.Else6 -push constant 3 -pop this 1 -label run.While0.Else6 -push local 0 -push constant 132 -eq -not -if-goto run.While0.Else7 -push constant 4 -pop this 1 -label run.While0.Else7 -label run.While0.While8 -push local 0 -push constant 0 -eq -not -not -if-goto run.While0.EndWhile8 -call Keyboard.keyPressed 0 -pop local 0 -push pointer 0 -call SquareGame.moveSquare 1 -pop temp 0 -goto run.While0.While8 -label run.While0.EndWhile8 -goto run.While0 -label run.EndWhile0 -push constant 0 -return diff --git a/projects/11/Test.jack b/projects/11/Test.jack deleted file mode 100644 index ff4bfae..0000000 --- a/projects/11/Test.jack +++ /dev/null @@ -1,7 +0,0 @@ -class Test{ -method void something(){ -} -constructor Test new(){ - do something(); -} -} diff --git a/projects/11/systemsub.txt b/projects/11/systemsub.txt deleted file mode 100644 index cad8797..0000000 --- a/projects/11/systemsub.txt +++ /dev/null @@ -1,59 +0,0 @@ -class Math{ -function int abs(int x, int y){} -function int multiply(int x, int y){} -function int divide(int x, int y){} -function int min(int x, int y){} -function int max(int x, int y){} -function int sqrt(int x){} -} -class String{ -constructor String new(int maxLength){} -method int dispose(){} -method int length(){} -method char charAt(int j){} -method void setCharAt(int j, char c){} -method String appendChar(char c){} -method void eraseLastChar(){} -method int intValue(){} -method void setInt(int val){} -function char backSpace(){} -function char doubleQuote(){} -function char newLine(){} -} -class Array{ -function Array new(int size){} -method void dispose(){} -} -class Output{ -function void moveCursor(int i, int j){} -function void printChar(char c){} -function void printString(String s){} -function void printInt(int i){} -function void println(){} -function void backSpace(){} -} -class Screen{ -function void clearScreen(){} -function void setColor(boolean b){} -function void drawPixel(int x, int y){} -function void drawLine(int x1, int y1, int x2, int y2){} -function void drawRectangle(int x1, int y1, int x2, int y2){} -function void drawCircle(int x, int y, int r){} -} -class Keyboard{ -function char keyPressed(){} -function char readChar(){} -function String readLine(String message){} -function int readInt(String message){} -} -class Memory{ -function int peek(int address){} -function void poke(int address, int value){} -function Array alloc(int size){} -function void deAlloc(Array o){} -} -class Sys{ -function void halt(){} -function void error(int errorCode){} -function void wait(int duration){} -} diff --git a/projects/11/systemsub1.txt b/projects/11/systemsub1.txt deleted file mode 100644 index 2095728..0000000 --- a/projects/11/systemsub1.txt +++ /dev/null @@ -1,42 +0,0 @@ -function int Math.multiply(int x, int y) -function int Math.divide(int x, int y) -function int Math.min(int x, int y) -function int Math.max(int x, int y) -function int Math.sqrt(int x) -constructor String String.new(int maxLength) -method int String.dispose() -method int String.length() -method char String.charAt(int j) -method void String.setCharAt(int j, char c) -method String String.appendChar(char c) -method void String.eraseLastChar() -method int String.intValue() -method void String.setInt(int val) -function char String.backSpace() -function char String.doubleQuote() -function char String.newLine() -function Array Array.new(int size) -method void Array.dispose() -function void Output.moveCursor(int i, int j) -function void Output.printChar(char c) -function void Output.printString(String s) -function void Output.printInt(int i) -function void Output.println() -function void Output.backSpace() -function void Screen.clearScreen() -function void Screen.setColor(boolean b) -function void Screen.drawPixel(int x, int y) -function void Screen.drawLine(int x1, int y1, int x2, int y2) -function void Screen.drawRectangle(int x1, int y1, int x2, int y2) -function void Screen.drawCircle(int x, int y, int r) -function char Keyboard.keyPressed() -function char Keyboard.readChar() -function String Keyboard.readLine(String message) -function int Keyboard.readInt(String message) -function int Memory.peek(int address) -function void Memory.poke(int address, int value) -function Array Memory.alloc(int size) -function void Memory.deAlloc(Array o) -function void Sys.halt() -function void Sys.error(int errorCode) -function void Sys.wait(int duration) -- cgit v1.2.3