github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/basic.lua (about)

     1  print("hello, world!")
     2  --> =hello, world!
     3  
     4  print(1+2)
     5  --> =3
     6  
     7  print(1 == 1.0)
     8  --> =true
     9  
    10  print(1, 2)
    11  --> =1	2
    12  
    13  print("hello," .. " " .. "world!")
    14  --> =hello, world!
    15  
    16  local function max(x, y)
    17    if x > y then
    18      return x
    19    end
    20    return y
    21  end
    22  print(max(2, 3), max(3, 2))
    23  --> =3	3
    24  
    25  local function sum(n)
    26      local s = 0
    27      for i = 1,n do
    28          s = s + i
    29      end
    30      return s
    31  end
    32  print(sum(10))
    33  --> =55
    34  
    35  local function fac(n)
    36    if n == 0 then
    37      return 1
    38    end
    39    return n * fac(n-1)
    40  end
    41  print(fac(10))
    42  --> =3628800
    43  
    44  local function twice(f)
    45    return function(x)
    46      return f(f(x))
    47    end
    48  end
    49  local function square(x)
    50    return x*x
    51  end
    52  print(twice(square)(2)) -- (2 ^ 2) ^ 2
    53  --> =16
    54  
    55  local function p(...)
    56    print(">>>", ...)
    57  end
    58  p(1, 2, 3)
    59  --> =>>>	1	2	3
    60  
    61  local function g(x)
    62    error(x .. "ld!", 0)
    63  end
    64  local function f(x)
    65      g(x .. ", wor")
    66  end
    67  print(pcall(f, "hello"))
    68  --> =false	hello, world!
    69  
    70  local function f()
    71      return 1, 2
    72  end
    73  local x, y
    74  x, y = f()
    75  print(x + y)
    76  --> =3
    77  
    78  print(0xffffffffffffffffffffffffffffff)
    79  --> =-1
    80  
    81  error("hello")
    82  --> ~^!!! runtime: