-
Notifications
You must be signed in to change notification settings - Fork 68
Add equals function to better calculate equality #395
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -253,7 +253,19 @@ defmodule ElixirScript.Translate.Form do | |
| {ast, state} | ||
| end | ||
|
|
||
| def compile({{:., _, [:erlang, op]}, _, [left, right]}, state) when op in [:+, :-, :*, :/, :==, :>, :<, :>=] do | ||
| def compile({{:., _, [:erlang, op]}, _, [left, right]}, state) when op in [:==, :===] do | ||
| ast = Helpers.call( | ||
| J.member_expression( | ||
| Helpers.core_module("erlang"), | ||
| J.identifier("equals") | ||
| ), | ||
| [compile!(left, state), compile!(right, state)] | ||
| ) | ||
|
|
||
| {ast, state} | ||
| end | ||
|
|
||
| def compile({{:., _, [:erlang, op]}, _, [left, right]}, state) when op in [:+, :-, :*, :/, :>, :<, :>=] do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functions should have a @SPEC type specification. |
||
| ast = J.binary_expression( | ||
| op, | ||
| compile!(left, state), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,15 +10,15 @@ function copy(subject, n = 1) { | |
| } | ||
|
|
||
| function first(subject) { | ||
| if (subject.length == 0) { | ||
| throw new Error(`Binary is of length 0`); | ||
| if (subject.length === 0) { | ||
| throw new Error('Binary is of length 0'); | ||
| } | ||
| return at(subject, 0); | ||
| } | ||
|
|
||
| function last(subject) { | ||
| if (subject.length == 0) { | ||
| throw new Error(`Binary is of length 0`); | ||
| if (subject.length === 0) { | ||
| throw new Error('Binary is of length 0'); | ||
| } | ||
| return subject.slice(-1); | ||
| } | ||
|
|
@@ -29,20 +29,19 @@ function list_to_bin(bytelist) { | |
|
|
||
| function part(subject, posOrTuple, len = null) { | ||
| if (len === null) { | ||
| var pos; | ||
| [pos, len] = posOrTuple.values; | ||
| return subject.substr(pos, len); | ||
| } else { | ||
| return subject.substr(posOrTuple, len); | ||
| const [pos, theLen] = posOrTuple.values; | ||
| return subject.substr(pos, theLen); | ||
| } | ||
|
|
||
| return subject.substr(posOrTuple, len); | ||
| } | ||
|
|
||
| //TODO: Support more options | ||
| //TODO: pattern cannot be list of strings | ||
| // TODO: Support more options | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
| // TODO: pattern cannot be list of strings | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
| function replace(subject, pattern, replacement, options = []) { | ||
| const opt_global = proplists.get_value(Symbol.for('global'), options); | ||
|
|
||
| var regex; | ||
| let regex; | ||
| if (opt_global !== Symbol.for('undefined')) { | ||
| regex = new RegExp(pattern, 'g'); | ||
| } else { | ||
|
|
@@ -52,8 +51,8 @@ function replace(subject, pattern, replacement, options = []) { | |
| return subject.replace(regex, replacement); | ||
| } | ||
|
|
||
| //TODO: Support more options, global is implied | ||
| //TODO: pattern cannot be list of strings | ||
| // TODO: Support more options, global is implied | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
| // TODO: pattern cannot be list of strings | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
| function split(subject, pattern, options = []) { | ||
| return subject.split(pattern); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,103 @@ function list_subtraction(list1, list2) { | |
| return list; | ||
| } | ||
|
|
||
| function arrayEquals(left, right) { | ||
| if (!Array.isArray(right)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (left.length !== right.length) { | ||
| return false; | ||
| } | ||
|
|
||
| for (let i = 0; i < left.length; i++) { | ||
| if (equals(left[i], right[i]) === false) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'equals' was used before it was defined. |
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function tupleEquals(left, right) { | ||
| if (right instanceof ErlangTypes.Tuple === false) { | ||
| return false; | ||
| } | ||
|
|
||
| if (left.length !== right.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return arrayEquals(left.values, right.values); | ||
| } | ||
|
|
||
| function bitstringEquals(left, right) { | ||
| if (right instanceof ErlangTypes.BitString === false) { | ||
| return false; | ||
| } | ||
|
|
||
| if (left.length !== right.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return arrayEquals(left.value, right.value); | ||
| } | ||
|
|
||
| function pidEquals(left, right) { | ||
| if (right instanceof ErlangTypes.PID === false) { | ||
| return false; | ||
| } | ||
|
|
||
| return left.id === right.id; | ||
| } | ||
|
|
||
| function referenceEquals(left, right) { | ||
| if (right instanceof ErlangTypes.Reference === false) { | ||
| return false; | ||
| } | ||
|
|
||
| return left.id === right.id; | ||
| } | ||
|
|
||
| function mapEquals(left, right) { | ||
| if (right instanceof Map === false) { | ||
| return false; | ||
| } | ||
|
|
||
| const leftEntries = Array.from(left.entries()); | ||
| const rightEntries = Array.from(right.entries()); | ||
|
|
||
| return arrayEquals(leftEntries, rightEntries); | ||
| } | ||
|
|
||
| function equals(left, right) { | ||
| if (Array.isArray(left)) { | ||
| return arrayEquals(left, right); | ||
| } | ||
|
|
||
| if (left instanceof ErlangTypes.Tuple) { | ||
| return tupleEquals(left, right); | ||
| } | ||
|
|
||
| if (left instanceof ErlangTypes.PID) { | ||
| return pidEquals(left, right); | ||
| } | ||
|
|
||
| if (left instanceof ErlangTypes.BitString) { | ||
| return bitstringEquals(left, right); | ||
| } | ||
|
|
||
| if (left instanceof ErlangTypes.Reference) { | ||
| return referenceEquals(left, right); | ||
| } | ||
|
|
||
| if (left instanceof Map) { | ||
| return mapEquals(left, right); | ||
| } | ||
|
|
||
| return left === right; | ||
| } | ||
|
|
||
| function div(left, right) { | ||
| return left / right; | ||
| } | ||
|
|
@@ -473,4 +570,5 @@ export default { | |
| list_to_binary, | ||
| nodes, | ||
| function_exported, | ||
| equals, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functions should have a @SPEC type specification.