Tail-Call Interpreters in Rust – Jimmy Ostler

(lordgoati.us)

66 points | by amatheus 3 days ago

3 comments

  • noelwelsh 3 hours ago
    Made my day that it references an article I wrote! :-D

    Another thing that is kinda neat: there is a duality (a bidirectional transform) between the AST for a tree walking interpreter and the AST for a stack machine. To create the stack machine AST, all you do is remove any occurrence of the expression type in the tree-walking AST; these values are now found on the stack. Obvious when you think about it, but useful nonetheless. I don't think there is a simple equivalent for a register machine as, unless you have an infinite number of registers, you need to do register allocation.

    (If the author reads this, my name changed from Noel to Neal at some point. I don't really mind though; my name is a bit unusual and I've been called all sorts of things.)

    • Thom2000 51 minutes ago
      > (If the author reads this, my name changed from Noel to Neal at some point. I don't really mind though; my name is a bit unusual and I've been called all sorts of things.)

      I think it'd be good to update the about page with that info: https://noelwelsh.com/landing/about/

      • noelwelsh 43 minutes ago
        Sorry, I wasn't clear. In OP's blog post they refer to me as Neal at one point (just after the heading "Indirect Dispatch"). My name is still Noel :-)
    • whilenot-dev 2 hours ago
      Just read your blog post[0], thanks for it!

      If I may leave some feedback here...

      It'd be cool if you could add some syntax highlighting, because being at it is, it's hard to distinguish comments from code paths. The code snippets are just Scala anyway, aren't they?

      Why, in the switch dispatch- and the subroutine threading-variants, is the recursive call in the dispatch function done with a call to loop(...)? Is that some Scala specific thing for recursion?

      [0]: https://noelwelsh.com/posts/understanding-vm-dispatch/

      • noelwelsh 51 minutes ago
        Thanks for pointing those out. I've fixed both issues.

        Syntax highlighting was a discrepancy between my local env and the Netlify env that deploys the site.

        `loop` is what I usually call tail-recursive loops in my personal style. I think I changed that to `dispatch` for the blog post and didn't push the change all the way through.

    • derdi 1 hour ago
      Hmm, what's a stack machine AST? I can't follow your description of the transform, what does "remove" mean here? I can remove all of the internal nodes of a tree, which leaves me with a soup of leaf nodes, but how is that an AST for a stack machine?
      • noelwelsh 57 minutes ago
        I should have said instruction set or intermediate representation (IR). For a stack machine a program is an array of instructions. For a tree-walking interpreter a program is a tree of instructions. The duality transforms one instruction set into the other.

        Hope that clears it up.

        • derdi 51 minutes ago
          Fair! Though I still don't know what you mean by "removing" nodes from the tree-walking interpreter's AST. Assume we have an AST like:

              (Add (LoadConst 1) (LoadVar x))
          
          The corresponding stack machine code might be:

              [PushConst 1, PushVar x, Add]
          
          In what way was anything "removed" from the tree?
          • noelwelsh 45 minutes ago
            It's a transform on the instruction set. If you have the following instruction set for a tree walking interpreter (Scala syntax)

              enum Expr:
                case Add(left: Expr, right: Expr)
                case Lit(val: Double)
            
            the corresponding stack machine instruction set is

              enum Expr:
                case Add
                case Lit(val: Double)
            
            The transformation in this direction is purely syntactic: where you see that a case has a parameter of type Expr in the instruction set, you simply remove that parameter for the corresponding stack machine instruction.

            The transformation in the other direction is not purely syntactic as you have to know that, e.g., Add gets two parameters from the stack and add those parameters back in.

            • derdi 7 minutes ago
              Got it, thank you! I had read your "remove any occurrence of the expression type in the tree-walking AST" as removing nodes from the AST, but in some sense it's about removing edges, as in, the references from one operation to others.
      • sirwhinesalot 46 minutes ago
        Not the OP and I honestly have no idea what they mean, but the translation of a tree-walking interpreter for expressions to a stack-machine compiler is almost trivial. For example, if you have (in pseudo-code):

          class Add : Node {
            Node left;
            Node right;
            int interpret() {
              int l = left.interpret();
              int r = right.interpret();
              return l + r;
            }
          }
        
        You can turn it into:

          class Add : Node {
            Node left;
            Node right;
            void compile(bc: ByteCode) {
              left.compile(bc);
              right.compile(bc);
              bc.push(OP_ADD);
            }
          }
        
        The inputs to OP_ADD are implicit, I guess that is what "remove" means?
  • sph 4 hours ago
    Ctrl-Shift-C, click on a paragraph, search for "text-shadow" on the right pane of the inspector, untick the checkbox.

    There is a time and place for text and retro-CRT effects, this isn't one.

    • goda90 1 hour ago
      Reminder about Firefox's built in reader mode, which is great for working around annoying styling or mobile rendering issues.
      • aboardRat4 2 minutes ago
        In which extensions, such as a dictionary, don't work.
    • narinciye 4 hours ago
      Thank you. I was about to get eye cancer
      • sscaryterry 3 hours ago
        Exactly, I stopped trying to read it, thought I didn't have my glasses on.
  • froh42 5 hours ago
    Why the fuck make a tech web page unreadable with CRT effects? Thank god, there is reader mode in Firefox.
    • klibertp 3 hours ago
      It's not that bad... But it is bad. It looks cool, and I managed to read the first few paragraphs. But when the code scrolled into view, it stopped looking cool and became an eye-destroying disaster.

      YMMV, but OP, if you read this, please consider disabling the text-shadow for code snippets... or disabling syntax highlighting in code snippets. These two things are not a good thing together.

    • ubercore 5 hours ago
      Everyone says they want the weird more personal internet back, then this is what we get when someone does a weird internet thing!
      • mister_mort 4 hours ago
        I think they could get a close-enough effect by removing the horizontal lines on the text glyphs themselves. This would still keep the colorscheme and slight glow, but drastically improve readability.
    • arethuza 4 hours ago
      I actually find it quite readable - YMMV.
      • rz2k 2 hours ago
        It must depend on what you’re using to read it. It’s also readable for me on an OLED iPad, and gives me ideas about what I actually want when I wish I could turn the contrast down while reading it with low ambient light.

        Bringing the brightness levels of the background and text closer together doesn’t ever seem to work, but being able to add a slight blurry dropshadow behind behind each character might be a way to reduce the artifacts OLEDs seem to create when my eyes are tired.

      • sph 4 hours ago
        Then, with all due respect, you should not be anywhere near the design or evaluation of accessible systems.

        There's people reading this blog with all types of devices, screens, lighting and range of visual acuity. Even if you're blessed with perfect vision and excellent displays, it should be obvious that readability has been lowered for no obvious reason.

        The point of a blog is for people to read it, so it fails at its foremost job.

        • arethuza 4 hours ago
          I wasn't saying that I thought it should be adopted as a standard for others to follow - just that (somewhat weirdly) I personally find the code segments on that page quite easy to read.
    • QuadmasterXLII 3 hours ago
      “everyone has reader mode” is I guess freeing?
      • layer8 3 hours ago
        Might as well give up on CSS and just use brutalist HTML in that case.
        • bigfishrunning 2 hours ago
          We should do that anyway, CSS was a mistake. I miss gopher.
    • frameer 4 hours ago
      [flagged]