aiscript-vm 0.2.0

AIScript programming language interpreter
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::collections::HashMap;

use aiscript_arena::{Gc, Mutation};
use regex::Regex;

use crate::BuiltinMethod;
use crate::{Value, VmError, float_arg, string_arg, vm::Context};

use crate::string::InternedString;

pub(crate) fn define_string_methods(ctx: Context) -> HashMap<InternedString, BuiltinMethod> {
    [
        ("is_empty", BuiltinMethod(is_empty)),
        // Case conversion
        ("to_uppercase", BuiltinMethod(to_uppercase)),
        ("to_lowercase", BuiltinMethod(to_lowercase)),
        // Trim functions
        ("trim", BuiltinMethod(trim)),
        ("trim_start", BuiltinMethod(trim_start)),
        ("trim_end", BuiltinMethod(trim_end)),
        // Contains and position
        ("contains", BuiltinMethod(contains)),
        ("starts_with", BuiltinMethod(starts_with)),
        ("ends_with", BuiltinMethod(ends_with)),
        ("index_of", BuiltinMethod(index_of)),
        ("last_index_of", BuiltinMethod(last_index_of)),
        // Substring and slicing
        ("substring", BuiltinMethod(substring)),
        ("slice", BuiltinMethod(slice)),
        // Split and join
        ("split", BuiltinMethod(split)),
        ("join", BuiltinMethod(join)),
        // Regex operations
        ("regex_match", BuiltinMethod(regex_match)),
        ("regex_replace", BuiltinMethod(regex_replace)),
        // Misc string operations
        ("repeat", BuiltinMethod(repeat)),
        ("reverse", BuiltinMethod(reverse)),
        ("replace", BuiltinMethod(replace)),
    ]
    .into_iter()
    .map(|(name, f)| (ctx.intern_static(name), f))
    .collect()
}

fn is_empty<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    Ok(Value::Boolean(receiver.as_string()?.is_empty()))
}

// Case conversion
fn to_uppercase<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let upper = receiver.as_string_value()?.as_str().to_uppercase();
    // Ok(Value::String(mc.intern(upper.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, upper)))
}

fn to_lowercase<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let lower = receiver.as_string_value()?.as_str().to_lowercase();
    // Ok(Value::String(mc.intern(lower.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, lower)))
}

// Trim functions
fn trim<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let s = receiver.as_string_value()?;
    let trimmed = s.as_str().trim();
    // Ok(Value::String(mc.intern(trimmed.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, trimmed.to_owned())))
}

fn trim_start<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let s = receiver.as_string_value()?;
    let trimmed = s.as_str().trim_start();
    // Ok(Value::String(mc.intern(trimmed.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, trimmed.to_owned())))
}

fn trim_end<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let s = receiver.as_string_value()?;
    let trimmed = s.as_str().trim_end();
    // Ok(Value::String(mc.intern(trimmed.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, trimmed.to_owned())))
}

// Contains and position functions
fn contains<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let substr = string_arg!(&args, 0, "contains")?;
    Ok(Value::Boolean(
        receiver
            .as_string()?
            .to_str()
            .unwrap()
            .contains(substr.to_str().unwrap()),
    ))
}

fn starts_with<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let prefix = string_arg!(&args, 0, "starts_with")?;
    Ok(Value::Boolean(
        receiver
            .as_string()?
            .to_str()
            .unwrap()
            .starts_with(prefix.to_str().unwrap()),
    ))
}

fn ends_with<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let suffix = string_arg!(&args, 0, "ends_with")?;
    Ok(Value::Boolean(
        receiver
            .as_string()?
            .to_str()
            .unwrap()
            .ends_with(suffix.to_str().unwrap()),
    ))
}

// Find and position functions
fn index_of<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let substr = string_arg!(&args, 0, "index_of")?;

    // Optional start position
    let start = if args.len() > 1 {
        float_arg!(&args, 1, "index_of")? as usize
    } else {
        0
    };

    let s = receiver.as_string_value()?;
    let s = s.as_str();
    let substr = substr.to_str().unwrap();

    if start > s.len() {
        return Ok(Value::Number(-1.0));
    }

    match s[start..].find(substr) {
        Some(pos) => Ok(Value::Number((pos + start) as f64)),
        None => Ok(Value::Number(-1.0)),
    }
}

fn last_index_of<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let substr = string_arg!(&args, 0, "last_index_of")?;

    let s = receiver.as_string_value()?;
    let s = s.as_str();
    let substr = substr.to_str().unwrap();

    match s.rfind(substr) {
        Some(pos) => Ok(Value::Number(pos as f64)),
        None => Ok(Value::Number(-1.0)),
    }
}

