2 ms·
Back in the 1980s I found a book at the public library where somebody had a BASIC program that would read a BASIC program without line numbers and with structur
by PaulHoule 2y ago
Back in the 1980s I found a book at the public library where somebody had a BASIC program that would read a BASIC program without line numbers and with structured programming loops and rewrite it an ordinary BASIC. It was oriented towards CP/M so I typed the program in and modified it to run on my TRS-80 Coco. It was kinda like
https://en.wikipedia.org/wiki/Ratfor https://en.wikipedia.org/wiki/Ratfor
Similarly I've written awk programs that write shell scripts (guilty pleasure), Python programs that write AVR-8 assembly, toolkits to write Java ASTs with static imports like
@Test
public void buildAList() {
var stream = callBuild(callAdd(callAdd(callAdd(callBuilder(), Literal.of(7)),
Literal.of(11)),Literal.of(15)));
var expr = callToList(stream);
var l = expr.evaluateRT();
assertEquals(3,l.size());
assertEquals(7, l.get(0));
assertEquals(11, l.get(1));
assertEquals(15, l.get(2));
}
and evaluate with a tree-walking interpreter or use as a code generator in maven, do transformations on the ASTs, etc.
There are many paths to metaprogramming but the Lisp family leads in putting it on your fingertips. Once you learn those methods you can apply them in the trashiest programming languages, but I think often people miss the point.
- touisteur 2y agoVery common for me to write shell scripts that generate shell scripts to run. Instead of creating gnuplot scripts to loaded, sometimes a good sequence of echo "set terminal png 800,600; plot..." | gnuplot > my.png or a very long imagemagick drawing command or a simple svg generated then converted to png then assembled (thank montage), can do the job. The whole advantage of generating intermediate shell scripts, is to replay one part specifically to debug a small part of a script pipeline.