-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathString.jack
151 lines (137 loc) · 4.33 KB
/
String.jack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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/12/String.jack
/**
* Represents character strings. In addition for constructing and disposing
* strings, the class features methods for getting and setting individual
* characters of the string, for erasing the string's last character,
* for appending a character to the string's end, and more typical
* string-oriented operations.
*/
class String {
field int maxLength;
field Array stringArray;
field int currentLength;
/** constructs a new empty string with a maximum length of maxLength
* and initial length of 0. */
constructor String new(int maxLength_) {
let maxLength = maxLength_;
let stringArray = Array.new(maxLength_);
let currentLength = 0;
return this;
}
constructor String newWithStr(int stringLength) {
var int i;
var int argi;
var int cache;
let maxLength = stringLength;
let stringArray = Array.new(stringLength);
let currentLength = stringLength;
while (i < stringLength) {
let argi = (i / 2) + 1;
let stringArray[i] = arguments[argi] & 127;
let cache = arguments[argi] / 128;
if (~(cache = 0))
let stringArray[i + 1] = cache;
let i = i + 2;
}
return this;
}
/** Returns the current length of this string. */
method int length() {
return currentLength;
}
method void clear() {
let currentLength = 0;
}
/** Returns the character at the j-th location of this string. */
method char charAt(int j) {
if (~(j < currentLength))
do Sys.error(15);
return stringArray[j];
}
/** Appends c to this string's end and returns this string. */
method String appendChar(char c) {
if (currentLength = maxLength)
do Sys.error(17);
let stringArray[currentLength] = c;
let currentLength = currentLength + 1;
return this;
}
/** Erases the last character from this string. */
method void eraseLastChar() {
if (~(currentLength = 0))
let currentLength = currentLength - 1;
}
/** Returns the integer value of this string,
* until a non-digit character is detected. */
method int intValue() {
var int v;
var int i;
var boolean isNegative;
var int cache;
let isNegative = stringArray[0] = 45;
if (isNegative) {
let i = 1;
}
while (i < currentLength) {
let cache = stringArray[i] - 48;
if (~((cache > -1) & (cache < 10))) {
let i = currentLength;
} else {
let v = (v * 10) + cache;
let i = i + 1;
}
}
if (~isNegative) {
return v;
}
return -v;
}
/** Sets this string to hold a representation of the given value. */
method void setInt(int val) {
var int shifted, valMagnitude;
if (~(val < 0)) {
let currentLength = 0;
} else {
let stringArray[0] = 45;
let currentLength = 1;
let val = -val;
}
if (~(val < 10000)) {
let valMagnitude = 5;
} else if (~(val < 1000)) {
let valMagnitude = 4;
} else if (~(val < 100)) {
let valMagnitude = 3;
} else if (~(val < 10)) {
let valMagnitude = 2;
} else {
let valMagnitude = 1;
}
let currentLength = currentLength + valMagnitude;
if (currentLength > maxLength)
do Sys.error(19);
let valMagnitude = currentLength;
let shifted = 1;
while (~(shifted = 0)) {
let shifted = val / 10;
let valMagnitude = valMagnitude - 1;
let stringArray[valMagnitude] = (val - (shifted * 10)) + 48;
let val = shifted;
}
}
/** 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;
}
}