fn declare_globals(&mut self, ast: &AST) -> Result<(), SyntaxError> {
        // add all global declarations to the module with a value of nil.
        for (name, _) in ast.scope.locals.iter() {
            self.module.borrow_mut().add_symbol(name.clone());
        }

        // next, go through and compile all global functions and classes.
        for node in &ast.items {
            match node {
                Stmt::FunDeclaration(fun, _) => {
                    // get the function's location in the module's variables array.
                    let index = self.module.borrow_mut().get_index(&fun.id.name).unwrap();
                    // compile the functions body.
                    self.function(fun)?;
                    // set the location in the module's variable array to the function's body.
                    self.define_global(index as u32);
                }
                Stmt::ClassDeclaration(class, _) => {
                    // get the class's location in the module's variables array.
                    let index = self.module.borrow_mut().get_index(&class.id.name).unwrap();
                    // compile the class body.
                    self.class(class)?;
                    // define the class in the global scope.
                    self.define_global(index as u32);
                }
                _ => continue,
            }
        }

        Ok(())
    }
/// Sort of foward declare all globally scoped functions.
Sort of foward declare all globally scoped functions.
Sort of foward declare all globally scoped functions .
fn declare_globals(&mut self, ast: &AST) -> Result<(), SyntaxError> {
        // add all global declarations to the module with a value of nil.
        for (name, _) in ast.scope.locals.iter() {
            self.module.borrow_mut().add_symbol(name.clone());
        }
        // next, go through and compile all global functions and classes.
        for node in &ast.items {
            match node {
                Stmt::FunDeclaration(fun, _) => {
                    // get the function's location in the module's variables array.
                    let index = self.module.borrow_mut().get_index(&fun.id.name).unwrap();
                    // compile the functions body.
                    self.function(fun)?;
                    // set the location in the module's variable array to the function's body.
                    self.define_global(index as u32);
                }
                Stmt::ClassDeclaration(class, _) => {
                    // get the class's location in the module's variables array.
                    let index = self.module.borrow_mut().get_index(&class.id.name).unwrap();
                    // compile the class body.
                    self.class(class)?;
                    // define the class in the global scope.
                    self.define_global(index as u32);
                }
                _ => continue,
            }
        }
        Ok(())
    }
fn declare_globals ( & mut self , ast : & AST ) -> Result < ( ) , SyntaxError > { // add all global declarations to the module with a value of nil. for ( name , _ ) in ast . scope . locals . iter ( ) { self . module . borrow_mut ( ) . add_symbol ( name . clone ( ) ) ; } // next, go through and compile all global functions and classes. for node in & ast . items { match node { Stmt :: FunDeclaration ( fun , _ ) => { // get the function's location in the module's variables array. let index = self . module . borrow_mut ( ) . get_index ( & fun . id . name ) . unwrap ( ) ; // compile the functions body. self . function ( fun ) ? ; // set the location in the module's variable array to the function's body. self . define_global ( index as u32 ) ; } Stmt :: ClassDeclaration ( class , _ ) => { // get the class's location in the module's variables array. let index = self . module . borrow_mut ( ) . get_index ( & class . id . name ) . unwrap ( ) ; // compile the class body. self . class ( class ) ? ; // define the class in the global scope. self . define_global ( index as u32 ) ; } _ => continue , } } Ok ( ( ) ) }
Sort of foward declare all globally scoped functions.
Sort of foward declare all globally scoped functions .
&mut self
ast
&AST