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. 🙃

22 points

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.

permalink
report
reply
5 points
*

It’s probably both

permalink
report
parent
reply
11 points

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

permalink
report
reply
7 points
*

shits and/or giggles

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

permalink
report
parent
reply
4 points
*

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

permalink
report
reply
6 points
*

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.
permalink
report
parent
reply
4 points

oh ok nm i misunderstood. thanks

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 4

    Monthly active users

  • 755

    Posts

  • 2.2K

    Comments