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

low level macros #34

Merged
merged 12 commits into from
Oct 30, 2021
Merged

low level macros #34

merged 12 commits into from
Oct 30, 2021

Conversation

certainty
Copy link
Owner

@certainty certainty commented Sep 25, 2021

This adds a lowlevel; macro facility ala defmacro. It introduces procedural macros and also lays the foundation for other procedural macros like explicit renaming, which we want to converge to evntually.

The following code demonstrates that.

(define my-cons
  (lowlevel-macro-transformer 
    (lambda (form) 
       (let ((args (cdr form)))
         `(cons ,(car args) ,(cadr args)))))

(my-cons 1 2)

Limitations

  • macros don’t play well with the REPL. The Macro definitions are lost after the evaluation. One idea would be to preserve the syntax environment after each parse.
  • source locations don’t play well with the expansion procedures. The expander converts the Datum to a value and back. The source location is stripped off. One idea here is to have syntax objects as values instead of lists.

new_ls.extend(self.expand_all(operands)?);

Ok(Datum::list(new_ls.into_iter(), loc))
}

fn expand_macro(&mut self, datum: &Datum, transformer: &syntax::Transformer) -> Result<Datum> {
match transformer {
syntax::Transformer::LowLevel(expander) => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both er- and lowlevel macros use the same mechanism to expand the form only with different arguments. Should this be consolidated? ER-macros might change more in the future when we track diverged bindings?

@@ -46,7 +48,7 @@ impl Expander {
exprs,
datum.source_location().clone()
);
let expanded_lambda = self.expand_macros(&lambda)?;
let expanded_lambda = self.expand_macros(&lambda)?.expect("expected lambda body to expand correctly");
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the parse succeeds without producing an expression, this indicates a compiler bug. Surface that consistently instead of using expect

let mut registry = Registry::new();
let source = registry
.add(&mut BufferSource::new(inp, "datum-parser-test"))
.unwrap();
let reader = Reader::new();
let datum = reader.parse(&source).unwrap();

datum.to_vec()[0].clone()
datum.to_vec().clone()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to clone. Instead add a way to turn the parse result into the vector and move it out

Remove lowlevel-macro-transformer.
This is the same as an er-macro-transformer that ignores it's other two
arguments
@certainty certainty changed the title Low level macros Explicit renaming macros Oct 13, 2021
We will implement explicit renaming macros later, possibly only after
we have introduced libraries.
@certainty certainty changed the title Explicit renaming macros low level macros Oct 30, 2021
@certainty certainty merged commit eb90b6f into main Oct 30, 2021
@certainty certainty deleted the low-level-macros branch October 30, 2021 14:48
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.

1 participant