Difference between revisions of "Lua"

From MiniScript Wiki
Jump to navigation Jump to search
Line 34: Line 34:
 
=== Data Types ===
 
=== Data Types ===
  
 +
Five of the seven basic types in Lua -- nil, number, string, function, and table -- have direct equivalents in MiniScript.  '''nil''' is called <c>[[null]]</c> 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).
 +
 +
<ms>greeting = "Hello, ""Bob""!  Pleased to meet you."
 +
print greeting  // prints: Hello, "Bob"!  Pleased to meet you.</ms>
 +
 +
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 ==
 
== Differences ==
 +
 +
=== Variable Scope ===
 +
 +
In Lua, variables are always global by default; you use the <c>local</c> 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.
 +
 +
 +
 +
=== Maps ===

Revision as of 21:12, 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.


Maps