fork download
  1. print("Hello World") --interpreted immediately
  2.  
  3. function dostuff() print("stuff"); end --basic function
  4.  
  5. fact = function(n) --all functions are function pointers in the global namespace
  6. if n == 0 then
  7. return 1
  8. else
  9. return n * fact(n-1)
  10. end
  11. end
  12. print(fact(3)) --interpreted, so we can call whenever we want
  13.  
  14. a = {} -- here's an object
  15. a.memberfunction = function() print("stuff") end; --now it has a member function
  16. b = a -- objects are pointers/references like in java, these are two references to the same object
  17. a[3] = "HI" -- objects/arrays/hashtables are one and the same
  18. a["memberfunction"]() --calls the function
  19. a.memberfunction() --also calls the function
  20. print(a[3])
  21. print(a[3].."WORLD") --concatenation is different than other languages, but simple
  22.  
  23. --c = a + b --error since objects/tables can't be added
  24. metatable = {}
  25. setmetatable(a, metatable ) --now a has a new metatable, used for operator overloading
  26. metatable.__add = function(left, right) print("ADDITION") return left end --now we have an add operator
  27. c = a + b --so now a can be added to things
  28. print(a)
  29. metatable.__tostring = function() return "A" end --now tostring will be more than the address
  30. print(a)
  31.  
  32.  
  33.  
  34.  
  35.  
Success #stdin #stdout 0.01s 2540KB
stdin
Standard input is empty
stdout
Hello World
6
stuff
stuff
HI
HIWORLD
ADDITION
table: 0x9fe9090
A