2
0

Syntax.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package python;
  23. #if macro
  24. import haxe.macro.Expr;
  25. import haxe.macro.Context;
  26. import haxe.macro.ExprTools;
  27. #end
  28. import haxe.extern.Rest;
  29. @:noPackageRestrict
  30. @:noClosure
  31. extern class Syntax {
  32. #if macro
  33. static var self = macro python.Syntax;
  34. #end
  35. @:noUsing macro public static function importModule(module:String):haxe.macro.Expr {
  36. return macro($self.code($v{"import " + module}) : Void);
  37. }
  38. @:noUsing macro public static function importAs(module:String, className:String):haxe.macro.Expr {
  39. var n = className.split(".").join("_");
  40. var e = "import " + module + " as " + n;
  41. return macro($self.code($v{e}) : Void);
  42. }
  43. #if !macro
  44. @:overload(function(className:String, args:Rest<Dynamic>):Dynamic {})
  45. static function construct<T>(cls:Class<T>, args:Rest<Dynamic>):T;
  46. #end
  47. @:noUsing
  48. @:deprecated("python.Syntax.newInstance() is deprecated. Use python.Syntax.construct() instead.")
  49. macro public static function newInstance(c:Expr, params:Array<Expr>):haxe.macro.Expr {
  50. return macro $self._newInstance($c, $a{params});
  51. }
  52. extern static function _newInstance(c:Dynamic, args:Array<Dynamic>):Dynamic;
  53. @:noUsing
  54. extern public static function isIn(a:Dynamic, b:Dynamic):Bool;
  55. @:noUsing
  56. extern public static function delete(a:Dynamic):Void;
  57. @:noUsing
  58. extern public static function binop(a:Dynamic, op:String, b:Dynamic):Dynamic;
  59. @:noUsing
  60. extern public static function assign(a:Dynamic, b:Dynamic):Void;
  61. #if !macro
  62. public static function code(code:String, args:Rest<Dynamic>):Dynamic;
  63. #end
  64. @:noUsing
  65. @:deprecated("python.Syntax.pythonCode() is deprecated. Use python.Syntax.code() instead.")
  66. macro public static function pythonCode(b:ExprOf<String>, rest:Array<Expr>):Expr {
  67. if (rest == null)
  68. rest = [];
  69. return macro @:pos(Context.currentPos()) untyped $self._pythonCode($b, $a{rest});
  70. };
  71. #if !macro
  72. @:noUsing
  73. public static function _pythonCode<T>(b:String, args:Array<Dynamic>):T;
  74. #end
  75. @:noUsing
  76. macro public static function arrayAccess(x:Expr, rest:Array<Expr>):ExprOf<Dynamic> {
  77. return macro $self._arrayAccess($x, $a{rest});
  78. }
  79. @:noUsing
  80. macro public static function arrayAccessWithTrailingColon(x:Expr, rest:Array<Expr>):ExprOf<Dynamic> {
  81. return macro $self._arrayAccess($x, $a{rest}, true);
  82. }
  83. extern static function _arrayAccess(a:Dynamic, args:Array<Dynamic>, ?trailingColon:Bool = false):Dynamic;
  84. @:noUsing
  85. extern public static function arraySet(a:Dynamic, i:Dynamic, v:Dynamic):Dynamic;
  86. extern static function _foreach(id:Dynamic, it:Dynamic, block:Dynamic):Dynamic;
  87. @:noUsing
  88. macro public static function foreach<T>(v:Expr, it:Expr, b:Expr):haxe.macro.Expr {
  89. var id = switch (v.expr) {
  90. case EConst(CIdent(x)): x;
  91. case _: Context.error("unexpected " + ExprTools.toString(v) + ": const ident expected", v.pos);
  92. }
  93. var iter = try {
  94. var it = macro($it.__iter__() : python.NativeIterator.NativeIteratorRaw<T>);
  95. Context.typeof(it);
  96. it;
  97. } catch (e:Dynamic) {
  98. macro($it : python.NativeIterable.NativeIterableRaw<T>);
  99. }
  100. return macro {
  101. var $id = null;
  102. $self._foreach($v, $it, cast $b);
  103. }
  104. }
  105. @:noUsing macro public static function importFromAs(from:String, module:String, className:String):haxe.macro.Expr {
  106. var n = className.split(".").join("_");
  107. var e = "from " + from + " import " + module + " as " + n;
  108. return macro($self.code($v{e}) : Void);
  109. }
  110. @:noUsing
  111. macro public static function callField(o:Expr, field:ExprOf<String>, params:Array<Expr>):haxe.macro.Expr {
  112. return macro @:pos(o.pos) $self.call($self.field($o, $field), $a{params});
  113. }
  114. extern static function call(e:Dynamic, args:Array<Dynamic>):Dynamic;
  115. @:noUsing
  116. extern public static function field(o:Dynamic, field:String):Dynamic;
  117. @:noUsing
  118. macro public static function tuple(args:Array<Expr>):Dynamic {
  119. var args = macro $a{args};
  120. return macro $self._tuple($args);
  121. }
  122. extern static function _tuple(args:Array<Dynamic>):Dynamic;
  123. @:noUsing
  124. extern public static function varArgs(args:Array<Dynamic>):Dynamic;
  125. macro public static function callNamedUntyped(e:Expr, args:Expr):Expr {
  126. return macro @:pos(e.pos) $self._callNamedUntyped($e, $args);
  127. }
  128. extern static function _callNamedUntyped(e:Dynamic, args:Dynamic):Dynamic;
  129. extern public static function opPow(a:Int, b:Int):Int;
  130. }