Advertisement
Guest User

sl.lua

a guest
Nov 26th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.21 KB | None | 0 0
  1. stack = {
  2.     memory = {};
  3.    
  4.     push = function(self, number)
  5.         table.insert(self.memory, number)
  6.     end;
  7.  
  8.     pop = function(self)
  9.         if #self.memory > 0 then
  10.             local temp = self.memory[#self.memory]
  11.             table.remove(self.memory, #self.memory)
  12.             return temp
  13.         else
  14.             print "Ошибка: стек пуст!"
  15.             error()
  16.         end
  17.     end;
  18.  
  19.     print_memory = function(self)
  20.         if #self.memory > 0 then       
  21.             for key, value in pairs(self.memory) do
  22.                 print(key, value)
  23.             end
  24.         else
  25.             print "Ошибка: невозможно вывести содержимое стека, так как стек пуст!"
  26.             error()
  27.         end
  28.     end;
  29. }
  30.  
  31. dictionary = {
  32.     [""] = function()
  33.     end;
  34.  
  35.     ["quit"] = function()
  36.         parser.working = false
  37.     end;
  38.  
  39.     ["+"] = function()
  40.         local op1 = stack:pop()
  41.         local op2 = stack:pop()
  42.         stack:push(op2 + op1)
  43.     end;
  44.  
  45.     ["-"] = function()
  46.         local op1 = stack:pop()
  47.         local op2 = stack:pop()
  48.         stack:push(op2 - op1)
  49.     end;
  50.  
  51.     ["*"] = function()
  52.         local op1 = stack:pop()
  53.         local op2 = stack:pop()
  54.         stack:push(op2 * op1)
  55.     end;
  56.  
  57.     ["/"] = function()
  58.         local op1 = stack:pop()
  59.         local op2 = stack:pop()
  60.         stack:push(op2 / op1)
  61.     end;
  62.  
  63.     ["^"] = function()
  64.         local op1 = stack:pop()
  65.         local op2 = stack:pop()
  66.         stack:push(op2 ^ op1)
  67.     end;
  68.  
  69.     ["sqrt"] = function()
  70.         stack:push(math.sqrt(stack:pop()))
  71.     end;
  72.  
  73.     ["negate"] = function()
  74.         stack:push(stack:pop() * -1)
  75.     end;
  76.  
  77.     ["and"] = function()
  78.         local op1 = stack:pop()
  79.         local op2 = stack:pop()
  80.         stack:push(op2 and op1)
  81.     end;
  82.  
  83.     ["or"] = function()
  84.         local op1 = stack:pop()
  85.         local op2 = stack:pop()
  86.         stack:push(op2 or op1)
  87.     end;
  88.  
  89.     ["not"] = function()
  90.         stack:push(not stack:pop())
  91.     end;
  92.  
  93.     ["=="] = function()
  94.         local op1 = stack:pop()
  95.         local op2 = stack:pop()
  96.         stack:push(op2 == op1)
  97.     end;
  98.  
  99.     [">"] = function()
  100.         local op1 = stack:pop()
  101.         local op2 = stack:pop()
  102.         stack:push(op2 > op1)
  103.     end;
  104.  
  105.     ["<"] = function()
  106.         local op1 = stack:pop()
  107.         local op2 = stack:pop()
  108.         stack:push(op2 < op1)
  109.     end;
  110.  
  111.     [">="] = function()
  112.         local op1 = stack:pop()
  113.         local op2 = stack:pop()
  114.         stack:push(op2 >= op1)
  115.     end;
  116.  
  117.     ["<="] = function()
  118.         local op1 = stack:pop()
  119.         local op2 = stack:pop()
  120.         stack:push(op2 <= op1)
  121.     end;
  122.  
  123.     ["=/="] = function()
  124.         local op1 = stack:pop()
  125.         local op2 = stack:pop()
  126.         stack:push(op2 ~= op1)
  127.     end;
  128.  
  129.     ["true"] = function()
  130.         stack:push(true)
  131.     end;
  132.  
  133.     ["false"] = function()
  134.         stack:push(false)
  135.     end;
  136.  
  137.     ["dup"] = function()
  138.         local op1 = stack:pop()
  139.         stack:push(op1)
  140.         stack:push(op1)
  141.     end;
  142.  
  143.     ["drop"] = function()
  144.         stack:pop()
  145.     end;
  146.  
  147.     ["swap"] = function()
  148.         local op1 = stack:pop()
  149.         local op2 = stack:pop()
  150.         stack:push(op1)
  151.         stack:push(op2)
  152.     end;
  153.  
  154.     ["print"] = function()
  155.         print(stack:pop())
  156.     end;
  157. }
  158.  
  159. parser = {
  160.     str = "";
  161.  
  162.     working = true;
  163.  
  164.     words = {};
  165.  
  166.     is_number = function(str)  
  167.         if string.sub(str, 1, 1) == "+" or string.sub(str, 1, 1) == "-" then
  168.             if tonumber(string.sub(str, 2, 2)) ~= nil then
  169.                 return true
  170.             else
  171.                 return false
  172.             end
  173.         else
  174.             if tonumber(string.sub(str, 1, 1)) ~= nil then
  175.                 return true
  176.             else
  177.                 return false
  178.             end
  179.         end
  180.     end;
  181.  
  182.     is_in_dictionary = function(word)
  183.         for key, _ in pairs(dictionary) do
  184.             if key == word then
  185.                 return true
  186.             end
  187.         end
  188.         return false
  189.     end;
  190.  
  191.     read_string = function(self)
  192.         local temp
  193.         io.write("]")
  194.         temp = io.read()
  195.         if string.sub(temp, #temp, #temp) ~= ":" then
  196.             self.str = temp
  197.         else
  198.             self.str = ""
  199.             temp = string.sub(temp, 1, #temp-1)
  200.             while string.sub(temp, #temp, #temp) ~= ";" do
  201.                 self.str = self.str .. " " .. temp
  202.                 io.write("]]")
  203.                 temp = io.read()
  204.             end
  205.             self.str = self.str .. " " .. string.sub(temp, 1, #temp-1)
  206.         end
  207.     end;
  208.  
  209.     split = function(self)
  210.         function current_char(pos)
  211.             return string.sub(self.str, pos, pos)
  212.         end
  213.        
  214.         self.words = {}
  215.         local i = 1
  216.         local start_point = 1
  217.  
  218.         while i <= string.len(self.str) do
  219.             while current_char(i) == " " or current_char(i) == "(" or current_char(i) == ")" do
  220.                 i = i + 1
  221.             end
  222.  
  223.             if current_char(i) == "'" then
  224.                 start_pos = i
  225.                 i = i + 1
  226.                 while current_char(i) ~= "'" do
  227.                     if i < string.len(self.str) then
  228.                         i = i + 1
  229.                     else
  230.                         print "Ошибка: не найден конец строки. Не хватает парной одинарной кавычки!"
  231.                         error()
  232.                     end
  233.                 end
  234.                 table.insert(self.words, string.sub(self.str, start_pos, i))
  235.                 i = i + 1
  236.  
  237.             elseif current_char(i) == '"' then
  238.                 start_pos = i
  239.                 i = i + 1
  240.                 while current_char(i) ~= '"' do
  241.                     if i < string.len(self.str) then
  242.                         i = i + 1
  243.                     else
  244.                         print "Ошибка: не найден конец строки. Не хватает парной кавычки!"
  245.                         error()
  246.                     end
  247.                 end
  248.                 table.insert(self.words, string.sub(self.str, start_pos, i))
  249.                 i = i + 1
  250.  
  251.             elseif current_char(i) == "|" then
  252.                 i = i + 1
  253.                 while current_char(i) ~= "|"  do
  254.                     if i < string.len(self.str) then
  255.                         i = i + 1
  256.                     else
  257.                         print "Ошибка: не найден конец комментария. Не хватает символа конца комментария!"
  258.                         error()
  259.                     end
  260.                 end
  261.                 i = i + 1
  262.  
  263.             else
  264.                 start_pos = i
  265.                 while current_char(i) ~= " " and current_char(i) ~= "|" and current_char(i) ~= "(" and current_char(i) ~= ")" and i <= string.len(self.str) do
  266.                     i = i + 1
  267.                 end
  268.                 table.insert(self.words, string.sub(self.str, start_pos, i-1))
  269.             end        
  270.         end
  271.     end;
  272.  
  273.     execute_words = function(self)
  274.         for i, word in pairs(self.words) do
  275.             if self.is_number(word) then
  276.                 stack:push(tonumber(word))
  277.  
  278.             elseif string.sub(word, 1, 1) == "'" or string.sub(word, 1, 1) == '"' then
  279.                 stack:push(string.sub(word, 2, #word-1))
  280.  
  281.             else
  282.                 --if word ~= nil then       -- НЕ ТРОГАТЬ!!! ТУТ КАКАЯ-ТО МАГИЯ!!!
  283.                 if self.is_in_dictionary(word) then
  284.                     dictionary[word]()
  285.                 else
  286.                     print("Ошибка: слово ".. word .. " не найдено в словаре!")
  287.                     error()
  288.                 end
  289.                 --end
  290.             end
  291.         end
  292.     end;       
  293. }
  294.  
  295. function main()
  296.     while parser.working do
  297.         parser:read_string()
  298.         parser:split()
  299.         parser:execute_words()
  300.     end
  301. end
  302.  
  303. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement