Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created May 28, 2016 19:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jnthn/75321e83b4155b2b99d87ddf8d6f0d18 to your computer and use it in GitHub Desktop.
role Poly { }
class PolyReal does Poly { }
class PolyVar does Poly {
has $.name;
}
class PolyExpr does Poly {
has $.operation;
has $.left;
has $.right;
submethod BUILD(:$left is raw, :$right is raw, :$!operation) {
$!left = extract-var($left);
$!right = extract-var($right);
}
sub extract-var(\maybe-real) {
maybe-real ~~ PolyReal
?? PolyVar.new(name => maybe-real.VAR.name)
!! maybe-real
}
}
sub poly-node(\left, \right, \op) {
PolyExpr.new(left => left, right => right, operation => op)
}
multi infix:<+>(Poly \left, Poly \right) {
poly-node(left, right, '+')
}
multi infix:<+>(Real \left, Poly \right) {
poly-node(left, right, '+')
}
multi infix:<+>(Poly \left, Real \right) {
poly-node(left, right, '+')
}
multi infix:<->(Poly \left, Poly \right) {
poly-node(left, right, '-')
}
multi infix:<->(Real \left, Poly \right) {
poly-node(left, right, '-')
}
multi infix:<->(Poly \left, Real \right) {
poly-node(left, right, '-')
}
multi infix:<*>(Poly \left, Poly \right) {
poly-node(left, right, '*')
}
multi infix:<*>(Real \left, Poly \right) {
poly-node(left, right, '*')
}
multi infix:<*>(Poly \left, Real \right) {
poly-node(left, right, '*')
}
my PolyReal $X;
say $X * ($X + 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment