Skip to content

Commit 0228d31

Browse files
authored
At primitive implementation for String>>#charAt: (#58)
2 parents 470bab2 + 1cc69ff commit 0228d31

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/som/interpreter/ast/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
# Inner
4747
#
4848
# +-----------------+
49-
# | OnStack |
49+
# | OnStack | boolean indicating whether the frame is still on the stack
5050
# +-----------------+
51-
# | Receiver |
51+
# | Receiver | the same as the receiver in the frame, not to be changed
5252
# +-----------------+
5353
# | ArgForInner 1 |
5454
# | ... |

src/som/interpreter/bc/frame.py

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
# | ... |
3333
# | Local n |
3434
# +-----------+
35+
#
36+
# Inner
37+
#
38+
# +-----------------+
39+
# | OnStack | boolean indicating whether the frame is still on the stack
40+
# +-----------------+
41+
# | Receiver | the same as the receiver in the frame, not to be changed
42+
# +-----------------+
43+
# | ArgForInner 1 |
44+
# | ... |
45+
# | ArgForInner n |
46+
# +-----------------+
47+
# | LocalForInner 1 |
48+
# | ... |
49+
# | LocalForInner n |
50+
# +-----------------+
3551

3652

3753
def create_frame(

src/som/primitives/string_primitives.py

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ def _substring(rcvr, start, end):
3939
return String(string[s:e])
4040

4141

42+
def _char_at(rcvr, idx):
43+
i = idx.get_embedded_integer() - 1
44+
string = rcvr.get_embedded_string()
45+
46+
if i < 0 or i >= len(string):
47+
return String("Error - index out of bounds")
48+
return String(string[i])
49+
50+
4251
def _hashcode(rcvr):
4352
return Integer(compute_hash(rcvr.get_embedded_string()))
4453

@@ -81,6 +90,7 @@ def _is_digits(self):
8190

8291
class StringPrimitivesBase(Primitives):
8392
def install_primitives(self):
93+
self._install_instance_primitive(BinaryPrimitive("charAt:", _char_at))
8494
self._install_instance_primitive(BinaryPrimitive("concatenate:", _concat))
8595
self._install_instance_primitive(UnaryPrimitive("asSymbol", _as_symbol))
8696
self._install_instance_primitive(UnaryPrimitive("length", _length))

0 commit comments

Comments
 (0)