Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inner destructuring #283

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions spec/compilers/case_with_array_destructuring
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class A extends _C {
} else if (_compare(a, [`a`])) {
return ``
} else if (Array.isArray(a) && a.length === 1) {
const [b] = a;
const a__arr = Array.from(a);
const b = a__arr[0];
return ``;
} else if (Array.isArray(a) && a.length === 2) {
const [c,d] = a;
const a__arr = Array.from(a);
const c = a__arr[0];
const d = a__arr[1];
return ``;
} else if (Array.isArray(a) && a.length >= 2) {
const __ = Array.from(a);
const e = __.shift();
const f = __.pop();
const g = __;
} else if (Array.isArray(a) && a.length >== 2) {
const a__arr = Array.from(a);
const e = a__arr.shift();
const f = a__arr.pop();
const g = a__arr;
return ``;
};
})();
Expand Down
4 changes: 3 additions & 1 deletion spec/compilers/case_with_tuple_destructuring
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class A extends _C {
} else if (_compare(a, [`A`, 0, true])) {
return `A`
} else if (Array.isArray(a)) {
const [b,c,d] = a;
const b = a[0];
const c = a[1];
const d = a[2];
return b;
};
})();
Expand Down
2 changes: 2 additions & 0 deletions src/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Mint

alias TypeOrVariable = Type | TypeVariable

alias DestructuringType = ArrayDestructuring | EnumDestructuring | TupleDestructuring
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary and should be replaced with Node


alias Expression = ParenthesizedExpression |
NegatedExpression |
InlineFunction |
Expand Down
4 changes: 3 additions & 1 deletion src/ast/array_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Mint
class Ast
class ArrayDestructuring < Node
alias ArrayDestructuringItem = DestructuringType | Variable | Spread

getter items

def initialize(@items : Array(Node),
def initialize(@items : Array(ArrayDestructuringItem),
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
4 changes: 3 additions & 1 deletion src/ast/enum_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Mint
class Ast
class EnumDestructuring < Node
alias EnumDestructuringParameter = DestructuringType | TypeVariable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not necessary.


getter name, option, parameters

def initialize(@parameters : Array(TypeVariable),
def initialize(@parameters : Array(EnumDestructuringParameter),
@option : String,
@name : String,
@input : Data,
Expand Down
4 changes: 3 additions & 1 deletion src/ast/tuple_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Mint
class Ast
class TupleDestructuring < Node
alias TupleDestructuringParameter = DestructuringType | Variable

getter parameters

def initialize(@parameters : Array(Variable),
def initialize(@parameters : Array(TupleDestructuringParameter),
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
82 changes: 55 additions & 27 deletions src/compilers/array_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
module Mint
class Compiler
def _compile(node : Ast::ArrayDestructuring, value)
private macro compile_item!(initial_var, item = item, index = index1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this needs to be a macro?

var_name = js.variable_of({{ item }})
vars = [
{{ initial_var }}
]

res = _compile_destructuring {{ item }}, var_name, "#{condition_var_name}[#{{{ index }}}]"
if res
condition += " && " + res[1]
vars.concat(res[0])
end

vars
end

def _compile(node : Ast::ArrayDestructuring, start_variable : String, condition_variable : String? = nil)
variable = "#{start_variable}__arr"

condition_var_name = condition_variable || start_variable
if node.spread?
statements = [
"const __ = Array.from(#{value})",
]

node
.items
.take_while(&.is_a?(Ast::Variable))
.each do |var|
statements << "const #{js.variable_of(var)} = __.shift()"
end

node
.items
.reverse
.take_while(&.is_a?(Ast::Variable))
.each do |var|
statements << "const #{js.variable_of(var)} = __.pop()"
end

statements << "const #{js.variable_of(node.items.select(Ast::Spread).first.variable)} = __"
statements
condition = "Array.isArray(#{condition_var_name}) && #{condition_var_name}.length >== #{node.items.size - 1}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be condition = if makes it easier to read.

else
variables =
node
condition = "Array.isArray(#{condition_var_name}) && #{condition_var_name}.length === #{node.items.size}"
end

variables = [
"const #{variable} = Array.from(#{start_variable})",
] + (
unless node.spread?
node.items.map_with_index do |item, index1|
compile_item! "const #{var_name} = #{variable}[#{index1}]"
end.flatten
else
tmp = [] of Array(String)

start_vars = node
.items
.join(',') { |param| js.variable_of(param) }
.take_while { |item| !item.is_a?(Ast::Spread) }
.map_with_index do |item, index1|
compile_item! "const #{var_name} = #{variable}.shift()"
end

["const [#{variables}] = #{value}"]
end
end_vars = node
.items
.reverse
.take_while { |item| !item.is_a?(Ast::Spread) }
.map_with_index do |item, index1|
compile_item! "const #{var_name} = #{variable}.pop()"
end

tmp += start_vars if start_vars
tmp += end_vars if end_vars

tmp << ["const #{js.variable_of(node.items.select(Ast::Spread).first.variable)} = #{variable}"]
tmp.flatten
end
)

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

if match = node.match
case match
when Ast::ArrayDestructuring
statements =
_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) }

tmp = case match
when Ast::ArrayDestructuring
_compile match, variable
when Ast::TupleDestructuring
_compile match, variable
when Ast::EnumDestructuring
_compile match, variable
else
end
if tmp
{
"Array.isArray(#{variable})",
js.statements([
"const [#{variables}] = #{variable}",
tmp[1],
js.statements(tmp[0].concat([
expression,
]),
}
when Ast::EnumDestructuring
variables =
match.parameters.map_with_index do |param, index1|
"const #{js.variable_of(param)} = #{variable}._#{index1}"
end

name =
js.class_of(lookups[match])

{
"#{variable} instanceof #{name}",
js.statements(variables + [expression]),
])),
}
else
compiled =
Expand Down
16 changes: 16 additions & 0 deletions src/compilers/destructuring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Mint
class Compiler
def _compile_destructuring(node : Ast::Node, variable : String, condition_variable : String? = nil)
case node
when Ast::ArrayDestructuring
_compile node, variable, condition_variable
when Ast::TupleDestructuring
_compile node, variable, condition_variable
when Ast::EnumDestructuring
_compile node, variable, condition_variable
else
# ignore
end
end
end
end
27 changes: 27 additions & 0 deletions src/compilers/enum_destructuring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Mint
class Compiler
def _compile(node : Ast::EnumDestructuring, variable : String, condition_variable : String? = nil)
name =
js.class_of(lookups[node])

condition_var_name = condition_variable || variable
condition = "#{condition_var_name} instanceof #{name}"

variables = node.parameters.map_with_index do |param, index1|
var_name = js.variable_of(param)
vars = ["const #{var_name} = #{variable}._#{index1}"]

res = _compile_destructuring param, var_name, "#{condition_var_name}._#{index1}"

if res
condition += " && " + res[1]
vars.concat(res[0])
end

vars
end.flatten

{variables, condition}
end
end
end
25 changes: 25 additions & 0 deletions src/compilers/tuple_destructuring.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Mint
class Compiler
def _compile(node : Ast::TupleDestructuring, variable : String, condition_variable : String? = nil) : {Array(String), String}
condition_var_name = condition_variable || variable
condition = "Array.isArray(#{condition_var_name})"

variables =
node.parameters.map_with_index do |param, index1|
var_name = js.variable_of(param)
vars = ["const #{var_name} = #{variable}[#{index1}]"]

res = _compile_destructuring param, var_name, "#{condition_var_name}[#{index1}]"

if res
condition += " && " + res[1]
vars.concat(res[0])
end

vars
end.flatten

{variables, condition}
end
end
end
11 changes: 11 additions & 0 deletions src/messages/enum_destructuring_expected_closing_parentheses.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
message EnumDestructuringExpectedClosingParentheses do
title "Syntax Error"

block do
text "I was looing for the"
code ")"
text "after paranthesed parameter in enum destructuring, but found"
code got
text "instead."
end
end
9 changes: 6 additions & 3 deletions src/parsers/array_destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint
start do |start_position|
head = start do
skip unless char! '['
value = variable || spread
value = enum_destructuring || tuple_destructuring || array_destructuring || variable || spread
whitespace
keyword ","
whitespace
Expand All @@ -16,8 +16,8 @@ module Mint
skip unless head

items =
[head.as(Ast::Node)].concat(list(terminator: ']', separator: ',') do
variable || spread
[head.as(Ast::ArrayDestructuring::ArrayDestructuringItem)].concat(list(terminator: ']', separator: ',') do
value = enum_destructuring || tuple_destructuring || array_destructuring || variable || spread
end.compact)

whitespace
Expand All @@ -31,5 +31,8 @@ module Mint
input: data)
end
end

def parse_destructuring_item
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left over empty method.

end
end
end
12 changes: 11 additions & 1 deletion src/parsers/enum_destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Mint
class Parser
syntax_error EnumDestructuringExpectedDoubleColon
syntax_error EnumDestructuringExpectedOption
syntax_error EnumDestructuringExpectedClosingParentheses

def enum_destructuring
start do |start_position|
Expand All @@ -11,7 +12,16 @@ module Mint

option = type_id! EnumDestructuringExpectedOption

parameters = many { type_variable }.compact
parameters = (many do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have only one syntax, in this case with parentheses. This will be a breaking change, but would fix the else branch since there it's not possible to inner destructure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to have a comma between parameters?

if char == '('
parended = char! '('
param = enum_destructuring || tuple_destructuring || array_destructuring || type_variable
char(')', EnumDestructuringExpectedClosingParentheses) if parended
else
param = type_variable.try(&.as Ast::Node)
end
param
end).compact

Ast::EnumDestructuring.new(
parameters: parameters,
Expand Down
6 changes: 4 additions & 2 deletions src/parsers/tuple_destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mint
start do |start_position|
head = start do
skip unless char! '{'
value = variable
value = enum_destructuring || tuple_destructuring || array_destructuring || variable
whitespace
char! ','
whitespace
Expand All @@ -16,7 +16,9 @@ module Mint
skip unless head

parameters = [head].concat(
list(terminator: '}', separator: ',') { variable }.compact)
list(terminator: '}', separator: ',') do
enum_destructuring || tuple_destructuring || array_destructuring || variable
end.compact)

whitespace

Expand Down
Loading