Skip to content

Commit

Permalink
At primitive implementation for String>>#charAt: (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr authored Apr 4, 2024
2 parents 470bab2 + 1cc69ff commit 0228d31
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/som/interpreter/ast/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
# Inner
#
# +-----------------+
# | OnStack |
# | OnStack | boolean indicating whether the frame is still on the stack
# +-----------------+
# | Receiver |
# | Receiver | the same as the receiver in the frame, not to be changed
# +-----------------+
# | ArgForInner 1 |
# | ... |
Expand Down
16 changes: 16 additions & 0 deletions src/som/interpreter/bc/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
# | ... |
# | Local n |
# +-----------+
#
# Inner
#
# +-----------------+
# | OnStack | boolean indicating whether the frame is still on the stack
# +-----------------+
# | Receiver | the same as the receiver in the frame, not to be changed
# +-----------------+
# | ArgForInner 1 |
# | ... |
# | ArgForInner n |
# +-----------------+
# | LocalForInner 1 |
# | ... |
# | LocalForInner n |
# +-----------------+


def create_frame(
Expand Down
10 changes: 10 additions & 0 deletions src/som/primitives/string_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def _substring(rcvr, start, end):
return String(string[s:e])


def _char_at(rcvr, idx):
i = idx.get_embedded_integer() - 1
string = rcvr.get_embedded_string()

if i < 0 or i >= len(string):
return String("Error - index out of bounds")
return String(string[i])


def _hashcode(rcvr):
return Integer(compute_hash(rcvr.get_embedded_string()))

Expand Down Expand Up @@ -81,6 +90,7 @@ def _is_digits(self):

class StringPrimitivesBase(Primitives):
def install_primitives(self):
self._install_instance_primitive(BinaryPrimitive("charAt:", _char_at))
self._install_instance_primitive(BinaryPrimitive("concatenate:", _concat))
self._install_instance_primitive(UnaryPrimitive("asSymbol", _as_symbol))
self._install_instance_primitive(UnaryPrimitive("length", _length))
Expand Down

0 comments on commit 0228d31

Please sign in to comment.