diff --git a/transpiler/src/main/eo/preface/pybool.eo b/transpiler/src/main/eo/preface/pybool.eo index 57a9cb001..e00d926c5 100644 --- a/transpiler/src/main/eo/preface/pybool.eo +++ b/transpiler/src/main/eo/preface/pybool.eo @@ -34,6 +34,9 @@ [] > as-string value.as-string > @ + [] > as-bool + pybool value > @ + [] > force value.write val > @ diff --git a/transpiler/src/main/eo/preface/pyint.eo b/transpiler/src/main/eo/preface/pyint.eo index 559ae18da..31f139676 100644 --- a/transpiler/src/main/eo/preface/pyint.eo +++ b/transpiler/src/main/eo/preface/pyint.eo @@ -72,6 +72,9 @@ pyint (value.pow (x.value)) p.fst.pow (p.snd) + [] > bitwise-not + pyint ((value.as-bytes.not).as-int) > @ + [x] > and fakeclasses.convert (pyint value) x > p seq > @ @@ -226,6 +229,13 @@ [] > force value.write val > @ + [] > xto-int + [] > apply + [stackUp] > @ + seq > @ + stackUp.forward (return (pyint value)) + 123 + seq > @ stdout (sprintf "dataizing value %d\n" value) value diff --git a/transpiler/src/main/eo/preface/pystring.eo b/transpiler/src/main/eo/preface/pystring.eo index c5c8cf9cc..11f53053f 100644 --- a/transpiler/src/main/eo/preface/pystring.eo +++ b/transpiler/src/main/eo/preface/pystring.eo @@ -7,6 +7,7 @@ +alias fakeclasses preface.fakeclasses +alias xmyArray preface.xmyArray +alias xStopIteration preface.xStopIteration ++alias xValueError preface.xValueError [value] > pystring @@ -27,6 +28,9 @@ [] > as-string pystring value > @ + [] > as-bool + pybool ((value.length.eq 0).not) > @ + [] > force value > @ @@ -74,6 +78,37 @@ [stackUp] > @ stackUp.forward (return iter) > @ + [] > xto-int + [] > apply + [stackUp] > @ + (* "0" "1" "2" "3" "4" "5" "6" "7" "8" "9") > digits + [s] > to-digit + memory 0 > result + seq > @ + while. + ((result.lt 10).and (((digits.get result).eq s).not)) + [unused] + seq > @ + stdout (sprintf "result %d\n" result) + result.write (result.plus 1) + 0 + result + memory 0 > result + memory TRUE > input-is-correct + memory 0 > i + seq > @ + (i.lt (value.length)).while + [unused] + value.slice i 1 > x + to-digit x > dig + seq > @ + input-is-correct.write (input-is-correct.and (dig.lt 10)) + result.write ((result.times 10).plus dig) + i.write (i.plus 1) + 123 + stackUp.forward (input-is-correct.if (return (pyint value)) ((goto (xValueError.apply.@)).result)) + 123 + seq > @ stdout (sprintf "dataizing value %s\n" value) value diff --git a/transpiler/src/main/eo/preface/xint.eo b/transpiler/src/main/eo/preface/xint.eo index f8032f9c3..5b269e046 100644 --- a/transpiler/src/main/eo/preface/xint.eo +++ b/transpiler/src/main/eo/preface/xint.eo @@ -1,11 +1,11 @@ +package preface +alias fakeclasses preface.fakeclasses -+alias return preface.return ++alias goto org.eolang.goto [] > xint fakeclasses.pyIntClass > @ [x] > apply [stackUp] > @ seq > @ - stackUp.forward (return x) + stackUp.forward (goto (x.xto-int.apply.@)) 123 diff --git a/transpiler/src/main/eo/preface/xmyArray.eo b/transpiler/src/main/eo/preface/xmyArray.eo index a3b47c672..0530b9603 100644 --- a/transpiler/src/main/eo/preface/xmyArray.eo +++ b/transpiler/src/main/eo/preface/xmyArray.eo @@ -43,6 +43,24 @@ self.value.write (tmp.copy.with (x.with-value (tmpx.copy))) stackUp.forward (return 0) 123 + [] > xremove + [self what] > apply + [stackUp] > @ + memory FALSE > removed + (goto ((xmyArray.apply (*)).@)).result > accum + memory 0 > i + seq > @ + (i.lt (self.value.length)).while + [unused] + self.value.get i > x + seq > @ + ((x.eq what).value.and (removed.not)).if + removed.write TRUE + (goto ((accum.xappend.apply accum x).@)).result + i.write (i.plus 1) + self.value.write (accum.value) + stackUp.forward (return 0) + 123 [] > xclear [self] > apply [stackUp] > @ diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintEO.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintEO.scala index f02b48c2f..0068defb8 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintEO.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintEO.scala @@ -45,7 +45,7 @@ object PrintEO { def unop(t: Unops.T): String = t match { case Unops.Minus => ".neg" - case Unops.Neg => ".unsupported" + case Unops.Neg => ".bitwise-not" case Unops.LNot => ".not" case Unops.Plus => "" } @@ -107,7 +107,7 @@ object PrintEO { case CallIndex(isCall, whom, args, _) if !isCall && args.size == 1 => orb + e(whom) + ".get " + e(args(0)._2) + crb case Field(whose, name, _) => orb + e(whose) + "." + name + crb - case Cond(cond, yes, no, _) => orb + e(cond) + ".if " + e(yes) + space + e(no) + crb + case Cond(cond, yes, no, _) => orb + e(cond) + ".as-bool.if " + e(yes) + space + e(no) + crb case CallIndex(true, whom, args, _) => "((" + e(whom) + crb + ".apply" + // todo: empty arg list hack 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 954c5fd97..e9da60bd1 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/PrintLinearizedMutableEOWithCage.scala @@ -9,8 +9,9 @@ import org.polystat.py2eo.parser.Expression.{ Field, GeneratorComprehension, Ident, Parameter, Slice, Star, T, isLiteral } import org.polystat.py2eo.parser.Statement.{ - AugAssign, Assign, Break, ClassDef, Decorators, FuncDef, IfSimple, NonLocal, Pass, - Raise, Return, Suite, Try, While, Continue + AnnAssign, AugAssign, Assign, Break, ClassDef, Decorators, + FuncDef, IfSimple, NonLocal, Pass, Raise, Return, Suite, Try, + While, Continue } object PrintLinearizedMutableEOWithCage { @@ -89,7 +90,7 @@ object PrintLinearizedMutableEOWithCage { } val callInit = init match { case None => "" - case Some(_) => s" (goto ((result.x__init__.apply pResult $consArgs).@))" + case Some(_) => s" ((goto ((result.x__init__.apply pResult $consArgs).@)).result)" } val decorates = bases.headOption.map(_._2) "write." :: indent( @@ -131,6 +132,8 @@ object PrintLinearizedMutableEOWithCage { ) case NonLocal(_, _) => List() case f: FuncDef => "write." :: indent(f.name :: printFun(List(), f)) + case AnnAssign(lhs, rhsAnn, Some(rhs), ann) => + printSt(Assign(List(lhs, rhs), ann.pos)) case AugAssign(op, lhs, rhs, ann) => List(s"(${pe(lhs)}).${augop(op)} (${pe(rhs)})") case Assign(List(lhs, rhs@Expression.Binop(op, _, _, _)), ann) if @@ -189,7 +192,7 @@ object PrintLinearizedMutableEOWithCage { case IfSimple(cond, yes, no, _) => val stsY = printSt(yes) val stsN = printSt(no) - pe(cond) + ".if" :: indent("seq" :: indent(stsY :+ "(pybool TRUE)")) ++ indent("seq" :: indent(stsN :+ "(pybool TRUE)")) + pe(cond) + ".as-bool.if" :: indent("seq" :: indent(stsY :+ "(pybool TRUE)")) ++ indent("seq" :: indent(stsN :+ "(pybool TRUE)")) case While(cond, body, Some(Pass(_)), _) => "write." :: indent( "tmp" :: @@ -197,7 +200,7 @@ object PrintLinearizedMutableEOWithCage { "[stackUp]" :: indent( "seq > @" :: indent( ( - pe(cond) + ".while" :: indent( + pe(cond) + ".as-bool.while" :: indent( "[unused]" :: indent( "cage 0 > tmp" :: @@ -306,7 +309,7 @@ object PrintLinearizedMutableEOWithCage { "seq > @" :: indent( ("stdout \"" + f.name + "\\n\"") :: f.args.map(parm => s"${parm.name}.<") ++ - (printSt(f.body) :+ "123") + (printSt(f.body) :+ "stackUp.forward (return 0)" :+ "123") ) ) ) diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/conditional/conditional-2.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/conditional/conditional-2.yaml index 639049ddb..722435ae0 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/conditional/conditional-2.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/conditional/conditional-2.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def cond2(): b = 2 diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/unary-operators/unary-invert.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/unary-operators/unary-invert.yaml index c0a9c4176..846b43afc 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/unary-operators/unary-invert.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/expressions/unary-operators/unary-invert.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def unaryInvert(): a = 2 diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/for_try_else_continue_break.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/for_try_else_continue_break.yaml index 8506aeaa1..16325567f 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/for_try_else_continue_break.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/for_try_else_continue_break.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def for_try_else_continue_break(): items = [1, 2, 3, 4, 5, 6, 7, "hello", 100, 200, 300, 400] @@ -17,4 +17,4 @@ python: | else: res.append(800) - return res == [1, 2, 3, 4, 5, 6, 7] \ No newline at end of file + return res == [1, 2, 3, 4, 5, 6, 7] diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/while_try_else_continue_break.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/while_try_else_continue_break.yaml index fb39a633a..45b04467a 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/while_try_else_continue_break.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break-continue/while_try_else_continue_break.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def while_try_else_continue_break(): items = [1, 2, 3, 4, 5, 6, 7, "hello", 100, 200, 300, 400] diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break/for_try_else_break.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break/for_try_else_break.yaml index baf2b5281..82302e5f3 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break/for_try_else_break.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/break/for_try_else_break.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def for_try_else_break(): items = [1, 2, 3, 4, 5, 6, 7, "hello", 1, 2, 3, 4] @@ -12,4 +12,4 @@ python: | else: res.remove(100) - return res == [1, 2, 3, 4, 5, 6, 7, 100] \ No newline at end of file + return res == [1, 2, 3, 4, 5, 6, 7, 100] diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/continue/for_try_else_continue1.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/continue/for_try_else_continue1.yaml index 8cd95b0a6..5f0dd3d70 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/continue/for_try_else_continue1.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/continue/for_try_else_continue1.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def for_try_else_continue1(): items = [1, 2, 3, 4, 5, 6, 7, "hello", 100, 200, 300, 400] @@ -10,5 +10,5 @@ python: | continue else: res.remove(100) - - return res == [1, 2, 3, 4, 5, 6, 7, 200, 300, 400] \ No newline at end of file + + return res == [1, 2, 3, 4, 5, 6, 7, 200, 300, 400] diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-1.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-1.yaml index b4310bde4..41d776e49 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-1.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-1.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def check(): a: Int = 5 @@ -10,4 +10,4 @@ python: | a = 1 print("Hi!") - return a == 1 \ No newline at end of file + return a == 1 diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-2.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-2.yaml index 32ecf4588..876523d1b 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-2.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/expression-statement/expression-statement-2.yaml @@ -1,7 +1,7 @@ -enabled: False +enabled: True python: | def check(): a = 5 b = 17 c = a + b - return c == a + b \ No newline at end of file + return c == a + b diff --git a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/nonlocal/nonlocal.yaml b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/nonlocal/nonlocal.yaml index 27bc7109d..aaab5dd8c 100644 --- a/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/nonlocal/nonlocal.yaml +++ b/transpiler/src/test/resources/org/polystat/py2eo/transpiler/simple-tests/simple-statements/nonlocal/nonlocal.yaml @@ -1,4 +1,4 @@ -enabled: False +enabled: True python: | def nonLocal(): x = 0