djowel

Untitled

Oct 21st, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 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-an-symbol
  101. ?
  102. *
  103. ==
  104. ?WTH
  105.  
  106. Symbols can be just about any string without spaces and the
  107. reserved [ and ] characters. The trailing elipsis signifies an
  108. open ended list (see Variable args below).
  109.  
  110. Function:
  111. Forward declarations:
  112.  
  113. [decl foo]
  114. [decl dup a]
  115.  
  116. Definitions:
  117.  
  118. [def foo][body]
  119.  
  120. [def dup a][[a][a]]
  121.  
  122. [def follow a b][[a][b]]
  123.  
  124. Function invocation:
  125.  
  126. [foo] [/ nullary function ]
  127. [dup apple pie] [/ unary function. argument is a string. ]
  128. [dup [apple pie]] [/ unary function. argument is a 1-element list. ]
  129. [follow [apple][pie]] [/ 2 arguments. argument is a 2-element list. ]
  130.  
  131. Example:
  132.  
  133. [def f3 a b c]
  134. [
  135. [def d][de Guzman]
  136. [a] [d], [b] [d], [c] [d]
  137. ]
  138.  
  139. [f3 [Joel][Mariel][Tenji]] [/ returns Joel de Guzman, Mariel de Guzman, Tenji de Guzman ]
  140.  
  141. Variable args:
  142.  
  143. [decl table title rows...]
  144.  
  145. The trailing elipsis signifies an open ended list. It is only
  146. allowed after the final formal argument preceding the closing bracket.
  147.  
  148. Example invocation:
  149.  
  150. [table title
  151. [[Heading 1] [Heading 2] [Heading 3]]
  152. [[R0-C0] [R0-C1] [R0-C2]]
  153. ]
  154.  
  155. Intrinsics:
  156.  
  157. [def head x] [/ Get first element from x. ]
  158. [def tail x] [/ Return a list or string without the first element of x. ]
  159. [def empty x] [/ Return 1 if x is empty else 0. ]
  160. [def at x n] [/ Return the nth element of x. ]
  161. [def size x] [/ Return size of x. ]
  162. [def append x e] [/ Append e to x. ]
  163. [def insert x e n] [/ Insert e to x at position n. ]
  164. [def reverse x] [/ Reverse x. ]
  165. [def join x y] [/ Join x and y as one longer list. ]
  166.  
  167. [def fold x s f] [/ For a list x, initial state s, and binary function f,
  168. fold returns the result of the repeated application of
  169. [f e s] for each element e of list x, to the result
  170. of the previous f invocation (s if it is the first call). ]
  171.  
  172. Many list operations can be implemented using fold. Yet, for the sake
  173. of efficiency, we provide these common operations as intrinsics.
  174.  
  175. [def transform x f] [/ For a list x and function f, transform returns a new
  176. list with elements created by applying [f e] to each
  177. element e of x. ]
  178.  
  179. [/ Since strings are just special forms of lists, all functions that accept
  180. lists can also accept strings.
  181. ]
  182.  
  183. [def + a b] [/ Add a and b. both a and b are numeric integer strings. ]
  184. [def - a b] [/ Subract a and b. both a and b are numeric integer strings. ]
  185. [def * a b] [/ Multiply a and b. both a and b are numeric integer strings. ]
  186. [def / a b] [/ Divide a and b. both a and b are numeric integer strings. ]
  187. [def % a b] [/ Remainder of a / b. both a and b are numeric integer strings. ]
  188.  
  189. [def == a b] [/ Returns 1 if a == b. ]
  190. [def < a b] [/ Returns 1 if a is less than b. ]
  191. [def && a b] [/ Returns a and b (boolean logic). ]
  192. [def || a b] [/ Returns a or b (boolean logic). ]
  193. [def if cond then else] [/ Returns then if cond is 1, otherwise returns else. ]
  194.  
  195. Other comparisons and booleans can be synthesized from the basics above.
  196. For example ! can be defined as [def ! x][[if [x][0][1]]], != can be defined
  197. as [def != a b][[![==[a][b]]]], etc.
  198.  
  199. [def >> file] [/ Loads file. ]
  200. [def << file x] [/ Saves x to file. ]
  201.  
  202. >> processes files (i.e. markups are significant). If you want to load
  203. files verbatim, enclose it in the escape markup. Example:
  204.  
  205. [""[>> text.gia]""]
  206.  
  207. Lambda Functions:
  208.  
  209. Functions are first class and can be passed and returned from other functions.
  210. For example, the transform intrinsic requires a lambda function for its
  211. last (f) argument.
  212.  
  213. Here's an example of a lambda function:
  214.  
  215. [lambda x][dup [x]]
  216.  
  217. Its syntax resembles a function definition.
  218.  
  219. Here's a sample invocation of transform:
  220.  
  221. [transform [list] [[lambda x][dup [x]]]]
  222.  
  223. This doubles all elements of the list.
  224.  
  225. Error conditions (todo):
  226.  
  227.  
  228.  
  229.  
Advertisement
Add Comment
Please, Sign In to add comment