diff --git a/transpiler/src/main/eo/preface/pyint.eo b/transpiler/src/main/eo/preface/pyint.eo index 31f139676..8b74a9d60 100644 --- a/transpiler/src/main/eo/preface/pyint.eo +++ b/transpiler/src/main/eo/preface/pyint.eo @@ -20,17 +20,16 @@ fakeclasses.convert (pyint value) x > p seq > @ if. - (fakeclasses.has-type x (fakeclasses.pyIntClass)) - pybool (value.eq (x.value)) - p.fst.eq (p.snd) + ((fakeclasses.has-type x (fakeclasses.pyIntClass)).not).and + ((fakeclasses.has-type x (fakeclasses.pyFloatClass)).not).and ((fakeclasses.has-type x (fakeclasses.pyComplexClass)).not) + pybool FALSE + if. + (fakeclasses.has-type x (fakeclasses.pyIntClass)) + pybool (value.eq (x.value)) + p.fst.eq (p.snd) [x] > neq - fakeclasses.convert (pyint value) x > p - seq > @ - if. - (fakeclasses.has-type x (fakeclasses.pyIntClass)) - pybool (value.neq (x.value)) - p.fst.neq (p.snd) + (eq x).not > @ [x] > less fakeclasses.convert (pyint value) x > p diff --git a/transpiler/src/main/eo/preface/xmyMap.eo b/transpiler/src/main/eo/preface/xmyMap.eo index 66646671d..fc4f37770 100644 --- a/transpiler/src/main/eo/preface/xmyMap.eo +++ b/transpiler/src/main/eo/preface/xmyMap.eo @@ -7,8 +7,10 @@ +alias sprintf org.eolang.txt.sprintf +alias mkCopy preface.mkCopy +alias pair preface.pair ++alias xmyArray preface.xmyArray [] > xmyMap + [value] > wrapper pyint 16 > x__id__ [initValue] > apply [stackUp] > @ @@ -27,14 +29,26 @@ [stackUp] > @ memory 0 > i seq > @ - stdout (sprintf "xget %s\n" key) (i.lt (self.value.length)).while [unused] seq > @ - stdout (sprintf "%s ?= %s\n" ((self.value.get i).fst) key) if. - (self.value.get i).fst.eq key - stackUp.forward (return ((self.value.get i).snd)) + (self.value.get i).value.fst.eq key + stackUp.forward (return ((self.value.get i).value.snd)) + i.write (i.plus 1) + stdout "get left the loop\n" + 0 + [] > xget-internal + [key] > apply + [stackUp] > @ + memory 0 > i + seq > @ + (i.lt (value.length)).while + [unused] + seq > @ + if. + (value.get i).value.fst.eq key + stackUp.forward (return ((value.get i).value.snd)) i.write (i.plus 1) stdout "get left the loop\n" 0 @@ -47,7 +61,7 @@ [unused] seq > @ if. - (self.value.get i).fst.eq key + (self.value.get i).value.fst.eq key stackUp.forward (return (pybool TRUE)) i.write (i.plus 1) stdout "c1\n" @@ -59,7 +73,7 @@ FALSE [acc item] seq > @ - acc.or ((elt.eq (item.fst)).value) + acc.or ((elt.eq (item.value.fst)).value) seq > @ stdout (sprintf "len = %d\n" (value.length)) pybool result @@ -70,12 +84,10 @@ (goto ((x__contains__.apply self key).@)).result.if stackUp.forward (return 0) 0 - stdout "2\n" mkCopy (self.value) > tmp - mkCopy (key.value) > kv - mkCopy (val.value) > vv - self.value.write (tmp.copy.with (pair (key.with-value (kv.copy)) (val.with-value (vv.copy)))) - stdout "3\n" + mkCopy (key) > kv + mkCopy (val) > vv + self.value.write (tmp.copy.with (wrapper (pair ((kv.copy)) ((vv.copy))))) stackUp.forward (return 0) 123 [] > xappend @@ -89,21 +101,63 @@ self.value.write (*) stackUp.forward (return 0) 123 + [] > xkeys + [self] > apply + [stackUp] > @ + (goto ((xmyArray.apply (*)).@)).result > accum + memory 0 > i + seq > @ + (i.lt (value.length)).while + [unused] + (self.value.get i).value.fst > x + seq > @ + (goto ((accum.xappend.apply accum x).@)).result + i.write (i.plus 1) + 0 + stdout (sprintf "keys len = %d\n" (accum.value.length)) + stackUp.forward (return accum) + 0 + [] > xkeys-internal + [] > apply + [stackUp] > @ + (goto ((xmyArray.apply (*)).@)).result > accum + memory 0 > i + seq > @ + (i.lt (value.length)).while + [unused] + (value.get i).value.fst > x + seq > @ + (goto ((accum.xappend.apply accum x).@)).result + i.write (i.plus 1) + 0 + stdout (sprintf "keys len = %d\n" (accum.value.length)) + stackUp.forward (return accum) + 0 [x] > eq memory TRUE > acc memory 0 > pos + (goto ((xkeys-internal.apply).@)).result > my-keys + (goto ((x.xkeys.apply x).@)).result > his-keys if. > res ((x.x__class__.x__id__.eq x__id__).value.and (value.length.eq (x.value.length))) - seq - (acc.and (pos.lt (value.length))).while - [unused] - seq > @ - acc.write (acc.and (((value.get pos).eq (x.value.get pos)).value)) - pos.write (pos.plus 1) - 123 - pybool acc + if. + my-keys.eq his-keys + seq + (acc.and (pos.lt (my-keys.value.length))).while + [unused] + my-keys.value.get i > key + (goto ((xget-internal.apply key).@)).result > my-value + (goto ((x.xget.apply x key).@)).result > his-value + seq > @ + stdout (sprintf "i = %d\n" pos) + acc.write (acc.and ((my-value.eq his-value).value)) + pos.write (pos.plus 1) + 123 + pybool acc + pybool FALSE pybool FALSE seq > @ + stdout "eq\n" res stdout "after\n" res diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/ExpressionPasses.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/ExpressionPasses.scala index 5e15ff29a..d0a7f2ffb 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/ExpressionPasses.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/ExpressionPasses.scala @@ -14,33 +14,55 @@ import org.polystat.py2eo.transpiler.StatementPasses.{EAfterPass, Names, NamesU, import scala.collection.immutable.HashMap object ExpressionPasses { - def simplifyCollectionComprehension(lhs : Boolean, e : T, ns : NamesU) : (EAfterPass, NamesU) = { + + def simplifyComprehensionList(inner : Statement.T, l : List[Comprehension], ann : GeneralAnnotation) : Statement.T = { + l.foldRight(inner : Statement.T)((comprehension, accum) => { + comprehension match { + case IfComprehension(cond) => + IfSimple(cond, accum, Pass(ann.pos), ann.pos) + case ForComprehension(what, in, isAsync) => + For(what, in, accum, None, isAsync, ann.pos) + } + }) + } + + def simplifyComprehension(lhs : Boolean, e : T, ns : NamesU) : (EAfterPass, NamesU) = { if (!lhs) { e match { case CollectionComprehension(kind, base, l, ann) => { + val inner = Assign(List(CallIndex( + true, + Field(Ident("collectionAccum", ann.pos), "append", ann.pos), + List((None, base)), + ann.pos + )), + ann.pos + ) val st = Suite( - List( - Assign(List(Ident("collectionAccum", ann.pos), CollectionCons(kind, List(), ann.pos)), ann.pos), - l.foldRight( - Assign(List(CallIndex( - true, - Field(Ident("collectionAccum", ann.pos), "append", ann.pos), - List((None, base)), - ann.pos - )), - ann.pos - ): Statement.T - )((comprehension, accum) => { - comprehension match { - case IfComprehension(cond) => - IfSimple(cond, accum, Pass(ann.pos), ann.pos) - case ForComprehension(what, in, isAsync) => - For(what, in, accum, None, isAsync, ann.pos) - } - }) - ), - ann.pos - ) + List( + Assign(List(Ident("collectionAccum", ann.pos), CollectionCons(kind, List(), ann.pos)), ann.pos), + simplifyComprehensionList(inner, l, ann) + ), + ann.pos + ) + (Right((st : Statement.T, Ident("collectionAccum", ann.pos))), ns) + } + case DictComprehension(Left((k, v)), l, ann) => { + val inner = Assign(List(CallIndex( + true, + Field(Ident("collectionAccum", ann.pos), "add", ann.pos), + List((None, k),(None, v)), + ann.pos + )), + ann.pos + ) + val st = Suite( + List( + Assign(List(Ident("collectionAccum", ann.pos), DictCons(List(), ann.pos)), ann.pos), + simplifyComprehensionList(inner, l, ann) + ), + ann.pos + ) (Right((st : Statement.T, Ident("collectionAccum", ann.pos))), ns) } case x : Any => (Left(x), ns) @@ -341,6 +363,12 @@ object ExpressionPasses { val l3 = call2comprehensions(l.zip(l2)) CollectionComprehension(kind, base, l3, ann.pos) }, l1, ns) + case DictComprehension(Left((k, v)), l, ann) => + val l1 = k :: v :: comprehensions2calls(l, ann) + reconstruct(lhs, { case k :: v :: l2 => + val l3 = call2comprehensions(l.zip(l2)) + DictComprehension(Left((k, v)), l3, ann.pos) + }, l1, ns) case GeneratorComprehension(base, l, ann) => val l1 = base :: comprehensions2calls(l, ann) reconstruct(lhs, { case base :: l2 => 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 76419d431..f532434e1 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala @@ -99,6 +99,8 @@ object PrintLinearizedMutableEOWithCage { name :: "[]" :: indent( "newUID.apply 0 > x__id__" :: + "[x] > eq" :: + " x__id__.eq (x.x__id__) > @" :: s"[$consArgs] > apply" :: indent( "[stackUp] > @" :: 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 1d2642da4..a6f254ef2 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala @@ -60,7 +60,7 @@ object Transpile { debugPrinter(simIf._1, "simplifyIf") val simAssList = StatementPasses.procStatement(StatementPasses.simplifyAssignmentList)(simIf._1, simIf._2) debugPrinter(simAssList._1, "simplifyAssList") - val simCompr = StatementPasses.procExprInStatement((ExpressionPasses.simplifyCollectionComprehension))(simAssList._1, simAssList._2) + val simCompr = StatementPasses.procExprInStatement((ExpressionPasses.simplifyComprehension))(simAssList._1, simAssList._2) debugPrinter(simCompr._1, "afterSimplifyCollectionComprehension") val simForAgain = StatementPasses.procStatement(StatementPasses.simplifyFor)(simCompr._1, simCompr._2) debugPrinter(simForAgain._1, "afterSimForAgain") diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/comparisons/comparison-3.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/comparisons/comparison-3.yaml index 4a354c9ba..f9de72c98 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/comparisons/comparison-3.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/comparisons/comparison-3.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def test(): return 1 in {'name': 'John', 1: [2, 4, 3]}.keys() \ No newline at end of file diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/dictionary-displays/dict-display-comprehension.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/dictionary-displays/dict-display-comprehension.yaml index 5bbd08818..07ddd458c 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/dictionary-displays/dict-display-comprehension.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/dictionary-displays/dict-display-comprehension.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def setDisplayComprehension(): l = { x : x * x for x in range(1, 6) if (x % 2) == 1 } diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display-comprehension.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display-comprehension.yaml index 4511cd25e..9af66ba5f 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display-comprehension.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display-comprehension.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def setDisplayComprehension(): l = { x * x for x in range(1, 6) if (x % 2) == 1 } diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display.yaml deleted file mode 100644 index 6885da663..000000000 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/set-displays/set-display.yaml +++ /dev/null @@ -1,5 +0,0 @@ -enabled: False -python: | - def setDisplay(): - l = { 1, 2, 3, 1 } - diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/library/dictionary/dict-4.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/library/dictionary/dict-4.yaml index c85a7e457..89b024d1a 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/library/dictionary/dict-4.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/library/dictionary/dict-4.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def test(): class A: @@ -9,4 +9,4 @@ python: | dict = {'name': 'John', 1: x} - return dict.get(1).__class__ == A \ No newline at end of file + return dict.get(1).__class__ == A