1-5 and part of 6
This commit is contained in:
parent
5e6763ed63
commit
0263e5fae0
2 changed files with 288 additions and 0 deletions
5
src/lines.txt
Normal file
5
src/lines.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
linea1
|
||||||
|
linea2
|
||||||
|
linea3
|
||||||
|
linea4
|
||||||
|
linea5
|
||||||
283
src/main.lua
Normal file
283
src/main.lua
Normal file
|
|
@ -0,0 +1,283 @@
|
||||||
|
local dbg = require 'debugger'
|
||||||
|
-- Func asand type test
|
||||||
|
-- print("hello world")
|
||||||
|
|
||||||
|
-- function factos (n)
|
||||||
|
-- -- dbg()
|
||||||
|
-- if n == 0 then
|
||||||
|
-- return 1
|
||||||
|
-- else
|
||||||
|
-- return n * factos(n - 1)
|
||||||
|
-- end
|
||||||
|
-- end
|
||||||
|
|
||||||
|
-- print("Enter a number: ")
|
||||||
|
-- a = io.read("*number")
|
||||||
|
-- print(factos(a))
|
||||||
|
-- print(type(factos))
|
||||||
|
-- print(type(false))
|
||||||
|
|
||||||
|
print("String test")
|
||||||
|
asciicore = "alo123\thola\n"
|
||||||
|
asciicore2 = "\97lo\9hola\49 \10"
|
||||||
|
io.write(asciicore); print(asciicore2);
|
||||||
|
paragraphcore = [[
|
||||||
|
increible
|
||||||
|
la maravilla
|
||||||
|
interpretada ]]
|
||||||
|
print(paragraphcore)
|
||||||
|
print(10 .. 20) --string conversion, concat
|
||||||
|
print(10 + 20) --int sum, string conversion
|
||||||
|
print(10 == "10")
|
||||||
|
|
||||||
|
print("Table test")
|
||||||
|
a = {} -- create a table and store its reference in `a'
|
||||||
|
k = "x"
|
||||||
|
a[k] = 10 -- new entry, with key="x" and value=10
|
||||||
|
a[20] = "great" -- new entry, with key=20 and value="great"
|
||||||
|
print(a["x"]) --> 10
|
||||||
|
k = 20
|
||||||
|
print(a[k]) --> "great"
|
||||||
|
a["x"] = a["x"] + 1 -- increments entry "x"
|
||||||
|
print(a["x"]) --> 11
|
||||||
|
print()
|
||||||
|
|
||||||
|
print(a["z"])
|
||||||
|
a["z"] = nil
|
||||||
|
print(a["z"])
|
||||||
|
a = nil
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Convention: tables in lua are usually indexed starting with 1, the stdlib follows this")
|
||||||
|
arr = {}
|
||||||
|
for i=0,9 do
|
||||||
|
arr[i] = i*2
|
||||||
|
end
|
||||||
|
for i=0,9 do
|
||||||
|
print(arr[i])
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
print("Convention: using nil as table end. ipairs follows this")
|
||||||
|
arr[10] = nil
|
||||||
|
arr[11] = 34
|
||||||
|
for i,number in ipairs(arr) do
|
||||||
|
print(number)
|
||||||
|
end
|
||||||
|
arr = nil
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Array indexing doesn't perform any type conversion")
|
||||||
|
i = 10; j = "10"; k = "+10"
|
||||||
|
a = {}
|
||||||
|
a[i] = "one value"
|
||||||
|
a[j] = "another value"
|
||||||
|
a[k] = "yet another value"
|
||||||
|
print(a[i]) --> one value
|
||||||
|
print(a[j]) --> another value
|
||||||
|
print(a[k]) --> yet another value
|
||||||
|
print(a[tonumber(j)]) --> one value
|
||||||
|
print(a[tonumber(k)]) --> one value
|
||||||
|
a = nil
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Logical operators test")
|
||||||
|
v = 3
|
||||||
|
--x = 4
|
||||||
|
x = false -- boolean, while a value, is evaluated as it should and replaced by the expression:
|
||||||
|
print(type(x))
|
||||||
|
if not x then x = v end
|
||||||
|
print(tostring(x))
|
||||||
|
print(type(x).."\n")
|
||||||
|
|
||||||
|
|
||||||
|
print(nil and false)
|
||||||
|
print(4 and false)
|
||||||
|
print(4 and 5)
|
||||||
|
if (4 and false) then
|
||||||
|
print("si")
|
||||||
|
else
|
||||||
|
print("no")
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
|
||||||
|
val = v and 7 or 14
|
||||||
|
print(val)
|
||||||
|
v = false
|
||||||
|
val = v and 7 or 14
|
||||||
|
print(val)
|
||||||
|
val = (v == false) and 7 or 14
|
||||||
|
print(val)
|
||||||
|
val = (v ~= false) and 7 or 14
|
||||||
|
print(val)
|
||||||
|
v = nil; print()
|
||||||
|
|
||||||
|
print("Operator precedence")
|
||||||
|
x = 3
|
||||||
|
print(x^3)
|
||||||
|
|
||||||
|
print("Tables")
|
||||||
|
local math = require 'math'
|
||||||
|
tab = {math.sin(1), math.sin(2), math.cos(3)}
|
||||||
|
print(tab[1] .. " " .. tab[3])
|
||||||
|
|
||||||
|
print("Using table to implement linked list")
|
||||||
|
list = nil
|
||||||
|
for line in io.lines("lines.txt") do
|
||||||
|
list = {next=list, value=line}
|
||||||
|
print(tostring(list.next) .. " " .. list.value)
|
||||||
|
end
|
||||||
|
|
||||||
|
l = list
|
||||||
|
while l do
|
||||||
|
print(l.value)
|
||||||
|
l = l.next
|
||||||
|
end
|
||||||
|
l = nil; list = nil; value = nil;
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Mixing initializations and constructing tables within tables")
|
||||||
|
polyline = {color="blue", thickness=2, npoints=4,
|
||||||
|
{x=0, y=0},
|
||||||
|
{x=-10, y=0},
|
||||||
|
{x=-10, y=1},
|
||||||
|
{x=0, y=1}
|
||||||
|
}
|
||||||
|
|
||||||
|
print(polyline[1])
|
||||||
|
print(polyline[1].x)
|
||||||
|
print(polyline.color)
|
||||||
|
print(polyline[2])
|
||||||
|
print(polyline[2].x)
|
||||||
|
polyline = nil
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Explicitly writing index as an expr")
|
||||||
|
opnames = {["+"] = "add", ["-"] = "sub",
|
||||||
|
["*"] = "mul", ["/"] = "div"}
|
||||||
|
|
||||||
|
i = 20; s = "-"
|
||||||
|
a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}
|
||||||
|
|
||||||
|
print(opnames[s]) --> sub
|
||||||
|
print(a[22]) --> ---
|
||||||
|
a = nil; i = nil; opnames = nil;
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Assignments")
|
||||||
|
x = 1; y = 2;
|
||||||
|
x, y, c = y, x --[[Variables are first evalueated and then swapped. Extra var is set to nil, extra value is discarded.
|
||||||
|
Always 1 value per var, no multi assignments]]
|
||||||
|
print(x .. " " .. y)
|
||||||
|
x = nil; y = nil
|
||||||
|
|
||||||
|
print("Local variables: chunk, func or control block. On repl")
|
||||||
|
local a = 3; print(a) --Would print 3
|
||||||
|
local a = 3
|
||||||
|
print(a) --Would print nil on repl since we're not anymore at the scope where a was declared. Use do-end (equiv to { }, scope delimiters)!
|
||||||
|
local a = nil
|
||||||
|
print()
|
||||||
|
|
||||||
|
print("Control structures: numeric for")
|
||||||
|
for i=1,4,2 do --initial value, max value, increment(optional = 1)
|
||||||
|
print(i)
|
||||||
|
end
|
||||||
|
|
||||||
|
function f()
|
||||||
|
return 3
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
|
||||||
|
for i=1,f() do
|
||||||
|
print(i)
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
f = nil
|
||||||
|
|
||||||
|
print("Control structures: generic for (foreach)")
|
||||||
|
a = {1, 2}
|
||||||
|
for i, v in ipairs(a) do print(i .. " " .. v) end
|
||||||
|
a = {x=1, y=2}
|
||||||
|
for i, v in pairs(a) do print(i .. " " .. v) end
|
||||||
|
for v in pairs(a) do print(v) end --only print keys
|
||||||
|
a = nil; print()
|
||||||
|
|
||||||
|
print("Functions")
|
||||||
|
--No strict control of number of parameters passed to func (extra params = discarded, missing params = vars set to nil)
|
||||||
|
function f(x, y, z)
|
||||||
|
z = z or 1
|
||||||
|
y = y or 1
|
||||||
|
x = x or 1
|
||||||
|
return x + y + z
|
||||||
|
end
|
||||||
|
print(f(2)) --4
|
||||||
|
print(f(2,3)) --6
|
||||||
|
print(f(2,3,4)) --9
|
||||||
|
print(f(2,3,4,9)) --9
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--Tables can be used as references for the content within, although with multiple retvals this should be seldomly used
|
||||||
|
function ft(a)
|
||||||
|
if (type(a) ~= "table") then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
a[1] = 3
|
||||||
|
return
|
||||||
|
end
|
||||||
|
a = {6}
|
||||||
|
ft(a); print(a[1])
|
||||||
|
print()
|
||||||
|
|
||||||
|
--Functions only return multiple values if they're the single or last expression:
|
||||||
|
function multireturn(a, b)
|
||||||
|
return a, b
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local a, b, c = multireturn(1, 2)
|
||||||
|
print(tostring(a) .. " " .. tostring(b) .. " " .. tostring(c))
|
||||||
|
local a, b, c = 10, multireturn(1, 2)
|
||||||
|
print(tostring(a) .. " " .. tostring(b) .. " " .. tostring(c))
|
||||||
|
local a, b, c = multireturn(1, 2), 10
|
||||||
|
print(tostring(a) .. " " .. tostring(b) .. " " .. tostring(c)) --See how c is nil? We only kept the function's first retval
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
|
||||||
|
--Same thing happens when expr is part of another function call (args)
|
||||||
|
print(multireturn(1,2))
|
||||||
|
print(10, multireturn(1,2))
|
||||||
|
print(multireturn(1,2), 10)
|
||||||
|
print(multireturn(1,2) .. 10)
|
||||||
|
print(10 .. multireturn(1,2)) --And when it's a part of a expression, it always truncates extra retvals
|
||||||
|
print()
|
||||||
|
|
||||||
|
--Variadic functions
|
||||||
|
function nsum(...)
|
||||||
|
local nsum = 0
|
||||||
|
for i,v in ipairs(arg) do
|
||||||
|
nsum = nsum + v
|
||||||
|
end
|
||||||
|
print(nsum .. "\n")
|
||||||
|
--[[Equivalent to
|
||||||
|
for i = 1,arg.n do
|
||||||
|
nsum = nsum + arg[i]
|
||||||
|
end
|
||||||
|
print(nsum .. "\n")
|
||||||
|
]]
|
||||||
|
|
||||||
|
for i,v in pairs(arg) do
|
||||||
|
print(i .. " " .. v)
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
return nsum
|
||||||
|
end
|
||||||
|
|
||||||
|
print(nsum(2,3,4))
|
||||||
|
|
||||||
|
--TODO: Re-read chapter 5
|
||||||
|
|
||||||
|
print("Function anonimity demonstration")
|
||||||
|
foo = function (x)
|
||||||
|
return x, 2*x
|
||||||
|
end
|
||||||
|
print(foo(2))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue