Skip to content

Commit 42d73db

Browse files
committed
import-1 almost passes
1 parent c247322 commit 42d73db

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

checker/src/main/scala/org/polystat/py2eo/checker/Check.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ object Check {
5353
parseYaml(test) match {
5454
case None => TestResult(module, category, None)
5555
case Some(parsed) =>
56-
Transpile(module, Transpile.Parameters(wrapInAFunction = false), parsed) match {
56+
Transpile(module, Transpile.Parameters(wrapInAFunction = false, isModule = false), parsed) match {
5757
case None => TestResult(module, category, None)
5858
case Some(transpiled) =>
5959
val file = File(outputPath / test.changeExtension("eo").name)
@@ -83,7 +83,7 @@ object Check {
8383
diffFile writeAll "Diff between original (left) and mutated (right) python files\n"
8484
diffFile appendAll diffPyOutput.mkString("\n")
8585

86-
Transpile(module, Transpile.Parameters(wrapInAFunction = false), mutatedPyText) match {
86+
Transpile(module, Transpile.Parameters(wrapInAFunction = false, isModule = false), mutatedPyText) match {
8787
case None =>
8888
diffFile appendAll "\n\nFailed to transpile mutated py file\n"
8989
CompilingResult.failed

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

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.polystat.py2eo.parser.Expression.{
1010
}
1111
import org.polystat.py2eo.parser.Statement.{
1212
AnnAssign, AugAssign, Assign, Break, ClassDef, Decorators,
13-
FuncDef, IfSimple, NonLocal, Pass, Raise, Return, Suite, Try,
13+
FuncDef, IfSimple, ImportModule, NonLocal, Pass, Raise, Return, Suite, Try,
1414
While, Continue
1515
}
1616

@@ -19,8 +19,6 @@ object PrintLinearizedMutableEOWithCage {
1919
val returnLabel = "returnLabel"
2020

2121
val headers = List(
22-
"+package org.eolang",
23-
"+alias goto org.eolang.goto",
2422
"+alias stdout org.eolang.io.stdout",
2523
"+alias sprintf org.eolang.txt.sprintf",
2624
"+alias cage org.eolang.cage",
@@ -78,6 +76,7 @@ object PrintLinearizedMutableEOWithCage {
7876

7977
private def printSt(st : Statement.T) : Text = {
8078
st match {
79+
case _ : ImportModule => List()
8180
case ClassDef(name, bases, body, decorators, ann) if bases.length <= 1 && decorators.l.isEmpty =>
8281
val Suite(l0, _) = GenericStatementPasses.simpleProcStatement(GenericStatementPasses.unSuite)(body)
8382
val l = l0.filter{ case Pass(_) => false case _ => true }
@@ -286,7 +285,7 @@ object PrintLinearizedMutableEOWithCage {
286285
}
287286
}
288287

289-
private def printFun(preface : List[String], f : FuncDef) : Text = {
288+
private def printFun(preface : List[String], f : FuncDef, isModule : Boolean = false) : Text = {
290289
// println(s"l = \n${PrintPython.printSt(Suite(l), "-->>")}")
291290
val funs = AnalysisSupport.foldSS[List[FuncDef]]((l, st) => st match {
292291
case f : FuncDef => (l :+ f, false)
@@ -305,19 +304,25 @@ object PrintLinearizedMutableEOWithCage {
305304

306305
val args2 = (f.args.map{ case Parameter(argname, kind, None, None, _) if kind != ArgKind.Keyword =>
307306
argname + "NotCopied" }).mkString(" ")
308-
"[]" :: indent(
309-
s"[$args2] > ap" :: indent(
310-
"[stackUp] > @" :: indent(
311-
preface ++ (
312-
"cage 0 > tmp" ::
313-
"cage 0 > toReturn" ::
314-
argCopies ++ memories ++ (
315-
"seq > @" :: indent(
316-
("stdout \"" + f.name + "\\n\"") ::
307+
val body =
308+
preface ++ (
309+
"cage 0 > tmp" ::
310+
"cage 0 > toReturn" ::
311+
argCopies ++ memories ++ (
312+
"seq > @" :: indent(
313+
("stdout \"" + f.name + "\\n\"") ::
317314
f.args.map(parm => s"${parm.name}.<") ++
318-
(printSt(f.body) :+ "stackUp.forward (return 0)" :+ "123")
319-
)
315+
(printSt(f.body) :+ "stackUp.forward (return 0)" :+ "123")
316+
)
320317
)
318+
)
319+
"[]" :: indent(
320+
s"[$args2] > ap" :: indent(
321+
if (isModule)
322+
body
323+
else (
324+
"[stackUp] > @" :: indent(
325+
body
321326
)
322327
)
323328
)
@@ -381,7 +386,21 @@ object PrintLinearizedMutableEOWithCage {
381386
ComputeAccessibleIdents.computeAccessibleIdents(FuncDef(testName, List(), None, None, None, st, Decorators(List()),
382387
HashMap(), isAsync = false, st.ann.pos))
383388
val hack = printFun(preface, theTest)
384-
headers ++ (("+junit" :: "" :: s"[unused] > ${theTest.name}" :: hack.tail))
389+
val almost =
390+
headers ++
391+
(("+junit" :: "" :: s"[unused] > ${theTest.name}" :: hack.tail))
392+
val externalIdents = AnalysisSupport.foldSS[List[String]]({
393+
case (acc, ImportModule(what, _, _)) => (what.last :: acc, true)
394+
case (acc, _) => (acc, true)
395+
})(List(), st)
396+
"+package org.eolang" ::
397+
"+alias goto org.eolang.goto" ::
398+
externalIdents.map(name => s"+alias $name xmodules.$name") ++
399+
almost.init ++ (
400+
" seq > @" ::
401+
(externalIdents.map(" " + _) :+
402+
" (goto (ap.@)).result")
403+
)
385404
}
386405

387406
def printModule(moduleName : String, st : Statement.T) : Text = {
@@ -390,8 +409,17 @@ object PrintLinearizedMutableEOWithCage {
390409
val theTest@FuncDef(_, _, _, _, _, _, _, _, _, _) =
391410
ComputeAccessibleIdents.computeAccessibleIdents(FuncDef(moduleName, List(), None, None, None, st, Decorators(List()),
392411
HashMap(), isAsync = false, st.ann.pos))
393-
val hack = printFun(preface, theTest)
394-
headers ++ ("" :: s"[] > $moduleName" :: hack.tail)
412+
val fakeStackUp = List(
413+
"[] > stackUp",
414+
" [p] > forward",
415+
" p > @"
416+
)
417+
val hack = printFun(fakeStackUp ++ preface, theTest, true)
418+
val almost =
419+
"+package xmodules" ::
420+
headers ++
421+
("" :: s"[] > x$moduleName" :: hack.tail)
422+
almost.init :+ " ((ap)).result > @"
395423
}
396424

397425
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ object SubstituteExternalIdent {
1313
val externalIdents = AnalysisSupport.foldSS[HashMap[String, String]]({
1414
case (acc, ImportModule(what, as, ann)) =>
1515
val alias = as.getOrElse(what.last)
16-
(acc.+((alias, (("modules" :: what) :+ "ap.@").mkString(".x"))), true)
16+
(acc.+((alias, (what).mkString(".x") + ".ap")), true)
1717
case (acc, _) => (acc, true)
1818
})(HashMap[String, String](), s)
1919
println(s"externalIdents = $externalIdents")
20-
val s0 = simpleProcStatement({
21-
case ImportModule(what, as, ann) => Pass(ann)
22-
case s => s
23-
})(s)
2420
val s1 = simpleProcExprInStatementAcc[NamesU]((acc, e) => {
2521
val (Left(e1), acc1) = procExpr[Unit]({
2622
case (false, Field(Ident(moduleName, ann), name, acci), acc)
@@ -30,7 +26,7 @@ object SubstituteExternalIdent {
3026
}
3127
)(false, e, acc)
3228
(acc1, e1)
33-
})(ns, s0)
29+
})(ns, s)
3430
(s1._2, s1._1)
3531
}
3632
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ object Transpile {
108108
}
109109
else
110110
PrintLinearizedMutableEOWithCage.printModule(moduleName, textractAllCalls._1)
111-
(eoText.init :+ " (goto (ap.@)).result > @").mkString("\n")
111+
(eoText).mkString("\n")
112112
}
113113
catch {
114114
case e: Throwable => {
@@ -149,7 +149,7 @@ object Transpile {
149149

150150
PrintEO.printSt(
151151
("y" + moduleName).replaceAll("[^0-9a-zA-Z]", ""), hacked,
152-
"+package org.eolang" ::
152+
(if (!opt.isModule) "+package org.eolang" else "+package xmodules") ::
153153
"+alias pyint preface.pyint" ::
154154
"+alias pyfloat preface.pyfloat" ::
155155
"+alias pystring preface.pystring" ::
@@ -162,7 +162,7 @@ object Transpile {
162162
"pybool TRUE > dummy-bool-usage" ::
163163
globals.map(name => s"memory 0 > $name").toList
164164
)
165-
.mkString("\n")
165+
.mkString("\n")
166166

167167
}
168168
}

0 commit comments

Comments
 (0)