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
use arret_runtime::binding::*;

use arret_runtime::boxed;
use arret_runtime::boxed::refs::Gc;
use arret_runtime::task::Task;

fn fold_float_op<FR>(
    task: &mut Task,
    operands_iter: impl Iterator<Item = Gc<boxed::Num>>,
    initial_value: f64,
    float_reduce: FR,
) -> Gc<boxed::Float>
where
    FR: Fn(f64, f64) -> f64,
{
    let mut float_acc = initial_value;

    for operand in operands_iter {
        match operand.as_subtype() {
            boxed::NumSubtype::Int(int_ref) => {
                float_acc = float_reduce(float_acc, int_ref.value() as f64);
            }
            boxed::NumSubtype::Float(float_ref) => {
                // Convert to float and break
                float_acc = float_reduce(float_acc, float_ref.value());
            }
        }
    }

    boxed::Float::new(task, float_acc)
}

fn fold_num_op<IR, FR>(
    task: &mut Task,
    op_name: &'static str,
    mut operands_iter: impl Iterator<Item = Gc<boxed::Num>>,
    initial_value: i64,
    int_reduce: IR,
    float_reduce: FR,
) -> Gc<boxed::Num>
where
    IR: Fn(i64, i64) -> Option<i64>,
    FR: Fn(f64, f64) -> f64,
{
    // Accumulate as an integer for as long as possible
    let mut int_acc = initial_value;

    while let Some(operand) = operands_iter.next() {
        match operand.as_subtype() {
            boxed::NumSubtype::Int(int_ref) => {
                if let Some(reduced_int) = int_reduce(int_acc, int_ref.value()) {
                    int_acc = reduced_int;
                } else {
                    task.panic(format!("attempt to {} with overflow", op_name));
                }
            }
            boxed::NumSubtype::Float(float_ref) => {
                // Switch to float
                let float_acc = float_reduce(int_acc as f64, float_ref.value());
                return fold_float_op(task, operands_iter, float_acc, float_reduce).as_num_ref();
            }
        }
    }

    boxed::Int::new(task, int_acc).as_num_ref()
}

#[arret_rfi_derive::rust_fun("(All #{[N Num]} N & N -> N)")]
pub fn stdlib_add(
    task: &mut Task,
    initial_num: Gc<boxed::Num>,
    rest: Gc<boxed::List<boxed::Num>>,
) -> Gc<boxed::Num> {
    use std::iter;
    use std::ops::Add;

    fold_num_op(
        task,
        "add",
        iter::once(initial_num).chain(rest.iter()),
        0,
        i64::checked_add,
        f64::add,
    )
}

#[arret_rfi_derive::rust_fun("(All #{[N Num]} N & N -> N)")]
pub fn stdlib_mul(
    task: &mut Task,
    initial_num: Gc<boxed::Num>,
    rest: Gc<boxed::List<boxed::Num>>,
) -> Gc<boxed::Num> {
    use std::iter;
    use std::ops::Mul;

    fold_num_op(
        task,
        "multiply",
        iter::once(initial_num).chain(rest.iter()),
        1,
        i64::checked_mul,
        f64::mul,
    )
}

#[arret_rfi_derive::rust_fun("(All #{[N Num]} N & N -> N)")]
pub fn stdlib_sub(
    task: &mut Task,
    initial_num: Gc<boxed::Num>,
    rest: Gc<boxed::List<boxed::Num>>,
) -> Gc<boxed::Num> {
    use std::ops::Sub;

    match initial_num.as_subtype() {
        boxed::NumSubtype::Int(int_ref) => {
            if rest.is_empty() {
                boxed::Int::new(task, -int_ref.value()).as_num_ref()
            } else {
                fold_num_op(
                    task,
                    "subtract",
                    rest.iter(),
                    int_ref.value(),
                    i64::checked_sub,
                    f64::sub,
                )
            }
        }
        boxed::NumSubtype::Float(float_ref) => {
            if rest.is_empty() {
                boxed::Float::new(task, -float_ref.value()).as_num_ref()
            } else {
                fold_float_op(task, rest.iter(), float_ref.value(), f64::sub).as_num_ref()
            }
        }
    }
}

#[arret_rfi_derive::rust_fun("(Float & Float -> Float)")]
pub fn stdlib_div(initial_float: f64, rest: Gc<boxed::List<boxed::Float>>) -> f64 {
    if rest.is_empty() {
        initial_float.recip()
    } else {
        let mut acc = initial_float;
        for operand in rest.iter() {
            acc /= operand.value()
        }

        acc
    }
}

#[arret_rfi_derive::rust_fun("(Int Int -> Int)")]
pub fn stdlib_quot(task: &mut Task, numerator: i64, denominator: i64) -> i64 {
    match numerator.checked_div(denominator) {
        Some(result) => result,
        None => {
            task.panic("division by zero".to_owned());
            unreachable!("returned from panic")
        }
    }
}

#[arret_rfi_derive::rust_fun("(Int Int -> Int)")]
pub fn stdlib_rem(task: &mut Task, numerator: i64, denominator: i64) -> i64 {
    match numerator.checked_rem(denominator) {
        Some(result) => result,
        None => {
            task.panic("division by zero".to_owned());
            unreachable!("returned from panic")
        }
    }
}

#[arret_rfi_derive::rust_fun("(Float -> Float)")]
pub fn stdlib_sqrt(radicand: f64) -> f64 {
    radicand.sqrt()
}