Guest User

Untitled

a guest
Feb 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. Syntax
  2. ------
  3.  
  4. [Ludwig Mies van der Rohe]
  5. _______________________________________
  6. Less is more.
  7. _______________________________________
  8.  
  9.  
  10. Expressions
  11. ~~~~~~~~~~~
  12.  
  13. Io has no keywords or statements. Everything is an expression composed entirely of messages, each of which is a runtime accessible object. The informal BNF description:
  14.  
  15. ---------------------------------------
  16. exp ::= { message | terminator }
  17. message ::= symbol [arguments]
  18. arguments ::= "(" [exp [ { "," exp } ]] ")"
  19. symbol ::= identifier | number | string
  20. terminator ::= "\n" | ";"
  21. ---------------------------------------
  22.  
  23. For performance reasons, String and Number literal messages have their results cached in their message objects.
  24.  
  25. Messages
  26. ~~~~~~~~
  27.  
  28. Message arguments are passed as expressions and evaluated by the receiver. Selective evaluation of arguments can be used to implement control flow. Examples:
  29.  
  30. ---------------------------------------
  31. for(i, 1, 10, i println)
  32. a := if(b == 0, c + 1, d)
  33. ---------------------------------------
  34.  
  35. In the above code, "`for`" and "`if`" are just normal messages, not special forms or keywords.
  36.  
  37. Likewise, dynamic evaluation can be used with enumeration without the need to wrap the expression in a block. Examples:
  38.  
  39. ---------------------------------------
  40. people select(person, person age < 30)
  41. names := people map(person, person name)
  42. ---------------------------------------
  43.  
  44. There is also some syntax sugar for operators (including assignment), which are handled by an Io macro executed on the expression after it is compiled into a message tree. Some sample source code:
  45.  
  46. ---------------------------------------
  47. Account := Object clone
  48. Account balance := 0
  49. Account deposit := method(amount,
  50. balance = balance + amount
  51. )
  52.  
  53. account := Account clone
  54. account deposit(10.00)
  55. account balance println
  56. ---------------------------------------
  57.  
  58. Like Self <<self>>, Io’s syntax does not distinguish between accessing a slot containing a method from one containing a variable.
  59.  
  60.  
  61. Operators
  62. ~~~~~~~~~
  63.  
  64. An operator is just a message whose name contains no alphanumeric characters (other than "`:`", "`_`", '`"`' or "`.`") or is one of the following words: `or`, `and`, `return`. Example:
  65.  
  66. ---------------------------------------
  67. 1 + 2
  68. ---------------------------------------
  69.  
  70. This just gets compiled into the normal message:
  71.  
  72. ---------------------------------------
  73. 1 +(2)
  74. ---------------------------------------
  75.  
  76. Which is the form you can use if you need to do grouping:
  77.  
  78. ---------------------------------------
  79. 1 +(2 * 4)
  80. ---------------------------------------
  81.  
  82. Standard operators follow C's precedence order, so:
  83.  
  84. ---------------------------------------
  85. 1 + 2 * 3 + 4
  86. ---------------------------------------
  87.  
  88. Is parsed as:
  89.  
  90. ---------------------------------------
  91. 1 +(2 *(3)) +(4)
  92. ---------------------------------------
  93.  
  94. User defined operators (that don't have a standard operator name) are performed left to right.
  95.  
  96. Assignment
  97. ~~~~~~~~~~
  98.  
  99. Io has two assignment messages, "`:=`" and "`=`".
  100.  
  101. ---------------------------------------
  102. a := 1
  103. ---------------------------------------
  104.  
  105. which compiles to:
  106.  
  107. ---------------------------------------
  108. setSlot("a", 1)
  109. ---------------------------------------
  110.  
  111. which creates the slot in the current context. And:
  112.  
  113. ---------------------------------------
  114. a = 1
  115. ---------------------------------------
  116.  
  117. which compiles to:
  118.  
  119. ---------------------------------------
  120. updateSlot("a", 1)
  121. ---------------------------------------
  122.  
  123. which sets the slot if it is found in the lookup path or raises an exception otherwise. By overloading `updateSlot` and `forward` in the `Locals` prototype, `self` is made implicit in methods.
  124.  
  125. Numbers
  126. ~~~~~~~
  127.  
  128. The following are valid number formats:
  129.  
  130. ---------------------------------------
  131. 123
  132. 123.456
  133. 0.456
  134. .456
  135. 123e-4
  136. 123e4
  137. 123.456e-7
  138. 123.456e2
  139. ---------------------------------------
  140.  
  141. Hex numbers are also supported (in any casing):
  142.  
  143. ---------------------------------------
  144. 0x0
  145. 0x0F
  146. 0XeE
  147. ---------------------------------------
  148.  
  149. Strings
  150. ~~~~~~~
  151.  
  152. Strings can be defined surrounded by a single set of double quotes with escaped quotes (and other escape characters) within.
  153.  
  154. ---------------------------------------
  155. s := "this is a \"test\".\nThis is only a test."
  156. ---------------------------------------
  157.  
  158. Or for strings with non-escaped characters and/or spanning many lines, triple quotes can be used.
  159.  
  160. ---------------------------------------
  161. s := """this is a "test".
  162. This is only a test."""
  163. ---------------------------------------
  164.  
  165. Comments
  166. ~~~~~~~~
  167.  
  168. Comments of the `//`, `/**/` and `#` style are supported. Examples:
  169.  
  170. ---------------------------------------
  171. a := b // add a comment to a line
  172.  
  173. /* comment out a group
  174. a := 1
  175. b := 2
  176. */
  177. ---------------------------------------
  178.  
  179. The "#" style is useful for unix scripts:
  180.  
  181. ---------------------------------------
  182. #!/usr/local/bin/io
  183. ---------------------------------------
  184.  
  185. That's it! You now know everything there is to know about Io's syntax. Control flow, objects, methods, exceptions are expressed with the syntax and semantics described above.
Add Comment
Please, Sign In to add comment