expressions.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. .. _expressions:
  2. =================
  3. Expressions
  4. =================
  5. .. index::
  6. single: Expressions
  7. ----------------
  8. Assignment
  9. ----------------
  10. .. index::
  11. single: assignment(=)
  12. single: new slot(<-)
  13. ::
  14. exp := derefexp '=' exp
  15. exp:= derefexp '<-' exp
  16. squirrel implements 2 kind of assignment: the normal assignment(=)::
  17. a = 10;
  18. and the "new slot" assignment.::
  19. a <- 10;
  20. The new slot expression allows to add a new slot into a table(see :ref:`Tables <tables>`). If the slot
  21. already exists in the table it behaves like a normal assignment.
  22. ----------------
  23. Operators
  24. ----------------
  25. .. index::
  26. single: Operators
  27. ^^^^^^^^^^^^^
  28. ?: Operator
  29. ^^^^^^^^^^^^^
  30. .. index::
  31. pair: ?: Operator; Operators
  32. ::
  33. exp := exp_cond '?' exp1 ':' exp2
  34. conditionally evaluate an expression depending on the result of an expression.
  35. ^^^^^^^^^^^^^
  36. Arithmetic
  37. ^^^^^^^^^^^^^
  38. .. index::
  39. pair: Arithmetic Operators; Operators
  40. ::
  41. exp:= 'exp' op 'exp'
  42. Squirrel supports the standard arithmetic operators ``+, -, *, / and %``.
  43. Other than that is also supports compact operators (``+=,-=,*=,/=,%=``) and
  44. increment and decrement operators(++ and --);::
  45. a += 2;
  46. //is the same as writing
  47. a = a + 2;
  48. x++
  49. //is the same as writing
  50. x = x + 1
  51. All operators work normally with integers and floats; if one operand is an integer and one
  52. is a float the result of the expression will be float.
  53. The + operator has a special behavior with strings; if one of the operands is a string the
  54. operator + will try to convert the other operand to string as well and concatenate both
  55. together. For instances and tables, ``_tostring`` is invoked.
  56. ^^^^^^^^^^^^^
  57. Relational
  58. ^^^^^^^^^^^^^
  59. .. index::
  60. pair: Relational Operators; Operators
  61. ::
  62. exp:= 'exp' op 'exp'
  63. Relational operators in Squirrel are : ``==, <, <=, <, <=, !=``
  64. These operators return true if the expression is false and a value different than true if the
  65. expression is true. Internally the VM uses the integer 1 as true but this could change in
  66. the future.
  67. ^^^^^^^^^^^^^^
  68. 3 ways compare
  69. ^^^^^^^^^^^^^^
  70. .. index::
  71. pair: 3 ways compare operator; Operators
  72. ::
  73. exp:= 'exp' <=> 'exp'
  74. the 3 ways compare operator <=> compares 2 values A and B and returns an integer less than 0
  75. if A < B, 0 if A == B and an integer greater than 0 if A > B.
  76. ^^^^^^^^^^^^^^
  77. Logical
  78. ^^^^^^^^^^^^^^
  79. .. index::
  80. pair: Logical operators; Operators
  81. ::
  82. exp := exp op exp
  83. exp := '!' exp
  84. Logical operators in Squirrel are : ``&&, ||, !``
  85. The operator ``&&`` (logical and) returns null if its first argument is null, otherwise returns
  86. its second argument.
  87. The operator ``||`` (logical or) returns its first argument if is different than null, otherwise
  88. returns the second argument.
  89. The '!' operator will return null if the given value to negate was different than null, or a
  90. value different than null if the given value was null.
  91. ^^^^^^^^^^^^^^^
  92. in operator
  93. ^^^^^^^^^^^^^^^
  94. .. index::
  95. pair: in operator; Operators
  96. ::
  97. exp:= keyexp 'in' tableexp
  98. Tests the existence of a slot in a table.
  99. Returns true if *keyexp* is a valid key in *tableexp* ::
  100. local t=
  101. {
  102. foo="I'm foo",
  103. [123]="I'm not foo"
  104. }
  105. if("foo" in t) dostuff("yep");
  106. if(123 in t) dostuff();
  107. ^^^^^^^^^^^^^^^^^^^
  108. instanceof operator
  109. ^^^^^^^^^^^^^^^^^^^
  110. .. index::
  111. pair: instanceof operator; Operators
  112. ::
  113. exp:= instanceexp 'instanceof' classexp
  114. Tests if a class instance is an instance of a certain class.
  115. Returns true if *instanceexp* is an instance of *classexp*.
  116. ^^^^^^^^^^^^^^^^^^^
  117. typeof operator
  118. ^^^^^^^^^^^^^^^^^^^
  119. .. index::
  120. pair: typeof operator; Operators
  121. ::
  122. exp:= 'typeof' exp
  123. returns the type name of a value as string.::
  124. local a={},b="squirrel"
  125. print(typeof a); //will print "table"
  126. print(typeof b); //will print "string"
  127. ^^^^^^^^^^^^^^^^^^^
  128. Comma operator
  129. ^^^^^^^^^^^^^^^^^^^
  130. .. index::
  131. pair: Comma operator; Operators
  132. ::
  133. exp:= exp ',' exp
  134. The comma operator evaluates two expression left to right, the result of the operator is
  135. the result of the expression on the right; the result of the left expression is discarded.::
  136. local j=0,k=0;
  137. for(local i=0; i<10; i++ , j++)
  138. {
  139. k = i + j;
  140. }
  141. local a,k;
  142. a = (k=1,k+2); //a becomes 3
  143. ^^^^^^^^^^^^^^^^^^^
  144. Bitwise Operators
  145. ^^^^^^^^^^^^^^^^^^^
  146. .. index::
  147. pair: Bitwise Operators; Operators
  148. ::
  149. exp:= 'exp' op 'exp'
  150. exp := '~' exp
  151. Squirrel supports the standard C-like bitwise operators ``&, |, ^, ~, <<, >>`` plus the unsigned
  152. right shift operator ``>>>``. The unsigned right shift works exactly like the normal right shift operator(``>>``)
  153. except for treating the left operand as an unsigned integer, so is not affected by the sign. Those operators
  154. only work on integer values; passing of any other operand type to these operators will
  155. cause an exception.
  156. ^^^^^^^^^^^^^^^^^^^^^
  157. Operators precedence
  158. ^^^^^^^^^^^^^^^^^^^^^
  159. .. index::
  160. pair: Operators precedence; Operators
  161. +---------------------------------------+-----------+
  162. | ``-, ~, !, typeof , ++, --`` | highest |
  163. +---------------------------------------+-----------+
  164. | ``/, *, %`` | ... |
  165. +---------------------------------------+-----------+
  166. | ``+, -`` | |
  167. +---------------------------------------+-----------+
  168. | ``<<, >>, >>>`` | |
  169. +---------------------------------------+-----------+
  170. | ``<, <=, >, >=`` | |
  171. +---------------------------------------+-----------+
  172. | ``==, !=, <=>`` | |
  173. +---------------------------------------+-----------+
  174. | ``&`` | |
  175. +---------------------------------------+-----------+
  176. | ``^`` | |
  177. +---------------------------------------+-----------+
  178. | ``&&, in`` | |
  179. +---------------------------------------+-----------+
  180. | ``+=, =, -=`` | ... |
  181. +---------------------------------------+-----------+
  182. | ``, (comma operator)`` | lowest |
  183. +---------------------------------------+-----------+
  184. .. _table_contructor:
  185. -----------------
  186. Table Constructor
  187. -----------------
  188. .. index::
  189. single: Table Contructor
  190. ::
  191. tslots := ( 'id' '=' exp | '[' exp ']' '=' exp ) [',']
  192. exp := '{' [tslots] '}'
  193. Creates a new table.::
  194. local a = {} //create an empty table
  195. A table constructor can also contain slots declaration; With the syntax: ::
  196. local a = {
  197. slot1 = "I'm the slot value"
  198. }
  199. An alternative syntax can be::
  200. '[' exp1 ']' = exp2 [',']
  201. A new slot with exp1 as key and exp2 as value is created::
  202. local a=
  203. {
  204. [1]="I'm the value"
  205. }
  206. Both syntaxes can be mixed::
  207. local table=
  208. {
  209. a=10,
  210. b="string",
  211. [10]={},
  212. function bau(a,b)
  213. {
  214. return a+b;
  215. }
  216. }
  217. The comma between slots is optional.
  218. ^^^^^^^^^^^^^^^^^^^^^^
  219. Table with JSON syntax
  220. ^^^^^^^^^^^^^^^^^^^^^^
  221. .. index::
  222. single: Table with JSON syntax
  223. Since Squirrel 3.0 is possible to declare a table using JSON syntax(see http://www.wikipedia.org/wiki/JSON).
  224. the following JSON snippet: ::
  225. local x = {
  226. "id": 1,
  227. "name": "Foo",
  228. "price": 123,
  229. "tags": ["Bar","Eek"]
  230. }
  231. is equivalent to the following squirrel code: ::
  232. local x = {
  233. id = 1,
  234. name = "Foo",
  235. price = 123,
  236. tags = ["Bar","Eek"]
  237. }
  238. -----------------
  239. clone
  240. -----------------
  241. .. index::
  242. single: clone
  243. ::
  244. exp:= 'clone' exp
  245. Clone performs shallow copy of a table, array or class instance (copies all slots in the new object without
  246. recursion). If the source table has a delegate, the same delegate will be assigned as
  247. delegate (not copied) to the new table (see :ref:`Delegation <delegation>`).
  248. After the new object is ready the "_cloned" meta method is called (see :ref:`Metamethods <metamethods>`).
  249. When a class instance is cloned the constructor is not invoked(initializations must rely on ```_cloned``` instead
  250. -----------------
  251. Array contructor
  252. -----------------
  253. .. index::
  254. single: Array constructor
  255. ::
  256. exp := '[' [explist] ']'
  257. Creates a new array.::
  258. a <- [] //creates an empty array
  259. Arrays can be initialized with values during the construction::
  260. a <- [1,"string!",[],{}] //creates an array with 4 elements