Skip to content

Instantly share code, notes, and snippets.

@TutorialDoctor
Created September 17, 2015 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TutorialDoctor/a58d0099b297affbe7b2 to your computer and use it in GitHub Desktop.
Save TutorialDoctor/a58d0099b297affbe7b2 to your computer and use it in GitHub Desktop.
-- LUA SYNTAX (WIP)
-- PRINTING
-- Print a string
print("Hello World")
-- Print multiple strings
print("Hello","World")
-- Join/Concatenate two strings (gives errors if not strings)
print("Hello" .. "World")
-- Joining two strings with spaces
print("Hello" .. " World")
-- Printing numbers
print(27)
-- INPUT
-- OPERATORS
print('\nOPERATORS:\n')
----------------------------------------------------------------------
-- Adding
print(1+2)
-- Subtracting
print(4-3)
-- Multiplying
print(3*3)
-- Dividing (not accurate if one of the terms isn't a decimal)
print(18/3)
-- Remainder/Modulus
print(9%4)
-- Power
--
-- Square root
-- must use at least one float
-- Comparisons
print(2<4) -- less than
print(4>9) -- greather than
print(5==6) -- is equal to
print(3==3)
print(4~=4) -- not equal to
print(4~=9)
-- COMMENTS
-- Single line comment
--[[This is a multiline comment in lua. It can take some getting used to--]]
-- VARIABLES
print('\nVARIABLES:\n')
-----------------------------------------------------------------------
-- Types
-----------------------------------------------------------------------
-- Character
at = "@"
print(at)
-- String (wrapped in quotes)
name = "Raphael"
print(name)
-- Integer (no quotes quotes)
age = 29
print(age)
-- Float
height = 6.3
print(height)
-- Boolean
is_cool = True
print(is_cool)
-- List/Array
array = {} --an empty array
colors = {"red","orange","yellow","green","blue","indigo","violet"}
numbers = {0,1,2,3,4,5,6,7,8,9}
mixed_array = {9,"alpha",63.3,true}
print(array)
print(colors)
print(array)
print(array)
-- Tuple
location = {0,0}
print(location)
-- Dictionary
dictionary = {key="value"}
print(dictionary["key"])
print(dictionary.key)
-- Overwriting a variable
is_cool = false
name = "Tutorial Doctor"
age = 10585 -- in days
height = 6.1
print(is_cool,name,age,height)
-- Set a boolean to the opposite of itself
is_cool = not is_cool --False
print(is_cool)
-- Casting (changing the type of a variable)
-- To a float
--age = float(age)
--print(age)
-- To an integer
--height = int(height)
--print(height)
-- To a string
--is_cool = str(is_cool)
--print(is_cool)
-- FUNCTIONS
-- Argument Parameters
function A(...)
print(arg)
end
A(3,5,6)
-- CONDITIONAL
-- LOOPS
function draw()
background(39, 39, 39, 255)
end
-- CLASSES
point = {x = 10, y = 0}
function point:move (dx, dy)
self.x = self.x + dx
self.y = self.y + dy
end
point:move(5, 5)
print(point.x, point.y)
point.move(point, 4, 2)
print(point.x, point.y)
-- MODULES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment