niv / neverwinter.nim

CLI tools and nim library used in Neverwinter Nights: Enhanced Edition development
MIT License
133 stars 29 forks source link

Add hash string literal constants #121

Closed mtijanic closed 7 months ago

mtijanic commented 7 months ago

int n = h"hello"; is a compile time equivalent of int n = HashString("hello");. As with raw strings, both h"" and H"" are accepted.

Similar to raw strings, it works as a hack on the lexer. The string is parsed like a regular string would be and then when the end is found, the whole token gets replaced with 0x12345678 hex integer token containing the hash value. Then, the parser just sees an integer instead.

What this doesn't allow for is hashing non-literal constants at compile time, like:

const string S = "foo";
const int H = h(S);

That would require an operator and then constant folding. I was thinking of overloading unary ~ for strings to do this, but it would require VM changes as well.

Testing

Added test case to constants.nss

Changelog

Added

Licence