Skip to content

Commit

Permalink
criação de Describer:class_dump()
Browse files Browse the repository at this point in the history
  • Loading branch information
luizfernandofo committed Jan 25, 2023
1 parent 5db49a6 commit 706c41b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 17 deletions.
29 changes: 16 additions & 13 deletions src/describer-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "utils"
local test_class_table = {
"class Pessoa",
"vars num, banana",
"method calc(x)",
"method calc()",
"vars y, z",
"begin",
"y = x + self.num",
Expand All @@ -19,7 +19,8 @@ local test_class_table = {
"io.print(y)",
"y = new Base",
"return y",
"end-method"
"end-method",
"end-class"
}

local test_method_table = {
Expand All @@ -38,15 +39,17 @@ local function test_set_class()

describer:insert_class(test_class_table)

print("\nDescricao da Classe")
print(describer.classes["Pessoa"].name)
print("Attr:")
Print_table(describer.classes["Pessoa"].attr)
print("------------")
for key, value in pairs(describer.classes["Pessoa"].methods) do
Print_table(value)
print("------------")
end
--print("\nDescricao da Classe")
--print(describer.classes["Pessoa"].name)
--print("Attr:")
--Print_table(describer.classes["Pessoa"].attr)
--print("------------")
--for key, value in pairs(describer.classes["Pessoa"].methods) do
--Print_table(value.body)
--print("------------")
--end

describer:class_dump("Pessoa")

end

Expand All @@ -63,5 +66,5 @@ local function test_set_method()
end


--test_set_class()
test_set_method()
test_set_class()
--test_set_method()
81 changes: 77 additions & 4 deletions src/describer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,91 @@ function Describer:describe_class(class_block)
return described_class_table
end


--- func desc
---@param class_name string
function Describer:get_class(class_name)
return self.classes[class_name]
end


local function method_dump(method_table)

if method_table then
local string_method_header = " method " .. method_table.name .. "("

if #method_table.params >= 1 then
for i = 1, #method_table.params - 1 do
string_method_header = string_method_header .. method_table.params[i] .. ", "
end

string_method_header = string_method_header .. method_table.params[#method_table.params]
end

string_method_header = string_method_header .. ")"

print(string_method_header)

if #method_table.vars >= 1 then
local vars_string = " vars "

for i = 1, #method_table.vars - 1 do
vars_string = vars_string .. method_table.vars[i] .. ", "
end

vars_string = vars_string .. method_table.vars[#method_table.vars]

print(vars_string)

end

print(" begin")
for i = 1, #method_table.body do
print(" " .. method_table.body[i])
end
print(" end-method")

end

end


function Describer:class_dump(class_name)

local class = Describer.classes[class_name]

print("class " .. class.name)

if #class.attr >= 1 then
local vars_string = " vars "

for i = 1, #class.attr - 1 do
vars_string = vars_string .. class.attr[i] .. ", "
end

vars_string = vars_string .. class.attr[#class.attr]

print(vars_string)

end

print("")
for key, method in pairs(class.methods) do
method_dump(method)
print("")
end

print("end-class")

end


function Describer:describe_method(method_block)

local described_method_table = {
name = nil,
params = nil,
vars = nil,
params = {},
vars = {},
body = {}
}

Expand All @@ -112,9 +184,10 @@ function Describer:describe_method(method_block)

local vars_keyword, vars = method_block[i]:match("^[%s]*([%l]+)[%s]+(.-)[%s]*$")

if vars_keyword ~= nil and vars_keyword < "vars" or vars_keyword > "vars" then
if vars_keyword ~= nil and (vars_keyword < "vars" or vars_keyword > "vars") then
Error("Erro em Describer:set_method: Esperado 'vars', lido " .. "'" .. vars_keyword .. "'")
else
elseif vars_keyword == "vars" then

described_method_table.vars = Arg_var_list(vars)

i = i + 1 -- avança para a linha do begin
Expand Down

0 comments on commit 706c41b

Please sign in to comment.