aboutsummaryrefslogtreecommitdiff
path: root/projects/10/ArrayTest
diff options
context:
space:
mode:
Diffstat (limited to 'projects/10/ArrayTest')
-rw-r--r--projects/10/ArrayTest/Main.jack38
-rw-r--r--projects/10/ArrayTest/Main.xml286
-rw-r--r--projects/10/ArrayTest/MainT.xml142
3 files changed, 466 insertions, 0 deletions
diff --git a/projects/10/ArrayTest/Main.jack b/projects/10/ArrayTest/Main.jack
new file mode 100644
index 0000000..aa237b1
--- /dev/null
+++ b/projects/10/ArrayTest/Main.jack
@@ -0,0 +1,38 @@
+// 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/ArrayTest/Main.jack
+
+// (identical to projects/09/Average/Main.jack)
+
+/** Computes the average of a sequence of integers. */
+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);
+ let i = 0;
+
+ while (i < length) {
+ let a[i] = Keyboard.readInt("ENTER THE NEXT NUMBER: ");
+ let i = i + 1;
+ }
+
+ let i = 0;
+ let sum = 0;
+
+ while (i < length) {
+ let sum = sum + a[i];
+ let i = i + 1;
+ }
+
+ do Output.printString("THE AVERAGE IS: ");
+ do Output.printInt(sum / length);
+ do Output.println();
+
+ return;
+ }
+}
diff --git a/projects/10/ArrayTest/Main.xml b/projects/10/ArrayTest/Main.xml
new file mode 100644
index 0000000..0ea96df
--- /dev/null
+++ b/projects/10/ArrayTest/Main.xml
@@ -0,0 +1,286 @@
+<class>
+ <keyword> class </keyword>
+ <identifier> Main </identifier>
+ <symbol> { </symbol>
+ <subroutineDec>
+ <keyword> function </keyword>
+ <keyword> void </keyword>
+ <identifier> main </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <varDec>
+ <keyword> var </keyword>
+ <identifier> Array </identifier>
+ <identifier> a </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <varDec>
+ <keyword> var </keyword>
+ <keyword> int </keyword>
+ <identifier> length </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <varDec>
+ <keyword> var </keyword>
+ <keyword> int </keyword>
+ <identifier> i </identifier>
+ <symbol> , </symbol>
+ <identifier> sum </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> length </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Keyboard </identifier>
+ <symbol> . </symbol>
+ <identifier> readInt </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <stringConstant> HOW MANY NUMBERS? </stringConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> a </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Array </identifier>
+ <symbol> . </symbol>
+ <identifier> new </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> length </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <whileStatement>
+ <keyword> while </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> &lt; </symbol>
+ <term>
+ <identifier> length </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> a </identifier>
+ <symbol> [ </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ </expression>
+ <symbol> ] </symbol>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Keyboard </identifier>
+ <symbol> . </symbol>
+ <identifier> readInt </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <stringConstant> ENTER THE NEXT NUMBER: </stringConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </whileStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> sum </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <whileStatement>
+ <keyword> while </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> &lt; </symbol>
+ <term>
+ <identifier> length </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> sum </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> sum </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> a </identifier>
+ <symbol> [ </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ </expression>
+ <symbol> ] </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </whileStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Output </identifier>
+ <symbol> . </symbol>
+ <identifier> printString </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <stringConstant> THE AVERAGE IS: </stringConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Output </identifier>
+ <symbol> . </symbol>
+ <identifier> printInt </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> sum </identifier>
+ </term>
+ <symbol> / </symbol>
+ <term>
+ <identifier> length </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Output </identifier>
+ <symbol> . </symbol>
+ <identifier> println </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <symbol> } </symbol>
+</class>
diff --git a/projects/10/ArrayTest/MainT.xml b/projects/10/ArrayTest/MainT.xml
new file mode 100644
index 0000000..68721ec
--- /dev/null
+++ b/projects/10/ArrayTest/MainT.xml
@@ -0,0 +1,142 @@
+<tokens>
+<keyword> class </keyword>
+<identifier> Main </identifier>
+<symbol> { </symbol>
+<keyword> function </keyword>
+<keyword> void </keyword>
+<identifier> main </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> var </keyword>
+<identifier> Array </identifier>
+<identifier> a </identifier>
+<symbol> ; </symbol>
+<keyword> var </keyword>
+<keyword> int </keyword>
+<identifier> length </identifier>
+<symbol> ; </symbol>
+<keyword> var </keyword>
+<keyword> int </keyword>
+<identifier> i </identifier>
+<symbol> , </symbol>
+<identifier> sum </identifier>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> length </identifier>
+<symbol> = </symbol>
+<identifier> Keyboard </identifier>
+<symbol> . </symbol>
+<identifier> readInt </identifier>
+<symbol> ( </symbol>
+<stringConstant> HOW MANY NUMBERS? </stringConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> a </identifier>
+<symbol> = </symbol>
+<identifier> Array </identifier>
+<symbol> . </symbol>
+<identifier> new </identifier>
+<symbol> ( </symbol>
+<identifier> length </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ; </symbol>
+<keyword> while </keyword>
+<symbol> ( </symbol>
+<identifier> i </identifier>
+<symbol> &lt; </symbol>
+<identifier> length </identifier>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> a </identifier>
+<symbol> [ </symbol>
+<identifier> i </identifier>
+<symbol> ] </symbol>
+<symbol> = </symbol>
+<identifier> Keyboard </identifier>
+<symbol> . </symbol>
+<identifier> readInt </identifier>
+<symbol> ( </symbol>
+<stringConstant> ENTER THE NEXT NUMBER: </stringConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<identifier> i </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> sum </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ; </symbol>
+<keyword> while </keyword>
+<symbol> ( </symbol>
+<identifier> i </identifier>
+<symbol> &lt; </symbol>
+<identifier> length </identifier>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> sum </identifier>
+<symbol> = </symbol>
+<identifier> sum </identifier>
+<symbol> + </symbol>
+<identifier> a </identifier>
+<symbol> [ </symbol>
+<identifier> i </identifier>
+<symbol> ] </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<identifier> i </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> do </keyword>
+<identifier> Output </identifier>
+<symbol> . </symbol>
+<identifier> printString </identifier>
+<symbol> ( </symbol>
+<stringConstant> THE AVERAGE IS: </stringConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Output </identifier>
+<symbol> . </symbol>
+<identifier> printInt </identifier>
+<symbol> ( </symbol>
+<identifier> sum </identifier>
+<symbol> / </symbol>
+<identifier> length </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Output </identifier>
+<symbol> . </symbol>
+<identifier> println </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<symbol> } </symbol>
+</tokens>