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

Nanopass in Silver #868

Open
krame505 opened this issue Dec 20, 2024 · 0 comments
Open

Nanopass in Silver #868

krame505 opened this issue Dec 20, 2024 · 0 comments
Assignees
Milestone

Comments

@krame505
Copy link
Member

krame505 commented Dec 20, 2024

We should add support for writing specifications in the nanopass style in Silver. This is also motivated for being able to explore extensions that use closed nonterminals and translation attributes instead of forwarding.

My proposal is that we just represent different languages in the nanopass form as grammars in Silver, with nonterminal/production/equation declarations copied from one language to the next. This requires two main features - a way to include a grammar in another grammar with some modifications, and a way to define and propagate the equations for translation passes.

For example, one may define the host language specification using regular nonterminals in grammar my:lang:host. If using the translation pass model for extensions, one can define their "pre-host" extended syntax language:

grammar my:lang:ext;

imports my:lang:host as h;

include my:lang:host {
  close nonterminals Expr, Stmt, Type;
  exclude productions intAdd, floatAdd, ...;
}

translation pass toHost from my:lang:ext to my:lang:host;
propagate toHost on Expr, Stmt, Type excluding someExtProd;

production addOp
top::Expr ::= e1::Expr e2::Expr
{ top.toHost = e1.typerep.addProd(@e1, @e2); }

dispatch AddOp = Expr ::= h:Expr h:Expr;
synthesized attribute addProd::AddOp occurs on Type;
...

Here the include declaration includes all the nonterminals, productions, attributes, occurrences and equations from my:lang:host, aside from those explicitly filtered out. One can specify close nonterminals or open nonterminals to change the closedness of included nonterminals. Excluding an attribute filters out its occurrences and equations.

The translation pass declaration defines the toHost translation attribute. This is also a new sort of automatic attribute that can be propagated. A translation pass attribute automatically occurs on all nonterminals in the source language that also appear in the target language. Translation pass attributes are automatically excluded from the include specifications.

One can also change attributes into annotations, for example a pass that does desugaring of for loops into while loops after the host language:

grammar my:lang:l1;

imports my:lang:host as h;

include my:lang:host {
  exclude productions forLoop;
  exclude attributes defs, errors;
  annotate attributes env, typerep;
}

translation pass toL1 from my:lang:host to my:lang:l1;
propagate toL1 on Expr, Stmt, Type excluding h:forLoop;

aspect production h:forLoop
top::h:Stmt ::= i::h:Name l::h:Expr u::h:Expr b::h:Stmt 
{
  top.tol1 = seq(varDecl(@i.toL1, @l.tol1, env=top.env.toL1), whileLoop(...)), env=top.env.toL1;
}

Here the propagate for toL1 will supply any annotations with the same name as the attributes occuring on the host nonterminals.

@github-project-automation github-project-automation bot moved this to To do - uncategorized in Hackathon Jan 3, 2025
@krame505 krame505 moved this from To do - uncategorized to To do - larger projects in Hackathon Jan 3, 2025
@krame505 krame505 moved this from To do - larger projects to To do - uncategorized in Hackathon Jan 3, 2025
@remexre remexre self-assigned this Jan 6, 2025
@krame505 krame505 added this to the Silver 0.6.1 milestone Jan 22, 2025
@remexre remexre moved this from To do to In progress in Hackathon Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

No branches or pull requests

2 participants