Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use v5.10;
- my $comment = <<'END';
- END
- # compile input down to C code. We'll need a support function for
- # implementing 'inp', so, why not actually write everything with
- # macros...
- my $preamble = << '__C';
- #include <stdio.h>
- #define INP(a) a = *(input++)
- #define ADD(a,b) a += b
- #define MUL(a,b) a *= b
- #define DIV(a,b) a /= b
- #define MOD(a,b) a %= b
- #define EQL(a,b) a = (a == b) ? 1 : 0
- long program(long *input) {
- signed long w = 0;
- signed long x = 0;
- signed long y = 0;
- signed long z = 0;
- __C
- my $postamble = << '__C';
- return z;
- }
- // increments number, returning false on overflow
- int increment(long *num) {
- int digit = 9;
- while (digit >= 0) {
- num[digit]++;
- if (num[digit] < 10) {
- return 1;
- }
- // carry to next higher digit
- num[digit] = 1;
- --digit;
- }
- return 0;
- }
- int main(int ac, char** av) {
- long low[14] = { 1, 4, 9, 1, 1, 6, 7, 5, 3, 1, 1, 1, 1, 4 };
- // while(increment(low)) {
- // }
- printf("low x = %d\n", program(low));
- long high[14] = { 6, 9, 9, 1, 4, 9, 9, 9, 9, 7, 5, 3, 6, 9 };
- // while(increment(high)) {
- // }
- printf("high x = %d\n", program(high));
- return 0;
- }
- __C
- my @clines = ();
- foreach ((<>)) {
- chomp;
- if (/^inp (\w)/) {
- push @clines, "INP($1);";
- } elsif (/^(\w+) (\w) ([wxyz]|-?\d+)/) {
- my $instr = uc $1;
- push @clines, "$instr($2,$3);"
- } else {
- die "parse error: $_\n";
- }
- }
- say $preamble;
- say join "\n", @clines;
- say $postamble;
Add Comment
Please, Sign In to add comment