1. 8

  2. 3

    What’s funny is that this is how a lot of old C projects used to work, they would use a Common Lisp program that would generate C code in the manner mentioned here. IIRC Chip Morningstar used to do this when he worked at LucasArts developing video games.

    1. 1

      The metalang99 readme actually rejects code generators:

      The main reason is that native macros allow you to interleave macro invocations with ordinary code. Code generators, on the other hand, force you to either “program in comments” or in DSL-specific files. Can you imagine using Datatype99 in this way? You would have to put nearly all code manipulating sum types into separate files or feeding all your sources to a code generator, thus losing IDE support and development convenience.

      1. 1

        And that’s exactly why I think C++‘s template metaprogramming is something truly special. The template sublanguage is pretty much a convoluted untyped lambda calculus and it lets you syntactically generate code that gets type checked after full instantiation. Still, the host language (the template sublanguage) and the object language (the remaining C-like language, forget about OOP) are very tightly integrated. You can call templated functions in the middle of non-templated functions, and the template arguments get deduced based on the surrounding typing context resulting in code generation followed by type-checking and other template instantiations if necessary, and then there’s also constexpr that allows you to run object-language code while evaluating the host language! This interleaving of type-checking/deduction/interpretation and code generation is not possible with external programs generating C code. And you get all of this with full IDE support.

        Don’t get me wrong, with all of the above said, C++ has so many rough edges and historical accidents that I’ve long chosen to move on to Haskell, but my heart is still itching for a modern language with all the modern tooling that has an untyped host language - simply typed, non-garbage collected/no runtime object language interplay like C++. I’ve had my eyes on terralang for a few years now, but it seems a little experimental (and I don’t have the time to become a contributor and fix issues as I encounter them 😔)

        1. 1

          There are two big problems with C++ templates:

          First, the type language and the expression language are completely distinct. I can write a == b but I can’t write decltype(a) == decltype(b), I have to instead write std::is_same_v<decltype(a), decltype(b)>. This makes anything that needs to move between the expression and type languages very clunky. This is slowly being improved, with things like constexpr / constinit and the reflection TS that let you stay in the expression language for much longer.

          The second problem is that (until concepts) it lacked any concept of a metatype. All types that you manipulate in the type language are instances of the type typename. This meant that your only option for type checking was at instantiation. You can’t write a template and type check it. This is largely addressed by concepts, except that concept syntax makes template syntax look pleasant in comparison.

          The C++ code that I write makes very heavy use of templates but it always makes me long for a cleaner language.