From ac0b05010df8f676fd12240073e5417ae865a686 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 15 Jan 2018 12:12:48 +0100 Subject: checkpoint - all but pong works - pong: exception in rectangle drawing --- projects/11/JackCompiler.hs | 4 +- projects/11/Pong/Ball.vm | 427 +++++++++++++++++++++++++++++++++++++++++++ projects/11/Pong/Bat.vm | 204 +++++++++++++++++++++ projects/11/Pong/Main.vm | 13 ++ projects/11/Pong/PongGame.vm | 309 +++++++++++++++++++++++++++++++ projects/11/systemsub.txt | 1 + 6 files changed, 956 insertions(+), 2 deletions(-) create mode 100644 projects/11/Pong/Bat.vm create mode 100644 projects/11/Pong/Main.vm create mode 100644 projects/11/Pong/PongGame.vm diff --git a/projects/11/JackCompiler.hs b/projects/11/JackCompiler.hs index d149041..025fe12 100644 --- a/projects/11/JackCompiler.hs +++ b/projects/11/JackCompiler.hs @@ -499,13 +499,13 @@ testCompiler xs = do testCompiler' :: IO () testCompiler' = do --xs <- readFile "./Square/Square.jack" - xs <- readFile "./Test.jack" + xs <- readFile "./Pong/Ball.jack" testCompiler [xs] testReader :: IO () testReader = do --xs <- readFile "./Square/Square.jack" - xs <- readFile "./Test.jack" + xs <- readFile "./Pong/Ball.jack" --print $ buildSRTable $ rights $ [jackReader $ xs] print $ jackReader $ xs diff --git a/projects/11/Pong/Ball.vm b/projects/11/Pong/Ball.vm index e69de29..b8154eb 100644 --- a/projects/11/Pong/Ball.vm +++ b/projects/11/Pong/Ball.vm @@ -0,0 +1,427 @@ +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 0 +push this 0 +sub +pop this 2 +push argument 1 +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 1 +lt +pop this 8 +push this 0 +push argument 0 +lt +pop this 9 +goto setDestination.Endif0 +label setDestination.Else0 +push this 0 +push argument 0 +lt +pop this 8 +push this 1 +push argument 1 +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 0 +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 0 +push constant 1 +eq +and +push this 2 +push constant 0 +lt +push argument 0 +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.vm b/projects/11/Pong/Bat.vm new file mode 100644 index 0000000..23b4033 --- /dev/null +++ b/projects/11/Pong/Bat.vm @@ -0,0 +1,204 @@ +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 0 +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 0 +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.vm b/projects/11/Pong/Main.vm new file mode 100644 index 0000000..94d76d7 --- /dev/null +++ b/projects/11/Pong/Main.vm @@ -0,0 +1,13 @@ +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.vm b/projects/11/Pong/PongGame.vm new file mode 100644 index 0000000..5521919 --- /dev/null +++ b/projects/11/Pong/PongGame.vm @@ -0,0 +1,309 @@ +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/systemsub.txt b/projects/11/systemsub.txt index 16c9552..cad8797 100644 --- a/projects/11/systemsub.txt +++ b/projects/11/systemsub.txt @@ -1,4 +1,5 @@ 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){} -- cgit v1.2.3