Difference between revisions of "Lua"

From MiniScript Wiki
Jump to navigation Jump to search
Line 95: Line 95:
 
| idx = a.indexOf("two")
 
| idx = a.indexOf("two")
 
|-
 
|-
| Get sub-array from i to j
+
| Get sub-array from ''i'' to ''j''
 
| style="font-style:italic;" | no standard method
 
| style="font-style:italic;" | no standard method
 
| print a[i:j]
 
| print a[i:j]

Revision as of 21:33, 10 December 2022

Lua is a scripting language first released in 1993, and particularly popular as an embedded language. This article compares Lua to MiniScript, and may be useful to people coming to MiniScript with a background in Lua.

For a side-by-side comparison on a collection of short programs, see here.

Similarities

Block Syntax

Both Lua and MiniScript delineate code blocks and control structures with explicit keywords. There are some minor differences in syntax, summarized in the table below.

Lua MiniScript
function / end function / end function
if / elseif / end if / else if / end if
while ... do / end while / end while
for i = 1, 10, 2 do / end
for i in range(1, 10, 2) / end for
repeat / until (no direct equivalent)

The most notable difference is that while Lua uses end alone to terminate a function, if, while, or for block, MiniScript uses end function, end if, end while, and end for respectively. MiniScript lacks a repeat-until structure; the standard idiom is to use a while true (infinite) loop, and break out of it when termination conditions are met.

Data Types

Five of the seven basic types in Lua -- nil, number, string, function, and table -- have direct equivalents in MiniScript. nil is called null in MiniScript, and tables are called maps.

Strings in both languages are immutable. MiniScript strings are Unicode, and index by Unicode character; interpretation of Lua strings is implementation-dependent, and index by byte. While Lua has several ways of writing string literals, in MiniScript, string literals are always enclosed in double-quotes (and if you need a double-quote inside a string literal, write it twice, as in SQL).

greeting = "Hello, ""Bob""!  Pleased to meet you."
print greeting   // prints: Hello, "Bob"!  Pleased to meet you.

While MiniScript maps are conceptually quite similar to Lua tables, their syntax and usage is rather different; see the section on Maps under Differences.

Differences

Variable Scope

In Lua, variables are always global by default; you use the local keyword to explicitly declare a local variable. In MiniScript, variables are always local by default (and are never declared; they spring into being when you assign to them). To update a global variable in MiniScript from within a function, you would use the globals or outer prefix.

Lists

Perhaps the biggest difference between Lua and Python is in handling of array-like data. MiniScript has a list data type with no direct Lua equivalent. Readers comfortable with Python should think of it as a Python-style list; others may find the following table helpful.

Task Lua MiniScript
Create an array literal a = {"one", "two", "three"}
a = ["zero", "one", "two"]
Print the first item in an array
print(a[1]) print a[0]
Print the entire array print(table.concat(a, ", ")) print a
Get the length of an array print(#a) print a.len
Append an item to an array table.insert(a, "foo")
a.push "foo"
Insert item at front of array table.insert(a, 1, "hey") a.insert 0, "hey"
Pop the last item off the array last = table.remove(a) last = a.pop
Pull the first item off the array first = table.remove(a, 1)
first = a.pull
Get index of an array item no standard method idx = a.indexOf("two")
Get sub-array from i to j no standard method print a[i:j]

The key differences to note:

  • MiniScript lists are 0-based, rather than 1-based
  • MiniScript lists are manipulated by slicing or dot-syntax methods like len, insert, and remove, rather than by functions in the table module.

Maps