Skip to content

Commit

Permalink
refactor: store_field_in_record
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Sep 10, 2024
1 parent ece9e50 commit 1c3ba3d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
28 changes: 16 additions & 12 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3690,24 +3690,28 @@ do
return i, node
end

local function store_field_in_record(ps, i, field_name, t, fields, field_order)
local function store_field_in_record(ps, i, field_name, newt, fields, field_order)
if not fields[field_name] then
fields[field_name] = t
fields[field_name] = newt
table.insert(field_order, field_name)
else
local prev_t = fields[field_name]
if t.typename == "function" and prev_t.typename == "function" then
return true
end

local oldt = fields[field_name]

if newt.typename == "function" then
if oldt.typename == "function" then
local p = new_type(ps, i, "poly")
p.types = { prev_t, t }
p.types = { oldt, newt }
fields[field_name] = p
elseif t.typename == "function" and prev_t.typename == "poly" then
table.insert(prev_t.types, t)
else
fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)")
return false
return true
elseif oldt.typename == "poly" then
table.insert(oldt.types, newt)
return true
end
end
return true
fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)")
return false
end

local function set_declname(def, declname)
Expand Down
28 changes: 16 additions & 12 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -3690,24 +3690,28 @@ local function parse_return(ps: ParseState, i: integer): integer, Node
return i, node
end

local function store_field_in_record(ps: ParseState, i: integer, field_name: string, t: Type, fields: {string: Type}, field_order: {string}): boolean
local function store_field_in_record(ps: ParseState, i: integer, field_name: string, newt: Type, fields: {string: Type}, field_order: {string}): boolean
if not fields[field_name] then
fields[field_name] = t
fields[field_name] = newt
table.insert(field_order, field_name)
else
local prev_t = fields[field_name]
if t is FunctionType and prev_t is FunctionType then
return true
end

local oldt = fields[field_name]

if newt is FunctionType then
if oldt is FunctionType then
local p = new_type(ps, i, "poly") as PolyType
p.types = { prev_t, t }
p.types = { oldt, newt }
fields[field_name] = p
elseif t is FunctionType and prev_t is PolyType then
table.insert(prev_t.types, t)
else
fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)")
return false
return true
elseif oldt is PolyType then
table.insert(oldt.types, newt)
return true
end
end
return true
fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)")
return false
end

local function set_declname(def: Type, declname: string)
Expand Down

0 comments on commit 1c3ba3d

Please sign in to comment.