djowel

Untitled

Oct 21st, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. [/=============================================================================
  2.  
  3. Gaea text template language!
  4.  
  5. Thanks to hkaiser, VeXocide and heller !!! on Spirit IRC
  6. Thanks to Daniel James for added comments and suggestions
  7.  
  8. ==============================================================================]
  9.  
  10. Reserved tokens:
  11. [
  12. ]
  13. [u
  14. [def
  15. [decl
  16. [lambda
  17. ...]
  18. [/
  19. ["
  20. [""
  21. ""]
  22. \[
  23. \]
  24.  
  25. (the last 2 is for escaping [ and ])
  26.  
  27. Comments:
  28.  
  29. [/ nested comments. ]
  30. [/ nested [/ nested comments] comments. ]
  31. [/ balanced square braces [ and ] (i.e starts with [ and ends with ] and
  32. with equal number of [ and ]), are allowed inside comments. This is a
  33. good way to comment out code. ]
  34.  
  35. Lists:
  36.  
  37. [a][b][c] [/ 1st ]
  38. [[a][b][c]] [/ 2nd ]
  39. [[a][[b][c]]] [/ 3rd ]
  40.  
  41. Lists can form linear or hierarchical data structures. 1st is a
  42. 3-element list. 2nd is a single element list with 3 elements. 3rd is a
  43. 2-element list where the first element is a single element list and the
  44. second has 2 elements. Formatting makes it clear:
  45.  
  46. 1st:
  47.  
  48. [a][b][c]
  49.  
  50. 2nd:
  51.  
  52. [
  53. [a][b][c]
  54. ]
  55.  
  56. 3rd:
  57.  
  58. [
  59. [a]
  60. [
  61. [b][c]
  62. ]
  63. ]
  64.  
  65. The first is an example of an "open-ended" list. The 2nd and 3rd are
  66. not. Open-ended lists are used to pass in variable number of arguments
  67. to a function (more below).
  68.  
  69. Strings:
  70.  
  71. This is a string
  72.  
  73. Jack and Jill went up the hill to fetch a pail of water.
  74. Jack fell down and broke his crown, and Jill came tumbling after.
  75.  
  76. 123 is a numeric string
  77.  
  78. Markups ([...]) are always significant, except inside escapes
  79. (see ["" Escapes below). Like HTML/XML spaces are coalesced unless
  80. they are inside the preformatted markup (see [" Preformatted below).
  81.  
  82. A string is just a special form of list with single character elements.
  83.  
  84. Unicode Code Points:
  85.  
  86. [u2018] [/ Generates unicode code point (hexadecimal). ]
  87.  
  88. Escapes:
  89.  
  90. \[ [/ Escapes the open bracket]
  91. \] [/ Escapes the close bracket]
  92. ["" x ""] [/ Escapes all occurances of [ and ] in x. ]
  93.  
  94. Preformatted:
  95.  
  96. [" some-text] [/ Make spaces, tabs and newlines significant ]
  97.  
  98. Symbols:
  99.  
  100. This-is-a-symbol
  101. ?
  102. *
  103. ==
  104. ?WTH
  105.  
  106. Symbols can be just about any string without spaces and the
  107. reserved [ and ] characters.
  108.  
  109. Function:
  110. Forward declarations:
  111.  
  112. [decl foo]
  113. [decl dup a]
  114.  
  115. Definitions:
  116.  
  117. [def foo][body]
  118.  
  119. [def dup a][[a][a]]
  120.  
  121. [def cat a b][[a][b]]
  122.  
  123. Function invocation:
  124.  
  125. [foo] [/ nullary function ]
  126. [dup apple pie] [/ unary function. argument is a string. ]
  127. [dup [apple pie]] [/ unary function. argument is a 1-element list. ]
  128. [cat [apple][pie]] [/ 2 arguments. argument is a 2-element list. ]
  129.  
  130. Example:
  131.  
  132. [def f3 a b c]
  133. [
  134. [def d][de Guzman]
  135. [a] [d], [b] [d], [c] [d]
  136. ]
  137.  
  138. [f3 [Joel][Mariel][Tenji]] [/ returns Joel de Guzman, Mariel de Guzman, Tenji de Guzman ]
  139.  
  140. Variable args:
  141.  
  142. [decl table title rows...]
  143.  
  144. The trailing elipsis signifies an open ended list. It is only
  145. allowed after the final formal argument preceding the closing bracket.
  146.  
  147. Example invocation:
  148.  
  149. [table title
  150. [[Heading 1] [Heading 2] [Heading 3]]
  151. [[R0-C0] [R0-C1] [R0-C2]]
  152. ]
  153.  
  154. Intrinsics:
  155.  
  156. [def head x] [/ Get first element from x. ]
  157. [def tail x] [/ Return a list or string without the first element of x. ]
  158. [def empty x] [/ Return 1 if x is empty else 0. ]
  159. [def at x n] [/ Return the nth element of x. ]
  160. [def size x] [/ Return size of x. ]
  161. [def append x e] [/ Append e to x. ]
  162. [def insert x e n] [/ Insert e to x at position n. ]
  163. [def reverse x] [/ Reverse x. ]
  164. [def join x y] [/ Join x and y as one longer list. ]
  165.  
  166. [def fold x s f] [/ For a list x, initial state s, and binary function f,
  167. fold returns the result of the repeated application of
  168. [f e s] for each element e of list x, to the result
  169. of the previous f invocation (s if it is the first call). ]
  170.  
  171. Many list operations can be implemented using fold. Yet, for the sake
  172. of efficiency, we provide these common operations as intrinsics.
  173.  
  174. [def transform x f] [/ For a list x and function f, transform returns a new
  175. list with elements created by applying [f e] to each
  176. element e of x. ]
  177.  
  178. [/ Since strings are just special forms of lists, all functions that accept
  179. lists can also accept strings.
  180. ]
  181.  
  182. [def + a b] [/ Add a and b. both a and b are numeric integer strings. ]
  183. [def - a b] [/ Subract a and b. both a and b are numeric integer strings. ]
  184. [def * a b] [/ Multiply a and b. both a and b are numeric integer strings. ]
  185. [def / a b] [/ Divide a and b. both a and b are numeric integer strings. ]
  186. [def % a b] [/ Remainder of a / b. both a and b are numeric integer strings. ]
  187.  
  188. [def == a b] [/ Returns 1 if a == b. ]
  189. [def < a b] [/ Returns 1 if a is less than b. ]
  190. [def && a b] [/ Returns a and b (boolean logic). ]
  191. [def || a b] [/ Returns a or b (boolean logic). ]
  192. [def if cond then else] [/ Returns then if cond is 1, otherwise returns else. ]
  193.  
  194. Other comparisons and booleans can be synthesized from the basics above.
  195. For example ! can be defined as [def ! x][[if [x][0][1]]], != can be defined
  196. as [def != a b][[![==[a][b]]]], etc.
  197.  
  198. [def >> file] [/ Loads file. ]
  199. [def << file x] [/ Saves x to file. ]
  200.  
  201. >> processes files (i.e. markups are significant). If you want to load
  202. files verbatim, enclose it in the escape markup. Example:
  203.  
  204. [""[>> text.gia]""]
  205.  
  206. Lambda Functions:
  207.  
  208. Functions are first class and can be passed and returned from other functions.
  209. For example, the transform intrinsic requires a lambda function for its
  210. last (f) argument.
  211.  
  212. Here's an example of a lambda function:
  213.  
  214. [lambda x][dup [x]]
  215.  
  216. Its syntax resembles a function definition.
  217.  
  218. Here's a sample invocation of transform:
  219.  
  220. [transform [list] [[lambda x][dup [x]]]]
  221.  
  222. This doubles all elements of the list.
  223.  
  224. Error conditions (todo):
  225.  
  226.  
  227.  
  228.  
Advertisement
Add Comment
Please, Sign In to add comment