Skip to main content

rl_lang/checker/scope/
declare.rs

1//! Variable and constant declaration for the type checker scope.
2
3use crate::{
4    checker::structs::{CheckType, ScopeItem, TypeChecker},
5    utils::span::Span,
6};
7
8impl TypeChecker {
9    /// Declares `name` in the current (innermost) scope.
10    ///
11    /// For constants, emits an error if the name is already declared in the
12    /// same scope. On success, pushes a hover entry showing the kind, name,
13    /// and type.
14    pub fn declare(&mut self, name: String, item_type: CheckType, is_const: bool, span: Span) {
15        if let Some(scope) = self.scopes.last_mut() {
16            if is_const && scope.contains_key(&name) {
17                self.errors
18                    .push(self.err(format!("'{}' is already declared", name), span));
19                return;
20            }
21
22            let kind = if is_const { "const" } else { "variable" };
23            let hover_text = format!("```rl\n{} {}: {}\n```", kind, name, item_type.info());
24
25            scope.insert(name, ScopeItem::new(item_type, is_const));
26            self.push_hover(span, hover_text);
27        }
28    }
29}