From 2357718357740a870cf57cb50acbc95b34797e8d Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 6 Nov 2009 13:38:14 -0800 Subject: Adding script compiler. --- compil.lex | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 compil.lex diff --git a/compil.lex b/compil.lex new file mode 100644 index 0000000..ad24a2c --- /dev/null +++ b/compil.lex @@ -0,0 +1,203 @@ + #include + /* In text block */ +%s I + /* In port block */ +%s P + /* In unknown 1 block */ +%s U1 + /* In unknown 2 block, arg1 */ +%s U21 + /* In unknown 2 block, arg2 */ +%s U22 + /* Script ended */ +%s E + + void putcode(int); + void putcode1(int, int); + void putcode2(int, int, int); + int cmd = 0; + int arg1 = 0; + int errstate = 0; + int got_ptr = 0; + +%% + +"" { + fprintf(yyout, "rooms_txts = {\n"); + BEGIN(I); + } + +.* { + fprintf(stderr, "Invalid file, must begin with \n"); + errstate = 1; + } + +"\n\n\n" { + int d = atoi(yytext + 10); + if (got_ptr) { + fprintf(yyout, "\x5d\x5d },\n"); + } + fprintf(yyout, " [%i] = { \x5b\x5b", d); + got_ptr = 1; + } + +"\n\n" { + putcode(1); + } + +"" { + putcode1(3, 255); + } + +"" { + int d = atoi(yytext + 9); + putcode1(3, d); + } + +"" { + int d = atoi(yytext + 9); + putcode1(4, d); + } + +"" { + putcode(5); + } + +"" { + putcode1(7, 1); + } + +"" { + int d = atoi(yytext + 9); + putcode1(7, d); + } + +"" { + putcode1(8, 1); + } + +"" { + int d = atoi(yytext + 9); + putcode1(8, d); + } + +"" { + putcode2(19, 255, 255); + } + +""a2=\""[[:digit:]]+"\"/>" { + int d = atoi(yytext + 4); + putcode2(19, arg1, d); + BEGIN(I); + } + +"" { + int d = atoi(yytext + 8); + putcode1(14, d); + } + +"" { + int d = atoi(yytext + 7); + putcode(d); + } + +""a1=\""[[:digit:]]+"\"/>" { + int d = atoi(yytext + 4); + putcode1(cmd, d); + BEGIN(I); +} + +""a1=\""[[:digit:]]+"\" " { + arg1 = atoi(yytext + 4); + BEGIN(U22); +} + +"a2=\""[[:digit:]]+"\"/>" { + int d = atoi(yytext + 4); + putcode2(cmd, arg1, d); + BEGIN(I); +} + +"\n" { + putcode(0); + } + +"\n" { + fprintf(yyout, "\x5d\x5d}\n}\n"); + BEGIN(E); + } + +"abcdef<"[^\>\n]*"/>" { + yytext[strlen(yytext) - 2] = 0; + fprintf(stderr, "Error: Invalid command: '%s'\n", yytext + 1); + errstate = 1; + } + +. { + fputc(*yytext, yyout); + } + +. { + fprintf(stderr, "Hu uh, something's wrong...\n"); + } + +%% + +int yywrap(void) { + exit(errstate ? -1 : 0); +} + +int main(int argc, char ** argv) { + if ((argc < 2) || (argc > 3)) { + fprintf(stderr, "Usage: %s [input]\n", argv[0]); + exit(-1); + } + if (!(yyout = fopen(argv[1], "wb"))) { + fprintf(stderr, "Error: can't open file %s\n", argv[1]); + exit(-1); + } + if (argc == 3) { + if (!(yyin = fopen(argv[2], "rb"))) { + fprintf(stderr, "Error: can't open file %s\n", argv[2]); + exit(-1); + } + } + fprintf(stderr, "Creating file %s\n", argv[1]); + yylex(); + exit(errstate ? -1 : 0); +} + +void unputs(char * s) { + int l = strlen(s), i; + + for (i = l - 1; i >= 0; i--) { + unput(s[i]); + } +} + +void putcode(int code) { + fprintf(yyout, "\x5d\x5d, { %i }, \x5b\x5b", code); +} + +void putcode1(int code, int arg) { + fprintf(yyout, "\x5d\x5d, { %i, %i }, \x5b\x5b", code, arg); +} + +void putcode2(int code, int arg1, int arg2) { + fprintf(yyout, "\x5d\x5d, { %i, %i, %i }, \x5b\x5b", code, arg1, arg2); +} -- cgit v1.2.3