summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compil.lex203
1 files changed, 203 insertions, 0 deletions
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 <string.h>
+ /* 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;
+
+%%
+
+<INITIAL>"<roomscripts>" {
+ fprintf(yyout, "rooms_txts = {\n");
+ BEGIN(I);
+ }
+
+<INITIAL>.* {
+ fprintf(stderr, "Invalid file, must begin with <roomscripts>\n");
+ errstate = 1;
+ }
+
+<I>"\n\n<ptr n=\""[[:digit:]]+"\" room=\""[^\"]*"\"/>\n" {
+ int d = atoi(yytext + 10);
+ if (got_ptr) {
+ fprintf(yyout, "\x5d\x5d },\n");
+ }
+ fprintf(yyout, " [%i] = { \x5b\x5b", d);
+ got_ptr = 1;
+ }
+
+<I>"\n<new/>\n" {
+ putcode(1);
+ }
+
+<I>"<rspd/>" {
+ putcode1(3, 255);
+ }
+
+<I>"<st spd=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 9);
+ putcode1(3, d);
+ }
+
+<I>"<st clr=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 9);
+ putcode1(4, d);
+ }
+
+<I>"<start/>" {
+ putcode(5);
+ }
+
+<I>"<rrep/>" {
+ putcode1(7, 1);
+ }
+
+<I>"<st rep=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 9);
+ putcode1(7, d);
+ }
+
+<I>"<rsiz/>" {
+ putcode1(8, 1);
+ }
+
+<I>"<st siz=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 9);
+ putcode1(8, d);
+ }
+
+<I>"<dport/>" {
+ putcode2(19, 255, 255);
+ }
+
+<I>"<port a1=\""[[:digit:]]+"\" " {
+ arg1 = atoi(yytext + 10);
+ BEGIN(P);
+ }
+
+<P>"a2=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 4);
+ putcode2(19, arg1, d);
+ BEGIN(I);
+ }
+
+<I>"<var n=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 8);
+ putcode1(14, d);
+ }
+
+<I>"<uk c=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 7);
+ putcode(d);
+ }
+
+<I>"<u1 c=\""[[:digit:]]+"\" " {
+ cmd = atoi(yytext + 7);
+ BEGIN(U1);
+}
+
+<U1>"a1=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 4);
+ putcode1(cmd, d);
+ BEGIN(I);
+}
+
+<I>"<u2 c=\""[[:digit:]]+"\" " {
+ cmd = atoi(yytext + 7);
+ BEGIN(U21);
+}
+
+<U21>"a1=\""[[:digit:]]+"\" " {
+ arg1 = atoi(yytext + 4);
+ BEGIN(U22);
+}
+
+<U22>"a2=\""[[:digit:]]+"\"/>" {
+ int d = atoi(yytext + 4);
+ putcode2(cmd, arg1, d);
+ BEGIN(I);
+}
+
+<I>"\n" {
+ putcode(0);
+ }
+
+<I>"</roomscripts>\n" {
+ fprintf(yyout, "\x5d\x5d}\n}\n");
+ BEGIN(E);
+ }
+
+<I>"abcdef<"[^\>\n]*"/>" {
+ yytext[strlen(yytext) - 2] = 0;
+ fprintf(stderr, "Error: Invalid command: '%s'\n", yytext + 1);
+ errstate = 1;
+ }
+
+<I>. {
+ 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 <output> [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);
+}