Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Scope Area and Borrow Checking in Function Definitions and Calls #13

Merged
merged 2 commits into from
Jun 15, 2023

Conversation

notJoon
Copy link
Owner

@notJoon notJoon commented Jun 15, 2023

  1. Function Parameters and Scopes: The previous implementation treated function parameters as uninitialized variables, leading to DeclaredWithoutInitialValue errors. But, for now, I changed check_function_def method to treat function params as initialized variables.
if let Some(args) = args {
   for (arg, is_borrowed) in args {
        self.insert_borrow(arg, BorrowState::Initialized);    // <-

            if *is_borrowed {
               // ...
            }
      }
}
  1. Add new scope for each function call and definition :
fn allocate_scope<F, T>(&mut self, action: F) -> T
    where
        F: FnOnce(&mut Self) -> T,
    {
        self.borrows.push(HashMap::new());
        let result = action(self);
        self.borrows.pop();

        result
    }

    fn check_statement(&mut self, stmt: &'a Statement) -> BorrowResult {
        match stmt {
            Statement::VariableDecl {
                name,
                value,
                is_borrowed,
            } => self.check_variable_decl(name, value, *is_borrowed),
            Statement::FunctionDef { name, args, body } => {
                self.allocate_scope(|s| s.check_function_def(name, args, body))
            }
            Statement::Scope(stmts) => self.allocate_scope(|s| s.check(stmts)),
            Statement::Return(expr) => self.check_return(expr),
            Statement::Expr(expr) => self.check_expression(expr),
        }
    }
  1. Add more tests

This modifies the check_function_def method to treat function parameters as initialized variables. Additionally, it introduces a new scope for each function call and each function definition, preventing variables from being declared more than once in the same scope.

@notJoon notJoon added bug Something isn't working enhancement New feature or request labels Jun 15, 2023
@notJoon notJoon merged commit dcae43c into main Jun 15, 2023
1 check passed
@notJoon notJoon deleted the add-function-scope branch June 15, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant