Skip to content

Commit 14ca009

Browse files
authored
Merge pull request #396 from polystat/fixDjangoAndTodo
support assign-6, assign-7
2 parents 21b079c + 9efefc5 commit 14ca009

File tree

10 files changed

+94
-78
lines changed

10 files changed

+94
-78
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
+package preface
2+
+alias xfakeclasses preface.xfakeclasses
3+
4+
[from to step] > pyslice
5+
xfakeclasses.pySliceClass > x__class__
6+
0 > force

transpiler/src/main/eo/preface/xfakeclasses.eo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
fakeclass 13 > pyStringClass
2323
fakeclass 17 > xpyTypeClass
2424
fakeclass 18 > pyTupleClass
25+
fakeclass 19 > pySliceClass
2526

2627
[typ1 typ2] > gt
2728
seq > @

transpiler/src/main/eo/preface/xmyArray.eo

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
+alias xfakeclasses preface.xfakeclasses
1010
+alias stdout org.eolang.io.stdout
1111
+alias sprintf org.eolang.txt.sprintf
12-
+alias xAssertionError preface.xAssertionError
1312

1413
[] > xmyArray
1514
[is-list initValue] > ap
1615
[stackUp] > @
1716
cage result > pResult
1817
[] > result
18+
0 > @
19+
0 > force
1920
cage initValue > value
2021
is-list.if (xfakeclasses.pyListClass) (xfakeclasses.pyTupleClass) > x__class__
2122
[] > to-my-array
@@ -134,10 +135,57 @@
134135
[] > xsetAtIndex
135136
[self key val] > ap
136137
[stackUp] > @
138+
(goto ((xmyArray.ap is-list *).@)).result > acc
139+
memory 0 > i
140+
val.iter 0 > it
137141
seq > @
138-
stdout "xsetAtIndex not supported for array\n"
139-
stackUp.forward (goto (xAssertionError.ap.@))
140-
0
142+
if.
143+
(key.x__class__.eq (xfakeclasses.pySliceClass))
144+
seq
145+
while.
146+
(i.lt (key.from))
147+
[unused]
148+
seq > @
149+
stdout (sprintf "i = %d\n" i)
150+
(goto ((acc.xappend.ap acc (value.get i)).@)).result
151+
i.write (i.plus 1)
152+
0
153+
stdout (sprintf "from = %d, to = %d\n" (key.from) (key.to))
154+
goto
155+
[exitLoop]
156+
while. > @
157+
(TRUE)
158+
[unused]
159+
(goto ((it.x__next__.ap it).@)) > to-append
160+
if. > @
161+
(to-append.x__class__.x__id__.eq (pyint 3))
162+
seq
163+
(goto ((acc.xappend.ap acc (to-append.result)).@)).result
164+
0
165+
exitLoop.forward 0
166+
stdout "after 2nd while\n"
167+
while.
168+
(i.lt (value.length))
169+
[unused]
170+
seq > @
171+
stdout (sprintf "i = %d\n" i)
172+
(goto ((acc.xappend.ap acc (value.get i)).@)).result
173+
i.write (i.plus 1)
174+
0
175+
value.write (acc.value)
176+
stackUp.forward (return 0)
177+
0
178+
seq
179+
while.
180+
(i.lt (value.length))
181+
[unused]
182+
seq > @
183+
(goto ((acc.xappend.ap acc ((key.value.eq i).if val (value.get i))).@)).result
184+
i.write (i.plus 1)
185+
0
186+
value.write (acc.value)
187+
stackUp.forward (return 0)
188+
0
141189
[] > x__iter__
142190
[self] > ap
143191
[stackUp] > @

transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintEO.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.polystat.py2eo.transpiler
33
import org.polystat.py2eo.parser.Expression.{
44
Binop, Binops, BoolLiteral, CallIndex, CollectionCons, CollectionKind, Compops, Cond,
55
DictCons, Field, FloatLiteral, FreakingComparison, IntLiteral, LazyLAnd,
6-
LazyLOr, NoneLiteral, Parameter, SimpleComparison, StringLiteral, T, Unop, Unops,
6+
LazyLOr, NoneLiteral, Parameter, SimpleComparison, Slice, StringLiteral, T, Unop, Unops,
77
UnsupportedExpr
88
}
99
import org.polystat.py2eo.parser.{AugOps, Expression, GeneralAnnotation, Statement, VarScope}
@@ -81,6 +81,9 @@ object PrintEO {
8181
case Left((k, v)) => s" (pair ${e(k)} ${e(v)})"
8282
}.mkString("")
8383
(s"((*${elts}))")
84+
case Slice(Some(from), Some(to), by0, ann) =>
85+
val by = by0.getOrElse(IntLiteral(1, ann.pos))
86+
s"(pyslice ${e(from)} ${e(to)} ${e(by)})"
8487
case NoneLiteral(_) => "(pystring \"None: is there a None literal in the EO language?\")"
8588
case IntLiteral(value, _) => s"(pyint $value)"
8689
case FloatLiteral(value, _) => s"(pyfloat $value)"

transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ object PrintLinearizedMutableEOWithCage {
2121
"+alias stdout org.eolang.io.stdout",
2222
"+alias sprintf org.eolang.txt.sprintf",
2323
"+alias cage org.eolang.cage",
24+
"+alias pyslice preface.pyslice",
2425
"+alias pyint preface.pyint",
2526
"+alias pair preface.pair",
2627
"+alias pyfloat preface.pyfloat",
@@ -163,9 +164,9 @@ object PrintLinearizedMutableEOWithCage {
163164
case Assign(List(lhs, rhs), _) if seqOfFields(lhs).isDefined =>
164165
val collectionCons = rhs match {
165166
case _ : Await | _ : Star | _ : DoubleStar |
166-
_ : CollectionComprehension | _ : DictComprehension | _ : GeneratorComprehension | _ : Slice =>
167+
_ : CollectionComprehension | _ : DictComprehension | _ : GeneratorComprehension =>
167168
throw new GeneratorException("these expressions must be wrapped in a function call " +
168-
"because a copy creation is needed and dataization is impossible")
169+
"because a copy creation is needed and dataization is impossible: " + rhs)
169170
case _ : CollectionCons | _ : DictCons => true
170171
case _ => false
171172
}
@@ -349,6 +350,7 @@ object PrintLinearizedMutableEOWithCage {
349350
"cage 0 > xcurrent-exception",
350351
"cage 0 > xexcinexc",
351352
"cage FALSE > xcaught",
353+
"pyslice 0 0 0 > dummy-pyslice-usage",
352354
"pyint 0 > dummy-int-usage",
353355
"pyfloat 0 > dummy-float-usage",
354356
"pybool TRUE > dummy-bool-usage",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.polystat.py2eo.transpiler
2+
3+
import org.polystat.py2eo.parser.Expression.{CollectionCons, Star, T}
4+
5+
object StarInCollectionConstructor {
6+
def apply(e : T) : T = e match {
7+
case CollectionCons(kind, l, ann) =>
8+
val l1 = l.flatMap({
9+
case Star(CollectionCons(kind1, l, _), _) => l
10+
case x : Any => List(x)
11+
})
12+
CollectionCons(kind, l1, ann)
13+
case x : Any => x
14+
}
15+
}

transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ object Transpile {
5252
debugPrinter(y._1, "afterSimplifyFor")
5353

5454
try {
55-
val rmWith = GenericStatementPasses.procStatement(SimplifyWith.apply)(y._1, y._2)
55+
val starInCollCons = GenericStatementPasses.simpleProcExprInStatement(StarInCollectionConstructor.apply)(y._1, y._2)
56+
debugPrinter(starInCollCons._1, "afterStarInCollCons")
57+
val rmWith = GenericStatementPasses.procStatement(SimplifyWith.apply)(starInCollCons._1, starInCollCons._2)
5658
debugPrinter(rmWith._1, "afterRmWith")
5759
val rmAssert = GenericStatementPasses.procStatement(SimplifyAssert.apply)(rmWith._1, rmWith._2)
5860
debugPrinter(rmAssert._1, "afterRmAssert")

transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/compound-statements/class/inheritance-test.yaml

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
enabled: False
1+
enabled: True
22
python: |
3-
def assignTest5():
3+
def assignTest6():
44
class c:
55
f = 1
66
a = [10, 11, 12, 13]
77
(x, y) = [z, t] = c.f = a[0] = a[1] = 7, 8
88
a[2:1] = (7, 8)
9-
i, *[j, k], l = (21, 22, 23, 24)
109
11-
b1 = (x == 7) and (y == 8) and (z == 7) and (t == 8) and (c.f == (7, 8)) and a == [(7, 8), (7, 8), 7, 8, 12, 13]
12-
b2 = (i == 21) and (j == 22) and (k == 23) and (l == 24)
13-
return b1 and b2
14-
15-
assert assignTest5()
10+
return (x == 7) and (y == 8) and (z == 7) and (t == 8) and (c.f == (7, 8)) and a == [(7, 8), (7, 8), 7, 8, 12, 13]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enabled: True
2+
python: |
3+
def assignTest7():
4+
i, *[j, k], l = (21, 22, 23, 24)
5+
6+
return (i == 21) and (j == 22) and (k == 23) and (l == 24)

0 commit comments

Comments
 (0)