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

Reframe macros as syntax with define-syntax #10

Open
jeremyheiler opened this issue Apr 11, 2021 · 0 comments
Open

Reframe macros as syntax with define-syntax #10

jeremyheiler opened this issue Apr 11, 2021 · 0 comments

Comments

@jeremyheiler
Copy link
Owner

jeremyheiler commented Apr 11, 2021

Currently macros allow for native code to rewrite the stream. These macros can replaced with programs written in Rejoice using a new define-syntax interpreter, defining "syntax" over the atom stream. Syntax requires two pieces of code: a regex-like pattern for matching the stream, and a template for the output stream.

Using lists as an example:

define-syntax [ %any* ] >> ~any &any list`

The pattern [ %any* ] expects the symbol [ followed by zero or more symbols, collected as a group and given the name any, and the symbol ]. The prefix % says the next symbol is a name, and not a literal. The first atom in the pattern must be a symbol, and it is how the syntax is dispatched. There cannot be more than one syntax using the same dispatch symbol. Then, for the output any is the name of the group. The prefix ~ says to ungroup the atoms. The prefix & returns the number of atoms in the group, and list` is a symbol.

Pattern language:

a -> literal value 'a'
%a -> any atom, given the name 'a'
%a+ -> one or more atoms, grouped and named 'a'
%a* -> zero or more atoms, grouped and named 'a'
%a? -> zero or one atoms, named 'a'
%a:sym -> any symbol, named 'a'
%a:int -> any integer, named 'a'
%a:bool -> any boolean, named 'a'
%a:char -> any character, named 'a'

Rewrite language:

a -> literal value 'a'
~a -> if 'a' is a group, splice it into the stream
&a -> if 'a' is a group, the length of the group

Out of scope: Identifying which symbols in a pattern are adjacent.

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

No branches or pull requests

1 participant