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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Hatter comes with a few built-in functions to help make your life
//! easier, all defined in this module.

// Each built-in Hatter functions comes in one of two flavors of
// function: `Native` or `Special`. A `Native` function is a regular
// function that takes an `Args` struct and returns a
// `Result<Value>`, but `Special` is more like a macro (or fexpr):
// its arguments are not evaluated, but instead passed to the
// function as syntax. The scope of the caller is also passed to the
// special function. This means the function can then decide how,
// when, and if to evaluate arguments or not. We use this to
// implement short circuiting in `&&` and `||`, but it could have
// other applications, too.

use {
    crate::{Args, Env, Native, Result, Special, Stmt, Symbol, Value},
    std::{collections::HashMap, rc::Rc},
};

/// Return the builtin Special functions that come with Hatter.
/// Ideally we build this at compile time, at least in release mode,
/// instead of creating them all at runtime.
pub(crate) fn specials() -> HashMap<String, Rc<Special>> {
    let mut map: HashMap<String, Rc<Special>> = HashMap::new();

    map.insert("&&".into(), rc!(and));
    map.insert("||".into(), rc!(or));

    map
}

/// Return the builtin Native functions that come with Hatter.
pub(crate) fn natives() -> HashMap<String, Rc<Native>> {
    let mut map: HashMap<String, Rc<Native>> = HashMap::new();

    macro_rules! native {
        ($name:expr => $fn:expr) => {
            map.insert($name.to_string(), rc!($fn));
        };
    }

    native!("type" => r#type);
    native!("when" => when);
    native!("==" => eq);
    native!("!=" => neq);
    native!("not" => not);
    native!("!" => not);
    native!("concat" => concat);
    native!("index" => index);
    native!("set_index" => set_index);
    native!("<<" => push);
    native!("push" => push);
    native!("pop" => pop);
    native!("." => index);
    native!(".." => range);
    native!("..=" => range_inclusive);
    native!("+" => add);
    native!("-" => sub);
    native!("*" => mul);
    native!("/" => div);
    native!("%" => r#mod);
    native!(">" => gt);
    native!(">=" => gte);
    native!("<" => lt);
    native!("<=" => lte);
    native!("print" => print);
    native!("puts" => puts);
    native!("to-uppercase" => to_uppercase);
    native!("to-lowercase" => to_lowercase);
    native!("replace" => replace);
    native!("contains?" => contains_);
    native!("split" => split);
    native!("count" => count);
    native!("len" => len);
    native!("empty?" => empty_);

    map
}

//////////////////////////////////////////////////////////////////////
// Internal Functions

/// Combine several Values into a String.
/// Used internally by String interpolation.
///
/// `concat("hi", "-", 23) #=> "hi-23"`
pub fn concat(args: Args) -> Result<Value> {
    let mut sum = String::new();
    for arg in args {
        sum.push_str(&arg.to_string());
    }
    Ok(sum.into())
}

/// Returns a Value if a condition is true.
/// Used internally by tag attributes.
///
/// ```ignore
/// when(true, "yep")   #=> "yep"
/// when(false, "nope") #=> None
/// ```
pub fn when(args: Args) -> Result<Value> {
    let fst = args.need(0)?;
    if matches!(fst, Value::None | Value::Bool(false)) {
        Ok(Value::None)
    } else {
        Ok(args.need(1)?)
    }
}

/// Returns the String name of a Value's type.
///
/// ```ignore
/// type('heyo')  #=> "String"
/// type(123)     #=> "Number"
/// ```
pub fn r#type(args: Args) -> Result<Value> {
    Value::String(args.need(0)?.typename().into()).ok()
}

//////////////////////////////////////////////////////////////////////
// Boolean Operators

/// Special: Short-circuiting `&&` operator.
pub fn and(env: &mut Env, args: &[Stmt]) -> Result<Value> {
    if args.len() != 2 {
        return error!("Expected 2 args, got {}", 2);
    }
    Ok((env.eval(&args[0])?.to_bool() && env.eval(&args[1])?.to_bool()).into())
}

/// Special: Short-circuiting `||` operator.
pub fn or(env: &mut Env, args: &[Stmt]) -> Result<Value> {
    if args.len() != 2 {
        return error!("Expected 2 args, got {}", 2);
    }
    Ok((env.eval(&args[0])?.to_bool() || env.eval(&args[1])?.to_bool()).into())
}

/// `==` operator: check if two Values are equal.
pub fn eq(args: Args) -> Result<Value> {
    if let Some(val) = args.get(0) {
        match val {
            Value::None => matches!(args.get(1), Some(Value::None)),
            Value::Bool(b1) => match args.get(1) {
                Some(Value::Bool(b2)) => b1 == b2,
                _ => false,
            },
            Value::Number(n1) => match args.get(1) {
                Some(Value::Number(n2)) => n1 == n2,
                _ => false,
            },
            Value::String(s1) => match args.get(1) {
                Some(Value::String(s2)) => s1 == s2,
                _ => false,
            },
            _ => false,
        }
        .into()
    } else {
        Value::None
    }
    .ok()
}

/// `!=` operator: check if two Values are not equal.
pub fn neq(args: Args) -> Result<Value> {
    Value::Bool(match eq(args)? {
        Value::Bool(b) => !b,
        _ => false,
    })
    .ok()
}

/// `!` operator: return the opposite Bool of a Value
pub fn not(args: Args) -> Result<Value> {
    if let Some(val) = args.get(0) {
        match val {
            Value::None | Value::Bool(false) => Value::Bool(true),
            _ => Value::Bool(false),
        }
    } else {
        Value::None
    }
    .ok()
}

/// `>` operator: check if a number is greater than another.
pub fn gt(args: Args) -> Result<Value> {
    Value::Bool(args.need_number(0)? > args.need_number(1)?).ok()
}

/// `>=` operator: check if a number is greater than or equal to another.
pub fn gte(args: Args) -> Result<Value> {
    Value::Bool(args.need_number(0)? >= args.need_number(1)?).ok()
}

/// `<` operator: check if a number is less than another.
pub fn lt(args: Args) -> Result<Value> {
    Value::Bool(args.need_number(0)? < args.need_number(1)?).ok()
}

/// `<=` operator: check if a number is less than or equal to another.
pub fn lte(args: Args) -> Result<Value> {
    Value::Bool(args.need_number(0)? <= args.need_number(1)?).ok()
}

//////////////////////////////////////////////////////////////////////
// Math Functions

/// `+` operator: add two numbers.
pub fn add(args: Args) -> Result<Value> {
    if let Some(Value::Number(_)) = args.get(0) {
        let mut sum = 0.0;
        let mut iter = args.iter();
        while let Some(Value::Number(x)) = iter.next() {
            sum += x;
        }
        return Value::Number(sum).ok();
    } else if let Some(Value::String(_)) = args.get(0) {
        let mut sum = String::new();
        let mut iter = args.iter();
        while let Some(Value::String(x)) = iter.next() {
            sum += x;
        }
        return Value::String(sum.into()).ok();
    }
    Value::None.ok()
}

/// `-` operator: subtract one number from another.
pub fn sub(args: Args) -> Result<Value> {
    Value::Number(args.need_number(0)? - args.need_number(1)?).ok()
}

/// `*` operator: multiply two numbers.
pub fn mul(args: Args) -> Result<Value> {
    Value::Number(args.need_number(0)? * args.need_number(1)?).ok()
}

/// `/` operator: divide one number by another.
pub fn div(args: Args) -> Result<Value> {
    Value::Number(args.need_number(0)? / args.need_number(1)?).ok()
}

/// `%` operator: find the remainder of dividing one number by anohter.
pub fn r#mod(args: Args) -> Result<Value> {
    Value::Number(args.need_number(0)? % args.need_number(1)?).ok()
}

