Search code examples
theory

Is it possible to write code to write code?


I've heard that there are some things one cannot do as a computer programmer, but I don't know what they are. One thing that occurred to me recently was: wouldn't it be nice to have a class that could make a copy of the source of the program it runs, modify that program and add a method to the class that it is, and then run the copy of the program and terminate itself. Is it possible for code to write code?


Solution

  • Start by looking at quines, then at Macro-Assemblers and then lex & yacc, and flex & bison. Then consider self-modifying code.

    Here's a quine (formatted, use the output as the new input):

    #include<stdio.h>
    
    main()
    {
      char *a = "main(){char *a = %c%s%c; int b = '%c'; printf(a,b,a,b,b);}";
      int b = '"';
      printf(a,b,a,b,b);
    }
    

    Now if you're just looking for things programmers can't do look for the opposite of np-complete.