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.

411 lines
6.4 KiB

  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('programs/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. bs = b:get(buffer.type.ptr, buffer.sizeof.int*2 + (buffer.sizeof.ptr))
  242. print('...string buffer:', bs)
  243. print('...with value:', bs:get(buffer.type.cstr))
  244. print('--- IO redirection')
  245. oldstdout = io.stdout
  246. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  247. print('A line!')
  248. print('An object:', {a=1, b=2, c="turkey"})
  249. print('Something mysterious :o')
  250. io.stdout:write('Writing directly to a file :D')
  251. io.stdout:flush()
  252. io.stdout = oldstdout
  253. print('...restored stdout.')
  254. f = io.open('stdout', io.MODE_READ)
  255. s = f:read(io.ALL)
  256. print('Buffered output was:')
  257. prepr(s)
  258. f = None
  259. print('...second time.')
  260. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  261. print('Hey there!')
  262. print('l'+'ol'*32)
  263. io.stdout:flush()
  264. io.stdout = oldstdout
  265. print('...restored.')
  266. print('Output was:')
  267. prepr(io.open('stdout', io.MODE_READ):read(io.ALL))
  268. print('--- Substrings')
  269. s = 'This is a test!'
  270. prepr(s)
  271. prepr(s:sub(1, -1))
  272. prepr(s:sub(3, -3))
  273. prepr(s:sub(3, 5))
  274. prepr(s:sub(3, 11))
  275. prepr(s:sub(-1000, -1000))
  276. print('--- Splitting')
  277. s = 'This is a test!'
  278. prepr(s)
  279. prepr(s:split(' '))
  280. prepr(s:split('i'))
  281. prepr(s:split('0'))
  282. prepr(s:split('aeiou'))
  283. l = s:split(' ')
  284. for i in l do
  285. prepr(i, type(i))
  286. end
  287. print('--- Continue/break')
  288. l = range(10)
  289. for i in l do
  290. print(i)
  291. if i >= 5 then break end
  292. end
  293. print('---')
  294. for i in l do
  295. if i%2 == 0 then continue end
  296. print(i)
  297. end
  298. print('--- All done!')