Advertisement
djowel

Gaea text template language

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