roan_engine/interpreter/
conditions.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
use crate::{context::Context, module::Module, value::Value, vm::VM};
use roan_ast::{GetSpan, If, ThenElse};
use tracing::debug;

use anyhow::Result;
use roan_error::{error::RoanError::NonBooleanCondition, TextSpan};

impl Module {
    /// Interpret a then-else expression.
    ///
    /// # Arguments
    /// * `then_else` - [ThenElse] expression to interpret.
    /// * `ctx` - The context in which to interpret the then-else expression.
    /// * `vm` - The virtual machine to use.
    ///
    /// # Returns
    /// The result of the then-else expression.
    pub fn interpret_then_else(
        &mut self,
        then_else: ThenElse,
        ctx: &mut Context,
        vm: &mut VM,
    ) -> Result<Value> {
        debug!("Interpreting then-else");

        self.interpret_expr(&then_else.condition, ctx, vm)?;
        let condition = vm.pop().unwrap();

        let b = match condition {
            Value::Bool(b) => b,
            _ => condition.is_truthy(),
        };

        if b {
            self.interpret_expr(&then_else.then_expr, ctx, vm)?;
        } else {
            self.interpret_expr(&then_else.else_expr, ctx, vm)?;
        }

        Ok(vm.pop().expect("Expected value on stack"))
    }

    /// Interpret an if statement.
    ///
    /// # Arguments
    /// * `if_stmt` - [`If`] - The if statement to interpret.
    /// * `ctx` - [`Context`] - The context in which to interpret the statement.
    pub fn interpret_if(&mut self, if_stmt: If, ctx: &mut Context, vm: &mut VM) -> Result<()> {
        debug!("Interpreting if statement");

        self.interpret_expr(&if_stmt.condition, ctx, vm)?;
        let condition_value = vm.pop().expect("Expected value on stack");

        let condition = match condition_value {
            Value::Bool(b) => b,
            Value::Null => false,
            _ => {
                return Err(NonBooleanCondition(
                    "If condition".into(),
                    TextSpan::combine(vec![if_stmt.if_token.span, if_stmt.condition.span()])
                        .unwrap(),
                )
                .into())
            }
        };

        if condition {
            self.execute_block(if_stmt.then_block, ctx, vm)?;
        } else {
            let mut executed = false;
            for else_if in if_stmt.else_ifs {
                self.interpret_expr(&else_if.condition, ctx, vm)?;
                let else_if_condition = vm.pop().expect("Expected value on stack");

                let else_if_result = match else_if_condition {
                    Value::Bool(b) => b,
                    Value::Null => false,
                    _ => {
                        return Err(NonBooleanCondition(
                            "Else if condition".into(),
                            else_if.condition.span(),
                        )
                        .into())
                    }
                };

                if else_if_result {
                    self.execute_block(else_if.block, ctx, vm)?;
                    executed = true;
                    break;
                }
            }

            if !executed {
                if let Some(else_block) = if_stmt.else_block {
                    self.execute_block(else_block.block, ctx, vm)?;
                }
            }
        }

        Ok(())
    }
}