Skip to content

Instantly share code, notes, and snippets.

@jonifreeman
Created September 7, 2012 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonifreeman/3667222 to your computer and use it in GitHub Desktop.
Save jonifreeman/3667222 to your computer and use it in GitHub Desktop.
Kiama rewrite
scala> import org.kiama.rewriting.Rewriter._
import org.kiama.rewriting.Rewriter._
scala> trait Expr
defined trait Expr
scala> case class Constant(x: Int) extends Expr
defined class Constant
scala> case class Add(e1: Expr, e2: Expr) extends Expr
defined class Add
scala> val e = Add(Constant(1), Constant(2))
e: Add = Add(Constant(1),Constant(2))
scala> val rewritePlus5 = everywheretd (rule { case Constant(x) => Constant(x+5) })
rewritePlus5: org.kiama.rewriting.Rewriter.Strategy = <function1>
scala> rewrite (rewritePlus5) (e)
res0: Add = Add(Constant(6),Constant(7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment