Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack = {
- memory = {};
- push = function(self, number)
- table.insert(self.memory, number)
- end;
- pop = function(self)
- if #self.memory > 0 then
- local temp = self.memory[#self.memory]
- table.remove(self.memory, #self.memory)
- return temp
- else
- print "Ошибка: стек пуст!"
- error()
- end
- end;
- print_memory = function(self)
- if #self.memory > 0 then
- for key, value in pairs(self.memory) do
- print(key, value)
- end
- else
- print "Ошибка: невозможно вывести содержимое стека, так как стек пуст!"
- error()
- end
- end;
- }
- dictionary = {
- [""] = function()
- end;
- ["quit"] = function()
- parser.working = false
- end;
- ["+"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 + op1)
- end;
- ["-"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 - op1)
- end;
- ["*"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 * op1)
- end;
- ["/"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 / op1)
- end;
- ["^"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 ^ op1)
- end;
- ["sqrt"] = function()
- stack:push(math.sqrt(stack:pop()))
- end;
- ["negate"] = function()
- stack:push(stack:pop() * -1)
- end;
- ["and"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 and op1)
- end;
- ["or"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 or op1)
- end;
- ["not"] = function()
- stack:push(not stack:pop())
- end;
- ["=="] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 == op1)
- end;
- [">"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 > op1)
- end;
- ["<"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 < op1)
- end;
- [">="] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 >= op1)
- end;
- ["<="] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 <= op1)
- end;
- ["=/="] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op2 ~= op1)
- end;
- ["true"] = function()
- stack:push(true)
- end;
- ["false"] = function()
- stack:push(false)
- end;
- ["dup"] = function()
- local op1 = stack:pop()
- stack:push(op1)
- stack:push(op1)
- end;
- ["drop"] = function()
- stack:pop()
- end;
- ["swap"] = function()
- local op1 = stack:pop()
- local op2 = stack:pop()
- stack:push(op1)
- stack:push(op2)
- end;
- ["print"] = function()
- print(stack:pop())
- end;
- }
- parser = {
- str = "";
- working = true;
- words = {};
- is_number = function(str)
- if string.sub(str, 1, 1) == "+" or string.sub(str, 1, 1) == "-" then
- if tonumber(string.sub(str, 2, 2)) ~= nil then
- return true
- else
- return false
- end
- else
- if tonumber(string.sub(str, 1, 1)) ~= nil then
- return true
- else
- return false
- end
- end
- end;
- is_in_dictionary = function(word)
- for key, _ in pairs(dictionary) do
- if key == word then
- return true
- end
- end
- return false
- end;
- read_string = function(self)
- local temp
- io.write("]")
- temp = io.read()
- if string.sub(temp, #temp, #temp) ~= ":" then
- self.str = temp
- else
- self.str = ""
- temp = string.sub(temp, 1, #temp-1)
- while string.sub(temp, #temp, #temp) ~= ";" do
- self.str = self.str .. " " .. temp
- io.write("]]")
- temp = io.read()
- end
- self.str = self.str .. " " .. string.sub(temp, 1, #temp-1)
- end
- end;
- split = function(self)
- function current_char(pos)
- return string.sub(self.str, pos, pos)
- end
- self.words = {}
- local i = 1
- local start_point = 1
- while i <= string.len(self.str) do
- while current_char(i) == " " or current_char(i) == "(" or current_char(i) == ")" do
- i = i + 1
- end
- if current_char(i) == "'" then
- start_pos = i
- i = i + 1
- while current_char(i) ~= "'" do
- if i < string.len(self.str) then
- i = i + 1
- else
- print "Ошибка: не найден конец строки. Не хватает парной одинарной кавычки!"
- error()
- end
- end
- table.insert(self.words, string.sub(self.str, start_pos, i))
- i = i + 1
- elseif current_char(i) == '"' then
- start_pos = i
- i = i + 1
- while current_char(i) ~= '"' do
- if i < string.len(self.str) then
- i = i + 1
- else
- print "Ошибка: не найден конец строки. Не хватает парной кавычки!"
- error()
- end
- end
- table.insert(self.words, string.sub(self.str, start_pos, i))
- i = i + 1
- elseif current_char(i) == "|" then
- i = i + 1
- while current_char(i) ~= "|" do
- if i < string.len(self.str) then
- i = i + 1
- else
- print "Ошибка: не найден конец комментария. Не хватает символа конца комментария!"
- error()
- end
- end
- i = i + 1
- else
- start_pos = i
- while current_char(i) ~= " " and current_char(i) ~= "|" and current_char(i) ~= "(" and current_char(i) ~= ")" and i <= string.len(self.str) do
- i = i + 1
- end
- table.insert(self.words, string.sub(self.str, start_pos, i-1))
- end
- end
- end;
- execute_words = function(self)
- for i, word in pairs(self.words) do
- if self.is_number(word) then
- stack:push(tonumber(word))
- elseif string.sub(word, 1, 1) == "'" or string.sub(word, 1, 1) == '"' then
- stack:push(string.sub(word, 2, #word-1))
- else
- --if word ~= nil then -- НЕ ТРОГАТЬ!!! ТУТ КАКАЯ-ТО МАГИЯ!!!
- if self.is_in_dictionary(word) then
- dictionary[word]()
- else
- print("Ошибка: слово ".. word .. " не найдено в словаре!")
- error()
- end
- --end
- end
- end
- end;
- }
- function main()
- while parser.working do
- parser:read_string()
- parser:split()
- parser:execute_words()
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement