arturo-lang / arturo

Simple, expressive & portable programming language for efficient scripting
http://arturo-lang.io
MIT License
723 stars 32 forks source link

[VM/values/comparison] add `<`/`>` support for Binary values #1158

Open github-actions[bot] opened 1 year ago

github-actions[bot] commented 1 year ago

[VM/values/comparison] add </> support for Binary values currently, = is supported but not < and >! Preferrably, the implementation should go to values/custom/vbinary and then integrate it here. see also: https://github.com/arturo-lang/arturo/pull/1139

https://github.com/arturo-lang/arturo/blob/269a723e23971f7c5ba310f6c3539852a6f016e3/src/vm/values/comparison.nim#L265

#  labels: critical,bug,values,easy

# TODO(VM/values/comparison) add `<`/`>` support for Attribute values
#  currently, `=` is supported but not `<` and `>`!
#  the logic should be the same as with normal strings/words/etc
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,easy

# TODO(VM/values/comparison) add `<`/`>` support for AttributeLabel values
#  currently, `=` is supported but not `<` and `>`!
#  the logic should be the same as with normal strings/words/etc
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,easy

# TODO(VM/values/comparison) how should we handle Dictionary values?
#  right now, both `<` and `>` simply return false
#  but is it even a normal idea to compare a Dictionary with something else, or
#  another dictionary for that matter?
#  How do other languages (e.g. Ruby, Python) handle this?
#  If the final decision is to not support this, then we should throw an appropriate
#  error message (e.g. "cannot compare Dictionary values"), along with an appropriate
#  (new) error template at VM/errors
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: enhancement,values,error handling,open discussion

# TODO(VM/values/comparison) how should we handle Object values?
#  right now, Object make feature a custom `compare` method which is called an used
#  for the comparison. However, if there is no such method, we end up having the exact
#  same issues we have with Dictionaries. So, how is that to be dealt with?
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: enhancement,values,error handling,open discussion

# TODO(VM/values/comparison) add `<`/`>` support for Path values?
#  currently, `=` is supported but not `<` and `>`!
#  the logic should be the same as with blocks, as it's - internally -
#  technically the exact same thing.
#  But then again, it seems a bit weird as a notion. Any ideas?
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,open discussion

# TODO(VM/values/comparison) add `<`/`>` support for PathLabel values?
#  currently, `=` is supported but not `<` and `>`!
#  the logic should be the same as with blocks, as it's - internally -
#  technically the exact same thing.
#  But then again, it seems a bit weird as a notion. Any ideas?
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,open discussion

# TODO(VM/values/comparison) add `<`/`>` support for Regex values?
#  currently, `=` is supported but not `<` and `>`!
#  Another tricky one: the logic should be either the same as with strings,
#  or disallow comparison altogether and throw an error.
#  In the first case, since Regex values encapsulate a VRegex object (from values/custom/vregex)
#  the ideal implementation would be done there (adding a `>` and `<` operator to VRegex)
#  and then link the method here :-)
#  labels: bug,values,open discussion

# TODO(VM/values/comparison) add `<`/`>` support for Binary values
#  currently, `=` is supported but not `<` and `>`!
#  Preferrably, the implementation should go to values/custom/vbinary
#  and then integrate it here.
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,easy

# TODO(VM/values/comparison) add `<`/`>` support for Range values
#  currently, `=` is supported but not `<` and `>`!
#  Preferrably, the implementation should go to values/custom/vrange
#  and then integrate it here.
#  The question is: when is range A smaller than range B? How do other
#  languages handle this?
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,open discussion

# TODO(VM/values/comparison) add `<`/`>` support for Inline values?
#  currently, `=` is supported but not `<` and `>`!
#  since Inline values are pretty much identical to Block values - internally -
#  comparison operators should work for them in an identical fashion.
#  The question is: when is Inline A smaller than Inline B? How do other
#  languages handle this?
#  see also: https://github.com/arturo-lang/arturo/pull/1139
#  labels: bug,values,open discussion

# TODO(VM/values/comparison) Re-visit/test handling of Block comparisons
#  How do other languages (e.g. Python, Ruby, etc) handle comparisons between blocks/arrays?
#  Do they even support it? If so, how?
#  labels: enhancement,values,open discussion,unit-test

proc `<`*(x: Value, y: Value): bool {.inline.}=
    if x.kind in {Integer, Floating, Rational} and y.kind in {Integer, Floating, Rational}:
        if x.kind==Integer:

a8557c13f4142938288329165fb14d20ee5b121f

RickBarretto commented 1 year ago

I have more questions than suggestions for this:

How should it work? Converting a binary to integer? If yes, should this integer be signed or unsigned? What should happen if my binary's size is other than a multiple of 2, let me say, 3 bits, for instance?

drkameleon commented 1 year ago

The :binary type in Arturo is actually a Block of bytes. And... every byte is just that a byte, or 8 bits. (In general, you may access an individual bit, but you cannot define anything, a concrete value, as having "3 bits"... everything, structurally has to be a multiple of 8 bits or a byte)

Now, that being said, and given that the underlying values are actually bytes (that is an unsigned 8-bit int: https://nim-lang.org/docs/system.html#byte), what would make sense for </> testing would be to sequentially compare the individual bytes.

So,... with a: [10 11 12 13 14] and b: [10 20 12 23 23], I'd think of sth like:

And this wouldn't be related to the bytearray sizes at all.

In any case, if you want to have a look into how other languages deal with it, there could be something we are missing 😉

RickBarretto commented 1 year ago

So,... with a: [10 11 12 13 14] and b: [10 20 12 23 23], I'd think of sth like:

  • compare the first element of A with the first element of B
  • they're equal, so let's move on
  • compare the secodn element of A wi th the second element of B
  • a[1] < b[1], so I guess this would mean a < b

I'm curious now, should it work with the same process for blocks and inlines?

RickBarretto commented 1 year ago

And this wouldn't be related to the bytearray sizes at all.

Got it! Thanks for the explanation!

In any case, if you want to have a look into how other languages deal with it, there could be something we are missing 😉

All right!

stale[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.