The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.sol 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. print('--- Empty functions')
  2. func f() end
  3. print(f())
  4. print('--- While loop')
  5. a = 1
  6. while a < 10 do
  7. print(a)
  8. a += 1
  9. end
  10. print("--- Range")
  11. func mul9(b)
  12. for i in range(#b) do
  13. b[i] *= 9
  14. end
  15. end
  16. l = [1 2 3 4 5]
  17. print(range(#l))
  18. print("--- Iter list")
  19. for i in l do print(i) end
  20. print("--- Index list")
  21. for i in range(#l) do print(i, l[i]) end
  22. print('--- mul9')
  23. mul9(l)
  24. for i in l do print(i) end
  25. print('--- Iter mul9')
  26. for i in range(#l) do print(i, l[i]) end
  27. print("--- Mapgen")
  28. PI = 3.14159265358979
  29. d = {
  30. integer = 1
  31. string = "hello"
  32. submap = {
  33. stamina = 100
  34. health = 42.0
  35. }
  36. sublist = [1 1 2 3 5 8],
  37. ["this time with spaces"] = PI*2,
  38. [1 + 5] = 1 + 5,
  39. [1 + 9] = 1 + 9
  40. }
  41. print(d)
  42. print('--- Map iter')
  43. for i in d do print(i, d[i]) end
  44. print('--- try')
  45. func bad(x)
  46. print(x)
  47. return x.c()
  48. end
  49. test1 = {c = func() return 15 end}
  50. test2 = {}
  51. print(try(bad, test1))
  52. print(try(bad, test2))
  53. print(bad(test1))
  54. --print(bad(test2))
  55. print('--- Induced errors')
  56. func raise(x)
  57. error(x)
  58. end
  59. print(try(raise, "lp0 on fire"))
  60. print('--- Indexing')
  61. print(d["integer"])
  62. print(d.integer)
  63. d.integer += 5
  64. print(d.integer)
  65. print('--- Function binding')
  66. func outer(a)
  67. func inner(b)
  68. return a+b
  69. end
  70. inner.closure.a = a
  71. return inner
  72. end
  73. i = outer(5)
  74. print(i(3), i(4), i(5))
  75. j = outer(8)
  76. print(j(3), j(4), j(5))
  77. print('--- Iterators')
  78. func myiter()
  79. if i>10 then return StopIteration end
  80. i+=1
  81. return i-1
  82. end
  83. myiter.closure.i = 1
  84. for i in myiter do print(i) end
  85. print('--- Method calls')
  86. d = {a = func(a, b) print(a, b) end}
  87. d.a(1, 2)
  88. d:a(3)
  89. print('--- Special methods')
  90. d = {__index = func(obj, key) print('Index', obj, key) return key end,
  91. __setindex = func(obj, key, val) print('SetIndex', obj, key, val) end,
  92. __call = func(obj, arg1, arg2) print('Call', obj, arg1, arg2) return arg1 end}
  93. print(d[3], d[5])
  94. d.a = 7
  95. print(d("q", "r"))
  96. e = {a=1, b=2}
  97. d = {__index = e, __setindex = e}
  98. print(d, d.a, d.b)
  99. d.c = 5
  100. d.b = 7
  101. print(d, e)
  102. print('--- Data sharing')
  103. d = {}
  104. e = [1 2 3 4 5]
  105. d.a = e
  106. d.b = e
  107. print(d)
  108. e[2]=7
  109. e[3]="c"
  110. print(d)
  111. d.a:insert(#(d.a), "q")
  112. d.b:remove(1)
  113. d.a[3]="f"
  114. print(d)
  115. print('--- Arithmetic structure operations')
  116. print('ab'+'cd')
  117. print('l'+'ol'*32)
  118. print([1 2 3]+[4 5])
  119. print([1 2 3]*5)
  120. print({a=1 b=2}+{c=3})
  121. print('--- Map/filter')
  122. l = [1 2 3 4 5]
  123. print(l)
  124. l:map(func (i) return i*3 end)
  125. print(l)
  126. l:filter(func (i) return i & 1 end)
  127. print(l)
  128. print('--- Map/filter chain')
  129. print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))
  130. print('--- Exec/eval')
  131. exec('print("Hello from exec!")')
  132. print(eval('5 + 3'))
  133. execfile('subtest.sol')
  134. print('--- Modulus')
  135. print(5%3)
  136. print(13%5)
  137. print(15%15)
  138. print('--- Special function manipulation')
  139. func foo(x)
  140. return x
  141. end
  142. print(foo)
  143. foo.name = "bar"
  144. print(foo)
  145. func something()
  146. return i
  147. end
  148. something.closure = {i=[1, 2, 3]}
  149. print(something())
  150. cl = something.closure
  151. cl.i:insert(0, "b")
  152. print(something())
  153. print('--- Function body swapping')
  154. func a()
  155. return 0
  156. end
  157. func b()
  158. return 2
  159. end
  160. print(a, a())
  161. print(b, b())
  162. print(a.stmt)
  163. print(b.stmt)
  164. temp = a.stmt
  165. a.stmt = b.stmt
  166. b.stmt = temp
  167. print(a, a())
  168. print(b, b())
  169. newbody = parse('return 4')
  170. print(newbody)
  171. a.stmt = newbody
  172. b.stmt = newbody
  173. print(a, a())
  174. print(b, b())
  175. print('--- More complicated ASTs')
  176. print(outer, outer.stmt, outer.stmt.stmtlist)
  177. print('--- Exec- and eval-by-parse')
  178. parse('print("Hello from parse()!")')()
  179. print(parse('5 + 3').stmtlist[0].expr())
  180. print('--- Mutating ASTs')
  181. func f()
  182. return 5 + 7
  183. end
  184. print(f, f.stmt, f())
  185. f.stmt.stmtlist[0].ret.right.ival = 11
  186. print(f, f.stmt, f())
  187. func g()
  188. a=1
  189. b=2
  190. print("a=", a, ", b=", b)
  191. end
  192. print(g, g.stmt, g())
  193. g.stmt.stmtlist[1].expr.value = parse('a').stmtlist[0].expr
  194. print(g, g.stmt, g())
  195. print('--- AST Environments')
  196. code = parse('print("a is", a, "and b is", b"); a = 4; b = 5')
  197. print(code)
  198. code()
  199. d = {a=1, b=2}
  200. print(d)
  201. code(d)
  202. print(d)
  203. e = {a="hello", b=["world"]}
  204. print(e)
  205. code(e)
  206. print(e)
  207. e = {a=1, b=2}
  208. d = {__index = e}
  209. print(d)
  210. print(e)
  211. code(d)
  212. print(d)
  213. print(e)
  214. print('--- Basic buffers')
  215. print('(buffer.fromstring = ', buffer.fromstring, ')')
  216. b = buffer.fromstring("Hello, world!")
  217. print(b)
  218. print('(b.get = ', b.get, ')')
  219. print(b:get(buffer.type.cstr))
  220. b:set(buffer.type.char, "Q")
  221. b:set(buffer.type.char, "L", 2)
  222. print(b:get(buffer.type.cstr))
  223. print(b:get(buffer.type.cstr, 5))
  224. print(b:get(buffer.type.uint32))
  225. --b:set(buffer.type.double, 1.243416560929)
  226. b:set(buffer.type.uint32, 1886545252)
  227. print(b:get(buffer.type.uint32))
  228. print(b:get(buffer.type.cstr))
  229. q = buffer.fromaddress(b:address(), b:size())
  230. print(q:get(buffer.type.cstr))
  231. q:set(buffer.type.cstr, "Goodbye!")
  232. print(q:get(buffer.type.cstr), b:get(buffer.type.cstr))
  233. s = "A string!"
  234. b = buffer.fromobject(s)
  235. prepr(s)
  236. print('...is a', buffer.objtype[b:get(buffer.type.int, 0)])
  237. print('(buffer.sizeof.ptr = ', buffer.sizeof.ptr, ')')
  238. print('(buffer.sizeof.int = ', buffer.sizeof.int, ')')
  239. print('(buffer.sizeof.int*2 = ', buffer.sizeof.int*2, ')')
  240. print('(buffer.sizeof.int*2 + buffer.sizeof.ptr = ', buffer.sizeof.int*2 + (buffer.sizeof.ptr), ')')
  241. print('...with value:', b:get(buffer.type.ptr, buffer.sizeof.int*2 + (buffer.sizeof.ptr)):get(buffer.type.cstr))
  242. print('--- IO redirection')
  243. oldstdout = io.stdout
  244. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  245. print('A line!')
  246. print('An object:', {a=1, b=2, c="turkey"})
  247. print('Something mysterious :o')
  248. io.stdout:write('Writing directly to a file :D')
  249. io.stdout:flush()
  250. io.stdout = oldstdout
  251. print('...restored stdout.')
  252. f = io.open('stdout', io.MODE_READ)
  253. s = f:read(io.ALL)
  254. print('Buffered output was:')
  255. prepr(s)
  256. f = None
  257. print('...second time.')
  258. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  259. print('Hey there!')
  260. print('l'+'ol'*32)
  261. io.stdout:flush()
  262. io.stdout = oldstdout
  263. print('...restored.')
  264. print('Output was:')
  265. prepr(io.open('stdout', io.MODE_READ):read(io.ALL))
  266. print('--- Substrings')
  267. s = 'This is a test!'
  268. prepr(s)
  269. prepr(s:sub(1, -1))
  270. prepr(s:sub(3, -3))
  271. prepr(s:sub(3, 5))
  272. prepr(s:sub(3, 11))
  273. prepr(s:sub(-1000, -1000))
  274. print('--- Splitting')
  275. s = 'This is a test!'
  276. prepr(s)
  277. prepr(s:split(' '))
  278. prepr(s:split('i'))
  279. prepr(s:split('0'))
  280. prepr(s:split('aeiou'))
  281. l = s:split(' ')
  282. for i in l do
  283. prepr(i, type(i))
  284. end
  285. print('--- All done!')