-
Notifications
You must be signed in to change notification settings - Fork 5
/
AddExplicitSelfToMethodCalls.scala
45 lines (43 loc) · 1.61 KB
/
AddExplicitSelfToMethodCalls.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.polystat.py2eo.transpiler
import org.polystat.py2eo.parser.Expression.{CallIndex, Field, Ident, T}
import org.polystat.py2eo.parser.Statement.{Assign, Suite}
import org.polystat.py2eo.transpiler.GenericStatementPasses.{EAfterPass, NamesU}
object AddExplicitSelfToMethodCalls {
// explicitly substitute the self to each method call
// @todo #47: This implementation does not work if a class method is returned as a function and then called
def apply(lhs : Boolean, e : T, ns : NamesU) : (EAfterPass, NamesU) = {
if (!lhs) {
e match {
case CallIndex(true, what@Field(obj@Ident(_, _), fname, fann), args, ann) =>
(Left(CallIndex(true, what, (None, obj) :: args, ann.pos)), ns)
case CallIndex(true, what@Field(obj, fname, fann), args, ann) =>
val (List(objName, idName), ns1) = ns(List("obj", "id"))
(
Right((
Suite(
List(
Assign(List(Ident(objName, ann.pos), obj), ann.pos),
Assign(List(
Ident(idName, ann.pos),
CallIndex(
true,
Field(Ident(objName, ann.pos), fname, fann),
(None, Ident(objName, ann.pos)) :: args,
ann.pos
)
), ann.pos)
),
ann.pos
),
Ident(idName, ann.pos)
)),
ns1
)
case CallIndex(isCall, Field(_, _, _), args, ann) => ???
case x : Any => (Left(x), ns)
}
} else {
(Left(e), ns)
}
}
}