//////////////////////////////////////////////////////////////////////
// String Functions

/// Rust's `String::to_uppercase(&self)`
pub fn to_uppercase(args: Args) -> Result<Value> {
    Value::String(args.need_string(0)?.to_uppercase().into()).ok()
}

/// Rust's `String::to_lowercase(&self)`
pub fn to_lowercase(args: Args) -> Result<Value> {
    Value::String(args.need_string(0)?.to_lowercase().into()).ok()
}

/// Does the string contain a substring?
/// `contains?("Mr Rogers", "Mr") #=> true`
pub fn contains_(args: Args) -> Result<Value> {
    Value::Bool(args.need_string(0)?.contains(args.need_string(1)?)).ok()
}

/// Count occurences of substring in string.
/// `count("Mr Rogers", "r") #=> 2`
pub fn count(args: Args) -> Result<Value> {
    Value::from(args.need_string(0)?.matches(args.need_string(1)?).count()).ok()
}

/// Split a string into a List by a separator.
/// `split("Mr Rogers", " ")` #=> ["Mr", "Rogers"]`
pub fn split(args: Args) -> Result<Value> {
    Value::from(
        args.need_string(0)?
            .split(args.need_string(1)?)
            .collect::<Vec<_>>(),
    )
    .ok()
}

/// Find and replace all matches in a target string.
///
/// `replace("Mr Rogers", "Ro", "Dod") #=> "Mr Dodgers"`
pub fn replace(args: Args) -> Result<Value> {
    let s = args.need_string(0)?;
    let search = args.need_string(1)?;
    let replace = args.need_string(2)?;
    Value::String(s.replace(search, replace).into()).ok()
}

//////////////////////////////////////////////////////////////////////
// Container (Map, List, Object) Functions

