diff --git a/transpiler/src/main/eo/preface/xfakeclasses.eo b/transpiler/src/main/eo/preface/xfakeclasses.eo index 28dfcf973..d7f4c8490 100644 --- a/transpiler/src/main/eo/preface/xfakeclasses.eo +++ b/transpiler/src/main/eo/preface/xfakeclasses.eo @@ -21,6 +21,7 @@ fakeclass 10 > pyListClass fakeclass 13 > pyStringClass fakeclass 17 > xpyTypeClass + fakeclass 18 > pyTupleClass [typ1 typ2] > gt seq > @ diff --git a/transpiler/src/main/eo/preface/xmyArray.eo b/transpiler/src/main/eo/preface/xmyArray.eo index 345164d45..1ea6299ed 100644 --- a/transpiler/src/main/eo/preface/xmyArray.eo +++ b/transpiler/src/main/eo/preface/xmyArray.eo @@ -12,12 +12,12 @@ +alias xAssertionError preface.xAssertionError [] > xmyArray - [initValue] > ap + [is-list initValue] > ap [stackUp] > @ cage result > pResult [] > result cage initValue > value - xfakeclasses.pyListClass > x__class__ + is-list.if (xfakeclasses.pyListClass) (xfakeclasses.pyTupleClass) > x__class__ [] > to-my-array pResult > @ [x] > with-value @@ -77,7 +77,7 @@ memory TRUE > acc memory 0 > pos if. > res - ((x.x__class__.eq (xfakeclasses.pyListClass)).value.and (value.length.eq (x.value.length))) + ((x.x__class__.eq (is-list.if (xfakeclasses.pyListClass) (xfakeclasses.pyTupleClass))).value.and (value.length.eq (x.value.length))) seq (acc.and (pos.lt (value.length))).while [unused] diff --git a/transpiler/src/main/eo/preface/xtuple.eo b/transpiler/src/main/eo/preface/xtuple.eo new file mode 100644 index 000000000..a932691d8 --- /dev/null +++ b/transpiler/src/main/eo/preface/xtuple.eo @@ -0,0 +1,6 @@ ++package preface ++alias xfakeclasses preface.xfakeclasses + +[] > xtuple + xfakeclasses.pyTupleClass > @ + diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/AddExplicitConstructionOfCollection.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/AddExplicitConstructionOfCollection.scala index 744d9546f..51fecd277 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/AddExplicitConstructionOfCollection.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/AddExplicitConstructionOfCollection.scala @@ -1,13 +1,18 @@ package org.polystat.py2eo.transpiler -import org.polystat.py2eo.parser.Expression.{CallIndex, CollectionCons, CollectionKind, DictCons, Ident, T} +import org.polystat.py2eo.parser.Expression.{BoolLiteral, CallIndex, CollectionCons, CollectionKind, DictCons, Ident, T} object AddExplicitConstructionOfCollection { def apply(e : T) : T = { e match { case CollectionCons(kind, l, ann) if kind == CollectionKind.List || kind == CollectionKind.Tuple => - CallIndex(true, Ident("xmyArray", e.ann.pos), List((None, e)), e.ann.pos) + CallIndex( + true, + Ident("xmyArray", e.ann.pos), + List((None, BoolLiteral(kind == CollectionKind.List, e.ann.pos)), (None, e)), + e.ann.pos + ) case DictCons(_, _) | CollectionCons(CollectionKind.Set, _, _) => CallIndex(true, Ident("xmyMap", e.ann.pos), List((None, e)), e.ann.pos) case _ => e diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala index c0441d6c2..45ae9f305 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala @@ -43,6 +43,7 @@ object PrintLinearizedMutableEOWithCage { "+alias xstr preface.xstr", "+alias xsum preface.xsum", "+alias xlist preface.xlist", + "+alias xtuple preface.xtuple", "+alias xint preface.xint", "+alias xfloat preface.xfloat", "+alias xiter preface.xiter", @@ -369,6 +370,7 @@ object PrintLinearizedMutableEOWithCage { "xstr > dummy-xstr", "xsum > dummy-xsum", "xlist > dummy-xlist", + "xtuple > dummy-xtuple", "xint > dummy-xint", "xfloat > dummy-xfloat", "xStopIteration > dummy-stop-iteration", diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/SimplifyAssignmentToCollectionCons.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/SimplifyAssignmentToCollectionCons.scala new file mode 100644 index 000000000..3b0cd1ae5 --- /dev/null +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/SimplifyAssignmentToCollectionCons.scala @@ -0,0 +1,26 @@ +package org.polystat.py2eo.transpiler + +import org.polystat.py2eo.parser.Expression.{CallIndex, CollectionCons, Field, Ident} +import org.polystat.py2eo.parser.Statement +import org.polystat.py2eo.parser.Statement.{Assign, Suite} +import org.polystat.py2eo.transpiler.GenericStatementPasses.NamesU + +object SimplifyAssignmentToCollectionCons { + def apply(s : Statement.T, ns : NamesU) : (Statement.T, NamesU) = s match { + case Assign(List(CollectionCons(_, lhs, _), rhs), ann) => + val (iter, ns1) = ns("it") + val iterID = Ident(iter, ann.pos) + val extractIter = Assign(List( + iterID, + CallIndex(true, Field(rhs, "__iter__", ann.pos), List(), ann.pos) + ), ann.pos) + val assignments = lhs.map(lhs => + Assign(List(lhs, CallIndex(true, Field(iterID, "__next__", ann.pos), List(), ann.pos)), ann.pos) + ) + ( + Suite((extractIter :: assignments), ann.pos), + ns1 + ) + case _ => (s, ns) + } +} diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala index 731f82a6a..07c0093d9 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala @@ -68,7 +68,9 @@ object Transpile { debugPrinter(simAssList._1, "simplifyAssList") val simAss2Index = GenericStatementPasses.procStatement(SimplifyAssigmentToIndex.simplify)(simAssList._1, simAssList._2) debugPrinter(simAss2Index._1, "simplifyAss2Index") - val simCompr = GenericStatementPasses.procExprInStatement((SimplifyComprehension.apply))(simAss2Index._1, simAss2Index._2) + val simAss2Coll = GenericStatementPasses.procStatement(SimplifyAssignmentToCollectionCons.apply)(simAss2Index._1, simAss2Index._2) + debugPrinter(simAss2Coll._1, "simplifyAss2Collection") + val simCompr = GenericStatementPasses.procExprInStatement((SimplifyComprehension.apply))(simAss2Coll._1, simAss2Coll._2) debugPrinter(simCompr._1, "afterSimplifyCollectionComprehension") val rmImport = SubstituteExternalIdent(simCompr._1, simCompr._2) val simForAgain = GenericStatementPasses.procStatement(SimplifyFor.apply)(rmImport._1, rmImport._2) diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/annotated-assignment/annotated-3.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/annotated-assignment/annotated-3.yaml index f513beecb..c04557eec 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/annotated-assignment/annotated-3.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/annotated-assignment/annotated-3.yaml @@ -1,8 +1,7 @@ -enabled: False +enabled: True python: | def test(): a: int;b: int;c: int a,b,c = range(3) - print(a,b,c) #As output 0 1 2 return len((a, b, c)) == 3 and (a, b, c).__class__ is tuple