@@ -725,6 +725,31 @@ object desugar {
725
725
tree
726
726
}
727
727
728
+ /** Translate infix operation expression
729
+ *
730
+ * l op r ==> l.op(r) if op is left-associative
731
+ * ==> r.op(l) if op is right-associative
732
+ */
733
+ def binop (left : Tree , op : Ident , right : Tree )(implicit ctx : Context ): Apply = {
734
+ def assignToNamedArg (arg : Tree ) = arg match {
735
+ case Assign (Ident (name), rhs) => cpy.NamedArg (arg)(name, rhs)
736
+ case _ => arg
737
+ }
738
+ def makeOp (fn : Tree , arg : Tree , selectPos : Position ) = {
739
+ val args : List [Tree ] = arg match {
740
+ case Parens (arg) => assignToNamedArg(arg) :: Nil
741
+ case Tuple (args) => args.mapConserve(assignToNamedArg)
742
+ case _ => arg :: Nil
743
+ }
744
+ Apply (Select (fn, op.name).withPos(selectPos), args)
745
+ }
746
+
747
+ if (isLeftAssoc(op.name))
748
+ makeOp(left, right, Position (left.pos.start, op.pos.end, op.pos.start))
749
+ else
750
+ makeOp(right, left, Position (op.pos.start, right.pos.end))
751
+ }
752
+
728
753
/** Make closure corresponding to function.
729
754
* params => body
730
755
* ==>
@@ -832,30 +857,6 @@ object desugar {
832
857
Block (ldef, call)
833
858
}
834
859
835
- /** Translate infix operation expression left op right
836
- */
837
- def makeBinop (left : Tree , op : Ident , right : Tree ): Tree = {
838
- def assignToNamedArg (arg : Tree ) = arg match {
839
- case Assign (Ident (name), rhs) => cpy.NamedArg (arg)(name, rhs)
840
- case _ => arg
841
- }
842
- if (isLeftAssoc(op.name)) {
843
- val args : List [Tree ] = right match {
844
- case Parens (arg) => assignToNamedArg(arg) :: Nil
845
- case Tuple (args) => args mapConserve assignToNamedArg
846
- case _ => right :: Nil
847
- }
848
- val selectPos = Position (left.pos.start, op.pos.end, op.pos.start)
849
- Apply (Select (left, op.name).withPos(selectPos), args)
850
- } else {
851
- val x = UniqueName .fresh()
852
- val selectPos = Position (op.pos.start, right.pos.end, op.pos.start)
853
- new InfixOpBlock (
854
- ValDef (x, TypeTree (), left).withMods(synthetic),
855
- Apply (Select (right, op.name).withPos(selectPos), Ident (x).withPos(left.pos)))
856
- }
857
- }
858
-
859
860
/** Create tree for for-comprehension `<for (enums) do body>` or
860
861
* `<for (enums) yield body>` where mapName and flatMapName are chosen
861
862
* corresponding to whether this is a for-do or a for-yield.
@@ -1066,10 +1067,10 @@ object desugar {
1066
1067
if (! op.isBackquoted && op.name == tpnme.raw.AMP ) AndTypeTree (l, r) // l & r
1067
1068
else if (! op.isBackquoted && op.name == tpnme.raw.BAR ) OrTypeTree (l, r) // l | r
1068
1069
else AppliedTypeTree (op, l :: r :: Nil ) // op[l, r]
1069
- else if (ctx.mode is Mode .Pattern )
1070
+ else {
1071
+ assert(ctx.mode is Mode .Pattern ) // expressions are handled separately by `binop`
1070
1072
Apply (op, l :: r :: Nil ) // op(l, r)
1071
- else // l.op(r), or val x = r; l.op(x), plus handle named args specially
1072
- makeBinop(l, op, r)
1073
+ }
1073
1074
case PostfixOp (t, op) =>
1074
1075
if ((ctx.mode is Mode .Type ) && ! op.isBackquoted && op.name == tpnme.raw.STAR ) {
1075
1076
val seqType = if (ctx.compilationUnit.isJava) defn.ArrayType else defn.SeqType
0 commit comments