-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.jack
More file actions
194 lines (171 loc) · 4.75 KB
/
Copy pathString.jack
File metadata and controls
194 lines (171 loc) · 4.75 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// This file is part of nand2tetris, as taught in The Hebrew University, and
// was written by Aviv Yaish. It is an extension to the specifications given
// [here](https://www.nand2tetris.org) (Shimon Schocken and Noam Nisan, 2017),
// as allowed by the Creative Common Attribution-NonCommercial-ShareAlike 3.0
// Unported [License](https://creativecommons.org/licenses/by-nc-sa/3.0/).
/**
* 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 Array str;
field int length;
/** constructs a new empty string with a maximum length of maxLength
* and initial length of 0. */
constructor String new(int maxLength)
{
if(maxLength = 0)
{
let maxLength = 1;
}
let str = Array.new(maxLength);
let length = 0;
return this;
}
/** Disposes this string. */
method void dispose()
{
do str.dispose();
let length = 0;
return;
}
/** Returns the current length of this string. */
method int length()
{
return length;
}
/** Returns the character at the j-th location of this string. */
method char charAt(int j)
{
return str[j];
}
/** Sets the character at the j-th location of this string to c. */
method void setCharAt(int j, char c)
{
let str[j] = c;
return;
}
/** Appends c to this string's end and returns this string. */
method String appendChar(char c)
{
let str[length] = c;
let length = length + 1;
return this;
}
/** Erases the last character from this string. */
method void eraseLastChar()
{
if (length > 0)
{
let length = length - 1;
}
return;
}
/** Returns the integer value of this string,
* until a non-digit character is detected. */
method boolean is_not_number(char c)
{
if(c < 48 | c > 57)
{
return true;
}
return false;
}
method int intValue()
{
// Pseudocode:
// 1. let val = 0
// 2. for (i = 0 .. str.length()) do
// 3. let d = integer value of str.charAt(i)
// 4. let val = (val*10) + d
// 5. return val
var int val, counter, d;
var boolean is_not_a_number,is_negative;
let val = 0;
let counter = 0;
let is_negative = false;
//check if the number is negative if so, go on by 1 char and flip the flag
if(str[counter] = 45)
{
let is_negative = true;
let counter = counter + 1;
}
while(counter < length)
{
let is_not_a_number = is_not_number(str[counter]);
if(is_not_a_number)
{
if(is_negative)
{
let val = -val;
return val;
}
return val;
}
let d = charAt(counter) - 48;
let val = (val*10) + d;
let counter = counter + 1;
}
if(is_negative)
{
let val = -val;
}
return val;
}
/** Sets this string to hold a representation of the given value. */
method void setIntRecursion(int num)
{
var int lastDigit, deductNum;
var char c;
let deductNum = num/10;
let lastDigit = num - (deductNum*10);
let c = (48 + lastDigit); //because 0 in 48 in ASCII
if (num < 10)
{
do appendChar(c);
}
else
{
do setIntRecursion(num/10);
do appendChar(c);
}
return;
}
method void setInt(int val)
{
// Pseudocode:
// 1. let lastDigit = val % 10
// 2. let c = character representing lastDigit
// 3. if (val < 10)
// 4. return c (as a string)
// 5. else
// 6. return int2String(val / 10).appendChar(c)
//if val is negative - change to positive and add '-' to the string
let length = 0;
if(val < 0)
{
let val = -val;
do appendChar(45);
}
do setIntRecursion(val);
return;
}
/** 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;
}
}