statements.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. .. _statements:
  2. =================
  3. Statements
  4. =================
  5. .. index::
  6. single: statements
  7. A squirrel program is a simple sequence of statements.::
  8. stats := stat [';'|'\n'] stats
  9. Statements in squirrel are comparable to the C-Family languages (C/C++, Java, C#
  10. etc...): assignment, function calls, program flow control structures etc.. plus some
  11. custom statement like yield, table and array constructors (All those will be covered in detail
  12. later in this document).
  13. Statements can be separated with a new line or ';' (or with the keywords case or default if
  14. inside a switch/case statement), both symbols are not required if the statement is
  15. followed by '}'.
  16. ------
  17. Block
  18. ------
  19. .. index::
  20. pair: block; statement
  21. ::
  22. stat := '{' stats '}'
  23. A sequence of statements delimited by curly brackets ({ }) is called block;
  24. a block is a statement itself.
  25. -----------------------
  26. Control Flow Statements
  27. -----------------------
  28. .. index::
  29. single: control flow statements
  30. squirrel implements the most common control flow statements: ``if, while, do-while, switch-case, for, foreach``
  31. ^^^^^^^^^^^^^^
  32. true and false
  33. ^^^^^^^^^^^^^^
  34. .. index::
  35. single: true and false
  36. single: true
  37. single: false
  38. Squirrel has a boolean type(bool) however like C++ it considers null, 0(integer) and 0.0(float)
  39. as *false*, any other value is considered *true*.
  40. ^^^^^^^^^^^^^^^^^
  41. if/else statement
  42. ^^^^^^^^^^^^^^^^^
  43. .. index::
  44. pair: if/else; statement
  45. pair: if; statement
  46. pair: else; statement
  47. ::
  48. stat:= 'if' '(' exp ')' stat ['else' stat]
  49. Conditionally execute a statement depending on the result of an expression.::
  50. if(a>b)
  51. a=b;
  52. else
  53. b=a;
  54. ////
  55. if(a==10)
  56. {
  57. b=a+b;
  58. return a;
  59. }
  60. ^^^^^^^^^^^^^^^^^
  61. while statement
  62. ^^^^^^^^^^^^^^^^^
  63. .. index::
  64. pair: while; statement
  65. ::
  66. stat:= 'while' '(' exp ')' stat
  67. Executes a statement while the condition is true.::
  68. function testy(n)
  69. {
  70. local a=0;
  71. while(a<n) a+=1;
  72. while(1)
  73. {
  74. if(a<0) break;
  75. a-=1;
  76. }
  77. }
  78. ^^^^^^^^^^^^^^^^^^
  79. do/while statement
  80. ^^^^^^^^^^^^^^^^^^
  81. .. index::
  82. pair: do/while; statement
  83. ::
  84. stat:= 'do' stat 'while' '(' expression ')'
  85. Executes a statement once, and then repeats execution of the statement until a condition
  86. expression evaluates to false.::
  87. local a=0;
  88. do
  89. {
  90. print(a+"\n");
  91. a+=1;
  92. } while(a>100)
  93. ^^^^^^^^^^^^^^^^^
  94. switch statement
  95. ^^^^^^^^^^^^^^^^^
  96. .. index::
  97. pair: switch; statement
  98. ::
  99. stat := 'switch' ''( exp ')' '{'
  100. 'case' case_exp ':'
  101. stats
  102. ['default' ':'
  103. stats]
  104. '}'
  105. Switch is a control statement allows multiple selections of code by passing control to one of the
  106. case statements within its body.
  107. The control is transferred to the case label whose case_exp matches with exp if none of
  108. the case match will jump to the default label (if present).
  109. A switch statement can contain any number if case instances, if 2 case have the same
  110. expression result the first one will be taken in account first. The default label is only
  111. allowed once and must be the last one.
  112. A break statement will jump outside the switch block.
  113. -----
  114. Loops
  115. -----
  116. .. index::
  117. single: Loops
  118. ^^^^^^^^
  119. for
  120. ^^^^^^^^
  121. .. index::
  122. pair: for; statement
  123. ::
  124. stat:= 'for' '(' [initexp] ';' [condexp] ';' [incexp] ')' statement
  125. Executes a statement as long as a condition is different than false.::
  126. for(local a=0;a<10;a+=1)
  127. print(a+"\n");
  128. //or
  129. glob <- null
  130. for(glob=0;glob<10;glob+=1){
  131. print(glob+"\n");
  132. }
  133. //or
  134. for(;;){
  135. print(loops forever+"\n");
  136. }
  137. ^^^^^^^^
  138. foreach
  139. ^^^^^^^^
  140. .. index::
  141. pair: foreach; statement
  142. ::
  143. 'foreach' '(' [index_id','] value_id 'in' exp ')' stat
  144. Executes a statement for every element contained in an array, table, class, string or generator.
  145. If exp is a generator it will be resumed every iteration as long as it is alive; the value will
  146. be the result of 'resume' and the index the sequence number of the iteration starting
  147. from 0.::
  148. local a=[10,23,33,41,589,56]
  149. foreach(idx,val in a)
  150. print("index="+idx+" value="+val+"\n");
  151. //or
  152. foreach(val in a)
  153. print("value="+val+"\n");
  154. -------
  155. break
  156. -------
  157. .. index::
  158. pair: break; statement
  159. ::
  160. stat := 'break'
  161. The break statement terminates the execution of a loop (for, foreach, while or do/while)
  162. or jumps out of switch statement;
  163. ---------
  164. continue
  165. ---------
  166. .. index::
  167. pair: continue; statement
  168. ::
  169. stat := 'continue'
  170. The continue operator jumps to the next iteration of the loop skipping the execution of
  171. the following statements.
  172. ---------
  173. return
  174. ---------
  175. .. index::
  176. pair: return; statement
  177. ::
  178. stat:= return [exp]
  179. The return statement terminates the execution of the current function/generator and
  180. optionally returns the result of an expression. If the expression is omitted the function
  181. will return null. If the return statement is used inside a generator, the generator will not
  182. be resumable anymore.
  183. ---------
  184. yield
  185. ---------
  186. .. index::
  187. pair: yield; statement
  188. ::
  189. stat := yield [exp]
  190. (see :ref:`Generators <generators>`).
  191. ---------------------------
  192. Local variables declaration
  193. ---------------------------
  194. .. index::
  195. pair: Local variables declaration; statement
  196. ::
  197. initz := id [= exp][',' initz]
  198. stat := 'local' initz
  199. Local variables can be declared at any point in the program; they exist between their
  200. declaration to the end of the block where they have been declared.
  201. *EXCEPTION:* a local declaration statement is allowed as first expression in a for loop.::
  202. for(local a=0;a<10;a+=1)
  203. print(a);
  204. --------------------
  205. Function declaration
  206. --------------------
  207. .. index::
  208. pair: Function declaration; statement
  209. ::
  210. funcname := id ['::' id]
  211. stat:= 'function' id ['::' id]+ '(' args ')' stat
  212. creates a new function.
  213. -----------------
  214. Class declaration
  215. -----------------
  216. .. index::
  217. pair: Class declaration; statement
  218. ::
  219. memberdecl := id '=' exp [';'] | '[' exp ']' '=' exp [';'] | functionstat | 'constructor' functionexp
  220. stat:= 'class' derefexp ['extends' derefexp] '{'
  221. [memberdecl]
  222. '}'
  223. creates a new class.
  224. -----------
  225. try/catch
  226. -----------
  227. .. index::
  228. pair: try/catch; statement
  229. ::
  230. stat:= 'try' stat 'catch' '(' id ')' stat
  231. The try statement encloses a block of code in which an exceptional condition can occur,
  232. such as a runtime error or a throw statement. The catch clause provides the exceptionhandling
  233. code. When a catch clause catches an exception, its id is bound to that
  234. exception.
  235. -----------
  236. throw
  237. -----------
  238. .. index::
  239. pair: throw; statement
  240. ::
  241. stat:= 'throw' exp
  242. Throws an exception. Any value can be thrown.
  243. --------------
  244. const
  245. --------------
  246. .. index::
  247. pair: const; statement
  248. ::
  249. stat:= 'const' id '=' 'Integer | Float | StringLiteral
  250. Declares a constant (see :ref:`Constants & Enumerations <constants_and_enumerations>`).
  251. --------------
  252. enum
  253. --------------
  254. .. index::
  255. pair: enum; statement
  256. ::
  257. enumerations := ( 'id' '=' Integer | Float | StringLiteral ) [',']
  258. stat:= 'enum' id '{' enumerations '}'
  259. Declares an enumeration (see :ref:`Constants & Enumerations <constants_and_enumerations>`).
  260. --------------------
  261. Expression statement
  262. --------------------
  263. .. index::
  264. pair: Expression statement; statement
  265. ::
  266. stat := exp
  267. In Squirrel every expression is also allowed as statement, if so, the result of the
  268. expression is thrown away.