Skip to content

Commit

Permalink
Fixed serious bug in Matching
Browse files Browse the repository at this point in the history
NOTE: it is important to understand that a null value matches and that
improper lists are used in matching to identify tail matching
  • Loading branch information
osolodo committed Mar 28, 2018
1 parent 7a6fafb commit 6a22422
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
13 changes: 4 additions & 9 deletions src/erl/asttrans.erl
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,8 @@ recurse_var_assignments(ConsCount, {c_var, _, Name}, V, isTail) ->
<<"=">>,
estree:identifier(atom_to_binary(Name, utf8)),
estree:call_expression(
estree:member_expression(V, estree:identifier(<<"slice">>), false),
[ estree:literal(ConsCount),
estree:call_expression(
estree:member_expression(V, estree:identifier(<<"size">>), false),
[]
)
]
estree:member_expression(V, estree:identifier(<<"nthSeg">>), false),
[estree:literal(ConsCount)]
)
)
];
Expand All @@ -802,13 +797,13 @@ recurse_var_assignments(_, {c_tuple, _, Elements}, V, _) ->
lists:append(
L,
recurse_var_assignments(0, Elem,
estree:expression_statement(
%estree:expression_statement(
estree:call_expression(
estree:member_expression(V,
estree:identifier(<<"nth">>), false),
[estree:literal(I)]
)
),
,%),
false
)
)}
Expand Down
4 changes: 2 additions & 2 deletions src/js/classes/datatype_int.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const Int = (() => {
}

match(other) {
if (other===null||(!isNaN(other) && this.equals(other))) {
return other;
if (other==null||(!isNaN(other) && this.equals(other))) {
return this;
}
else {
return undefined;
Expand Down
44 changes: 28 additions & 16 deletions src/js/classes/datatype_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const List = (() => {
[nthNode](n) {
if (n < 0 || n >= this.size()) {
throw "index out of bounds error";
}
}

let i = 0;
let walker = this;
Expand All @@ -58,7 +58,7 @@ const List = (() => {
next: () => {
// If the next node of the current iterator isn't another list OR is an empty list, then we know
// we have reached the end of the linked list
let isLastNode = this.iterator.next === undefined || List.isEmptyList(this.iterator.next);
let isLastNode = !this.iterator || this.iterator.next === undefined || List.isEmptyList(this.iterator.next);
let v = List.isList(this.iterator) ? this.iterator.value : this.iterator;

if (this.iterator === "done" || List.isEmptyList(this)) {
Expand All @@ -83,6 +83,10 @@ const List = (() => {
return List.isList(nth) ? nth.value : nth;
}

nthSeg(n){
return this[nthNode](n);
}

size() {
return [...this].length;
}
Expand Down Expand Up @@ -128,21 +132,29 @@ const List = (() => {
}

match(other) {
if(other===null)return other;
if (List.isList(other) && this.size() === other.size()) {
for (let i = 0; i < this.size(); i++) {
if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
if (this.nth(i).match(other.nth(i)) === undefined) {
return undefined;
}
}
else {
if (this.nth(i) !== other.nth(i) && other.nth(i) !== null) {
return undefined;
}
}
if(other===null)return this;
if (List.isList(other)) {

if(this.nth(0).match(other.nth(0))){//If the first values match
if(other.next==null)return this;//Improper list (tail match)
let a= this.next().match(other.next());
if(a!=undefined) return this;
}
return other;
return undefined;
// for (let i = 0; i < this.size(); i++) {
// if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
// if(other===null)return this;
// if (this.nth(i).match(other.nth(i)) === undefined) {
// return undefined;
// }
// }
// else {
// if (this.nth(i) !== other.nth(i) && other.nth(i) !== null) {
// return undefined;
// }
// }
// }
// return other;
}
else {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/js/classes/datatype_tuple.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const Tuple = (() => {

match(other) {
if(other===null)return other;
if (List.isTuple(other) && this.size() === other.size()) {
if (Tuple.isTuple(other) && this.size() === other.size()) {
for (let i = 0; i < this.size(); i++) {
if (ErlangDatatype.isErlangDatatype(this.nth(i))) {
if (this.nth(i).match(other.nth(i)) === undefined) {
Expand Down

0 comments on commit 6a22422

Please sign in to comment.