Skip to content

Commit

Permalink
Extract _compile methods for case branch destructuring types
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey committed May 22, 2021
1 parent 5639279 commit 63f0cac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
21 changes: 15 additions & 6 deletions src/compilers/array_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module Mint
class Compiler
def _compile(node : Ast::ArrayDestructuring, value)
def _compile(node : Ast::ArrayDestructuring, variable : String) : Tuple(String, Array(String))
statements = [] of String
if node.spread?
statements = [
"const __ = Array.from(#{value})",
]
statements << "const __ = Array.from(#{variable})"

node
.items
Expand All @@ -22,15 +21,25 @@ module Mint
end

statements << "const #{js.variable_of(node.items.select(Ast::Spread).first.variable)} = __"
statements
else
variables =
node
.items
.join(',') { |param| js.variable_of(param) }

["const [#{variables}] = #{value}"]
statements << "const [#{variables}] = #{variable}"
end
condition =
if node.spread?
"Array.isArray(#{variable}) && #{variable}.length >= #{node.items.size - 1}"
else
"Array.isArray(#{variable}) && #{variable}.length === #{node.items.size}"
end

{
condition,
statements,
}
end
end
end
54 changes: 5 additions & 49 deletions src/compilers/case_branch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,14 @@ module Mint

if match = node.match
case match
when Ast::ArrayDestructuring
statements =
when Ast::ArrayDestructuring, Ast::TupleDestructuring, Ast::EnumDestructuring
compiled =
_compile(match, variable)

statements << expression

if match.spread?
{
"Array.isArray(#{variable}) && #{variable}.length >= #{match.items.size - 1}",
js.statements(statements),
}
else
{
"Array.isArray(#{variable}) && #{variable}.length === #{match.items.size}",
js.statements(statements),
}
end
when Ast::TupleDestructuring
variables =
match
.parameters
.join(',') { |param| js.variable_of(param) }

{
"Array.isArray(#{variable})",
js.statements([
"const [#{variables}] = #{variable}",
expression,
]),
}
when Ast::EnumDestructuring
variables =
case lookups[match].as(Ast::EnumOption).parameters[0]?
when Ast::EnumRecordDefinition
match.parameters.compact_map do |param|
case param
when Ast::TypeVariable
"const #{js.variable_of(param)} = #{variable}._0.#{param.value}"
end
end
else
match.parameters.map_with_index do |param, index1|
"const #{js.variable_of(param)} = #{variable}._#{index1}"
end
end

name =
js.class_of(lookups[match])

compiled[1] << expression
{
"#{variable} instanceof #{name}",
js.statements(variables + [expression]),
compiled[0],
js.statements(compiled[1]),
}
else
compiled =
Expand Down
28 changes: 28 additions & 0 deletions src/compilers/enum_destructuring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Mint
class Compiler
def _compile(node : Ast::EnumDestructuring, variable : String) : Tuple(String, Array(String))
variables =
case lookups[node].as(Ast::EnumOption).parameters[0]?
when Ast::EnumRecordDefinition
node.parameters.compact_map do |param|
case param
when Ast::TypeVariable
"const #{js.variable_of(param)} = #{variable}._0.#{param.value}"
end
end
else
node.parameters.map_with_index do |param, index1|
"const #{js.variable_of(param)} = #{variable}._#{index1}"
end
end

name =
js.class_of(lookups[node])

{
"#{variable} instanceof #{name}",
variables,
}
end
end
end
15 changes: 15 additions & 0 deletions src/compilers/tuple_destructuring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Mint
class Compiler
def _compile(node : Ast::TupleDestructuring, variable : String) : Tuple(String, Array(String))
variables =
node
.parameters
.join(',') { |param| js.variable_of(param) }

{
"Array.isArray(#{variable})",
["const [#{variables}] = #{variable}"],
}
end
end
end

0 comments on commit 63f0cac

Please sign in to comment.