1. 11
  1.  

    1. 4

      Does anyone happen to know of a small self-contained example which uses the InternPool technique to implement an AST and an interpreter for a small toy language (e.g. integers and addition)? The answers to one of the audience questions seems to suggest that it should be possible, but I can’t immediately see it from only the API of the InternPool.

      1. 6

        The API of InternPool is a little specialized to its specific use case of, well, interning. For something like an AST or IR, you’re not trying to intern things; just to construct a sequence, which will be immutable after initial construction. So, the constraints are a bit different in this case, leading to a different API design. In particular, the “friendly” representation isn’t really a “key” in any sense, it’s just unwrapped data. I’d name the types a little differently here.

        You probably wouldn’t have a get function, since the logic to construct the AST is all centralised in one place anyway – the parser – so that logic might as well just live there.

        Here’s a skeleton of a toy example I threw together, it might help to get the idea across: https://zigbin.io/039842

        1. 3

          Thanks, that’s really helpful. You should consider turning that into a proper blog post! I think we need simple examples of data-oriented design like that, in order for these techniques to catch on in the programming languages community.

      2. 1

        At its core it’s just an array hash map. You put something in the map and find out its index. That’s it.

        All the complications added on top of it are just organizing memory layout with various techniques to maintain trivial serializability, efficient memory usage, type safety, and thread safety.