Lua Syntax Textbox C
This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
LXSH: Lexing & Syntax Highlighting in Lua. LXSH is a collection of lexers lexing and syntax highlighters highlighting written in Lua lua using the excellent pattern-matching library LPeg lpeg. Several syntaxes are currently supported: Lua, C, BibTeX and shell script. The syntax highlighters support three output formats: HTML html designed. FastColoredTextBox has built-in syntax highlighter for languages: C#, VB, HTML, SQL, PHP, JS, XML, Lua. Note: You can create own syntax highlighter for any language. Property HighlightingRangeType specifies which part of the text will be highlighted as you type (by built-in highlighter). Value ChangedRange provides better performance.
Programming in Lua |
Part I. The LanguageChapter 3. Expressions |
3.6 – Table Constructors
Constructors are expressions that create andinitialize tables.They are a distinctive feature of Luaand one of its most useful and versatile mechanisms.
The simplest constructor is the empty constructor,{}
, which creates an empty table;we saw it before.Constructors also initialize arrays(called also sequences or lists).For instance, the statementwill initialize days[1]
with the string 'Sunday'
(the first element has always index 1, not 0),days[2]
with 'Monday'
,and so on:
Constructors do not need to use only constant expressions.We can use any kind of expression for the value of each element.For instance, we can build a short sine table as
To initialize a table to be used as a record,Lua offers the following syntax:which is equivalent to
No matter what constructor we use to create a table,we can always add and remove other fields of any type to it:That is, all tables are created equal;constructors only affect their initialization.
Every time Lua evaluates a constructor,it creates and initializes a new table.Consequently, we can use tables to implement linked lists:This code reads lines from the standard inputand stores them in a linked list, in reverse order.Each node in the list is a table with two fields:value
, with the line contents,and next
, with a reference to the next node.The following code prints the list contents:(Because we implemented our list as a stack,the lines will be printed in reverse order.)Although instructive,we hardly use the above implementation in real Lua programs;lists are better implemented as arrays,as we will see in Chapter 11.
We can mix record-style and list-styleinitializations in the same constructor:The above example also illustrates how we can nest constructorsto represent more complex data structures.Each of the elements polyline[1]
, ..., polyline[4]
is a table representing a record:
Those two constructor forms have their limitations.For instance,you cannot initialize fields with negative indices,or with string indices that are not proper identifiers.For such needs, there is another, more general, format.In this format,we explicitly write the index to be initialized as an expression,between square brackets:That syntax is more cumbersome, but more flexible too:Both the list-style and the record-style forms are specialcases of this more general one.The constructoris equivalent toand the constructoris equivalent to
For those that really want their arrays starting at 0,it is not difficult to write the following:Now, the first value, 'Sunday'
, is at index 0.That zero does not affect the other fields,but 'Monday'
naturally goes to index 1,because it is the first list value in the constructor;the other values follow it.Despite this facility,I do not recommend the use of arrays starting at 0 in Lua.Remember that most functions assume thatarrays start at index 1,and therefore will not handle such arrays correctly.
You can always put a comma after the last entry.These trailing commas are optional, but are always valid:Such flexibility makes it easier to write programs that generate Lua tables,because they do not need to handle the last element as a special case.
Finally, you can always use a semicolon instead of a comma in a constructor.We usually reserve semicolons to delimitdifferent sections in a constructor,for instance to separate its list part from its record part:
Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved. |
Welcome to the third part of these Lua 5.2 personal notes. If you have not yet, you may want to read my previous notes on running Lua 5.2 scripts from C++ or passing variables from Lua 5.2 to C++.
This new tutorial explains how to call C++ functions from Lua 5.2, including passing and receiving arguments from them. These notes are based on the Lua-User Wiki Sample Code and the Lua 5.2 Reference Manual, as usual, and on the tutorial by Christian Stigen Larsen.
First, I will show you some code (in Lua and C++) and the execution output. Afterwards comes the details of the most difficult parts.
Lua script
The Lua script will just call a C++ function with some input arguments, and then will receive and print two output arguments. The code looks like this:
C++ program
The C++ code is almost exactly the same as in the previous tutorial. The only differences are the declaration and pushing of the function that Lua will call. The whole code is:
The output is:
The details
Lua functions are first-class values. This means that they can be treated as conventional values, in particular, they can be stored in variables. Thus, one can pass a function to Lua by simply pushing the value to the Lua stack and making its name global:
Lua will be able to access displayLuaFunction() from its global name “displayLuaFunction”. Also, there is a macro, lua_register(), that allows writing these two steps in one sentence. Instead of the previous code, one might have coded it as:
(my thanks to kpityu for pointing this out in the comments).

In order C++ functions to communicate properly with Lua, they must follow some rules. The first one is its signature, which has to be consistent with the type defined by lua_CFcuntion:
i. e., the function must return an integer and only accept one input argument of type pointer-to lua_state.
Lua Syntax Textbox Command
The second rule is just a protocolto follow for input/output function argument interchange between Lua and C++. As you may have guessed, all this passing of variables is done via the Lua stack. When a C++ function is called from Lua, for example
a new Lua stack, independent of the rest of stacks, is generated for the function. This stack contains the input arguments of the function. Thus, from inside of the C++ function one knows the amount of input arguments by getting the position of the top of this stack:
and the input arguments can be accessed directly from the stack:
Notice that the order in the stack is the same as in the function call, hence, the first element popped is actually the last input argument. Since initially the only elements of the stack are the input arguments, you may access a given index by directly pointing to it. For instance, for the second input argument can be converted to a string — having checked the stack has at least two elements — by this statement:
Lua Syntax Textbox Color
Once the function has finished, the return values must be placed in the stack so Lua can reach them:
The final part is to return the number of variables pushed to the stack that we want Lua to see as the C++ function return value:
Lua Syntax Textbox C Sharp
And that is it!
With this we have covered the basic protocol for creating new C++ functions that are Lua compatible. In case you want more examples you can check the Lua libraries, which are made of C++ functions following these rules.
Stay tuned for the next episode of these personal notes. I am not sure if I will be talking about linking C++ dynamic libraries to Lua or about encapsulating and passing objects from and to Lua.
Read the next tutorial!