// Substring and slicing
fn substring<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let start = float_arg!(&args, 0, "substring")? as usize;

    let s = receiver.as_string()?;
    let end = if args.len() > 1 {
        float_arg!(&args, 1, "substring")? as usize
    } else {
        s.len() as usize
    };

    let s = s.to_str().unwrap();

    // Handle start and end bounds
    let start = start.min(s.len());
    let end = end.min(s.len());
    let start = start.min(end); // Ensure start <= end

    // Ok(Value::String(mc.intern(s[start..end].as_bytes())))
    Ok(Value::IoString(Gc::new(mc, s[start..end].to_owned())))
}

fn slice<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let start = float_arg!(&args, 0, "slice")? as isize;
    let s = receiver.as_string()?;
    let end = if args.len() > 1 {
        float_arg!(&args, 1, "slice")? as isize
    } else {
        s.len() as isize
    };

    let s = s.to_str().unwrap();
    let len = s.len() as isize;

    // Convert negative indices to positive
    let start = if start < 0 {
        (len + start).max(0)
    } else {
        start.min(len)
    } as usize;
    let end = if end < 0 {
        (len + end).max(0)
    } else {
        end.min(len)
    } as usize;
    let start = start.min(end); // Ensure start <= end

    // Ok(Value::String(mc.intern(s[start..end].as_bytes())))
    Ok(Value::IoString(Gc::new(mc, s[start..end].to_owned())))
}

// Split and join
fn split<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let delimiter = string_arg!(&args, 0, "split")?.to_str().unwrap();

    let s = receiver.as_string_value()?;
    let parts = s
        .as_str()
        .split(delimiter)
        .map(|part| Value::IoString(Gc::new(mc, part.to_string())))
        .collect();

    // Convert to array
    Ok(Value::array(mc, parts))
}

fn join<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let receiver = receiver.as_string_value()?;
    let separator = receiver.as_str();

    if args.len() != 1 {
        return Err(VmError::RuntimeError(
            "join: expected exactly one array argument".into(),
        ));
    }

    // Get the array from args[0]
    let vec = match &args[0] {
        Value::List(list) => &list.borrow().data,
        _ => {
            return Err(VmError::RuntimeError(
                "join: argument must be an array".into(),
            ));
        }
    };

    let mut result = String::new();
    for (i, value) in vec.iter().enumerate() {
        if i > 0 {
            result.push_str(separator);
        }
        match value {
            Value::String(s) => result.push_str(s.to_str().unwrap()),
            Value::IoString(s) => result.push_str(s),
            _ => {
                return Err(VmError::RuntimeError(format!(
                    "join: array element {} must be a string",
                    i
                )));
            }
        }
    }

    Ok(Value::IoString(Gc::new(mc, result)))
}

// Regex operations
fn regex_match<'gc>(
    _mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let pattern = string_arg!(&args, 0, "regex_match")?;

    let regex = Regex::new(pattern.to_str().unwrap())
        .map_err(|e| VmError::RuntimeError(format!("Invalid regex pattern: {}", e)))?;

    Ok(Value::Boolean(
        regex.is_match(receiver.as_string_value()?.as_str()),
    ))
}

fn regex_replace<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let pattern = string_arg!(&args, 0, "regex_replace")?;
    let replacement = string_arg!(&args, 1, "regex_replace")?;

    let regex = Regex::new(pattern.to_str().unwrap())
        .map_err(|e| VmError::RuntimeError(format!("Invalid regex pattern: {}", e)))?;

    let result = regex
        .replace_all(
            receiver.as_string_value()?.as_str(),
            replacement.to_str().unwrap(),
        )
        .into_owned();

    // Ok(Value::String(mc.intern(result.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, result)))
}

// Misc string operations
fn repeat<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let count = float_arg!(&args, 0, "repeat")? as usize;

    let repeated = receiver.as_string_value()?.as_str().repeat(count);
    // Ok(Value::String(mc.intern(repeated.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, repeated)))
}

fn reverse<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    _args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let reversed: String = receiver
        .as_string()?
        .to_str()
        .unwrap()
        .chars()
        .rev()
        .collect();
    // Ok(Value::String(mc.intern(reversed.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, reversed)))
}

fn replace<'gc>(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
) -> Result<Value<'gc>, VmError> {
    let from = string_arg!(&args, 0, "replace")?;
    let to = string_arg!(&args, 1, "replace")?;

    let result = receiver
        .as_string()?
        .to_str()
        .unwrap()
        .replace(from.to_str().unwrap(), to.to_str().unwrap());
    // Ok(Value::String(mc.intern(result.as_bytes())))
    Ok(Value::IoString(Gc::new(mc, result)))
}