summaryrefslogtreecommitdiff
path: root/PE/compil.lex
blob: bc6614d5a617d4a28cbfd1e2a62de8982ac0cc48 (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
 #include <string.h>
 #define yputc(a) putc(a, yyout)
 #include "table.h"

 int oldpt = -1, nbpts = -1;
 int curptr = 0, p, full = 0;
 void putptr(void);

%%

"<NBPTS "[[:digit:]]+">\n" {
    int i;
    nbpts = atoi(yytext + 7);
    fprintf(stderr, "nbpts = %i\n", nbpts);
    unsigned short int z = 0;
    
    fwrite(&nbpts, 2, 1, yyout);
    
    for (i = 0; i < nbpts; i++) {
	fwrite(&z, 2, 1, yyout);
    }
    
    curptr = 1;
    p = ftell(yyout);
    fseek(yyout, 2, SEEK_SET);
    fwrite(&p, 2, 1, yyout);
    fseek(yyout, p, SEEK_SET);
}

"<PT"[[:digit:]]+">\n" {
    int d = atoi(yytext + 3);
    if (d != (oldpt + 1)) {
	fprintf(stderr, "Pointeur incorrect ligne %i: %i\n", yylineno, d);
    }
    oldpt = d;
    yputc(0xfe);
    yputc(d);
}

"<UNK "[[:xdigit:]]+">" {
    int d = strtoul(yytext + 5, NULL, 16);
    yputc(d);
}

"<TAG"[[:digit:]]+">" {
    int d = atoi(yytext + 4);
    yputc(0xfb);
    yputc(d);
}

"<CHOICES "[[:digit:]]+">\n" {
    int d = atoi(yytext + 9);
    yputc(0xfb);
    yputc(9);
    yputc(d);
}

"<TIMER "[[:digit:]]+">" {
    int d = atoi(yytext + 7);
    yputc(0xfb);
    yputc(7);
    yputc(d);
}

"<UNKCMD "[[:digit:]]+">" {
    int d = atoi(yytext + 8);
    yputc(0xfb);
    yputc(d);
}

"<CLOSE>\n"	{ yputc(0xff); putptr(); }
"\n<CLOSE>\n"	{ yputc(0xff); putptr(); }
"<AYA>"		yputc(0xfa);
"\n<TCLOSE>\n"	{ yputc(0xf9); putptr(); }
"<EMPTYPTR>\n"  { putptr(); }
"<PAUSE>\n"	yputc(0xf8);
"\n"		yputc(0xf7);

"<ae>"		yputc(0x5c);
"<oe>"		yputc(0x5d);

"<"[^>]*">"	{
    fprintf(stderr, "Commande inconnue ligne %i: %s\n", yylineno, yytext);
}

. {
    int i, trouve = 0;
    for (i = 0; i <= MAXCHAR; i++) {
	if (table[i] == *yytext) {
	    trouve = 1;
	    yputc(i);
	}
    }
    
    if (!trouve) {
	fprintf(stderr, "Caractère inconnu ligne %i: %s\n", yylineno, yytext);
    }
}

%%

int yywrap(void) {
    if ((nbpts != -1) && (!full)) {
	fprintf(stderr, "Pas assez de pointeurs\n");
    }
    exit(0);
}

void putptr(void) {
    if (nbpts == -1)
	return;
    if (curptr == nbpts) {
	full = 1;
	curptr++;
	return;
    }
    if (curptr > nbpts) {
	fprintf(stderr, "Trop de pointeurs ligne %i\n", yylineno);
	return;
    }
    p = ftell(yyout);
    fseek(yyout, 2 * curptr + 2, SEEK_SET);
    fwrite(&p, 2, 1, yyout);
    fseek(yyout, 0, SEEK_END);
    curptr++;
}

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();
    return 0;
}