Advertisement
Guest User

paranthesis only interpreter

a guest
Jan 21st, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1.  
  2. local getc=function(str,pos) return str:sub(pos,pos) end
  3. local validchars={["("]=true,[")"]=true,}
  4.  
  5. local skipinvalid=function(str,start)
  6.     local pos=start
  7.     while not validchars[getc(str,pos)] and pos<=#str do
  8.         pos=pos+1
  9.     end
  10.     return pos
  11. end
  12.  
  13. local read read=function(input,start)
  14.     local data={}
  15.     local d
  16.     local pos=start
  17.     while pos<=#input do
  18.         pos=skipinvalid(input,pos)
  19.         local char=getc(input,pos)
  20.         --print(char)
  21.         if char=="(" then
  22.             pos,d=read(input,pos+1)
  23.             table.insert(data,d)
  24.         elseif char==")" then
  25.             return pos+1,data
  26.         end
  27.     end
  28.     --table.remove(data)
  29.     --error("unbalanced brackets")
  30.     return pos+1,data
  31. end
  32.  
  33. local parse=function(str)
  34.     local _,data=read(str,1)
  35.     return data
  36. end
  37.  
  38. local output output=function(data)
  39.     io.write("(")
  40.     for i,v in ipairs(data) do
  41.         output(v)
  42.     end
  43.     io.write(")")
  44. end
  45.  
  46. local make_output=function(data)
  47.     for i,v in ipairs(data) do
  48.         output(v)
  49.     end
  50. end
  51.  
  52. local is_Empty=function(data)
  53.     return #data==0
  54. end
  55.  
  56. local copy copy=function(data)
  57.     local new={}
  58.     for i,v in ipairs(data) do
  59.         new[i]=copy(v)
  60.     end
  61.     return new
  62. end
  63.  
  64. local equals equals=function(a,b)
  65.     local eq=#a==#b
  66.     for i=1,#a do
  67.         if not eq then break end
  68.         eq=eq and equals(a[i],b[i])
  69.     end
  70.     return eq
  71. end
  72.  
  73. local substitute substitute=function(X,Y,Z)
  74.     --If X is Y, replace it with Z
  75.     if equals(X,Y) then
  76.         --print("R1")
  77.         return copy(Z)
  78.  
  79.     --If X is empty, leave it untouched
  80.     elseif #X==0 then
  81.         --print("R2")
  82.         return X
  83.  
  84.     --If X has exactly one element, then substutute Y with Z in that element
  85.     elseif #X==1 then
  86.         --print("R3")
  87.         X[1]=substitute(X[1],Y,Z)
  88.         return X
  89.  
  90.     else
  91.         --If the first element of X is Y, leave it untouched
  92.         -- interpreted as "if the first element of X equals Y, then leave the first element of X untouched"
  93.         local newX={}
  94.         if not equals(X[1],Y) then
  95.         --  print("R4")
  96.             newX[1]=substitute(X[1],Y,Z)
  97.         else
  98.             newX[1]=copy(X[1])
  99.         end
  100.  
  101.         --In each element of X starting from the second element, substutute Y with Z
  102.         --print("R5")
  103.         for i=2,#X do
  104.             newX[i]=substitute(X[i],Y,Z)
  105.         end
  106.         return newX
  107.     end
  108.     error("asdasd")
  109. end
  110.  
  111.  
  112.  
  113.  
  114. local applyRules=function(data)-->halt, rules matched
  115.     --If there are no global groups, then halt the program.
  116.     local count=#data
  117.     if count==0 then
  118.         --make_output(data)
  119.         return true,true
  120.    
  121.  
  122.     --If the first global group is empty and it is the only global group, then halt the program.
  123.     elseif count==1 and is_Empty(data[1]) then
  124.         --make_output(data)
  125.         return true,true
  126.  
  127.  
  128.     --If the first global group is empty and the second global group is also empty, then remove one of them.
  129.     elseif count>=2 and is_Empty(data[1]) and is_Empty(data[2]) then
  130.         table.remove(data,math.random(1,2))
  131.         return false,true
  132.  
  133.     --If the first global group is empty and the second global group is non-empty, then swap them.
  134.     elseif count>=2 and is_Empty(data[1]) and (not is_Empty(data[2])) then
  135.         data[1],data[2]=data[2],data[1]
  136.         return false,true
  137.  
  138.  
  139.     --If the first global group has exactly one element, then replace the first global group with the content of that element (so, fo example, ((()())) will be replaced with ()()).
  140.     elseif count>=1 and not is_Empty(data[1]) and #data[1]==1 then
  141.         data[1]=data[1][1]
  142.         return false,true
  143.  
  144.  
  145.     --If the first global group has two or more elements and it is the only global group, then halt the program.
  146.     elseif count==1 and #data[1]>1 then
  147.         --make_output(data)
  148.         return true,true
  149.     end
  150.     return false,false
  151. end
  152.  
  153. local compute=function(data)
  154.     --Let's call the first one A and the second one B.
  155.     local A=copy(data[1])
  156.     local B=copy(data[2])
  157.     --First, we remove B:
  158.     table.remove(data,2)
  159.     --remove A from global as we will insert it's innards later
  160.     --We first select the first element of A (let's call it C and color it red):
  161.     local C=copy(A[1])
  162.     --Now, we remove C and
  163.     table.remove(A,1)
  164.     --then we remove parentheses of A:
  165.     --this is done when substituting
  166.  
  167.    
  168.  
  169.     -- Finally, for each remaining element of A (the groups that are left green after removing C and parentheses of A), we recursively substitute C with B. How substitution works is explained in the next paragraph.
  170.  
  171.     table.remove(data,1)--remove a to make room to a's innards
  172.     for i=1,#A do
  173.         local innards=substitute(A[i],C,B)
  174.         table.insert(data,i,innards)
  175.     end
  176.  
  177. end
  178.  
  179.  
  180.  
  181. local make_random=function(count)
  182.     local toreturn={"(",")"}
  183.     for i=1,count do
  184.         local pivot=math.random(1,#toreturn+1)
  185.         table.insert(toreturn,pivot,"(")
  186.         pivot2=math.random(pivot+1,#toreturn+1)
  187.         table.insert(toreturn,pivot2,")")
  188.     end
  189.     return table.concat(toreturn)
  190. end
  191.  
  192.  
  193.  
  194. --replace io.read("*a") with the source code surrounded with [[]] to embed code
  195. local input=io.read("*a")
  196.  
  197. local halt=false
  198. local matchrule=true
  199. --load data
  200. local data=parse(input)
  201. -- do the actual computation
  202. -- all the setup for this line
  203. while not halt do
  204.     halt,matchrule=applyRules(data)
  205.     if not matchrule then
  206.         compute(data)
  207.     end
  208. --uncomment the next to lines to see how the state evolves
  209. --  make_output(data)
  210. --  print("")
  211. end
  212. --print("\nFINAL output")
  213. make_output(data)
  214. io.write("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement