From c89bcc6a364ded67504e88db1bf71417250e2ea3 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 17:02:33 +0100 Subject: checkpoint - revised Keyboard to use String for int conversion - almost finished String --- projects/12/KeyboardTest/Keyboard.jack | 30 +---- projects/12/KeyboardTest/Keyboard.vm | 199 +-------------------------------- 2 files changed, 3 insertions(+), 226 deletions(-) (limited to 'projects/12/KeyboardTest') 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 -- cgit v1.2.3