Lua1.1 Data Structure

Source: Internet
Author: User

Analyze several data structures commonly used in Lua:
Let's take a look at the following in opcode. h:

typedef unsigned char Byte;typedef unsigned short Word;typedef union{ struct {char c1; char c2;} m; Word w;} CodeWord;typedef union{ struct {char c1; char c2; char c3; char c4;} m; float f;} CodeFloat;

These two union types are mainly used for VM commands.
In lua1.1, the VM command is a byte. When the number of files to be saved is a word or float, take word as an example. You can assign a value to W of the codeword type directly, use the two fields of the codeword struct m to generate commands and store them in the Command array.
The implementation of the following code_word method shows how this structure is used.

static void code_word (Word n){ CodeWord code; code.w = n; code_byte(code.m.c1); code_byte(code.m.c2);}

Opcode enumeration is all commands supported by the lua1.1 virtual machine.

Type enumeration is the data types in Lua.

typedef enum{ T_MARK, T_NIL, T_NUMBER, T_STRING, T_ARRAY, T_FUNCTION, T_CFUNCTION, T_USERDATA} Type;

The value union is the data definition of Lua.

typedef union{ Cfunction f; real n; char *s; Byte *b; struct Hash *a; void *u;} Value;

The tag field indicates the object type, and the value indicates the object value.

typedef struct Object{ Type tag; Value value;} Object;

Symbol, the name of the symbol, and the value of the object symbol. It is of the object type.

typedef struct{ char *name; Object object;} Symbol;struct List{ Symbol *s; struct List *next;};

A symbol table is an array of symbols. Next is mainly used for searching in searchlist.

Hash. h defines the joined array, that is, the table type in Lua.

// Table

Typedef struct node {Object ref; // key object Val of the element; // value of the element struct node * Next; // pointer to the next element .} Node;

// Table definition

typedef struct Hash{ char mark; unsigned int nhash; Node **list;} Hash;

Where:
Mark During garbage collection
Number of elements in the nhash table
List element list

Lua1.1 Data Structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.