Advertisement
ZNZNCOOP

NI

Dec 9th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.24 KB | None | 0 0
  1. term= require('term')
  2. c= require('component')
  3. side= require('sides')
  4. term.clear()
  5. prosh_rez= ''
  6. rez= ''
  7. function drop(line)
  8.    s= {}
  9.    for i=1,#line do
  10.         s[#s+1]= string.sub(line,i,i)
  11.    end
  12.    for i=1,#s do
  13.    end
  14.    return s
  15. end
  16.  
  17.  
  18.  
  19. function tempS()
  20.    if temp ~= '' then
  21.       return true
  22.    elseif temp == '' then
  23.       return false
  24.    end
  25.  
  26. end
  27.  
  28. function parse(lines)
  29.    tokens= {}
  30.    textS= false
  31.    forS= false
  32.    tS= false
  33.    specT= {'<','>','=',',','.','(',')',':',"'",'?'}
  34.    Numbers= {'0','1','2','3','4','5','6','7','8','9'}
  35.    temp= ''
  36.    i= 0
  37.    d= 0
  38.    line= 2
  39.    simb= 2
  40. s= drop(lines)
  41. i= 0
  42. while i<#s do
  43. i= i+1
  44.         forS= false
  45.         if (string.byte(s[i]) > 48 and string.byte(s[i]) < 58) then
  46.            if temp~= '' then
  47.               tokens[#tokens+1]= temp
  48.               temp= ''
  49.            end
  50.            c= ''
  51.  
  52.            while true do
  53.               stat= false
  54.               for h=1,#Numbers do
  55.                  if (Numbers[h] == s[i]) then
  56.                     c= c..s[i]
  57.                     stat= true
  58.                  end
  59.               end
  60.               if (stat == false) then
  61.                  tokens[#tokens+1]= c
  62.                  c= ''
  63.                  break
  64.               end
  65.               i= i+1
  66.            end
  67.         end
  68.         if g == line and i == simb then
  69.            --print(textS)
  70.         end
  71.  
  72.         if s[i] == ' ' and textS == false then
  73.            if temp ~= '' then
  74.               tokens[#tokens+1]= temp
  75.            end
  76.            temp= ''
  77.            forS= true
  78.            while true do
  79.               --print(i," ",s[i])
  80.               if s[i+1] ~= ' ' then
  81.                  break
  82.               end
  83.               i= i+1
  84.            end
  85.         end
  86.        
  87.         for q=1,#specT do
  88.           if s[i] == specT[q] and tS == false then
  89.             forS= true
  90.             if temp ~= "" then
  91.                tokens[#tokens+1]= temp
  92.             end
  93.             tokens[#tokens+1]= s[i]
  94.             temp= ''
  95.             break
  96.           end
  97.         end
  98.         if s[i+1] == nil then
  99.            if tempS() then
  100.               temp= temp..s[i]
  101.               tokens[#tokens+1]= temp
  102.               temp= ''
  103.            end
  104.            break
  105.         end
  106.         if forS == false then
  107.            temp= temp..s[i]
  108.            --print(temp)
  109.         end
  110.    end
  111.    for i=1,#tokens do
  112.       --print(i," = ",tokens[i])
  113.    end
  114.    return tokens
  115. end
  116.  
  117. function red(siDe,length)
  118.   c= require('component')
  119.   l= c.list()
  120.       for k,v in pairs(l) do
  121.       if (v == 'redstone') then
  122.          r= c.proxy(k)
  123.          break
  124.       end
  125.    end
  126.    if (siDe == 'right') then
  127.      rez= r.setOutput(side.left,length+0)
  128.    elseif (siDe == 'left') then
  129.       rez= r.setOutput(side.right,length+0)
  130.    end
  131. end
  132.  
  133. function broadcasts(port,message)
  134.   c= require('component')
  135.   l= c.list()
  136.   for k,v in pairs(l) do
  137.   if (v == "modem") then
  138.      q= c.proxy(k)
  139.      break
  140.   end
  141.   end
  142.    port= tonumber(port)
  143.    q.open(port)
  144.    rez= q.broadcast(port,message)
  145.    q.close(port)
  146. end
  147. function S(s)
  148.   ss= {'right','left'}
  149.    for i=1,#ss do
  150.       if (s == ss[i]) then return true end
  151.    end
  152.    return false
  153. end
  154.  
  155. function isNumber(obj)
  156.    d= tonumber(obj)
  157.    if (type(d) == 'number') then return true end
  158.    return false
  159. end
  160.  
  161. function isString(obj)
  162.    if (type(obj) == 'string') then return true end
  163.    return false
  164. end
  165.  
  166. function isComponents(nameC)
  167.   c= require('component')
  168.   l= c.list()
  169.    for k,v in pairs(l) do
  170.       if (v == nameC) then
  171.          return true
  172.       end
  173.    end
  174.    return false
  175. end
  176. function analyze(tokens)
  177.    i= 0
  178.    step= 0
  179.    endstep= 0
  180.    loop= 0
  181.    count= 0
  182.    inFor= false
  183.    while i<#tokens do
  184.     i= i+1
  185.       if (tokens[i] == 'redstone' and tokens[i+1] == '.' and S(tokens[i+2]) == true and tokens[i+3] == ':' and isNumber(tokens[i+4])) then
  186.          if (isComponents('redstone')) then
  187.          red(tokens[i+2],tokens[i+4]+0)
  188.          print('true')
  189.        else
  190.          print('false')
  191.        end
  192.       end
  193.       --print(tokens[i],' ',tokens[i+1],' ',tokens[i+2],' ',tokens[i+3],' ',tokens[i+4])
  194.       if (tokens[i] == 'redstone' and tokens[i+1] == '.' and S(tokens[i+2]) == true and tokens[i+3] == ':' and tokens[i+4] == 'close') then
  195.          if (isComponents('redstone')) then
  196.          red(tokens[i+2],0)
  197.          print('true')
  198.        else
  199.          print('false')
  200.        end
  201.       end
  202.       if (tokens[i] == 'broadcast' and tokens[i+1] == ':' and isNumber(tokens[i+2]) and tokens[i+3] == ':' and isString(tokens[i+4])) then
  203.          if (isComponents('modem')) then
  204.              broadcasts(tokens[i+2],tokens[i+4])
  205.              print('true')
  206.          else
  207.             print('false')
  208.          end
  209.       end
  210.         if (tokens[i] == 'broadcast' and tokens[i+1] == ':' and isNumber(tokens[i+2]) and tokens[i+3] == ':' and tokens[i+4] == 'rez') then
  211.         if (isComponents('modem')) then
  212.              if (prosh_rez ~= nil) then
  213.                 broadcasts(tokens[i+2],prosh_rez)
  214.                 print('true')
  215.              else
  216.                 print('false')
  217.              end
  218.         else
  219.             print('false')
  220.         end
  221.       end
  222.       if (tokens[i] == 'broadcast' and tokens[i+1] == ':' and tokens[i+2] == 'rez' and tokens[i+3] == ':' and isString(tokens[i+4])) then
  223.         if (isComponents('modem')) then
  224.              if (prosh_rez ~= nil and isNumber(prosh_rez)) then
  225.                 broadcasts(prosh_rez,tokens[i+4])
  226.                 print('true')
  227.              else
  228.                 print('false')
  229.              end
  230.         else
  231.             print('false')
  232.         end
  233.       end
  234.       if (tokens[i] == 'rez' and tokens[i+1] == '<' and tokens[i+2] ~= nil) then
  235.          if (isNumber(tokens[i+2])) then
  236.             prosh_rez= tonumber(tokens[i+2])
  237.             print(prosh_rez)
  238.             print('true')
  239.          elseif (isString(tokens[i+2])) then
  240.             prosh_rez= tokens[i+2]
  241.             print(prosh_rez)
  242.             print('true')
  243.          else
  244.             print('false')
  245.          end
  246.       end
  247.       if (tokens[i] == 'pr' and tokens[i+1] == "'" and isString(tokens[i+2]) and tokens[i+3] == "'") then
  248.          print(tokens[i+2])
  249.          rez= true
  250.       end
  251.       if (tokens[i] == 'pr' and tokens[i+1] == 'rez') then
  252.          print(rez)
  253.          rez= true
  254.       end
  255.       if (tokens[i] == 'wr' and tokens[i+1] == "'" and isString(tokens[i+2]) and tokens[i+3] == "'") then
  256.          io.write(tokens[i+2])
  257.          rez= true
  258.       end
  259.       --print(i)
  260.       if (tokens[i] == 'for' and tokens[i+1] == ':') then
  261.          --print('yes1')
  262.          step= i+2
  263.          loop= tonumber(tokens[i+2])
  264.          for q=i,#tokens do
  265.             if (tokens[q] == '?') then
  266.                endstep= q
  267.             end
  268.          end
  269.          inFor= true
  270.       end
  271.       if (count<loop and i+1 == endstep and inFor == true) then
  272.       --print('yes2')
  273.          i= step
  274.          count= count+1
  275.       end
  276.       if (count == loop and inFor == true) then
  277.          i= endstep
  278.          step= 0
  279.          endstep= 0
  280.          loop= 0
  281.          count= 0
  282.          inFor= false
  283.       end
  284.    end
  285. end
  286.  
  287. while true do
  288.    io.write("Ni >")
  289.    mess= io.read()
  290.    t= parse(mess)
  291.    if (tokens[1] == 'exit') then break end
  292.    analyze(t)
  293. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement