erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.63k stars 54 forks source link

Call macro or method for logging when exit a current method #350

Closed GreasySlug closed 1 year ago

GreasySlug commented 1 year ago

When debug_call_info!(self) is called, self.level -= 1 or self.stack_dec() must be called in methods before return the result.

However, there was no consistency in where it was placed or which was used.

In this PR, I made sure to place it just before Err(()) and Ok(expr).

Method call errors now use self.stack_dec() and just before return use self.level -= 1 for consistency.

@mtshiba

GreasySlug commented 1 year ago

Method call errors now use self.stack_dec() and just before return use self.level -= 1 for consistency.

I decided on my own to do it this way, but I think it would be even more consistent to change all self.level -= 1 to self.stack_dec()

What do you think about this?

mtshiba commented 1 year ago

stack_dec was originally defined to be something like .map_err(|_| self.stack_dec())?.

However, it is better to unify all self.level -=1 into self.stack_dec(), since we can, for example, have this method display the data when debugging.

pub(crate) fn stack_dec(&mut self) {
    self.level -= 1;
}
// ↓
pub(crate) fn stack_dec(&mut self) {
    self.level -= 1;
    log!(
        c GREEN,
        "\n{} ({}) exit {}, cur: {}",
        " ".repeat($self.level),
        $self.level,
        fn_name!(),
        $self.peek().unwrap()
    );
}
GreasySlug commented 1 year ago

I like that idea. That makes sense.