TiarkRompf / virtualization-lms-core

A Framework for Runtime Code Generation and Compiled DSLs
http://scala-lms.github.com
BSD 3-Clause "New" or "Revised" License
324 stars 91 forks source link

LMS not working when use scala.tools.reflect.ToolBox #113

Closed cchavez closed 7 years ago

cchavez commented 8 years ago

Hi all.

I have created a DSL over Symbol type for >, >=, <, <=, ==, !=, &&, || and ! The DSL I defined was using as sample the dslapi sample from https://github.com/scala-lms/tutorials.git file src/test/scala/lms/tutorial/dslapi.scala.

When I ran the DSL using Scala Scripting Engine org.apache.clerezza.scala:script-engine:1.0.0, it works as expected, but when I use scala.tools.reflect.ToolBox it does not work for ==, !=, &&, || and !.

In the case of &&, ||and !, I was able to rewrite the way is defined in the sample, the code that is in the file virtualization-lms-core/src/common/BooleanOps.scala does not work, in order to make it work I did the following:

`` trait SegmentBooleanOps extends BooleanOps {

implicit def booleanToBooleanOpsCls(b: Boolean) = new BooleanOpsCls(unit(b)) implicit def repBooleanToBooleanOpsCls(repB: Rep[Boolean]) = new BooleanOpsCls(repB)

class BooleanOpsCls(lhs: Rep[Boolean]) { def unary_!()(implicit pos: SourceContext): Rep[Boolean] = infixunary!(lhs) def &&(rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] = infix&&(lhs, rhs) def ||(rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] = infix||(lhs, rhs) }

override def infixunary!(lhs: Rep[Boolean])(implicit pos: SourceContext) = segment_booleannegate(lhs) override def infix&&(lhs: Rep[Boolean], rhs: =>Rep[Boolean])(implicit pos: SourceContext) = segment_booleanand(lhs, rhs) override def infix||(lhs: Rep[Boolean], rhs: =>Rep[Boolean])(implicit pos: SourceContext) = segment_boolean_or(lhs, rhs)

def segment_boolean_negate(lhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] def segment_boolean_and(lhs: Rep[Boolean], rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] def segment_boolean_or(lhs: Rep[Boolean], rhs: Rep[Boolean])(implicit pos: SourceContext): Rep[Boolean] } `` I adeed the Class BooleanOpsCls and the implicit, in the way it works using scala.tools.reflect.ToolBox.

For example 'A > 100.0 && 'B > 100.0 was delayed until the generation as expected.

In the case of == and != the code in virtualization-lms-core/src/common/Equal.scala does not work. The following sentence 'A == 100.00, it gets evaluated instead to wait until the generation.

I Added the following:

``trait SegmentEqual extends Base with Variables with SegmentOps { // Rep is an object, so it has an == and != method defined by default, // so there is no type error to force the implicit conversions. def __equal(lhs: Symbol, rhs: Any)(implicit pos: SourceContext) : Rep[Boolean] = segmentequality(unit(lhs), unit(rhs), EQ) def infix!=(lhs: Symbol, rhs: Any)(implicit pos: SourceContext) : Rep[Boolean] = segment_equality(unit(lhs), unit(rhs), NEQ)

def segment_equality(lhs: Rep[Symbol], rhs: Rep[Any], op: EqualityOps)(implicit pos: SourceContext) : Rep[Boolean] } ``

Pretty much the same as in the file virtualization-lms-core/src/common/Equal.scala, but in this case 'A == 100.00, it got executed instead to delay in the generation.

As I mention before when I run using org.apache.clerezza.scala:script-engine:1.0.0 it does works. But with scala.tools.reflect.ToolBox it does not work.

I'm using LMS from url "https://oss.sonatype.org/content/repositories/snapshots/" I could not find any official release with the most recent code.