From 9542deeb483a00b6fabed7574720926ce97d7511 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Tue, 16 Aug 2022 11:54:23 +0800 Subject: Projects, 01-06 completed --- projects/10/ExpressionLessSquare/Main.jack | 28 + projects/10/ExpressionLessSquare/Main.xml | 114 +++ projects/10/ExpressionLessSquare/MainT.xml | 64 ++ projects/10/ExpressionLessSquare/Square.jack | 99 +++ projects/10/ExpressionLessSquare/Square.xml | 967 +++++++++++++++++++++++ projects/10/ExpressionLessSquare/SquareGame.jack | 60 ++ projects/10/ExpressionLessSquare/SquareGame.xml | 544 +++++++++++++ projects/10/ExpressionLessSquare/SquareGameT.xml | 268 +++++++ projects/10/ExpressionLessSquare/SquareT.xml | 449 +++++++++++ 9 files changed, 2593 insertions(+) create mode 100644 projects/10/ExpressionLessSquare/Main.jack create mode 100644 projects/10/ExpressionLessSquare/Main.xml create mode 100644 projects/10/ExpressionLessSquare/MainT.xml create mode 100644 projects/10/ExpressionLessSquare/Square.jack create mode 100644 projects/10/ExpressionLessSquare/Square.xml create mode 100644 projects/10/ExpressionLessSquare/SquareGame.jack create mode 100644 projects/10/ExpressionLessSquare/SquareGame.xml create mode 100644 projects/10/ExpressionLessSquare/SquareGameT.xml create mode 100644 projects/10/ExpressionLessSquare/SquareT.xml (limited to 'projects/10/ExpressionLessSquare') diff --git a/projects/10/ExpressionLessSquare/Main.jack b/projects/10/ExpressionLessSquare/Main.jack new file mode 100644 index 0000000..94764ad --- /dev/null +++ b/projects/10/ExpressionLessSquare/Main.jack @@ -0,0 +1,28 @@ +// 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/10/ExpressionLessSquare/Main.jack + +/** Expressionless version of projects/10/Square/Main.jack. */ + +class Main { + static boolean test; // Added for testing -- there is no static keyword + // in the Square files. + + function void main() { + var SquareGame game; + let game = game; + do game.run(); + do game.dispose(); + return; + } + + function void more() { // Added to test Jack syntax that is not used in + var boolean b; // the Square files. + if (b) { + } + else { // There is no else keyword in the Square files. + } + return; + } +} diff --git a/projects/10/ExpressionLessSquare/Main.xml b/projects/10/ExpressionLessSquare/Main.xml new file mode 100644 index 0000000..f71a0ef --- /dev/null +++ b/projects/10/ExpressionLessSquare/Main.xml @@ -0,0 +1,114 @@ + + class + Main + { + + static + boolean + test + ; + + + function + void + main + ( + + + ) + + { + + var + SquareGame + game + ; + + + + let + game + = + + + game + + + ; + + + do + game + . + run + ( + + + ) + ; + + + do + game + . + dispose + ( + + + ) + ; + + + return + ; + + + } + + + + function + void + more + ( + + + ) + + { + + var + boolean + b + ; + + + + if + ( + + + b + + + ) + { + + + } + else + { + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/MainT.xml b/projects/10/ExpressionLessSquare/MainT.xml new file mode 100644 index 0000000..441dfed --- /dev/null +++ b/projects/10/ExpressionLessSquare/MainT.xml @@ -0,0 +1,64 @@ + + class + Main + { + static + boolean + test + ; + function + void + main + ( + ) + { + var + SquareGame + game + ; + let + game + = + game + ; + do + game + . + run + ( + ) + ; + do + game + . + dispose + ( + ) + ; + return + ; + } + function + void + more + ( + ) + { + var + boolean + b + ; + if + ( + b + ) + { + } + else + { + } + return + ; + } + } + diff --git a/projects/10/ExpressionLessSquare/Square.jack b/projects/10/ExpressionLessSquare/Square.jack new file mode 100644 index 0000000..33a54ad --- /dev/null +++ b/projects/10/ExpressionLessSquare/Square.jack @@ -0,0 +1,99 @@ +// 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/10/ExpressionLessSquare/Square.jack + +/** Expressionless version of projects/10/Square/Square.jack. */ + +class Square { + + field int x, y; + field int size; + + constructor Square new(int Ax, int Ay, int Asize) { + let x = Ax; + let y = Ay; + let size = Asize; + do draw(); + return x; + } + + method void dispose() { + do Memory.deAlloc(this); + return; + } + + method void draw() { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + return; + } + + method void erase() { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + return; + } + + method void incSize() { + if (x) { + do erase(); + let size = size; + do draw(); + } + return; + } + + method void decSize() { + if (size) { + do erase(); + let size = size; + do draw(); + } + return; + } + + method void moveUp() { + if (y) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let y = y; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveDown() { + if (y) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let y = y; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveLeft() { + if (x) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let x = x; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveRight() { + if (x) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let x = x; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } +} diff --git a/projects/10/ExpressionLessSquare/Square.xml b/projects/10/ExpressionLessSquare/Square.xml new file mode 100644 index 0000000..ed0e6ec --- /dev/null +++ b/projects/10/ExpressionLessSquare/Square.xml @@ -0,0 +1,967 @@ + + class + Square + { + + field + int + x + , + y + ; + + + field + int + size + ; + + + constructor + Square + new + ( + + int + Ax + , + int + Ay + , + int + Asize + + ) + + { + + + let + x + = + + + Ax + + + ; + + + let + y + = + + + Ay + + + ; + + + let + size + = + + + Asize + + + ; + + + do + draw + ( + + + ) + ; + + + return + + + x + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + Memory + . + deAlloc + ( + + + + this + + + + ) + ; + + + return + ; + + + } + + + + method + void + draw + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + erase + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + incSize + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + decSize + ( + + + ) + + { + + + if + ( + + + size + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveUp + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveDown + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveLeft + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveRight + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/SquareGame.jack b/projects/10/ExpressionLessSquare/SquareGame.jack new file mode 100644 index 0000000..2866f0d --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGame.jack @@ -0,0 +1,60 @@ +// 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/10/ExpressionLessSquare/SquareGame.jack + +/** Expressionless version of projects/10/Square/SquareGame.jack. */ + +class SquareGame { + field Square square; + field int direction; + + constructor SquareGame new() { + let square = square; + let direction = direction; + return square; + } + + method void dispose() { + do square.dispose(); + do Memory.deAlloc(square); + return; + } + + method void moveSquare() { + if (direction) { do square.moveUp(); } + if (direction) { do square.moveDown(); } + if (direction) { do square.moveLeft(); } + if (direction) { do square.moveRight(); } + do Sys.wait(direction); + return; + } + + method void run() { + var char key; + var boolean exit; + + let exit = key; + while (exit) { + while (key) { + let key = key; + do moveSquare(); + } + + if (key) { let exit = exit; } + if (key) { do square.decSize(); } + if (key) { do square.incSize(); } + if (key) { let direction = exit; } + if (key) { let direction = key; } + if (key) { let direction = square; } + if (key) { let direction = direction; } + + while (key) { + let key = key; + do moveSquare(); + } + } + return; + } +} + diff --git a/projects/10/ExpressionLessSquare/SquareGame.xml b/projects/10/ExpressionLessSquare/SquareGame.xml new file mode 100644 index 0000000..288c6cd --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGame.xml @@ -0,0 +1,544 @@ + + class + SquareGame + { + + field + Square + square + ; + + + field + int + direction + ; + + + constructor + SquareGame + new + ( + + + ) + + { + + + let + square + = + + + square + + + ; + + + let + direction + = + + + direction + + + ; + + + return + + + square + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + square + . + dispose + ( + + + ) + ; + + + do + Memory + . + deAlloc + ( + + + + square + + + + ) + ; + + + return + ; + + + } + + + + method + void + moveSquare + ( + + + ) + + { + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveUp + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveDown + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveLeft + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveRight + ( + + + ) + ; + + + } + + + do + Sys + . + wait + ( + + + + direction + + + + ) + ; + + + return + ; + + + } + + + + method + void + run + ( + + + ) + + { + + var + char + key + ; + + + var + boolean + exit + ; + + + + let + exit + = + + + key + + + ; + + + while + ( + + + exit + + + ) + { + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + exit + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + decSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + incSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + key + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + square + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + direction + + + ; + + + } + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/SquareGameT.xml b/projects/10/ExpressionLessSquare/SquareGameT.xml new file mode 100644 index 0000000..278a8a9 --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGameT.xml @@ -0,0 +1,268 @@ + + class + SquareGame + { + field + Square + square + ; + field + int + direction + ; + constructor + SquareGame + new + ( + ) + { + let + square + = + square + ; + let + direction + = + direction + ; + return + square + ; + } + method + void + dispose + ( + ) + { + do + square + . + dispose + ( + ) + ; + do + Memory + . + deAlloc + ( + square + ) + ; + return + ; + } + method + void + moveSquare + ( + ) + { + if + ( + direction + ) + { + do + square + . + moveUp + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveDown + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveLeft + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveRight + ( + ) + ; + } + do + Sys + . + wait + ( + direction + ) + ; + return + ; + } + method + void + run + ( + ) + { + var + char + key + ; + var + boolean + exit + ; + let + exit + = + key + ; + while + ( + exit + ) + { + while + ( + key + ) + { + let + key + = + key + ; + do + moveSquare + ( + ) + ; + } + if + ( + key + ) + { + let + exit + = + exit + ; + } + if + ( + key + ) + { + do + square + . + decSize + ( + ) + ; + } + if + ( + key + ) + { + do + square + . + incSize + ( + ) + ; + } + if + ( + key + ) + { + let + direction + = + exit + ; + } + if + ( + key + ) + { + let + direction + = + key + ; + } + if + ( + key + ) + { + let + direction + = + square + ; + } + if + ( + key + ) + { + let + direction + = + direction + ; + } + while + ( + key + ) + { + let + key + = + key + ; + do + moveSquare + ( + ) + ; + } + } + return + ; + } + } + diff --git a/projects/10/ExpressionLessSquare/SquareT.xml b/projects/10/ExpressionLessSquare/SquareT.xml new file mode 100644 index 0000000..cd03a1e --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareT.xml @@ -0,0 +1,449 @@ + + class + Square + { + field + int + x + , + y + ; + field + int + size + ; + constructor + Square + new + ( + int + Ax + , + int + Ay + , + int + Asize + ) + { + let + x + = + Ax + ; + let + y + = + Ay + ; + let + size + = + Asize + ; + do + draw + ( + ) + ; + return + x + ; + } + method + void + dispose + ( + ) + { + do + Memory + . + deAlloc + ( + this + ) + ; + return + ; + } + method + void + draw + ( + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + return + ; + } + method + void + erase + ( + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + return + ; + } + method + void + incSize + ( + ) + { + if + ( + x + ) + { + do + erase + ( + ) + ; + let + size + = + size + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + decSize + ( + ) + { + if + ( + size + ) + { + do + erase + ( + ) + ; + let + size + = + size + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + moveUp + ( + ) + { + if + ( + y + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + y + = + y + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveDown + ( + ) + { + if + ( + y + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + y + = + y + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveLeft + ( + ) + { + if + ( + x + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + x + = + x + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveRight + ( + ) + { + if + ( + x + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + x + = + x + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + } + -- cgit v1.2.3