by gamegoblin · 11y ago · 0 comments
#include <stdio.h>
#include <stdlib.h>
#COMPILETIME
int fib(int n) {
if(n < 2) return n;
return fib(n-1) + fib(n-2);
}
int main() {
printf("int fib(int n) {\n");
printf(" switch(n) {\n");
for(int i=0; i < 30; i++) {
printf("case %d: return %d\n", i, fib(i));
}
printf("default: return fib(n-1) + fib(n-2);\n");
printf("}\n}");
}
#ENDCOMPILETIME
and then at compile time that would get run and the source could generated would be: int fib(int n) {
switch(n) {
case 0: return 0;
case 1: return 1;
case 2: return 1;
case 3: return 2;
case 4: return 3;
case 5: return 5;
case 6: return 8;
... etc up to i=29...
default: return fib(n-1) + fib(n-2);
}
}Your example could look like this in Common Lisp:
(defun fib (n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
(defmacro spliced-fibs (x n)
`(case ,x
,(map-iota (lambda (v) (list v (fib v))) n)))
(defun hardcoded-fib (num)
(spliced-fibs num 29))
spliced-fibs macro will generate you a full case block at compile time, so the hardcoded-fib will be basically one big switch/case block. map-iota call maps a function over a list of numbers from 0 to n. Note that a macro can call ordinary functions at compile time, which themselves can use other macros, etc. So a lot of code can be used both at compile and run time.In particular string mixins will do what you want; they will also do much more, like for instance include external files written in domain specific languages and compile them in native D code; all this is done by the compiler, using language facilities, without external tools.
(These is not some theoretically cool feature; it's used in real world code, like dproto, which compiles protobuf definitions into native code, or HTML templating engines which compile templates to the equivalent of Java servlets)
That feature is called compile-time function evaluation, and there are a bunch of languages that support CTFE[1]. Blow's language has, from what I've seen, the cleanest CTFE syntax and most straightforward usage approach. In terms of what you can use today, check out D[2] (which has multiple competing ways to do CTFE).
[1]: http://en.wikipedia.org/wiki/Compile_time_function_execution [2]: http://dlang.org/
It's macro facilities are actually fairly amazing, and allow for a lot of boilerplate code to be generated at compile time (think: the kind of things reflection is used for in other languages).
Sadly, it's a GCed language, so while it's very popular for game development, it's use is mainly in the indie scene, and so it's probably never something I'll use for anything other than a toy.
[0]: http://haxe.org/
I'm not sure what you mean by 'in the language itself', since isn't all compile-time metaprogramming in the language itself? But this question has an example of computing Fibonacci sequences in C++ templates:
http://stackoverflow.com/questions/908256/getting-template-m...
The second part of the question is about how to turn that into a table which can be referenced at runtime, which would seem to be the same as your question.
I occasionally write C# which writes C# (using .NET APIs to compile it at runtime, typically) and C# in T4 Text Templates which write C# (they integrate nicely into Visual Studio's build process right out of the box.)