Lua1.0 code analysis table. c

Source: Internet
Author: User

Table. C code analysis
Global symbols, constants, strings, associated arrays, and definitions of file lists.
Global symbols:
There are five basic symbols at first, and the Lua preset functions and library functions are registered in it.
Constant:
The initial constants are the names of type in Lua.
String table, associated array table, all the arrays defined in Table. c In the file list can be considered as the Lua Global Registry space and the Lua environment.
Function Analysis

/*** Given a name, search it at symbol table and return its index. If not** found, allocate at end of table, checking oveflow and return its index.** On error, return -1.*/int lua_findsymbol (char *s){ int i; for (i=0; i<lua_ntable; i++)  if (streq(s,s_name(i)))   return i; if (lua_ntable >= MAXSYMBOL-1) {  lua_error ("symbol table overflow");  return -1; } s_name(lua_ntable) = strdup(s); if (s_name(lua_ntable) == NULL) {  lua_error ("not enough memory");  return -1; } s_tag(lua_ntable++) = T_NIL;  return (lua_ntable-1);}

The comment is clear:
If a name is given, it is searched in the symbol table. If it is found, its index is returned. If it is not found, add it at the end of the array.
If an error occurs,-1 is returned.

/*** Given a constant string, eliminate its delimeters (" or ‘), search it at** constant table and return its index. If not found, allocate at end of** the table, checking oveflow and return its index.**** For each allocation, the function allocate a extra char to be used to** mark used string (it‘s necessary to deal with constant and string** uniformily). The function store at the table the second position allocated,** that represents the beginning of the real string. On error, return -1.***/int lua_findenclosedconstant (char *s){ int i, j, l=strlen(s); char *c = calloc (l, sizeof(char)); /* make a copy */  c++; /* create mark space */ /* introduce scape characters */ for (i=1,j=0; i<l-1; i++) {  if (s[i] == ‘\\‘)  {   switch (s[++i])   {    case ‘n‘: c[j++] = ‘\n‘; break;    case ‘t‘: c[j++] = ‘\t‘; break;    case ‘r‘: c[j++] = ‘\r‘; break;    default : c[j++] = ‘\\‘; c[j++] = c[i]; break;   }  }  else   c[j++] = s[i]; } c[j++] = 0;  for (i=0; i<lua_nconstant; i++)  if (streq(c,lua_constant[i]))  {   free (c-1);   return i;  } if (lua_nconstant >= MAXCONSTANT-1) {  lua_error ("lua: constant string table overflow");  return -1; } lua_constant[lua_nconstant++] = c; return (lua_nconstant-1);}

Given a constant string, search for it in the constant table. If it finds its index. If no, it is allocated at the end and Its Index is returned. Note overflow.
For each new assigned constant string, assign one character before it for marking. The real string is stored from the second digit.

/*** Given a constant string, search it at constant table and return its index.** If not found, allocate at end of the table, checking oveflow and return** its index.**** For each allocation, the function allocate a extra char to be used to** mark used string (it‘s necessary to deal with constant and string** uniformily). The function store at the table the second position allocated,** that represents the beginning of the real string. On error, return -1.***/int lua_findconstant (char *s){ int i; for (i=0; i<lua_nconstant; i++)  if (streq(s,lua_constant[i]))   return i; if (lua_nconstant >= MAXCONSTANT-1) {  lua_error ("lua: constant string table overflow");  return -1; } {  char *c = calloc(strlen(s)+2,sizeof(char));  c++; /* create mark space */  lua_constant[lua_nconstant++] = strcpy(c,s); } return (lua_nconstant-1);}

Similar to the above operations, there is no complexity above. The above is for long strings (special characters in strings, such as strings with single or double quotation marks). The following is for short strings. Because lua1.0 does not have source files for lexical analysis and syntax analysis, this is the case for the moment. Let's look at similar issues in later versions.

/*** Mark an object if it is a string or a unmarked array.*/void lua_markobject (Object *o){ if (tag(o) == T_STRING)  lua_markstring (svalue(o)) = 1; else if (tag(o) == T_ARRAY && markarray(avalue(o)) == 0)   lua_hashmark (avalue(o));}

Mark a string or array for garbage collection.

/*** Mark all strings and arrays used by any object stored at symbol table.*/static void lua_marktable (void){ int i; for (i=0; i<lua_ntable; i++)  lua_markobject (&s_object(i));}

Mark the string, array, and object in the symbol array.

/*** Simulate a garbage colection. When string table or array table overflows,** this function check if all allocated strings and arrays are in use. If** there are unused ones, pack (compress) the tables.*/static void lua_pack (void){ lua_markstack (); lua_marktable ();  { /* pack string */  int i, j;  for (i=j=0; i<lua_nstring; i++)   if (lua_markstring(lua_string[i]) == 1)   {    lua_string[j++] = lua_string[i];    lua_markstring(lua_string[i]) = 0;   }   else   {    free (lua_string[i]-1);   }  lua_nstring = j; }{ /* pack array */  int i, j;  for (i=j=0; i<lua_narray; i++)   if (markarray(lua_array[i]) == 1)   {    lua_array[j++] = lua_array[i];    markarray(lua_array[i]) = 0;   }   else   {    lua_hashdelete (lua_array[i]);   }  lua_narray = j; }}

Garbage collection. When a new string or array is created, if the memory overflows, it is adjusted. Release a string or array if any.
Mark all objects on the stack
Mark table
Compressing string Arrays
Compress the array.
Lua_createstring
Lua_createarray
Lua_addfile
The annotations are clear and I will not elaborate on them.

/*** Internal function: return next global variable*/void lua_nextvar (void){ int index; Object *o = lua_getparam (1); if (o == NULL) { lua_error ("too few arguments to function `nextvar‘"); return; } if (lua_getparam (2) != NULL) { lua_error ("too many arguments to function `nextvar‘"); return; } if (tag(o) == T_NIL) {  index = 0; } else if (tag(o) != T_STRING) {  lua_error ("incorrect argument to function `nextvar‘");  return; } else {  for (index=0; index<lua_ntable; index++)   if (streq(s_name(index),svalue(o))) break;  if (index == lua_ntable)  {   lua_error ("name not found in function `nextvar‘");   return;  }  index++;  while (index < lua_ntable-1 && tag(&s_object(index)) == T_NIL) index++;  if (index == lua_ntable-1)  {   lua_pushnil();   lua_pushnil();   return;  } } {  Object name;  tag(&name) = T_STRING;  svalue(&name) = lua_createstring(lua_strdup(s_name(index)));  if (lua_pushobject (&name)) return;  if (lua_pushobject (&s_object(index))) return; }}

Obtain the next global variable.
A parameter must be input, but it can be null. If the parameter is null, the first global variable is returned.
The parameter type must be string.
Search for the given string in the global table. If no string is found, an error is returned. If it is found, find the first non-empty global variable in the global variable.

Lua1.0 code analysis table. c

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.