Home

Kinds of Metaprogramming in Python

Show example of each.

Summary of Metaprogramming

2) I'm trying to tie this blog together with larger themes about programming, and readers have given me feedback that they appreciate this. A current theme is metaprogramming.

Why? Well, the goal this entire project is to treat Unix shell as a real programming language. Shell is a dynamic langauge, and metaprogramming is one of the key features of dynamic languages.

Some people think that a dynamically-typed language is just a statically-typed language with the type checking, as if the authors were too lazy to add a type system. But dynamic typing adds expressive power over static languages; metaprogramming is one of the key mechanisms for this.

I've always been a dynamic language person. There seems to be "meme" that: why wouldn't you want a dynamic language? All else being equal, why wouldn't you want errors earlier rather than later? Isn't the reason that languages are dynamically typed just because it's easier for the authors to implement, not because it's easier for the developers to write programs in?

I want static parsing but I still like dynamic typing.

These are fallacies, and a main reason is metaprogramming. We've already seen

Shell is one of the ultimate dynamic languages -- it is typed up front.

We've seen two uses for metaprogramming:

1) id_kind -- treating the names of variables as data.

2) ASDL -- classes are dynamically generated from stuff in another language.

3) I mentioned re2c, but I would call this a form of metaprogramming. re2c is a program that takes a program as input and produces a program as output. The input is:

1) a bunch of regular expressions and semantic actions 2) a bunch of goto statements. states are represented using LOCATIONS OF INSTRUCTIONS rather than "variables" in memory.

4) Meta-languages. yacc and ANTLR are meta-languages because they are a language for describing a language. (I still have to write the meta-language rant mentioned in the post on pratt Parsing).

Isn't this just code generation? All code generation is just metaprogramming. It can be implemented with textually, or as an interpreter for a language, just like my PEG library Annex. Tomorrow I will write a post called code generation vs. metaprogrmming.

There is no compiler that does this as far as I know.

SHOW: Example solution: enum solution

Point of this post:

Example of "extreme metaprgramming". Metaprogramming keeps programs short. Size is code's worst enemy.

Post 1: Motivation. Summary of metaporgramming so far.

Post 2: Go through the code for the search Process

Post 3: Wrapping up. Links to the demo scene blog. Links to Synthesis OS by the author of the original superoptimization paper.

Possible ways I can use it elsewhere: in ASDL, because ASDL assigns. If I have 8 values, that fits in 3 bits, and I have 5 bits left over for lookup tables. That's perfect for GlobsAreExpanded and IsSubst. Free bits.

dash: does it use bits?