summaryrefslogtreecommitdiff
path: root/compil.lex
blob: 1bac4f2ae821eaf834f6df31bab3ac8fd12fba29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 #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;

    const int max_tagsize = 64;

    char unknown_tag[65];
    char * unknown_tag_ptr;

    int in_unknown_tag = 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>.    {
        if (*yytext == '<') {
            in_unknown_tag = 1;
            unknown_tag_ptr = unknown_tag;
        } else if (*yytext == '>') {
            *unknown_tag_ptr = 0;
            fprintf(stderr, "Got an unknown tag: %s\n", unknown_tag);
            in_unknown_tag = 0;
        } else if (in_unknown_tag) {
            *unknown_tag_ptr++ = *yytext;
            if ((unknown_tag_ptr - unknown_tag) == max_tagsize) {
                in_unknown_tag = 0;
            }
        }
        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);
}