summaryrefslogtreecommitdiff
path: root/c-embed-lua/hi.lua
blob: fb6621c679ae94e4744f0eeab43a89ec2f816d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local NL = "\n"    -- New line character
local HW = 70      -- Header width

local function header(title)
	print(NL .. string.rep("*", HW))
	print(string.format(" > %s", title))
	print(string.rep("*", HW) .. NL)
end

-- Loops and tables.

header("Loops and tables")

for i=1, 5 do
	print("- Loop with index " .. i)
end

local names = {
	"John",
	"Bob",
	"Grug",
}

for _, name in pairs(names) do
	print(string.format("* This create is called %s.", name))
end

-- Math stuff.

header("Math stuff")

print("Square root of 16:", math.sqrt(16))
print("Absolute value of -7:", math.abs(-7))
print("Ceiling of 2.3:", math.ceil(2.3))
print("Floor of 2.9:", math.floor(2.9))
print("Rounding 3.6:", math.floor(3.6 + 0.5))

print("Cosine of 0:", math.cos(0))
print("Sine of 90 degrees:", math.sin(math.rad(90)))

math.randomseed(os.time())
print("Random number (1-100):", math.random(1, 100))

print("2 to the power of 3:", math.pow(2, 3))
print("Natural log of 2.71828:", math.log(2.71828))
print("Base-10 log of 1000:", math.log10(1000))

print("Pi:", math.pi)
print("Convert 180 degrees to radians:", math.rad(180))
print("Convert Pi radians to degrees:", math.deg(math.pi))

-- Coroutines.

header("Coroutines")

function producer()
	return coroutine.create(function()
		for i = 1, 3 do
			print("Producing:", i)
			coroutine.yield(i)
		end
	end)
end

function consumer(prod)
	while true do
		local status, value = coroutine.resume(prod)
		if not status or value == nil then break end
		print("Consumed:", value)
	end
end

co = producer()
consumer(co)

function sneaky()
	print("Coroutine starts")
	coroutine.yield()  -- Pause and give control back.
	print("Coroutine resumes after yield")
end

co = coroutine.create(sneaky)

print("First resume:")
coroutine.resume(co)

print("Second resume:")
coroutine.resume(co)

-- IO stuff.

header("File IO")

print("Reading file adams.txt...\n")

local file = io.open("adams.txt", "r")
if not file then
	print("Failed to open file.")
	return
end

for line in file:lines() do
	print(line)
end

file:close()