/// `.` operator and `[]` operator
/// Look up a List item by number or a Map item by key.
/// Returns None or the Value.
pub fn index(args: Args) -> Result<Value> {
    if args.len() != 2 {
        return Value::None.ok();
    }
    let subject = args.need(0)?;

    match subject {
        Value::Map(map) => map
            .borrow()
            .get(Symbol::from(args.need_string(1)?))
            .unwrap_or(&Value::None)
            .clone(),
        Value::List(list) => {
            let mut idx = args.need_number(1)? as isize;
            if idx < 0 {
                let len = list.borrow().len();
                if (idx.abs() as usize) <= len {
                    idx += list.borrow().len() as isize;
                }
            }
            list.borrow()
                .get(idx as usize)
                .unwrap_or(&Value::None)
                .clone()
        }
        Value::Object(o) => o.get(args.need_string(1)?).unwrap_or(Value::None),
        _ => Value::None,
    }
    .ok()
}

/// Set a specific index in a List or Map.
/// If List, must be equal to or below the length.
///
/// ```ignore
/// a[1] = 2
/// map[key] = val
/// ```
fn set_index(args: Args) -> Result<Value> {
    match args.need(0)? {
        Value::Map(map) => {
            map.borrow_mut()
                .insert(Symbol::from(args.need_string(1)?), args.need(2)?.into());
        }
        Value::List(list) => {
            let mut idx = args.need_number(1)? as isize;
            if idx < 0 {
                let len = list.borrow().len();
                if (idx.abs() as usize) < len {
                    idx += list.borrow().len() as isize;
                }
            }
            let idx = idx as usize;
            if idx > list.borrow().len() {
                return Value::None.ok();
            }
            list.borrow_mut().insert(idx, args.need(2)?.into());
        }
        Value::Object(o) => {
            o.set(args.need_string(1)?, args.need(2)?);
        }
        _ => {}
    }
    Value::None.ok()
}

/// Get the length of a Map, List, or String. Returns 0 for all
/// other values.
///
/// ```ignore
/// len([])            #=> 0
/// len([5])           #=> 1
/// len('hi')          #=> 2
/// len({name:'Ra'})   #=> 1
/// ```
pub fn len(args: Args) -> Result<Value> {
    match args.need(0)? {
        Value::List(list) => list.borrow().len().into(),
        Value::Map(map) => map.borrow().len().into(),
        Value::String(s) => s.len().into(),
        _ => Value::Number(0.0),
    }
    .ok()
}

/// True if the length of a Map, List, or String is `0`.
///
/// ```ignore
/// empty?([])  #=> true
/// empty?([1]) #=> false
/// ```
pub fn empty_(args: Args) -> Result<Value> {
    Value::Bool(len(args)?.to_f64() == 0.0).ok()
}

//////////////////////////////////////////////////////////////////////
// List Functions

/// Add a Value to a List. Modifies the List.
///
/// ```ignore
/// a := 1..=3  #=> [1,2,3]
/// push(a, 4)
/// a           #=> [1,2,3,4]
/// ```
pub fn push(args: Args) -> Result<Value> {
    if let Value::List(list) = args.need(0)? {
        list.borrow_mut().push(args.need(1)?);
    }
    Value::None.ok()
}

/// Remove the last Value from a List. Modifies the List.
/// ```ignore
/// a := 1..=3  #=> [1,2,3]
/// pop(a)      #=> 3
/// len(a)      #=> 2
/// ```
pub fn pop(args: Args) -> Result<Value> {
    if let Value::List(list) = args.need(0)? {
        Value::from(list.borrow_mut().pop())
    } else {
        Value::None
    }
    .ok()
}

//////////////////////////////////////////////////////////////////////
// Range Functions

/// `..` operator
///
/// `1..5  #=> [1,2,3,4]`
pub fn range(args: Args) -> Result<Value> {
    let start = args.need_number(0)? as i32;
    let end = args.need_number(1)? as i32;
    Ok((start..end).collect::<Vec<_>>().into())
}

/// `..=` operator
///
/// `1..=5  #=> [1,2,3,4,5]`
pub fn range_inclusive(args: Args) -> Result<Value> {
    let start = args.need_number(0)? as i32;
    let end = args.need_number(1)? as i32;
    Ok((start..=end).collect::<Vec<_>>().into())
}

//////////////////////////////////////////////////////////////////////
// I/O Functions

/// Print one or more Values, without newline.
pub fn print(mut args: Args) -> Result<Value> {
    while !args.is_empty() {
        let arg = args.remove(0);
        if args.is_empty() {
            args.env.print(format!("{}", arg));
        } else {
            args.env.print(format!("{} ", arg));
        }
    }
    Value::None.ok()
}

/// Print one or more Values, wit newlines.
pub fn puts(mut args: Args) -> Result<Value> {
    while !args.is_empty() {
        let arg = args.remove(0);
        if args.is_empty() {
            args.env.print(format!("{}", arg));
        } else {
            args.env.print(format!("{} ", arg));
        }
    }
    args.env.print("\n");
    Value::None.ok()
}