summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-17 17:02:33 +0100
committerYuchen Pei <me@ypei.me>2018-01-17 17:02:33 +0100
commitc89bcc6a364ded67504e88db1bf71417250e2ea3 (patch)
tree9a185393d07695c762080bf2fe555545d0323055
parentfd8e3bee660f22032b51bc12452353b141e125be (diff)
checkpoint
- revised Keyboard to use String for int conversion - almost finished String
-rw-r--r--projects/12/KeyboardTest/Keyboard.jack30
-rw-r--r--projects/12/KeyboardTest/Keyboard.vm199
-rw-r--r--projects/12/MemoryTest/MemoryTest.out2
-rw-r--r--projects/12/String.jack66
4 files changed, 69 insertions, 228 deletions
diff --git a/projects/12/KeyboardTest/Keyboard.jack b/projects/12/KeyboardTest/Keyboard.jack
index 2aba72e..a35704a 100644
--- a/projects/12/KeyboardTest/Keyboard.jack
+++ b/projects/12/KeyboardTest/Keyboard.jack
@@ -89,37 +89,9 @@ class Keyboard {
* entered text is detected). Also handles user backspaces.
*/
function int readInt(String message) {
- var int n, i;
- var char t;
var String s;
- var boolean neg, done;
do Output.printString(message);
- let n = 0;
- let neg = false;
let s = Keyboard.readLine("");
- let i = 0;
- if (s.charAt(0) = 45) {
- let i = 1;
- let neg = true;
- }
- let t = s.charAt(i);
- if ((t < 48) | (t > 57)) {
- do Sys.error(3);
- do Output.printString("Keyboard.readInt: the input data is not number!");
- }
- let done = false;
- while (~done) {
- if ((s.charAt(i) > 47) & (s.charAt(i) < 58)) {
- let n = n * 10 + (s.charAt(i) - 48);
- } else {
- let done = true;
- }
- let i = i + 1;
- let done = done | (i + 1 > s.length());
- }
- if (neg) {
- let n = - n;
- }
- return n;
+ return s.intValue();
}
}
diff --git a/projects/12/KeyboardTest/Keyboard.vm b/projects/12/KeyboardTest/Keyboard.vm
index bebd916..5dc3426 100644
--- a/projects/12/KeyboardTest/Keyboard.vm
+++ b/projects/12/KeyboardTest/Keyboard.vm
@@ -102,209 +102,14 @@ goto WHILE_EXP0
label WHILE_END0
push local 1
return
-function Keyboard.readInt 6
+function Keyboard.readInt 1
push argument 0
call Output.printString 1
pop temp 0
push constant 0
-pop local 0
-push constant 0
-pop local 4
-push constant 0
call String.new 1
call Keyboard.readLine 1
-pop local 3
-push constant 0
-pop local 1
-push local 3
-push constant 0
-call String.charAt 2
-push constant 45
-eq
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 1
-pop local 1
-push constant 0
-not
-pop local 4
-label IF_FALSE0
-push local 3
-push local 1
-call String.charAt 2
-pop local 2
-push local 2
-push constant 48
-lt
-push local 2
-push constant 57
-gt
-or
-if-goto IF_TRUE1
-goto IF_FALSE1
-label IF_TRUE1
-push constant 3
-call Sys.error 1
-pop temp 0
-push constant 47
-call String.new 1
-push constant 75
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 121
-call String.appendChar 2
-push constant 98
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 100
-call String.appendChar 2
-push constant 46
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 100
-call String.appendChar 2
-push constant 73
-call String.appendChar 2
-push constant 110
-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 116
-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 105
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 112
-call String.appendChar 2
-push constant 117
-call String.appendChar 2
-push constant 116
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 100
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 116
-call String.appendChar 2
-push constant 97
-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
-push constant 110
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 116
-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 33
-call String.appendChar 2
-call Output.printString 1
-pop temp 0
-label IF_FALSE1
-push constant 0
-pop local 5
-label WHILE_EXP0
-push local 5
-not
-not
-if-goto WHILE_END0
-push local 3
-push local 1
-call String.charAt 2
-push constant 47
-gt
-push local 3
-push local 1
-call String.charAt 2
-push constant 58
-lt
-and
-if-goto IF_TRUE2
-goto IF_FALSE2
-label IF_TRUE2
-push local 0
-push constant 10
-call Math.multiply 2
-push local 3
-push local 1
-call String.charAt 2
-push constant 48
-sub
-add
-pop local 0
-goto IF_END2
-label IF_FALSE2
-push constant 0
-not
-pop local 5
-label IF_END2
-push local 1
-push constant 1
-add
-pop local 1
-push local 5
-push local 1
-push constant 1
-add
-push local 3
-call String.length 1
-gt
-or
-pop local 5
-goto WHILE_EXP0
-label WHILE_END0
-push local 4
-if-goto IF_TRUE3
-goto IF_FALSE3
-label IF_TRUE3
-push local 0
-neg
pop local 0
-label IF_FALSE3
push local 0
+call String.intValue 1
return
diff --git a/projects/12/MemoryTest/MemoryTest.out b/projects/12/MemoryTest/MemoryTest.out
index 8259ed2..e69de29 100644
--- a/projects/12/MemoryTest/MemoryTest.out
+++ b/projects/12/MemoryTest/MemoryTest.out
@@ -1,2 +0,0 @@
-|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|
-| 333 | 334 | 222 | 122 | 100 | 10 |
diff --git a/projects/12/String.jack b/projects/12/String.jack
index 228d71c..2d5c440 100644
--- a/projects/12/String.jack
+++ b/projects/12/String.jack
@@ -11,39 +11,102 @@
* string-oriented operations.
*/
class String {
+ field int maxLen;
+ field int len;
+ field Array s;
/** constructs a new empty string with a maximum length of maxLength
* and initial length of 0. */
constructor String new(int maxLength) {
+ let s = new Array(maxLength);
+ let maxLen = maxLength;
+ let len = 0;
+ return this;
}
/** Disposes this string. */
method void dispose() {
+ do s.dispose();
+ do Memory.deAlloc(this);
+ return;
}
/** Returns the current length of this string. */
method int length() {
+ return len;
}
/** Returns the character at the j-th location of this string. */
method char charAt(int j) {
+ if ((j < 0) | (j + 1 > len)){
+ Output.printString("String.charAt: index out of range!");
+ Sys.error(5);
+ }
+ return s[j];
}
/** Sets the character at the j-th location of this string to c. */
method void setCharAt(int j, char c) {
+ let s[j] = c;
+ return;
}
/** Appends c to this string's end and returns this string. */
method String appendChar(char c) {
+ if (len = maxLen) {
+ Output.printString("String.appendChar: reached max length!");
+ Sys.error(5);
+ }
+ let s[len] = c;
+ let len = len + 1;
+ return this;
}
/** Erases the last character from this string. */
method void eraseLastChar() {
+ if (len = 0){
+ Output.printString("String.eraseLastChar: string is already empty!");
+ Sys.error(5);
+ }
+ let len = len - 1;
+ return;
}
/** Returns the integer value of this string,
* until a non-digit character is detected. */
method int intValue() {
+ var int n, i;
+ var char c;
+ var boolean neg, done;
+ let n = 0;
+ if (s[0] = 45) {
+ let i = 1;
+ let neg = true;
+ } else {
+ let i = 0;
+ let neg = false;
+ }
+ let c = s[i];
+ if ((t < 48) | (t > 57)) {
+ do Sys.error(3);
+ do Output.printString("String.intValue: the input data is not number!");
+ }
+ let done = false;
+ while ((~done) & (i < len)) {
+ let t = s[i];
+ if ((t > 47) & (t < 58)) {
+ let n = n * 10 + (t - 48);
+ } else {
+ let done = true;
+ }
+ let i = i + 1;
+ }
+ if (neg) {
+ return -n;
+ } else {
+ return n;
+ }
+
}
/** Sets this string to hold a representation of the given value. */
@@ -52,13 +115,16 @@ class String {
/** Returns the new line character. */
function char newLine() {
+ return 128;
}
/** Returns the backspace character. */
function char backSpace() {
+ return 129;
}
/** Returns the double quote (") character. */
function char doubleQuote() {
+ return 34;
}
}