Skip to content

Commit

Permalink
refactor(.)
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroASB committed Jan 26, 2023
1 parent 706c41b commit c133ce5
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 96 deletions.
55 changes: 55 additions & 0 deletions sketch/tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Test 1
vars a, b, c, d
a = 10
b = 20
c = new Pessoa
d = new Account

c.name = 5
b = c.name
io.print(b)
c.setName(a)
a = c.name
io.print(a)
a = 500
Pessoa.setName._delete(1):
c.setName(a)
a = c.name
io.print(a)


io.dump(c)
io.dump(d)


# Test 2
vars account, ret, value, tax
account = new Account

value = 200
account.value = value
value = 100
tax = 5
io.print(value)
' account.setId(value)
io.print(ret)


# Test 3
vars account, ret, value, tax, person, a, b, c
person = new Pessoa
account = new Account


person._prototype = account
person.id = 10
person.printId()

value = 200
account.value = value
value = 100
tax = 5

ret = account.transfer(value, tax)

person.setId(tax)
2 changes: 1 addition & 1 deletion src/args.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Arg_var_list(vars_list_string)
return {}
end

local var = string.match(vars_list_string, "^" .. _Variable_Last_def_pattern_ .. "$")
local var = string.match(vars_list_string, "^" .. _Variable_last_def_pattern_ .. "$")

if var == nil then
Error("Erro em Arg_var_list: Vars inválido")
Expand Down
35 changes: 24 additions & 11 deletions src/eval.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "env"
require "describer"
require "types"
require "math"


function Eval_vars_def(env, ast)
Expand All @@ -13,15 +14,15 @@ end


function Eval_io_dump(env, ast)
local var_name = ast.arg.params
local var_name = ast.arg.params[1]
local var = env:getVar(var_name)

local describer = Get_describer()
if var.type ~= "Class" then
Error("Erro em Eval_io_dump: Variável " .. var_name .. " não é um objeto de uma classe")
return
end

Describer:class_dump(var.class.name)
describer:class_dump(var.class.name)
end


Expand Down Expand Up @@ -83,9 +84,7 @@ function Eval_method_call(env, ast)
local var = env:getVar(var_name)
method_env:setVar(method_table.params[index], var)
end

local return_value = Method_interpreter(method_env, method_buffer)

return return_value
end

Expand All @@ -97,7 +96,12 @@ function Eval_binary_operation(env, ast)
local operations_functions = {
["+"] = function(var1, var2) return (var1 + var2) end,
["-"] = function(var1, var2) return (var1 - var2) end,
["/"] = function(var1, var2) return (var1 / var2) end,
["/"] = function(var1, var2)
if var2 == 0 then
Error("Erro em Eval_binary_operation: Divisão por zero")
end
return (var1 / var2)
end,
["*"] = function(var1, var2) return (var1 * var2) end
}

Expand All @@ -116,7 +120,7 @@ function Eval_binary_operation(env, ast)
return
end

local result = operation_function(first_var.value, second_var.value)
local result = math.floor(operation_function(first_var.value, second_var.value))
local var = NumberVar:new(nil, ast.arg.var_name, result)

return var
Expand All @@ -140,6 +144,10 @@ function Eval_assign(env, ast)
lhs_var = var
elseif lhs.type == "attr_case" then
lhs_var = var:get_attr(lhs.arg.attr_name)
if lhs_var == nil then
Error("Erro em Parser_assign: Atributo '"..lhs.arg.attr_name .."' não existe em '" .. lhs.arg.var_name .. "'")
return
end
end

if rhs.type == "number_arg" then
Expand All @@ -152,15 +160,21 @@ function Eval_assign(env, ast)
elseif rhs.type == "attr_arg" then
local temp_var = env:getVar(rhs.arg.var_name)
local rhs_attr = temp_var:get_attr(rhs.arg.attr_name)

if rhs_attr == nil then
Error("Erro em Parser_assign: Atributo '"..rhs.arg.attr_name .."' não existe em '" .. rhs.arg.var_name .. "'")
return
end

rhs_var = rhs_attr:copy(lhs_var.name)

elseif rhs.type == "method_call_arg" then
local temp_var = Eval_method_call(env, ast.rhs)
if temp_var == nil then
Error("Erro em Parser_assign: Retorno de metodo vazio")
Error("Erro em Parser_assign: Retorno de método vazio")
return
end
rhs_var = temp_var:copy()
rhs_var = temp_var:copy(lhs_var.name)

elseif rhs.type == "obj_creation_arg" then
ast.rhs.arg.obj_name = lhs_var.name
Expand All @@ -173,8 +187,7 @@ function Eval_assign(env, ast)
Error("Erro em Parser_assign: Tipo de rhs não reconhecido")
return {}
end



if lhs.type == "var_case" then
env:setVar(lhs_var.name, rhs_var)
elseif lhs.type == "attr_case" then
Expand Down
61 changes: 51 additions & 10 deletions src/executor.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-- Executor
require "utils"
require "patterns"
require "env"
Expand All @@ -8,7 +7,8 @@ require "args"


