Skip to content
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

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Mingun
Copy link
Contributor

@Mingun Mingun commented Mar 28, 2024

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 Mingun changed the title Add Nim to TranslatorSpec and fix variaus errors found thanks to that Add Nim to TranslatorSpec and fix various errors found thanks to that Mar 28, 2024
Octal escape sequences the least used form of escape sequences and hex supported everywhere.
The only outsiders are Java, C++ and Rust
BaseTranslator does not insert space between operator and expression.
When expression is a number, "~<number>" and "!<number>" becomes "not<number>" which is an identifier

Fixes test (1):
```
[info]   - nim:~777 *** FAILED ***
[info]     "not[]777" was not equal to "not[ ]777" (TranslatorSpec.scala:183)
[info]     Analysis:
[info]     "not[]777" -> "not[ ]777"
```
Integer division operator is `div`
Float   division operator is `/`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant