Skip to content

Commit 165ece6

Browse files
committed
Fixed bug with headTail pattern matching preventing more advanced matches
1 parent e76c71c commit 165ece6

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

lib/elixir_script/translator/module.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ defmodule ElixirScript.Translator.Module do
5757
body: imports ++ structs ++ private_functions ++ exported_functions ++ body ++ [default]
5858
}
5959

60-
IO.inspect(result.body)
61-
6260
result
6361
end
6462

lib/elixir_script/translator/pattern_matching.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ defmodule ElixirScript.Translator.PatternMatching do
6565
)
6666
end
6767

68-
def head_tail() do
68+
def head_tail(headParameter, tailParameter) do
6969
JS.call_expression(
7070
@head_tail,
71-
[]
71+
[headParameter, tailParameter]
7272
)
7373
end
7474

@@ -138,7 +138,11 @@ defmodule ElixirScript.Translator.PatternMatching do
138138
end
139139

140140
defp do_build_match([{:|, _, [head, tail]}], env) do
141-
{ [head_tail()], [Translator.translate!(head, env), Translator.translate!(tail, env)] }
141+
{ head_patterns, head_params } = do_build_match(head, env)
142+
{ tail_patterns, tail_params } = do_build_match(tail, env)
143+
params = head_params ++ tail_params
144+
145+
{ [head_tail(hd(head_patterns), hd(tail_patterns))], params }
142146
end
143147

144148
defp do_build_match({:<>, _, [prefix, value]}, env) do

src/javascript/lib/core/bit_string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BitString {
66

77
this.value = Object.freeze(this.process(args));
88
this.length = this.value.length;
9-
this.bit_size = this.raw_value.reduce((prev, current) => prev + current.size);
9+
this.bit_size = this.raw_value().reduce((prev, current) => prev + current.size);
1010
this.byte_size = (this.bit_size / 8) + (this.bit_size % 8 > 0 ? 1 : 0);
1111
}
1212

src/javascript/lib/core/patterns/resolvers.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ function resolveVariable(): Function {
6464
};
6565
}
6666

67-
function resolveHeadTail(): Function {
67+
function resolveHeadTail(pattern: Types.HeadTail): Function {
68+
const headMatches = buildMatch(pattern.head);
69+
const tailMatches = buildMatch(pattern.tail);
70+
6871
return function(value: any, args: Array<any>): boolean {
6972
if(!Checks.is_array(value) || value.length < 2){
7073
return false;
@@ -73,10 +76,9 @@ function resolveHeadTail(): Function {
7376
const head = value[0];
7477
const tail = value.slice(1);
7578

76-
args.push(head);
77-
args.push(tail);
78-
79-
return true;
79+
if(headMatches(head, args) && tailMatches(tail, args)){
80+
return true;
81+
}
8082
};
8183
}
8284

src/javascript/lib/core/patterns/types.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export class Capture {
2929
}
3030

3131
export class HeadTail {
32-
constructor() {
32+
head: any;
33+
tail: any;
34+
35+
constructor(head: any = null, tail: any = null) {
36+
this.head = head;
37+
this.tail = tail;
3338
}
3439
}
3540

@@ -67,8 +72,8 @@ export function capture(value: any): Capture {
6772
return new Capture(value);
6873
}
6974

70-
export function headTail(): HeadTail {
71-
return new HeadTail();
75+
export function headTail(head: any = null, tail: any = null): HeadTail {
76+
return new HeadTail(head, tail);
7277
}
7378

7479
export function type(type: any, objPattern: Object = {}): Type {

src/javascript/tests/patterns/defmatch.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('defmatch', () => {
123123

124124
let fn = Patterns.defmatch(
125125
Patterns.make_case(
126-
[Patterns.headTail()],
126+
[Patterns.headTail($, $)],
127127
(head, tail) => tail
128128
)
129129
);

0 commit comments

Comments
 (0)