Lua

From TRCCompSci - AQA Computer Science
Revision as of 09:26, 3 June 2019 by Admin (talk | contribs) (Conditions)
Jump to: navigation, search

Comments

-- Two dashes start a one-line comment.

--[[
     Adding two ['s and ]'s makes it a
     multi-line comment.
--]]

Write to Console

print("helloworld")
io.write('helloworld')

Escape Characters

io.write('helloworld\n')

Read from Console

input = io.read()
-- Convert input to a number
num = tonumber(io.read())

Variables

Numbers

num = 42  -- All numbers are doubles.

Strings

s = 'walternate'  -- Immutable strings like Python.
t = "double-quotes are also fine"
u = [[ Double brackets
       start and end
       multi-line strings.]]
-- String concatenation uses the .. operator:
message = 'Winter is coming, ' .. line

Empty / Null

t = nil  -- Undefines t; Lua has garbage collection.

Global Variables

  -- Variables are global by default.
  thisIsGlobal = 5  -- Camel case is common.

Local Variables

  -- How to make a variable local:
  local line = io.read()  -- Reads next stdin line.

If Statement

Lua implements IF statements, but doesn't implement a Switch Case statement. An example if is shown below:

if num < 40 then
  print('below 40')
elseif name ~= 'wayne' then  
  -- ~= is not equals.
  -- Equality check is == like Python; ok for strs.
  io.write('over 40 and Name is wayne\n')  -- Defaults to stdout.
else
  print('above 40')
end

Operators

Within if statements and also within loops, the following relational and conditional operates exist:

Relational

Symbols Explanation
== Equal
~= Not equal
<= Less than or equal
>= Greater than or equal
< Less than
> Greater than

Conditional

Operator Description Example
and Called Logical AND operator. If both the operands are non zero then condition becomes true. (A and B) is false
or Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A or B) is true.
not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A and B) is true.

Loops

Just like other languages Lua has the standard 3 types of loop. While will run 0 or more times (may never run), Repeat will run at least once, For will run an exact number of times:

While

-- Blocks are denoted with keywords like do/end:
while num < 50 do
  num = num + 1  -- No ++ or += type operators.
end

For

karlSum = 0
for i = 1, 100 do  -- The range includes both ends.
  karlSum = karlSum + i
end

-- Use "100, 1, -1" as the range to count down:
fredSum = 0
for j = 100, 1, -1 do 
   fredSum = fredSum + j 
end

-- In general, the range is begin, end[, step].

Repeat / Do While

-- Another loop construct:
repeat
  print('the way of the future')
  num = num - 1
until num == 0

Classes

Just like other programming languages, Lua is can be used in an OOP way:

Define a class

-- Meta class, class defined with {}, variables can be set like 'area'
Shape = {area = 0} 

-- Base class is named, followed by ':' to create a method for the class
function Shape:new (o,side)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   side = side or 0
   self.area = side*side;
   return o
end

-- Base class method printArea
function Shape:printArea ()
   print("The area is ",self.area)
end

Creating an object

-- Creating an object
myshape = Shape:new(nil,10)

--to access the methods of the object you need to use ':'
myshape:printArea()

Inheritance

Square = Shape:new()

-- Derived class method new

function Square:new (o,side)
   o = o or Shape:new(o,side)
   setmetatable(o, self)
   self.__index = self
   return o
end

You can override an inherited method by redeclaring it:

-- Derived class method printArea

function Square:printArea ()
   print("The area of square is ",self.area)
end