macros.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**--------------------------------------------------------------------------**\
  2. ===================================
  3. y_inline - PAWN inline functions.
  4. ===================================
  5. Description:
  6. This library allows a user to write inline functions in their script. It
  7. first detects all inline functions and generates data on them, such as
  8. parameter counts and addresses. When an inline function is passed in code
  9. its current context data is stored. Finally, when an inline function is
  10. found to be called at some point its current local stack is stored in global
  11. memory. When the function actually is called, the stack is restored, and
  12. additional parameters which are the inline function parameters, are passed.
  13. Legal:
  14. Version: MPL 1.1
  15. The contents of this file are subject to the Mozilla Public License Version
  16. 1.1 (the "License"); you may not use this file except in compliance with
  17. the License. You may obtain a copy of the License at
  18. http://www.mozilla.org/MPL/
  19. Software distributed under the License is distributed on an "AS IS" basis,
  20. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  21. for the specific language governing rights and limitations under the
  22. License.
  23. The Original Code is the YSI Inline Function include.
  24. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  25. Portions created by the Initial Developer are Copyright (C) 2011
  26. the Initial Developer. All Rights Reserved.
  27. Contributors:
  28. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  29. Thanks:
  30. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  31. ZeeX - Very productive conversations.
  32. koolk - IsPlayerinAreaEx code.
  33. TheAlpha - Danish translation.
  34. breadfish - German translation.
  35. Fireburn - Dutch translation.
  36. yom - French translation.
  37. 50p - Polish translation.
  38. Zamaroht - Spanish translation.
  39. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  40. for me to strive to better.
  41. Pixels^ - Running XScripters where the idea was born.
  42. Matite - Pestering me to release it and using it.
  43. Very special thanks to:
  44. Thiadmer - PAWN, whose limits continue to amaze me!
  45. Kye/Kalcor - SA:MP.
  46. SA:MP Team past, present and future - SA:MP.
  47. Version:
  48. 1.0
  49. Changelog:
  50. 22/06/13:
  51. Rewrote the library from scratch for better performance all round.
  52. 20/10/12:
  53. Fixed a bug with "Callback_Release" with public functions.
  54. 15/11/11:
  55. Changed the precedence of "using" types.
  56. 19/09/11:
  57. First version
  58. \**--------------------------------------------------------------------------**/
  59. #if 0
  60. // This:
  61. inline func(a, &b, c[100])
  62. {
  63. // Inline function code.
  64. }
  65. // Becomes:
  66. static const func[]="func:.....";if(I@E(func))for(new a, b, c[100];I@F();)while(I@L(0,-1,100))
  67. {
  68. // Inline function code.
  69. }
  70. // Which is:
  71. static const
  72. func[] = "func:.....";
  73. if (I@E(func))
  74. {
  75. for (new a, b, c[100]; I@F(); )
  76. {
  77. while (I@L(0, -1, 100))
  78. {
  79. // Inline function code.
  80. }
  81. }
  82. }
  83. // Translated:
  84. static const
  85. func[] = "func\02;" #FUNC_ADDRESS #PARAMETERS #INTERNAL_RETURN #PREVIOUS_FUNC_NAME #PARENT_SCOPE_START;
  86. if (Inline_Entry(func))
  87. {
  88. for (new a, b, c[100]; Inline_Allocator(); )
  89. {
  90. while (Inline_Main(INLINE_TYPE_VAR, INLINE_TYPE_REF, INLINE_TYPE_ARR(100)))
  91. {
  92. // Inline function code.
  93. }
  94. }
  95. }
  96. // Run-time modified to something like:
  97. goto somewhere_after_this_func;
  98. func(parent, parameteters, a, b, c[100])
  99. {
  100. new parent, local, variables;
  101. // Inline function code.
  102. }
  103. // Multiple inlines in a row are combined in to one big jump.
  104. somewhere_after_this_func:
  105. #endif
  106. #define LAMBDA_i<%9>{%0}(%1)%8; _DO_LAMBDA|||new _0|||,0|||<%9>{%0}(%1)%8;
  107. #define LAMBDA_ii<%9>{%0}(%1)%8; _DO_LAMBDA|||new _0,_1|||,0,0|||<%9>{%0}(%1)%8;
  108. #define LAMBDA_iii<%9>{%0}(%1)%8; _DO_LAMBDA|||new _0,_1,_2|||,0,0,0|||<%9>{%0}(%1)%8;
  109. #define LAMBDA_iiii<%9>{%0}(%1)%8; _DO_LAMBDA|||new _0,_1,_2,_3|||,0,0,0,0|||<%9>{%0}(%1)%8;
  110. #define LAMBDA_iiiii<%9>{%0}(%1)%8; _DO_LAMBDA|||new _0,_1,_2,_3,_4|||,0,0,0,0,0|||<%9>{%0}(%1)%8;
  111. //#define LAMBDA_v<%9>{%0}(%1) _DO_LAMBDA|||new _0|||,0|||<%9>{%0}(%1)
  112. //#define LAMBDA_s<%9>{%0}(%1) _DO_LAMBDA|||new _0|||,0|||<%9>{%0}(%1)
  113. // #define LAMBDA_a<%9>{%0}(%1) _DO_LAMBDA|||new _0|||,0|||<%9>{%0}(%1)
  114. #define _DO_LAMBDA|||%6|||%5|||<%9>{%0}(%1)%8; LAM@0()%8;{LAM@1();static const YSII[]="@:....";if(I@E(YSII))for(%6;I@F();)while(I@L(I@K()%5))YSI_gInlineRet+=(%0);LAM@2(%9(callback_tag:YSII%1));}
  115. // Make sure there's a space after the "return".
  116. //#define YSI_gInlineRet+=(%0);%9(%1);}=> YSI_gInlineRet+=(%0);%3= %9(%1);
  117. //#define return= return
  118. //#define return%0({%1}%2)%3; %0({%1}%2)%3=>
  119. #define return%0({%1}%2)%3; I@=%0({%1}%2)%3;return I@;
  120. #define inline%0(%1) static %0[_:I@E:32]=#%0":....";if(I@E(_:_YI@CP:%0))for(new%1,,;I@F();)while(I@L(I@K(1)))
  121. #define I@E:32]=#%9const%7\32;%8":....";if(I@E(_:_YI@CP:%9const%7\32;%8))for(%1)while(I@L(I@K(1))) 32]=#%8":....";if(I@E(_:_YI@CP:%8))for(%1)while(I@L(I@K()))
  122. //#define i@Im:%9&%0[]=#%9&%0":....";if(I@E(_:_YI@CP:%9&%0)) %0[]=#%0":....";if(I@E(_:_YI@CP:%0,3))
  123. #define _YI@CP:%8))for(new%0,%1; @Ia:@Ib:@Ic:@Id:@Iz:%8))for(new|||%0|||%1;
  124. // Detect 0 parameters.
  125. #define @Ia:@Ib:@Ic:@Id:@Iz:%8))for(new||||||,; %8))for(;
  126. // Detect arrays. Get the size, defaulting to YSI_MAX_STRING (passing cellmax).
  127. #define @Ib:@Ic:@Id:@Iz:%8))for(new%0|||%2[%3]|||%4,%5;I@F();)while(I@L(%7)) @Ib:@Ic:@Id:@Iz:%8))for(new%0,%2[_:@Iy:%3]|||%4|||%5;I@F();)while(I@L(%7,@Ix&&&sizeof %2&&&))
  128. #define @Iy:]%0)while(I@L(%1&&&%2&&&%3)) YSI_MAX_INLINE_STRING]%0)while(I@L(%1%3))
  129. #define @Ix&&&%0&&& %0 // Remove this variable when the size is used correctly.
  130. // Default for passing strings, not arrays.
  131. stock const
  132. @Ix = cellmax;
  133. // Detect references.
  134. #define @Ic:@Id:@Iz:%8))for(new%0|||%1&%2|||%4,%5;I@F();)while(I@L(%7)) @Ib:@Ic:@Id:@Iz:%8))for(new%0,%2|||%4|||%5;I@F();)while(I@L(%7,-1))
  135. // Detect normal variables.
  136. #define @Id:@Iz:%8))for(new%0|||%2|||%4,%5;I@F();)while(I@L(%7)) @Ib:@Ic:@Id:@Iz:%8))for(new%0,%2|||%4|||%5;I@F();)while(I@L(%7,0))
  137. // Detect end of line.
  138. #define @Iz:%8))for(new%1,%0||||||; %8))for(new %0;
  139. // Drop support for anonymous functions.
  140. #define anonymous%0(%1) anonymous_no_longer_supported;for(new %1;FALSE;)
  141. // Parameter type for passing callbacks about.
  142. #define callback:%0) const callback_tag:callback_check:%0$$$
  143. #define callback_tag:callback_check:%0,%1$$$ callback_tag:%0[],%1)
  144. #define callback_check:%0$$$ %0[])
  145. #define using%0\10;%1 callback_tag:@Ik:@Il:%0
  146. // Get ONLY this ONE parameter (this is a VERY important stage)!
  147. #define @Ik:@Il:%0, @Ip:@Iq:@Io:@Iu:@Iw:|||%0|||,
  148. #define @Il:%0) @Ip:@Iq:@Io:@Iu:@Iw:|||%0|||)
  149. #define @Ip:%9|||%0callback%1||| @Ir:@Is:"\03;"#%1||||
  150. #define @Iq:%9|||%0public%1||| @Ir:@Is:"\03;"#%1||||
  151. #define @Io:%9|||%0inline%1||| @In:@It:%0%1||||
  152. #define @Iu:%9|||%0resolved%1||| @In:@It:%1[E_CALLBACK_DATA:0]||||
  153. #define @Iw:|||%1||| @In:@It:#%1||||
  154. // Callbacks with "On" in the name (often overidden by y_hooks and ALS).
  155. #define @Ir:@Is:%0On%1|||| @In:@It:%0"On"#%1||||
  156. #define @Is:%0|||| @In:@It:#%0|||| //Using_unknown_callback
  157. // Callbacks with additional parameters (MUST have matching parameters (y_ini)).
  158. #define @In:@It:%0(%1)%3||||%2) %0%3%2,.bExtra=true,.extra=%1)
  159. #define @It:%0|||| %0
  160. // Defer adding the close bracket till after other macros have run.
  161. #define _Y_INLINE_END )