nim-lang / RFCs

A repository for your Nim proposals.
136 stars 23 forks source link

user defined quote literals, eg: -12.3E+4022'dec128 ; -128'i8 is transformed as `i8("-128")` #228

Closed timotheecour closed 3 years ago

timotheecour commented 4 years ago

summary

details

benefits

examples

all these types can be implemented as library solution and preserve nice native looking syntax, and also, would render as numerical types, not strings (pending updating syntax highlighters, including github linguist, as evidenced by ugly highlighting in this post)

note

VM

likewise for VM: TFullReg could be simplified as:

  TFullReg* = object
    case kind*: TRegisterKind
    of rkNone: nil
    # of rkInt: intVal*: BiggestInt
    # of rkFloat: floatVal*: BiggestFloat
    of rkLit:
      value*: uint64 #
      typ*: PType
...

with following benefits:

Araq commented 4 years ago

Pretty nice but please work out how backwards compat can work. Having to patch existing macros is not acceptable. Been there, done that, it's expensive and a desaster for the ecosystem.

JohnAD commented 3 years ago

It is my intent to have a PR for this placed before the end of February 2021. Perhaps sooner as I already have it working; I just need to write more test cases to confirm it handles more border conditions and errors gracefully.

However my solution takes a somewhat different tack: rather than combine all numeric literals into a single token for later parsing, my solution leaves the current literals as-is and adds a new one: tkStrNumPrefixLit. (I'm not convinced that is a good name. I may shorten it to tkStrNumLit.) The tokenizer only assigns this token when it peeks and sees an identifier adjacent to the number. The tokenizer scans the identifier on the next pass.

In the parser, when the new token is seen it attempts to run the dotExpr function to resolve it. Essentially turning:

var a = 1234.56E7m     # or 1234.56E7'm

into

var A = "1234.56E7".m

My motive is to allow this function to enable the IEEE 745 decimal library (#308) I'm writing to also use this. Specifically "m" will be the suffix (a convention used in C# and a few other languages.) One of my goals to have this fully convert/resolve at compile-time.

The f32, f64, u8, etc suffixes will still be built-in to the lexer and will take priority over any proc/func/template.

Because this adds a token, other files such as docgen.nim etc will also need update, but those updates will be minimal. These changes do not modify the AST structure in any way, so macros should behave the same.

I will also make updates to documentation.

Admittedly, my solution is not as "pure" and comprehensive as yours. But it could be an intermediate place to make it more pure with another later PR.

Araq commented 3 years ago

https://github.com/nim-lang/Nim/pull/17489 will soon be merged. This RFC has been implemented.