aboutsummaryrefslogtreecommitdiff
path: root/projects
diff options
context:
space:
mode:
Diffstat (limited to 'projects')
-rw-r--r--projects/11/Average/Main.vm147
-rw-r--r--projects/11/ComplexArrays/Main.vm681
-rw-r--r--projects/11/ConvertToBin/Main.jack2
-rw-r--r--projects/11/ConvertToBin/Main.vm110
-rw-r--r--projects/11/ConvertToBin/setram.tst3
-rwxr-xr-xprojects/11/JackCompilerbin0 -> 284352 bytes
-rw-r--r--projects/11/JackCompiler.hibin0 -> 10319 bytes
-rw-r--r--projects/11/JackCompiler.hs97
-rw-r--r--projects/11/JackCompiler.obin0 -> 408776 bytes
-rw-r--r--projects/11/Pong/Ball.vm0
-rw-r--r--projects/11/Seven/Main.vm10
-rw-r--r--projects/11/Square/Main.vm11
-rw-r--r--projects/11/Square/Square.vm298
-rw-r--r--projects/11/Square/SquareGame.vm168
-rw-r--r--projects/11/Test.jack7
-rw-r--r--projects/11/systemsub.txt58
-rw-r--r--projects/11/systemsub1.txt42
17 files changed, 1602 insertions, 32 deletions
diff --git a/projects/11/Average/Main.vm b/projects/11/Average/Main.vm
new file mode 100644
index 0000000..91772b9
--- /dev/null
+++ b/projects/11/Average/Main.vm
@@ -0,0 +1,147 @@
+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.vm b/projects/11/ComplexArrays/Main.vm
new file mode 100644
index 0000000..dc83733
--- /dev/null
+++ b/projects/11/ComplexArrays/Main.vm
@@ -0,0 +1,681 @@
+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
index e627486..e1ab941 100644
--- a/projects/11/ConvertToBin/Main.jack
+++ b/projects/11/ConvertToBin/Main.jack
@@ -26,6 +26,8 @@ class Main {
*/
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
diff --git a/projects/11/ConvertToBin/Main.vm b/projects/11/ConvertToBin/Main.vm
new file mode 100644
index 0000000..15d63c0
--- /dev/null
+++ b/projects/11/ConvertToBin/Main.vm
@@ -0,0 +1,110 @@
+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
new file mode 100644
index 0000000..a33eece
--- /dev/null
+++ b/projects/11/ConvertToBin/setram.tst
@@ -0,0 +1,3 @@
+load Main.vm
+set RAM[8000] 1023;
+vmstep;
diff --git a/projects/11/JackCompiler b/projects/11/JackCompiler
new file mode 100755
index 0000000..39d2d13
--- /dev/null
+++ b/projects/11/JackCompiler
Binary files differ
diff --git a/projects/11/JackCompiler.hi b/projects/11/JackCompiler.hi
new file mode 100644
index 0000000..50c8c9c
--- /dev/null
+++ b/projects/11/JackCompiler.hi
Binary files differ
diff --git a/projects/11/JackCompiler.hs b/projects/11/JackCompiler.hs
index 4621bb1..d149041 100644
--- a/projects/11/JackCompiler.hs
+++ b/projects/11/JackCompiler.hs
@@ -15,6 +15,7 @@ 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)
@@ -61,6 +62,7 @@ classVarScopeStrs = ["static", "field"]
subroutineTypeStrs = ["constructor", "function", "method"]
alphaUnderscore = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['_']
alphaNumUnderscore = alphaUnderscore ++ ['0'..'9']
+alphaNumUnderscoredot = '.':alphaNumUnderscore
parse' parser = parse parser ""
@@ -300,6 +302,7 @@ someArgs = do
many jSpace >> char ')'
return exps
+
-- vm writer starts from here
buildCTable :: [JClassVarDec] -> (Table, Int)
@@ -329,11 +332,11 @@ buildLTable args lcls =
--data JSubroutineHeader = JSubroutineHeader JSubroutineType JTypeAndId [JTypeAndId] deriving (Show, Eq)
--data JSubroutineBody = JSubroutineBody [JTypeAndId] [JStatement] deriving (Show, Eq)
-jackCompiler :: [[Char]] -> [[Char]]
-jackCompiler = vmWriter . fmap (fromRight (JClass "" [] []) . jackReader)
+jackCompiler :: Table -> [[Char]] -> [[Char]]
+jackCompiler initTable = vmWriter initTable . fmap (fromRight (JClass "" [] []) . jackReader)
-vmWriter :: [JClass] -> [[Char]]
-vmWriter xs = vClass (buildSRTable xs) <$> xs
+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
@@ -342,7 +345,7 @@ vClass u (JClass name vars subs) = mconcat $ vSubroutineDec name t u n <$> subs
vSubroutineDec :: [Char] -> Table -> Table -> Int -> JSubroutineDec -> [Char]
vSubroutineDec cName t u n sub =
vFunction (cName ++ "." ++ sName) nLcls ++ kindSpec ++
- vStatement t s u sName 0 stmts where
+ vStatement t s u cName sName 0 stmts where
JSubroutineDec (JSubroutineHeader _ (_, sName) args) (JSubroutineBody lcls stmts) = sub
nLcls = length lcls
s = buildLTable args lcls
@@ -359,35 +362,35 @@ vSubroutineDec cName t u n sub =
-- | JDoStatement JSubroutineCall
-- | JReturnStatement (Maybe JExpression)
-vStatement _ _ _ _ _ [] = ""
+vStatement _ _ _ _ _ _ [] = ""
-vStatement t s u name n ((JLetStatment var exp):stmts) =
+vStatement t s u cName name n ((JLetStatment var exp):stmts) =
-- vExpression: push the result of exp; vPopToVar: pop to the var addr
- vExpression t s u name exp ++ vPopToVar t s u name var ++ vStatement t s u name n 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 name n ((JIfStatement cond thenStmts elseStmts):stmts) =
- vExpression t s u name cond ++ vNot ++ vThen thenStmts ++
- maybe (vLabel labelElse) vElse elseStmts ++ vStatement t s u name (n + 1) stmts where
+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 labelElse 0 xs
+ vThen xs = vIfGoto labelElse ++ vStatement t s u cName labelElse 0 xs
vElse xs = vGoto labelEndIf ++ vLabel labelElse ++
- vStatement t s u labelIf 0 xs ++ vLabel labelEndIf
+ vStatement t s u cName labelIf 0 xs ++ vLabel labelEndIf
-vStatement t s u name n ((JWhileStatment cond loopStmts):stmts) =
- vLabel labelWhile ++ vExpression t s u name cond ++ vNot ++ vIfGoto labelEndWhile ++
- vStatement t s u labelWhile 0 loopStmts ++ vGoto labelWhile ++
- vLabel labelEndWhile ++ vStatement t s u name (n + 1) stmts where
+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 name n ((JDoStatement subCall):stmts) =
- vSubroutineCall t s u name subCall ++ vPop "temp" 0 ++ vStatement t s u name n stmts
+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 name n ((JReturnStatement ret):stmts) =
- maybe (vPush "constant" 0) (vExpression t s u name) ret ++ vReturn ++
- vStatement t s u 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]
@@ -451,14 +454,17 @@ 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
+ 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
+ --hello = trace (cName ++ " " ++ name ++ " " ++ show name') ""
(method, nArgs) =
if fst (u Map.! name'') == "method"
- then let (seg, n) = getSegN t s name in (vPush seg n, length args + 1)
+ 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
@@ -467,7 +473,7 @@ vNot = "not\n"
vNeg = "neg\n"
vSub = "sub\n"
vMul = "call Math.multiply 2\n"
-vDiv = "call Math.division 2\n"
+vDiv = "call Math.divide 2\n"
vAnd = "and\n"
vOr = "or\n"
vLt = "lt\n"
@@ -482,12 +488,30 @@ vCall xs n = "call " ++ xs ++ " " ++ show n ++ "\n"
vPush xs n = "push " ++ xs ++ " " ++ show n ++ "\n"
vPop xs n = "pop " ++ xs ++ " " ++ show n ++ "\n"
-inputSeven = ["// This file is part of www.nand2tetris.org\n// and the book \"The Elements of Computing Systems\"\n// by Nisan and Schocken, MIT Press.\n// File name: projects/11/Seven/Main.jack\n\n/**\n * Computes the value of 1 + (2 * 3) and prints the result\n * at the top-left of the screen. \n */\nclass Main {\n\n function void main() {\n do Output.printInt(1 + (2 * 3));\n return;\n }\n}\n"]
-inputSeven1 = "do Output.printInt(1 + (2 * 3));"
+-- testing
+
+testCompiler :: [[Char]] -> IO ()
+testCompiler xs = do
+ initTable <- sysSubroutineTable
+ print $ jackCompiler initTable xs
+
+testCompiler' :: IO ()
+testCompiler' = do
+ --xs <- readFile "./Square/Square.jack"
+ xs <- readFile "./Test.jack"
+ testCompiler [xs]
+
+testReader :: IO ()
+testReader = do
+ --xs <- readFile "./Square/Square.jack"
+ xs <- readFile "./Test.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]
+
+--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]
{--
fst3 (x, y, z) = x
@@ -504,13 +528,22 @@ replCrWithNl = fmap cr2nl
-- 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
- zipWithM writeFile (chExt <$> jackFiles) (show . jack <$> codes)
+ 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 ++ "ast"
+ chExt xs = take (length xs - 4) xs ++ "vm"
--}
diff --git a/projects/11/JackCompiler.o b/projects/11/JackCompiler.o
new file mode 100644
index 0000000..a3cf3d3
--- /dev/null
+++ b/projects/11/JackCompiler.o
Binary files differ
diff --git a/projects/11/Pong/Ball.vm b/projects/11/Pong/Ball.vm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/projects/11/Pong/Ball.vm
diff --git a/projects/11/Seven/Main.vm b/projects/11/Seven/Main.vm
new file mode 100644
index 0000000..a209c18
--- /dev/null
+++ b/projects/11/Seven/Main.vm
@@ -0,0 +1,10 @@
+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.vm b/projects/11/Square/Main.vm
new file mode 100644
index 0000000..a3b355b
--- /dev/null
+++ b/projects/11/Square/Main.vm
@@ -0,0 +1,11 @@
+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.vm b/projects/11/Square/Square.vm
new file mode 100644
index 0000000..871ccf7
--- /dev/null
+++ b/projects/11/Square/Square.vm
@@ -0,0 +1,298 @@
+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.vm b/projects/11/Square/SquareGame.vm
new file mode 100644
index 0000000..f2dd542
--- /dev/null
+++ b/projects/11/Square/SquareGame.vm
@@ -0,0 +1,168 @@
+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
new file mode 100644
index 0000000..ff4bfae
--- /dev/null
+++ b/projects/11/Test.jack
@@ -0,0 +1,7 @@
+class Test{
+method void something(){
+}
+constructor Test new(){
+ do something();
+}
+}
diff --git a/projects/11/systemsub.txt b/projects/11/systemsub.txt
new file mode 100644
index 0000000..16c9552
--- /dev/null
+++ b/projects/11/systemsub.txt
@@ -0,0 +1,58 @@
+class Math{
+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
new file mode 100644
index 0000000..2095728
--- /dev/null
+++ b/projects/11/systemsub1.txt
@@ -0,0 +1,42 @@
+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)