Welcome to the world of compiler golf. Your task is to write a program that generates another program to play a variant of FizzBuzz on spec.
Your compiler
Write a compiler that generates variants of the FizzBuzz program to spec. The spec of this variant is expressed in the form of an array of integer/string pairs.
- The input may be in any form that is convenient to your language. (My examples use n:xxxx, but this is just for illustrative purposes.)
- Each integer input may only be used once per invocation of your compiler.
- The integer of each pair will have a value of at least one.
- The string of each pair will be made of only exactly four ASCII letters.
- The output must be a single complete program that conforms to the rules below.
- The output may be in any convenient form, as long as it is a textual program. (So no returning lambda expressions.)
Behavior is undefined for inputs not conforming to the above rules.
Your generated FizzBuzz program
The program generated by your compiler will take a single integer, n, as input. It will output a sequence of numbers starting from one up to and including n, replacing numbers with FizzBuzz strings when required.
- The generated program must be in the same language as the compiler.
- The input n may be in any form convenient to your language.
- n will have a value of at least one.
- A number that is a multiple of at least one of the integers input to the compiler must be replaced by all of the strings paired with those integers joined together.
- A number that is not to be replaced by a FizzBuzz string must be output in decimal ASCII.
For example;
> GenFizzBuzz 3:Fizz 5:Buzz
> a.out 5
1
2
Fizz
4
Buzz
Scoring
Your entry will be scored by the length of the programs your compiler generates added to the length of your compiler. Run your compiler many times with each the following parameters and add the lengths of the generated programs together with the length of the compiler to find your score.
- Just Count. (No inputs - The generated program will count 1 to n without replacements.)
- Just Golf. (1:Golf - The generated program will output "Golf" n times.)
- Classic FizzBuzz. (3:Fizz, 5:Buzz)
(Note that your compiler is required to generate code for any valid input, not just these listed.)