kaitai_struct_compiler icon indicating copy to clipboard operation
kaitai_struct_compiler copied to clipboard

Add Nim to TranslatorSpec and fix various errors found thanks to that

Open Mingun opened this issue 1 year ago • 0 comments

This PR includes #291, because Nim octal escapes will consume all possible digits, so string with escape sequence followed by digit will be treated incorrectly. Hex escape form does not suffer from such problem. Instead of overriding logic in Nim I decided to make a Hex representation as default because most languages have it. If you do not agree, please tell me and I change that.

This PR fixes the following bugs found during testing:

  • Bitwise NOT operation can glue to the number because in Nim it is represented as a not keyword and require a space after it, so ~777 not become identifier not777. Lua was suffer from the same problem.
  • Removed extra parenthesis around ternary operator (represented by if ...: ... else: ... expression), casts and string concatenation (& operator)
  • Like Python, Nim have different operators for integer and float division, div and / accordingly. Attempt to use div with float numbers lead to a compilation error. This was fixed in the same way as Python does that by inspecting deduced types of an arguments
  • .first, .last, .to_i, .substring methods called on expressions does not wrap them into parenthesis, so (1 + 2).to_i becomes 1 + 2.to_i

I checked all generated expressions on https://play.nim-lang.org/ so I assume that their all are correct.

I also notice a strange thing. Nim has an extendable $ operator to converting everything to a string. However, .to_s implementation generates intToStr(int(value)) which means conversion to integer, then conversion to string. It seems that it is possible to replace that stuff with $ operator stuff, but maybe there are some nuances. @sealmove, you a created Nim implementation, can you check, is that possible?

Mingun avatar Mar 28 '24 18:03 Mingun