From 6a224221c469280361a899fa6b4cfab5de21b246 Mon Sep 17 00:00:00 2001 From: osolodo Date: Wed, 28 Mar 2018 16:19:15 +0100 Subject: [PATCH] Fixed serious bug in Matching NOTE: it is important to understand that a null value matches and that improper lists are used in matching to identify tail matching --- src/erl/asttrans.erl | 13 +++------- src/js/classes/datatype_int.js | 4 +-- src/js/classes/datatype_list.js | 44 ++++++++++++++++++++------------ src/js/classes/datatype_tuple.js | 2 +- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/erl/asttrans.erl b/src/erl/asttrans.erl index 63b95c0..e4f4b42 100644 --- a/src/erl/asttrans.erl +++ b/src/erl/asttrans.erl @@ -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)] ) ) ]; @@ -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 ) )} diff --git a/src/js/classes/datatype_int.js b/src/js/classes/datatype_int.js index 182470d..6b04faf 100644 --- a/src/js/classes/datatype_int.js +++ b/src/js/classes/datatype_int.js @@ -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; diff --git a/src/js/classes/datatype_list.js b/src/js/classes/datatype_list.js index c7f19bf..7c8c424 100644 --- a/src/js/classes/datatype_list.js +++ b/src/js/classes/datatype_list.js @@ -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; @@ -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)) { @@ -83,6 +83,10 @@ const List = (() => { return List.isList(nth) ? nth.value : nth; } + nthSeg(n){ + return this[nthNode](n); + } + size() { return [...this].length; } @@ -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; diff --git a/src/js/classes/datatype_tuple.js b/src/js/classes/datatype_tuple.js index fd36a31..800426d 100644 --- a/src/js/classes/datatype_tuple.js +++ b/src/js/classes/datatype_tuple.js @@ -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) {