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

Simplify error checking #654

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jpellegrini
Copy link
Contributor

@egallesio this is mostly a matter of style. See if you agree...

In the compiler, instead of

  (if some-condition
      ( X ...
          ...
          ... )
      (compiler-error Y))

we do

  (unless some-condition (compiler-error Y))
  ( X ...
      ...
      ... )

Or when, when it makes sense

This makes error checking isolated in the place where it programatically happens, and also reduces source code size a little bit, making it clearer.

There are already uses of the macros when and unless in the compiler, so we're not introducing a dependency on them.

In the compiler, instead of

```
  (if some-condition
      ( X ...
          ...
          ... )
      (compiler-error Y))
```

we do

```
  (unless some-condition (compiler-error Y))
  ( X ...
      ...
      ... )
```

Or `when`, when it makes sense

This makes error checking isolated in the place where it
programatically happens, and also reduces source code size a little
bit, makeing it clearer.

There are already uses of the macros `when` and `unless` in the
compiler, so we're not introducing a dependency on them.
@jpellegrini
Copy link
Contributor Author

I know that there are other places in STklos source that use the if style for error checking; I only changed the compiler, but I could in the future do the same in other files, gradually -- if you believe this is a good change.

@egallesio
Copy link
Owner

Hi @jpellegrini,

The form you propose is not equivalent to the original writing. In fact compiler-error calls error in interactive mode, bit it doesn't when we compile a file (it just prints an error message).
For instance, in

(define (compile-quote expr env tail?)
  (unless (= (length expr) 2) (compiler-error 'quote expr "bad usage in ~S" expr))
  (compile-constant (cadr expr) env tail?))

If you call compile-quote with (quote), you'll access the cadr of a list of length 1, which will abort the compilation of the rest of the file.

@jpellegrini
Copy link
Contributor Author

Oh, I saw that the new form wasn't equivalent, but I thought it wouldn't be relevant, because it passed all tests. Maybe we need more tests then! 😁

@egallesio
Copy link
Owner

Oh, I saw that the new form wasn't equivalent, but I thought it wouldn't be relevant, because it passed all tests. Maybe we need more tests then! 😁

The problem here is that the tests (in make test) are not compiled, but just loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants