roan_engine/interpreter/
functions.rs

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
use crate::{
    context::Context,
    module::{Module, StoredFunction},
    value::Value,
    vm::{native_fn::NativeFunction, VM},
};
use anyhow::Result;
use roan_ast::{CallExpr, GetSpan};
use roan_error::{
    error::RoanError::{MissingParameter, TypeMismatch, UndefinedFunctionError},
    frame::Frame,
    print_diagnostic, TextSpan,
};
use tracing::debug;

impl Module {
    /// Executes a native function with the provided arguments.
    pub fn execute_native_function(
        &mut self,
        mut native: NativeFunction,
        args: Vec<Value>,
        vm: &mut VM,
    ) -> Result<()> {
        let result = native.call(args)?;
        vm.push(result);

        Ok(())
    }

    /// Executes a user-defined function with the provided arguments.
    pub fn execute_user_defined_function(
        &mut self,
        function: roan_ast::Fn,
        def_module: &mut Module,
        args: Vec<Value>,
        ctx: &mut Context,
        vm: &mut VM,
        call: &CallExpr,
    ) -> Result<()> {
        debug!("Executing user-defined function: {}", function.name);

        self.enter_scope();

        {
            let exprs = call
                .args
                .iter()
                .map(|arg| arg.span())
                .collect::<Vec<TextSpan>>();

            let mut offset = 0;
            for (i, (param, arg)) in function
                .params
                .iter()
                .zip(args.iter().chain(std::iter::repeat(&Value::Null)))
                .enumerate()
            {
                let ident = param.ident.literal();
                // Maybe we could find a better way to handle this
                if ident == "self" {
                    offset += 1;

                    def_module.declare_variable(ident, arg.clone());
                    continue;
                }

                let expr = exprs.get(i - offset);
                if param.is_rest {
                    let rest: Vec<Value> = args
                        .iter()
                        .skip(function.params.len() - 1)
                        .cloned()
                        .collect();

                    if expr.is_none() {
                        def_module.declare_variable(ident, Value::Vec(rest));
                    } else {
                        if let Some(_type) = param.type_annotation.as_ref() {
                            for arg in &rest {
                                if _type.is_any() {
                                    continue;
                                };

                                arg.check_type(&_type.type_name.literal(), expr.unwrap().clone())?;
                            }
                        }

                        def_module.declare_variable(ident, Value::Vec(rest));
                    }
                } else {
                    if let Some(_type) = param.type_annotation.as_ref() {
                        if arg.is_null() && _type.is_nullable {
                            def_module.declare_variable(ident, Value::Null);
                            continue;
                        }

                        if expr.is_none() {
                            return Err(MissingParameter(ident.clone(), call.span()).into());
                        }

                        if arg.is_null() && !_type.is_nullable {
                            return Err(TypeMismatch(
                                format!("Expected type {} but got null", _type.type_name.literal()),
                                expr.unwrap().clone(),
                            )
                            .into());
                        }

                        if _type.is_array {
                            match arg {
                                Value::Vec(vec) => {
                                    for arg in vec {
                                        if _type.is_any() {
                                            continue;
                                        };

                                        arg.check_type(
                                            &_type.type_name.literal(),
                                            expr.unwrap().clone(),
                                        )?;
                                    }
                                    def_module.declare_variable(ident.clone(), arg.clone());
                                }
                                _ => {
                                    return Err(TypeMismatch(
                                        format!(
                                            "Expected array type {} but got {}",
                                            _type.type_name.literal(),
                                            arg.type_name()
                                        ),
                                        expr.unwrap().clone(),
                                    )
                                    .into());
                                }
                            }
                        } else {
                            if arg.is_null() && !_type.is_nullable && !_type.is_any() {
                                arg.check_type(&_type.type_name.literal(), expr.unwrap().clone())?;
                            }
                            def_module.declare_variable(ident, arg.clone());
                        }
                    } else {
                        def_module.declare_variable(ident, arg.clone());
                    }
                }
            }
        }

        let frame = Frame::new(
            function.name.clone(),
            function.fn_token.span.clone(),
            Frame::path_or_unknown(def_module.path()),
        );
        vm.push_frame(frame);

        for stmt in function.body.stmts {
            def_module.interpret_stmt(stmt, ctx, vm)?;
        }

        vm.pop_frame();
        self.exit_scope();

        Ok(())
    }

    /// Interpret a call expression.
    ///
    /// # Arguments
    /// * `call` - [CallExpr] to interpret.
    /// * `ctx` - The context in which to interpret the call.
    ///
    /// # Returns
    /// The result of the call.
    pub fn interpret_call(
        &mut self,
        call: &CallExpr,
        ctx: &mut Context,
        vm: &mut VM,
    ) -> Result<Value> {
        log::debug!("Interpreting call");

        let args = self.interpret_possible_spread(call.args.clone(), ctx, vm)?;

        let stored_function = self
            .find_function(&call.callee)
            .ok_or_else(|| UndefinedFunctionError(call.callee.clone(), call.token.span.clone()))?
            .clone();

        match stored_function {
            StoredFunction::Native(n) => {
                self.execute_native_function(n, args, vm)?;

                Ok(vm.pop().unwrap())
            }
            StoredFunction::Function {
                function,
                defining_module,
            } => {
                let mut def_module = ctx.query_module(&defining_module).unwrap();

                match self.execute_user_defined_function(
                    function,
                    &mut def_module,
                    args,
                    ctx,
                    vm,
                    call,
                ) {
                    Ok(_) => Ok(vm.pop().unwrap_or(Value::Void)),
                    Err(e) => {
                        print_diagnostic(e, Some(def_module.source.content()));
                        std::process::exit(1);
                    }
                }
            }
        }
    }
}