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

Transforming boolean or's and and's to if else blocks to preserve lazy evaluation. #47

Merged
merged 1 commit into from
Jan 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ trait JribbleAnalysis {
isPrimitive(sym) && scalaPrimitives.isCoercion(getPrimitive(sym))
}

def isBooleanOr(sym: Symbol): Boolean = {
import scalaPrimitives._
isPrimitive(sym) && (getPrimitive(sym) == ZOR)
}

def isBooleanAnd(sym: Symbol): Boolean = {
import scalaPrimitives._
isPrimitive(sym) && (getPrimitive(sym) == ZAND)
}

def isInterface(sym: Symbol): Boolean = sym.hasFlag(INTERFACE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ with JribbleAnalysis
DefDef(sym, mods, List(params), rhs)

val unitLiteral = Literal(Constant()) setType UnitClass.tpe
val trueLiteral = Literal(Constant(true)) setType BooleanClass.tpe
val falseLiteral = Literal(Constant(false)) setType BooleanClass.tpe
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,25 @@ with JribbleNormalization
case tree@Apply(fun @ Select(receiver, name), args) if isCoercion(fun.symbol) =>
assert(args.isEmpty)
treeGen.mkCast(transform(receiver), tree.tpe)
//boolean or/and are both lazy operators and need to short-circuit when applicable
case tree@Apply(fun @ Select(receiver, name), args) if isBooleanOr(fun.symbol) =>
assert(args.tail.isEmpty)
val (rhsStats, rhsExpr) = removeNonJribbleExpressions(args.head, false)
if (rhsStats.isEmpty) {
val funT = transform(fun)
treeCopy.Apply(tree, funT, List(rhsExpr))
} else {
transform(If(receiver, trueLiteral, args.head) setType tree.tpe setPos tree.pos)
}
case tree@Apply(fun @ Select(receiver, name), args) if isBooleanAnd(fun.symbol) =>
assert(args.tail.isEmpty)
val (rhsStats, rhsExpr) = removeNonJribbleExpressions(args.head, false)
if (rhsStats.isEmpty) {
val funT = transform(fun)
treeCopy.Apply(tree, funT, List(rhsExpr))
} else {
transform(If(receiver, args.head, falseLiteral) setType tree.tpe setPos tree.pos)
}
case Apply(fun, args) =>
val funT :: argsT = transformTrees(fun :: args)
treeCopy.Apply(tree, funT, argsT)
Expand Down
Loading