melt-umn / silver

An attribute grammar-based programming language for composable language extensions
http://melt.cs.umn.edu/silver/
GNU Lesser General Public License v3.0
57 stars 7 forks source link

Implement forward production attributes #761

Closed krame505 closed 1 year ago

krame505 commented 1 year ago

Changes

This adds support for forward production attributes, as proposed previously. For example

production listComp
top::Expr ::= n::String l::Expr res::Expr cond::Expr
{
  local localErrors::[Message] =
     l.errors ++ res.errors ++ cond.errors ++
     case l.typerep of
     | listType(_) -> []
     | _ -> [err(l, "Input expected a list")]
     end ++
     case cond.typerep of
     | boolType() -> []
     | _ -> [err(cond, "Condition expected a bool")]
     end;

  forward fwrd =
    stmtExpr(
      seqStmt(
        varDecl("result", emptyList(res.typerep)),
        forEach(n, @l, ifStmt(@cond, appendList("result", @res)))),
      var("result"));

  forwards to if null(localErrors) then @fwrd else errorExpr(localErrors);
}

Here we want to decorate the children through forwarding, but conditionally forward to an error production if there are local errors. Instead we can decorate the forward tree under a local production attribute. Forward production attributes avoid the need to write copy equations for all the inherited attributes on fwrd by decorating the prod attr with the current node as its forward parent.

(I thought there was a github issue for this opened, but maybe not?)

Documentation

The above example and some explanation are included in this writeup, and we may want to mention forward prod attrs in the forthcoming paper on forwarding. This should eventually all get written up for the website docs, too.