Lua - Basic Syntax

Hello there, future Lua programmers! I'm excited to embark on this journey with you as we explore the fascinating world of Lua programming. As your experienced computer science teacher, I'll guide you through the basics of Lua syntax, making sure you understand every step of the way. Let's dive in!

Lua - Basic Syntax

First Lua Program

Let's start with the traditional "Hello, World!" program. It's a rite of passage for every programmer, and it's incredibly simple in Lua:

print("Hello, World!")

When you run this program, you'll see:

Hello, World!

Isn't that amazing? With just one line of code, you've already created your first Lua program! The print() function is used to display text on the screen. Remember, in Lua, we use parentheses () to call functions and double quotes "" to define strings (text).

Tokens in Lua

Now, let's talk about tokens. In programming, tokens are the smallest elements that the compiler recognizes. Think of them as the building blocks of your code. In Lua, we have several types of tokens:

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators
  6. Separators

We'll explore some of these in more detail as we go along.

Comments

Before we dive deeper, let's learn about comments. Comments are notes that programmers leave in the code to explain what's happening. They're ignored by the computer but are incredibly useful for humans reading the code.

In Lua, we have two types of comments:

  1. Single-line comments:

    -- This is a single-line comment
    print("Hello") -- This is also a comment
  2. Multi-line comments:

    --[[
    This is a multi-line comment.
    It can span several lines.
    Very useful for longer explanations!
    --]]

Identifiers

Identifiers are names we give to variables, functions, and other elements in our programs. In Lua, identifiers can be any string of letters, digits, and underscores, not beginning with a digit. Here are some valid identifiers:

age
player1
_score
myFunction

And some invalid ones:

2player  -- Starts with a digit
my-var   -- Contains a hyphen

Remember, Lua is case-sensitive, which means score and Score are considered different identifiers.

Keywords

Keywords are reserved words in Lua that have special meanings. You can't use these as identifiers. Here's a table of Lua keywords:

Keyword Keyword Keyword Keyword
and break do else
elseif end false for
function goto if in
local nil not or
repeat return then true
until while

Let's use some of these keywords in a simple program:

if true then
    print("This will always be printed!")
end

In this example, if, then, and end are keywords that help structure our code.

Whitespace in Lua

Whitespace refers to spaces, tabs, and newlines in your code. In Lua, whitespace is generally ignored, which means you can format your code for readability without affecting how it runs.

For example, these two pieces of code are identical to Lua:

print("Hello")print("World")
print("Hello")
print("World")

However, for the sake of readability (and your future self who might need to understand this code later), it's always better to use whitespace to make your code clear and organized.

Here's a more complex example showcasing various elements we've discussed:

-- This program calculates the area of a circle

--[[
   Formula: area = π * radius^2
   We'll use 3.14159 as an approximation for π
--]]

local pi = 3.14159
local radius = 5

local area = pi * (radius ^ 2)

print("The area of a circle with radius " .. radius .. " is approximately " .. area)

Let's break this down:

  1. We start with comments explaining what the program does and the formula we're using.
  2. We declare local variables pi and radius using the local keyword.
  3. We calculate the area using the formula.
  4. Finally, we print the result, using .. to concatenate strings and variables.

When you run this program, you'll see:

The area of a circle with radius 5 is approximately 78.53975

And there you have it! We've covered the basic syntax of Lua, from your first "Hello, World!" program to variables, comments, and a bit of math. Remember, programming is like learning a new language - it takes practice and patience. Don't worry if everything doesn't click immediately. Keep experimenting, try writing your own small programs, and most importantly, have fun!

In our next lesson, we'll dive into data types and variables in Lua. Until then, happy coding!

Credits: Image by storyset