-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm100-tokenize.lex
202 lines (182 loc) · 4.22 KB
/
m100-tokenize.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
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
/* m100-tokenize.lex TRS-80 Model 100 BASIC tokenizer *
*
* Flex uses this to create lex.tokenize.c.
*
* Compile with: flex m100-tokenize.lex
* gcc lex.tokenize.c
*/
#include <ctype.h> /* For toupper() */
%option prefix="tokenize"
%option case-insensitive
/* Define states that simply copy text instead of lexing */
%x string
%x remark
%x data
%x datastring
/* Functions defined in m100-tokenize-main.c */
int yyput(uint8_t); /* putchar, but for yyout instead of stdout. */
uint8_t lastput=255; /* last written character, for EOF without nl */
int fixup_ptrs(); /* rewrite line pointers, if possible. */
/* An array to store line pointers, to be fixed up at EOF */
uint16_t ptr[65536];
int nlines = 0;
%%
^[[:space:]]*[0-9]+[ ]? {
ptr[nlines++] = ftell(yyout); /* Cache the location of the current line */
yyput('*'); yyput('*'); /* Dummy placeholder pointer to next line.*/
uint16_t linenum=atoi(yytext); /* BASIC line number. */
yyput(linenum & 0xFF);
yyput(linenum >> 8);
}
\" yyput('"'); BEGIN(string);
<string>\" yyput('"'); BEGIN(INITIAL);
<data>\" yyput('"'); BEGIN(datastring);
<datastring>\" yyput('"'); BEGIN(data);
/* Newline ends <string>, <remark>, <data>, and <datastring> conditions. */
<*>\r?\n yyput('\0'); BEGIN(INITIAL);
/* DATA statements can be ended with a colon (if not quoted as a string) */
<data>: yyput(':'); BEGIN(INITIAL);
<<EOF>> {
(lastput == '\0') || yyput('\0'); /* Handle EOF without preceding newline */
fixup_ptrs();
yyterminate();
}
/* Table of tokens, case-insensitive due to %option above. */
END yyput(128);
FOR yyput(129);
NEXT yyput(130);
DATA yyput(131); BEGIN(data);
INPUT yyput(132);
DIM yyput(133);
READ yyput(134);
LET yyput(135);
GO[ \t]*TO yyput(136);
RUN yyput(137);
IF yyput(138);
RESTORE yyput(139);
GOSUB yyput(140);
RETURN yyput(141);
REM yyput(142); BEGIN(remark);
STOP yyput(143);
WIDTH yyput(144);
ELSE yyput(':'); yyput(145);
LINE yyput(146);
EDIT yyput(147);
ERROR yyput(148);
RESUME yyput(149);
OUT yyput(150);
ON yyput(151);
"DSKO$" yyput(152);
OPEN yyput(153);
CLOSE yyput(154);
LOAD yyput(155);
MERGE yyput(156);
FILES yyput(157);
SAVE yyput(158);
LFILES yyput(159);
LPRINT yyput(160);
DEF yyput(161);
POKE yyput(162);
PRINT yyput(163);
"?" yyput(163);
CONT yyput(164);
LIST yyput(165);
LLIST yyput(166);
CLEAR yyput(167);
CLOAD yyput(168);
CSAVE yyput(169);
"TIME$" yyput(170);
"DATE$" yyput(171);
"DAY$" yyput(172);
COM yyput(173);
MDM yyput(174);
KEY yyput(175);
CLS yyput(176);
BEEP yyput(177);
SOUND yyput(178);
LCOPY yyput(179);
PSET yyput(180);
PRESET yyput(181);
MOTOR yyput(182);
MAX yyput(183);
POWER yyput(184);
CALL yyput(185);
MENU yyput(186);
IPL yyput(187);
NAME yyput(188);
KILL yyput(189);
SCREEN yyput(190);
NEW yyput(191);
"TAB(" yyput(192);
TO yyput(193);
USING yyput(194);
VARPTR yyput(195);
ERL yyput(196);
ERR yyput(197);
"STRING$" yyput(198);
INSTR yyput(199);
"DSKI$" yyput(200);
"INKEY$" yyput(201);
CSRLIN yyput(202);
OFF yyput(203);
HIMEM yyput(204);
THEN yyput(205);
NOT yyput(206);
STEP yyput(207);
"+" yyput(208);
"-" yyput(209);
"*" yyput(210);
"/" yyput(211);
"^" yyput(212);
AND yyput(213);
OR yyput(214);
XOR yyput(215);
EQV yyput(216);
IMP yyput(217);
MOD yyput(218);
"\\" yyput(219);
">" yyput(220);
"=" yyput(221);
"<" yyput(222);
SGN yyput(223);
INT yyput(224);
ABS yyput(225);
FRE yyput(226);
INP yyput(227);
LPOS yyput(228);
POS yyput(229);
SQR yyput(230);
RND yyput(231);
LOG yyput(232);
EXP yyput(233);
COS yyput(234);
SIN yyput(235);
TAN yyput(236);
ATN yyput(237);
PEEK yyput(238);
EOF yyput(239);
LOC yyput(240);
LOF yyput(241);
CINT yyput(242);
CSNG yyput(243);
CDBL yyput(244);
FIX yyput(245);
LEN yyput(246);
"STR$" yyput(247);
VAL yyput(248);
ASC yyput(249);
"CHR$" yyput(250);
"SPACE$" yyput(251);
"LEFT$" yyput(252);
"RIGHT$" yyput(253);
"MID$" yyput(254);
"'" yyput(':'); yyput(0x8E); yyput(0xFF); BEGIN(remark);
/* Anything not yet matched is a variable name, so capitalize it. */
<INITIAL>. {
char *yptr = yytext;
while ( *yptr )
yyput( toupper( *yptr++ ) );
}
%%
/* The main() routine, yyput(), fixup_ptrs() */
#include "m100-tokenize-main.c"