I wrote a simple transpiler (that may be a bit of a stretch) in perl to cut down on some boiler plate code. I'm very new to perl, this being maybe my third ever project in it. My main question would be to ask for feedback on my regular expressions and making it more open to expansion (adding error messages, etc).
My code:
#!/usr/bin/perl
$/ = undef;
while(<>) {
print "#include <SDL2/SDL.h>\n#include <stdint.h>\n\n";
s/.include ([^;]+);/#include "\1"/g;
s/^include ([^;]+);/#include <\1>/g;
s/entry:/int main(void) {/g;
s/entry ([A-z][A-z0-9]*)\s*,\s*([A-z][A-z0-9]*)\s*:/int main(int \1, char* \2\[\]) {\n\tSDL_Init(SDL_INIT_EVERYTHING);/g;
s/([A-z][A-z0-9]*)\s+:u(8|16|32|64)(\**)/uint\2_t\3 \1/g;
s/([A-z][A-z0-9]*)\s+:(8|16|32|64)(\**)/int\2_t\3 \1/g;
s/([A-z][A-z0-9]*)\s+:Color\s+({[^}]+})/struct { uint8_t r, g, b, a; }\1 = \2/g;
s/([A-z][A-z0-9]*)\s+:Color/struct\s+{ uint8_t r, g, b, a; }\1;/g;
s/([A-z][A-z0-9]*)\s+:-([A-z][A-z0-9]*)/struct \2 \1/g;
s/([A-z][A-z0-9]*)\s+:Win\s*{([^}]+)}/SDL_Window* \1 = SDL_CreateWindow(\2)/g;
s/([A-z][A-z0-9]*)\s+:Win/SDL_Window* \1/g;
s/([A-z][A-z0-9]*)\s+:Ren\s*{([^}]+)}/SDL_Renderer* \1 = SDL_CreateRenderer(\2)/g;
s/([A-z][A-z0-9]*)\s+:Ren/SDL_Renderer* \1/g;
s/@([A-z][A-z0-9]*)\s*\(([^)]+)\)/typedef struct \1 {\2} \1;/g;
s/([A-z][A-z0-9]*)\s+:@@\s*\(([^)]+)\)/struct {\2}\1;/g;
s/@/struct /g;
s/([A-z][A-z0-9]*)\s+:([A-z][A-z0-9]*) ({[^}]*})/\2 \1 = \3/g;
s/([A-z][A-z0-9]*)\s+:([A-z][A-z0-9]*)/\2 \1/g;
s/Wait\s+a\s+minute!!!/SDL_Delay(60*1000);/g;
s/wait\s+([0-9bx]+)/SDL_Delay(\1)/g;
s/clear\s+([A-z][A-z0-9]*)/SDL_RenderClear(\1)/g;
s/set\s+([A-z][A-z0-9]*)\s+color\s+to\s+([A-z][A-z0-9]*)/SDL_SetRenderDrawColor(\1, \2.r, \2.g, \2.b, \2.a)/g;
s/set\s+([A-z][A-z0-9]*)\s+([A-z][A-z0-9]*)\s+to\s+([A-z0-9*\/+-]+)/\1.\2 = \3/g;
s/show\s+([A-z][A-z0-9]*)/SDL_RenderPresent(\1)/g;
s/([A-z][A-z0-9]*)\s([A-z][A-z0-9]*\**)\s+([A-z][A-z0-9]*)\s*:/void \1(\2 \3) {/g;
s/([A-z][A-z0-9]*)\s*:/void \1(void) {/g;
s/-!-/}/g;
print;
}
print "\n";
Sample input:
q :@@(
a :u8;
)
@pixel (
r :u8; g :u8; b :u8; a :u8;
)
huh a :u8*:
-!-
letmecheck this :pixel:
-!-
entry:
p :pixel {0, 255, 0, 255};
win :Win {"Hello world!", 300, 300, 600, 600, 0};
ren :Ren {win, -1, 0};
set p g to 127;
set ren color to p;
clear ren;
show ren;
wait 500;
set p b to 127;
set ren color to p;
clear ren;
show ren;
wait 500;
set p r to 127;
set ren color to p;
clear ren;
show ren;
wait 500;
-!-
output:
#include <SDL2/SDL.h>
#include <stdint.h>
struct {
uint8_t a;
}q;
typedef struct pixel {
uint8_t r; uint8_t g; uint8_t b; uint8_t a;
} pixel;
void huh(uint8_t* a) {
}
void letmecheck(pixel this) {
}
int main(void) {
pixel p = {0, 255, 0, 255};
SDL_Window* win = SDL_CreateWindow("Hello world!", 300, 300, 600, 600, 0);
SDL_Renderer* ren = SDL_CreateRenderer(win, -1, 0);
p.g = 127;
SDL_SetRenderDrawColor(ren, p.r, p.g, p.b, p.a);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
SDL_Delay(500);
p.b = 127;
SDL_SetRenderDrawColor(ren, p.r, p.g, p.b, p.a);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
SDL_Delay(500);
p.r = 127;
SDL_SetRenderDrawColor(ren, p.r, p.g, p.b, p.a);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
SDL_Delay(500);
}
I am open to any and all feedback, thanks in advance!
EDIT: This is asking for a review of the PERL code, not the C code
A-z
should beA-Za-z
. These are not equivalent. \$\endgroup\$