43 lines
1.1 KiB
Lua
43 lines
1.1 KiB
Lua
-- A guided tour of Lua running on calog.
|
|
-- Demonstrates: local variables, a function, a numeric for loop building a
|
|
-- table, iterating that table, then cryptoHashSha256 and jsonStringify natives.
|
|
-- Run: bin/calog examples/scripts/languages/lua.lua
|
|
|
|
-- A local variable.
|
|
local squareCount = 6
|
|
|
|
-- A function.
|
|
local function square(n)
|
|
return n * n
|
|
end
|
|
|
|
-- A numeric for loop that builds a table of squares.
|
|
local squares = {}
|
|
for i = 1, squareCount do
|
|
squares[i] = square(i)
|
|
end
|
|
|
|
-- Iterate the table and print each element.
|
|
local total = 0
|
|
for i, v in ipairs(squares) do
|
|
calogPrint("square of", i, "is", v)
|
|
total = total + v
|
|
end
|
|
calogPrint("sum of squares 1..6 =", total)
|
|
|
|
-- Hash a string with the crypto native.
|
|
local message = "calog loves Lua"
|
|
local digest = cryptoHashSha256(message)
|
|
calogPrint("sha256(" .. message .. ") =", digest)
|
|
|
|
-- Build a small record and serialize it as JSON.
|
|
local record = {
|
|
language = "lua",
|
|
version = "5.4",
|
|
squares = squares,
|
|
total = total,
|
|
digest = digest
|
|
}
|
|
calogPrint("json:", jsonStringify(record))
|
|
|
|
calogExit(0)
|