this post was submitted on 16 Oct 2024
86 points (100.0% liked)

Programmer Humor

32448 readers
915 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

So, this uses a macro, but if you're thinking anything is possible with a macro, it's actually not in Rust. The input does still need to parse as valid Rust tokens.

Which means the authors asked themselves at some point: Is the Rust syntax a superset of the Python syntax?
And well, it's not. In particular, some Python keywords will just be tokenized as an identifier (like a variable name).

But it is close enough that the authors decided against requiring a massive string to be passed in, which does amuse me. πŸ™ƒ

top 10 comments
sorted by: hot top controversial new old
[–] cabhan@discuss.tchncs.de 55 points 3 weeks ago (1 children)

With projects like these, I'm always torn between thinking that it's cool it's possible, and horror that someone somewhere will try to use this in production code.

[–] BestBouclettes@jlai.lu 8 points 3 weeks ago* (last edited 3 weeks ago)

It's probably both

[–] quaff@lemmy.ca 32 points 3 weeks ago (1 children)

Personally, I can’t wait to inline PHP in my rust code!

[–] flashgnash@lemm.ee 14 points 3 weeks ago

Better yet inline PHP inside the inline python in your rust code

[–] flashgnash@lemm.ee 22 points 3 weeks ago (1 children)

This is absurd, the amount of effort that must have gone into this purely for shits and/or giggles

[–] fossphi@lemm.ee 23 points 3 weeks ago* (last edited 3 weeks ago)

shits and/or giggles

That's probably the single most impactful cause for doing things with computers

[–] JoYo@lemmy.ml 8 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

is it converting the syntax to rust? wouldn't that be easier at the LLIL level or i guess python bytecode level?

[–] Ephera@lemmy.ml 13 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

From what I understand, it works like this:

  1. Rust compiler reads the pseudo-Python and tokenizes it according to Rust's rules.
  2. Macro code converts the tokens back to (now proper) Python, while filling in the captured variables. I believe, this is the code that does this.
  3. Python code is executed in an actual Python interpreter, via PyO3.
[–] JoYo@lemmy.ml 6 points 3 weeks ago

oh ok nm i misunderstood. thanks

[–] HStone32@lemmy.world 4 points 2 weeks ago

What is it about python users just refusing to adapt to other languages?