-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm100-decomment.lex
96 lines (77 loc) · 2.4 KB
/
m100-decomment.lex
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
/* m100-decomment.lex TRS-80 Model 100 BASIC decommenter
*
* Removes comments (REM, ') from Model 100 BASIC.
* Uses output from jumps.lex to decide which commented out lines to keep.
*
* Compile with: flex m100-decomment.lex && gcc lex.decomment.c
*/
/* Change "yy" prefix to "decomment" for file names */
%option warn
%option prefix="decomment"
#include <string.h>
#include <ctype.h>
void insert(int set[], int n);
void print_set(int set[]);
void print_intersection(int seta[], int setb[]);
/* Insert a number into the set, if it isn't already there. */
/* Minor optimization: start at the end of the array since it is sorted. */
void insert(int set[], int n) {
int i, len=set[0];
for (i=len; i>0; i--) {
if (set[i] == n) return;
if (set[i] < n) break;
}
i++;
memmove(set+i+1, set+i, len*sizeof(set[0]));
set[i] = n;
set[0]++;
}
/* Define states that simply copy text instead of lexing */
%x string
/* Set of line numbers that should be kept despite only containing a REM statement */
/* jumps[0] is length of set. */
int jumps[65537] = {0,};
%%
/* A line which starts with REM or ' should be removed unless it is in the JUMPS list */
^[ \t:]*[0-9]+[ \t:]*([']|REM)[^\r\n]*[\r\n]+ {
int n = atoi(yytext);
int i = 1, len = jumps[0];
for (i=1; i<=len; i++) {
if (jumps[i] == n) {
ECHO;
break;
}
}
}
\" ECHO; BEGIN(string);
<string>\" ECHO; BEGIN(INITIAL);
/* Newline also matches <string> start condition. */
<*>\r?\n ECHO; BEGIN(INITIAL);
[ \t:]*([']|REM)[^\r\n]* ; /* Delete REM and tick comments at end of line. */
%%
int main(int argc, char *argv[]) {
char *inputfile, *outputfile;
yyin = stdin; yyout = stdout;
/* First arg (if any) is input file name */
if ( (argc>1) && strcmp(argv[1], "-")) {
inputfile=argv[1];
yyin = fopen( inputfile, "r" );
if (yyin == NULL) { perror(inputfile); exit(1); }
}
/* Second arg (if any) is output file name */
if ( (argc>2) && strcmp(argv[2], "-")) {
outputfile=argv[2];
yyout = fopen( outputfile, "w+" );
if (yyout == NULL) { perror(outputfile); exit(1); }
}
/* Remaining args are line numbers to keep (from m100-jumps.lex) */
for (int i=3; argc>i; i++) {
insert(jumps, atoi(argv[i]));
}
while (yylex())
;
return 0;
}
int yywrap() {
return 1; /* Always only read one file */
}