Skip to main content

luaur_compiler/methods/
compiler_get_constant_number.rs

1use crate::enums::type_constant_folding::Type;
2use crate::records::compile_error::CompileError;
3use crate::records::compiler::Compiler;
4use crate::records::constant::Constant;
5use luaur_ast::records::ast_expr::AstExpr;
6
7impl Compiler {
8    pub fn get_constant_number(&mut self, node: *mut AstExpr) -> i32 {
9        unsafe {
10            let c = self.constants.find(&node);
11
12            if let Some(constant) = c {
13                if (*constant).r#type == Type::Type_Number {
14                    let cid = (*self.bytecode).add_constant_number((*constant).data.value_number);
15                    if cid < 0 {
16                        CompileError::raise(
17                            &(*node).base.location,
18                            core::format_args!(
19                                "Exceeded constant limit; simplify the code to compile"
20                            ),
21                        );
22                    }
23                    return cid;
24                }
25            }
26
27            -1
28        }
29    }
30}