kaitai_struct_compiler
kaitai_struct_compiler copied to clipboard
Add Nim to TranslatorSpec and fix various errors found thanks to that
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
notkeyword and require a space after it, so~777not become identifiernot777. 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,
divand/accordingly. Attempt to usedivwith 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,.substringmethods called on expressions does not wrap them into parenthesis, so(1 + 2).to_ibecomes1 + 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?