--- Executor do corpo principal do programa
---@param main_block_buffer string
---@param main_env table: Ambiente de variáveis
---@param main_block_buffer string: Buffer do corpo principal
function Main_interpreter(main_env, main_block_buffer)
Check_type("Main_interpreter", main_block_buffer, "main_block_buffer", "string")

Expand All @@ -20,14 +20,13 @@ function Main_interpreter(main_env, main_block_buffer)
var_list_string = main_block_buffer:match("^" .. _Variables_def_pattern_ .. "\n")

if var_list_string then
var_list = Parser_vars_def({types={type="vars_def"}, tokens={var_list_string}})
var_list = Parser_vars_def({ types={type="vars_def"}, tokens={var_list_string} })
Eval_vars_def(main_env, var_list)
main_block_buffer = Pop_statement(main_block_buffer, "^" .. _Variables_def_pattern_ .. "\n")
control_flag = 1
end

while true do

while main_block_buffer ~= "" do
for index, pattern_info in ipairs(Statements_patterns) do
types, pattern = table.unpack(pattern_info)
tokens = { main_block_buffer:match("^" .. pattern .. "\n") }
Expand All @@ -49,7 +48,6 @@ function Main_interpreter(main_env, main_block_buffer)
control_flag = 1

main_block_buffer = Pop_statement(main_block_buffer, pattern)
if(main_block_buffer == "") then break end
end

end
Expand All @@ -61,7 +59,7 @@ function Method_interpreter(method_env, method_buffer)

local pattern, tokens, types, ast, eval_return

while true do
while method_buffer ~= "" do
for index, pattern_info in ipairs(Statements_patterns) do
types, pattern = table.unpack(pattern_info)
tokens = { method_buffer:match("^" .. pattern .. "\n") }
Expand All @@ -82,7 +80,6 @@ function Method_interpreter(method_env, method_buffer)
end

method_buffer = Pop_statement(method_buffer, pattern)
if(method_buffer == "") then break end
end

end
Expand All @@ -94,7 +91,7 @@ function If_interpreter(if_env, if_buffer)

local pattern, tokens, types, ast, eval_return

while true do
while if_buffer ~= "" do
for index, pattern_info in ipairs(Statements_patterns) do
types, pattern = table.unpack(pattern_info)
tokens = { if_buffer:match("^" .. pattern .. "\n") }
Expand All @@ -115,7 +112,51 @@ function If_interpreter(if_env, if_buffer)
end

if_buffer = Pop_statement(if_buffer, pattern)
if(if_buffer == "") then break end
end

end


--- Realiza a execução de um bloco
--- O bloco pode ser um método ou um if/else
---@param env table: Ambiente de variáveis
---@param block_buffer string: Buffer do bloco
---@param parser_function function: Função parser (Parser_method_stmt | Parser_if_stmt)
---@return table|nil: Retorno de um statement "return <value>", caso exista
function Block_executor(env, block_buffer, parser_function)
Check_type("Block_executor", env, "env", "table")
Check_type("Block_executor", block_buffer, "block_buffer", "string")

local pattern, tokens, types, ast, eval_return

while block_buffer ~= "" do
for index, pattern_info in ipairs(Statements_patterns) do
types, pattern = table.unpack(pattern_info)

-- Aplicação de regex no início do buffer
tokens = { block_buffer:match("^" .. pattern .. "\n") }

-- Match válido
if #tokens >= 1 then
-- Criação da abstract syntax tree
ast = parser_function({types=types, tokens=tokens})

eval_return = Eval_controller(env, ast)

if eval_return then
return eval_return
end

break
end
end

if #tokens < 1 then
Error("Erro em Block_executor: Sintaxe incorreta")
end

-- Retirando o statement já analisado
block_buffer = Pop_statement(block_buffer, pattern)
end

end
12 changes: 2 additions & 10 deletions src/files.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "utils"

--- Verifica se o arquivo é válido, se ele existe e o abre para leitura
---@param file_name string
---@return file*
---@param file_name string: Nome do arquivo
---@return file*: Arquivo
function Get_file(file_name)
if type(file_name) ~= "string" then
Error("Erro em Get_file: Nome do arquivo diferente de string")
Expand All @@ -16,11 +16,3 @@ function Get_file(file_name)

return file or {}
end


--- Lê a próxima linha do arquivo
---@param file file*
---@return string
function Read_line(file)
return file:read("*line")
end
4 changes: 3 additions & 1 deletion src/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ end
---- arg: Arg_var
function Parser_return(lexer)
check_lexer(lexer, "Parser_return", "return", 1)

local ast = {}
ast.type = lexer.types.type
ast.arg = Arg_var(lexer.tokens[1])
Expand Down Expand Up @@ -278,6 +278,8 @@ function Parser_main_stmt(lexer)
return Parser_if(lexer)
elseif lexer.types.type == "assignment" then
return Parser_assign(lexer)
elseif lexer.types.type == "prototype" then
return Parser_prototype(lexer)
else
Error("Erro em Parser_main_stmt: Declaração com sintaxe incorreta")
return {}
Expand Down
Loading

0 comments on commit c133ce5

Please sign in to comment.