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.

419 lines
6.7 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. print('(removed from core language)')
  132. --exec('print("Hello from exec!")')
  133. --print(eval('5 + 3'))
  134. --execfile('programs/subtest.sol')
  135. print('--- Modulus')
  136. print(5%3)
  137. print(13%5)
  138. print(15%15)
  139. print('--- Special function manipulation')
  140. func foo(x)
  141. return x
  142. end
  143. print(foo)
  144. foo.name = "bar"
  145. print(foo)
  146. func something()
  147. return i
  148. end
  149. something.closure = {i=[1, 2, 3]}
  150. print(something())
  151. cl = something.closure
  152. cl.i:insert(0, "b")
  153. print(something())
  154. print('--- Function body swapping')
  155. func a()
  156. return 0
  157. end
  158. func b()
  159. return 2
  160. end
  161. print(a, a())
  162. print(b, b())
  163. print(a.stmt)
  164. print(b.stmt)
  165. temp = a.stmt
  166. a.stmt = b.stmt
  167. b.stmt = temp
  168. print(a, a())
  169. print(b, b())
  170. newbody = parse('return 4')
  171. print(newbody)
  172. a.stmt = newbody
  173. b.stmt = newbody
  174. print(a, a())
  175. print(b, b())
  176. print('--- More complicated ASTs')
  177. print(outer, outer.stmt, outer.stmt.stmtlist)
  178. print('--- Exec- and eval-by-parse')
  179. parse('print("Hello from parse()!")')()
  180. print(parse('5 + 3').stmtlist[0].expr())
  181. print('--- Mutating ASTs')
  182. func f()
  183. return 5 + 7
  184. end
  185. print(f, f.stmt, f())
  186. f.stmt.stmtlist[0].ret.right.ival = 11
  187. print(f, f.stmt, f())
  188. func g()
  189. a=1
  190. b=2
  191. print("a=", a, ", b=", b)
  192. end
  193. print(g, g.stmt, g())
  194. g.stmt.stmtlist[1].expr.value = parse('a').stmtlist[0].expr
  195. print(g, g.stmt, g())
  196. print('--- AST Environments')
  197. code = parse('print("a is", a, "and b is", b"); a = 4; b = 5')
  198. print(code)
  199. code()
  200. d = {a=1, b=2}
  201. print(d)
  202. code(d)
  203. print(d)
  204. e = {a="hello", b=["world"]}
  205. print(e)
  206. code(e)
  207. print(e)
  208. e = {a=1, b=2}
  209. d = {__index = e}
  210. print(d)
  211. print(e)
  212. code(d)
  213. print(d)
  214. print(e)
  215. print('--- Basic buffers')
  216. print('(buffer.fromstring = ', buffer.fromstring, ')')
  217. b = buffer.fromstring("Hello, world!")
  218. print(b)
  219. print('(b.get = ', b.get, ')')
  220. print(b:get(buffer.type.cstr))
  221. b:set(buffer.type.char, "Q")
  222. b:set(buffer.type.char, "L", 2)
  223. print(b:get(buffer.type.cstr))
  224. print(b:get(buffer.type.cstr, 5))
  225. print(b:get(buffer.type.uint32))
  226. --b:set(buffer.type.double, 1.243416560929)
  227. b:set(buffer.type.uint32, 1886545252)
  228. print(b:get(buffer.type.uint32))
  229. print(b:get(buffer.type.cstr))
  230. q = buffer.fromaddress(b:address(), b:size())
  231. print(q:get(buffer.type.cstr))
  232. q:set(buffer.type.cstr, "Goodbye!")
  233. print(q:get(buffer.type.cstr), b:get(buffer.type.cstr))
  234. s = "A string!"
  235. b = buffer.fromobject(s)
  236. prepr(s)
  237. print('...is a', buffer.objtype[b:get(buffer.type.int, 0)])
  238. print('(buffer.sizeof.ptr = ', buffer.sizeof.ptr, ')')
  239. print('(buffer.sizeof.int = ', buffer.sizeof.int, ')')
  240. print('(buffer.sizeof.int*2 = ', buffer.sizeof.int*2, ')')
  241. print('(buffer.sizeof.int*2 + buffer.sizeof.ptr = ', buffer.sizeof.int*2 + (buffer.sizeof.ptr), ')')
  242. --bs = b:get(buffer.type.ptr, buffer.sizeof.int*2 + (buffer.sizeof.ptr))
  243. --print('...string buffer:', bs)
  244. --print('...with value:', bs:get(buffer.type.cstr))
  245. print('--- IO redirection')
  246. oldstdout = io.stdout
  247. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  248. print('A line!')
  249. print('An object:', {a=1, b=2, c="turkey"})
  250. print('Something mysterious :o')
  251. io.stdout:write('Writing directly to a file :D')
  252. io.stdout:flush()
  253. io.stdout = oldstdout
  254. print('...restored stdout.')
  255. f = io.open('stdout', io.MODE_READ)
  256. s = f:read(io.ALL)
  257. print('Buffered output was:')
  258. prepr(s)
  259. f = None
  260. print('...second time.')
  261. io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
  262. print('Hey there!')
  263. print('l'+'ol'*32)
  264. io.stdout:flush()
  265. io.stdout = oldstdout
  266. print('...restored.')
  267. print('Output was:')
  268. prepr(io.open('stdout', io.MODE_READ):read(io.ALL))
  269. print('--- Substrings')
  270. s = 'This is a test!'
  271. prepr(s)
  272. prepr(s:sub(1, -1))
  273. prepr(s:sub(3, -3))
  274. prepr(s:sub(3, 5))
  275. prepr(s:sub(3, 11))
  276. prepr(s:sub(-1000, -1000))
  277. print('--- Splitting')
  278. s = 'This is a test!'
  279. prepr(s)
  280. prepr(s:split(' '))
  281. prepr(s:split('i'))
  282. prepr(s:split('0'))
  283. prepr(s:split('aeiou'))
  284. l = s:split(' ')
  285. for i in l do
  286. prepr(i, type(i))
  287. end
  288. print('--- Continue/break')
  289. l = range(10)
  290. for i in l do
  291. print(i)
  292. if i >= 5 then break end
  293. end
  294. print('---')
  295. for i in l do
  296. if i%2 == 0 then continue end
  297. print(i)
  298. end
  299. print('--- Control expressions')
  300. print('1 is', if 1 then 'true' else 'false' end)
  301. print('0 is', if 0 then 'true' else 'false' end)
  302. print(for i in l do continue 1000 + 2 * i end)
  303. print(for i in l do print('(', i, ')') if i >= 5 then break i end end)
  304. print('--- All done!')