Lua1.0 Script Initial Impressions

Source: Internet
Author: User

Reprint Source: http://my.oschina.net/xhan/blog/305949

Let's get a Hello, world!. Check to see if the interpreter is working correctly:
Print ("Hello, World") can output normally, indicating that the interpreter can work properly.
Take a look at some of the official self-brought test files:


Array.lua

$debuga = @ () i=0while i<10 do a[i] = i*i i=i+1endr,v = Next (A,nil) and R ~= Nil do print ("array[": R.. "] = ".. V) r,v = next (a,r) end

Defines an array, sets the value, and outputs it. The subscript for the array starts with 0, which is different from the later version of Lua, where the LUA array subscript is starting from 1 in later versions. The first line of the file $debug there is no use, this usage to Lua1.1 can see what role. and then again. This uses the next function to iterate through the array.


Globals.lua

K,v=nextvar (k) while K-do print (k) K,v=nextvar (k) EndPrint (i)

Print global variables Nextvar is used to return the next global variable to see what the global variables are: type, Tonumber, Next, Nextvar, print in table.c, Callfunc, EXECSTR, test in LUA.C Defined in Readfrom, WriteTo, read, write, execute, remove in IOLIB.C, IO library functions strfind, strlen, Strsub, Strlower, Strupper in St RLIB.C definition, String library functions abs, sin, cos, tan, ASIN, ACOs, Atan, ceil, floor, mod, sqrt, pow, min, Max defined in MATHLB.C, math library functions. K is defined in the current file and does not know why V is not present in the global variable. This should be a bug of Lua1.0, this wait until Lua1.1 time to analyze again.


Save.lua

$debugfunction  savevar  (n,v)  if v = nil then return end; if  type (v)  =  "number"  then print (N..) =".. V)  return end if type (v)  =  "string"  then print (N.. ") = ' ". V.. "'")  return end if type (v)  =  "table"  then   if v.__visited_ _ ~= nil then     print (n    "="    v.__visited__);    else    print (n.. ") [Email protected] () ")     v.__visited__ = n;    local r,f;     r,f = next (V,nil);    while r ~= nil do       if r ~=  "__visited__"  then         if type (R)  =  ' string '  then           savevar (n.. ") [‘".. R: "']", f)     else          savevar (n.. ") [".. R.. "]", f)     end      end       r,f = next (v,r)     end   end endendfunction save   () Local n,v  n,v = nextvar (nil)   while n ~= nil  Do     savevar (n,v);      n,v = nextvar (n)    endenda = 3x = @{a = 4, b =  "Name", [email  Protected][4,5,67]}b = @{t=5}x.next = bsave ()

The global variables that are defined in the current LUA code are printed because of the qualification of type (v) in Savevar, the globals defined in C code are not printed because their type is cfunction.


Sort.lua

$debugfunction  quicksort (r,s)     if s<=r then return end         -- caso basico da recursao     local v=x[r]    local i=r    local j=s+1         i=i+1; while x[i]<v do i=i+1 end         j=j-1; while x[j]>v do j=j-1 end         x[i],x[j]=x[j],x[i]    while j>i do             -- separacao         i=i+1; while x[i]<v do i=i+1 end         j=j-1; while x[j]>v do j=j-1 end         x[i],x[j]=x[j],x[i]    end    x[i],x[j]=x[j],x[i]    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;--&NBSP;UNDO&NBSP;LAST&NBSP;SWAP&NBSP;&NBSP;&NBSP;&NBSP;X[J],X[R]=X[R],X[J]     quicksort (r,j-1)         -- recursao     quicksort (j+1,s) endfunction sort (a,n)              -- selection sort local i=1 while i<=n do   local m=i  local j=i+1  while j<=n do   if  a[j]<a[m] then m=j end   j=j+1  end a[i],a[m]=a[m],a[i]             -- swap a[i] and a [M] i=i+1 endendfunction main ()  [email protected] ()  n=-1 n=n+1;     x[n]= "a"  n=n+1;    x[n]= "Waldemar"  n=n+1;    x[n]= "Luiz"  n=n+1;     x[n]= "Lula"  n=n+1;    x[n]= "Peter"  n=n+1;     x[n]= "Raquel"  n=n+1;    x[n]= "Camilo"  n=n+1;     X[n]= "Andre"  n=n+1;    x[n]= "Marcelo"  n=n+1;    x[n]= "Sedrez " n=n+1;    x[n]=" Z "-- quicksort (1,n-1)  print (x[0] ...", "... X[1] ... ",".. X[2] ... ",".. X[3] ... ",".. X[4] ... ",".. X[5] ... ",".. X[6] ... ",".. X[7] ... ",".. X[8] ... ",".. X[9] ... ",".. X[10])  sort  (x, n-1)  print (x[0] ... ","... X[1] ... ",".. X[2] ... ",".. X[3] ... ",".. X[4] ... ",".. X[5] ... ",".. X[6] ... ",".. X[7] ... ",".. X[8] ... ",".. X[9] ... ",".. X[10]) Endmain ()

Quick Sort and select sort


Test.lua

$debugfunction Somap (x1,y1,x2,y2) return x1+x2, Y1+y2endfunction Norma (x, y) return x*x+y*yendfunction Retorno_multiplo () Print (Norma (Somap (2,3,4,5))) end

There is no response to running this file because it has only function definitions and no calls. Add a print look: print (Retorno_multiplo ()) is good, can be executed and output normally. Or you can add a Retorno_multiplo to the file after the command line, because Lua1.0 treats the arguments after the first parameter (the file name) as a function call.


Type.lua

$debugfunction  check  (Object, class)  local v = next (Object,nil)  while  v ~= nil do   if class[v] = nil then print (" unknown field:  "&NBSP; &NBSP;V)     elseif type (Object[v])  ~= class[v].type        then print ("wrong type for field " &NBSP; &NBSP;V)    end   v = next (object,v);  end v = next ( Class,nil);  while v ~= nil do   if object[v] = nil  then     if class[v].default ~= nil then        object[v] = class[v].default     else print (" field  ". V.. "  not initialized ")      end   end   v  = next (class,v);  endendtypetrilha = @{x = @{default = 0, type  =  "Number"},                y = @{default = 0, type =  "Number"},                name = @{type =  "string"}               }function trilha   (t)   check (T,typetrilha) end--t1 =  @trilha { x = 4, name =   "3"}t1 = @{ x = 4, name =  "3"}a =  "NA" ... " Me "Trilha (t1) print (a)

Type checking of associative arrays and setting default values.
Overall, the syntax differs greatly from the current version. For the syntax-related part of the analysis, the plan mainly analyzes the implementation of the compiler backend, which is the execution part of the bytecode in the virtual machine. Of course, there is no good analysis of the front end, Lua1.0 lexical analysis and syntactic analysis are generated using tools (Lex and YACC), see the original file on the understanding.

Lua1.0 Script Initial Impressions

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.