Skip to content

Commit

Permalink
refactor: more metatables (__tostring on Node, earlier setmetatable)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 15, 2025
1 parent 212833d commit bab9a31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
17 changes: 11 additions & 6 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2451,20 +2451,25 @@ do
return fail(ps, i, "syntax error, expected 'end' to close construct started at " .. ps.filename .. ":" .. ps.tokens[istart].y .. ":" .. ps.tokens[istart].x .. ":")
end

local node_mt = {
__tostring = function(n)
return n.f .. ":" .. n.y .. ":" .. n.x .. " " .. n.kind
end,
}

local function new_node(ps, i, kind)
local t = ps.tokens[i]
return { f = ps.filename, y = t.y, x = t.x, tk = t.tk, kind = kind or (t.kind) }
return setmetatable({ f = ps.filename, y = t.y, x = t.x, tk = t.tk, kind = kind or (t.kind) }, node_mt)
end

local function new_type(ps, i, typename)
local token = ps.tokens[i]
local t = {}
local t = setmetatable({}, type_mt)
t.typeid = new_typeid()
t.f = ps.filename
t.x = token.x
t.y = token.y
t.typename = typename
setmetatable(t, type_mt)
return t
end

Expand Down Expand Up @@ -5947,7 +5952,7 @@ local function mark_array(x)
end

function tl.new_type_reporter()
local self = {
local self = setmetatable({
next_num = 1,
typeid_to_num = {},
typename_to_num = {},
Expand All @@ -5957,7 +5962,7 @@ function tl.new_type_reporter()
symbols_by_file = {},
globals = {},
},
}
}, { __index = TypeReporter })

local names = {}
for name, _ in pairs(simple_types) do
Expand All @@ -5976,7 +5981,7 @@ function tl.new_type_reporter()
self.next_num = self.next_num + 1
end

return setmetatable(self, { __index = TypeReporter })
return self
end

function TypeReporter:store_function(ti, rt)
Expand Down
17 changes: 11 additions & 6 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -2451,20 +2451,25 @@ local function verify_end(ps: ParseState, i: integer, istart: integer, node: Nod
return fail(ps, i, "syntax error, expected 'end' to close construct started at " .. ps.filename .. ":" .. ps.tokens[istart].y .. ":" .. ps.tokens[istart].x .. ":")
end

local node_mt: metatable<Node> = {
__tostring = function(n: Node): string
return n.f .. ":" .. n.y .. ":" .. n.x .. " " .. n.kind
end
}

local function new_node(ps: ParseState, i: integer, kind?: NodeKind): Node
local t = ps.tokens[i]
return { f = ps.filename, y = t.y, x = t.x, tk = t.tk, kind = kind or (t.kind as NodeKind) }
return setmetatable({ f = ps.filename, y = t.y, x = t.x, tk = t.tk, kind = kind or (t.kind as NodeKind) }, node_mt)
end

local function new_type(ps: ParseState, i: integer, typename: TypeName): Type
local token = ps.tokens[i]
local t: Type = {}
local t: Type = setmetatable({}, type_mt)
t.typeid = new_typeid()
t.f = ps.filename
t.x = token.x
t.y = token.y
t.typename = typename
setmetatable(t, type_mt)
return t
end

Expand Down Expand Up @@ -5947,7 +5952,7 @@ local function mark_array<T>(x: T): T
end

function tl.new_type_reporter(): TypeReporter
local self: TypeReporter = {
local self: TypeReporter = setmetatable({
next_num = 1,
typeid_to_num = {},
typename_to_num = {},
Expand All @@ -5957,7 +5962,7 @@ function tl.new_type_reporter(): TypeReporter
symbols_by_file = {},
globals = {},
},
}
}, { __index = TypeReporter })

local names = {}
for name, _ in pairs(simple_types) do
Expand All @@ -5976,7 +5981,7 @@ function tl.new_type_reporter(): TypeReporter
self.next_num = self.next_num + 1
end

return setmetatable(self, { __index = TypeReporter })
return self
end

function TypeReporter:store_function(ti: TypeInfo, rt: FunctionType)
Expand Down

0 comments on commit bab9a31

Please sign in to comment.