-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib_strings.py
More file actions
206 lines (153 loc) · 8.13 KB
/
Copy pathstdlib_strings.py
File metadata and controls
206 lines (153 loc) · 8.13 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
195
196
197
198
199
200
201
202
203
204
205
206
# stdlib_strings.py
"""
Engage Standard Library - Strings Module
Provides string manipulation functions for the Engage programming language.
"""
from typing import Dict, Callable
from engage_stdlib import BaseModule
class StringsModule(BaseModule):
"""
Strings module providing string manipulation functions.
"""
def get_functions(self) -> Dict[str, Callable]:
"""Return all string manipulation functions."""
return {
'length': self._length,
'substring': self._substring,
'split': self._split,
'join': self._join,
'to_upper': self._to_upper,
'to_lower': self._to_lower,
}
def _length(self, args):
"""Get the length of a string."""
from engage_interpreter import String, Number, ResultValue
if len(args) != 1:
return ResultValue('Error', String("length() expects exactly one argument."))
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("length() argument must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("length() expects a string argument."))
try:
return Number(len(args[0].value))
except Exception as e:
return ResultValue('Error', String(f"length() failed: {str(e)}"))
def _substring(self, args):
"""Extract a substring from a string."""
from engage_interpreter import String, Number, ResultValue
if len(args) < 2 or len(args) > 3:
return ResultValue('Error', String("substring() expects 2 or 3 arguments (string, start, [end])."))
# Validate string argument
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("substring() first argument must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("substring() first argument must be a string."))
# Validate start index
if not hasattr(args[1], 'value'):
return ResultValue('Error', String("substring() start index must have a value."))
if not isinstance(args[1].value, (int, float)):
return ResultValue('Error', String("substring() start index must be a number."))
string_val = args[0].value
start = int(args[1].value)
# Validate start index bounds
if start < 0:
return ResultValue('Error', String("substring() start index cannot be negative."))
if start > len(string_val):
return ResultValue('Error', String("substring() start index exceeds string length."))
try:
if len(args) == 3:
# Validate end index
if not hasattr(args[2], 'value'):
return ResultValue('Error', String("substring() end index must have a value."))
if not isinstance(args[2].value, (int, float)):
return ResultValue('Error', String("substring() end index must be a number."))
end = int(args[2].value)
# Validate end index bounds
if end < 0:
return ResultValue('Error', String("substring() end index cannot be negative."))
if end < start:
return ResultValue('Error', String("substring() end index cannot be less than start index."))
if end > len(string_val):
end = len(string_val) # Clamp to string length
result = string_val[start:end]
else:
result = string_val[start:]
return String(result)
except Exception as e:
return ResultValue('Error', String(f"substring() failed: {str(e)}"))
def _split(self, args):
"""Split a string by a delimiter."""
from engage_interpreter import String, ResultValue
if len(args) != 2:
return ResultValue('Error', String("split() expects exactly two arguments (string, delimiter)."))
# Validate string argument
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("split() first argument must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("split() first argument must be a string."))
# Validate delimiter argument
if not hasattr(args[1], 'value'):
return ResultValue('Error', String("split() second argument must have a value."))
if not isinstance(args[1].value, str):
return ResultValue('Error', String("split() second argument (delimiter) must be a string."))
string_val = args[0].value
delimiter = args[1].value
try:
parts = string_val.split(delimiter)
# Return as a simple string representation for now
# In a full implementation, this would return a Vector
return String(str(parts))
except Exception as e:
return ResultValue('Error', String(f"split() failed: {str(e)}"))
def _join(self, args):
"""Join strings with a delimiter."""
from engage_interpreter import String, ResultValue
if len(args) < 2:
return ResultValue('Error', String("join() expects at least two arguments (delimiter, string1, [string2, ...])."))
# Validate delimiter argument
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("join() first argument (delimiter) must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("join() first argument (delimiter) must be a string."))
delimiter = args[0].value
strings = []
# Validate all string arguments
for i, arg in enumerate(args[1:], 1):
if not hasattr(arg, 'value'):
return ResultValue('Error', String(f"join() argument {i+1} must have a value."))
# Convert to string if not already a string
try:
strings.append(str(arg.value))
except Exception as e:
return ResultValue('Error', String(f"join() failed to convert argument {i+1} to string: {str(e)}"))
try:
result = delimiter.join(strings)
return String(result)
except Exception as e:
return ResultValue('Error', String(f"join() failed: {str(e)}"))
def _to_upper(self, args):
"""Convert a string to uppercase."""
from engage_interpreter import String, ResultValue
if len(args) != 1:
return ResultValue('Error', String("to_upper() expects exactly one argument."))
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("to_upper() argument must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("to_upper() expects a string argument."))
try:
return String(args[0].value.upper())
except Exception as e:
return ResultValue('Error', String(f"to_upper() failed: {str(e)}"))
def _to_lower(self, args):
"""Convert a string to lowercase."""
from engage_interpreter import String, ResultValue
if len(args) != 1:
return ResultValue('Error', String("to_lower() expects exactly one argument."))
if not hasattr(args[0], 'value'):
return ResultValue('Error', String("to_lower() argument must have a value."))
if not isinstance(args[0].value, str):
return ResultValue('Error', String("to_lower() expects a string argument."))
try:
return String(args[0].value.lower())
except Exception as e:
return ResultValue('Error', String(f"to_lower() failed: {str(e)}"))