|
120 | 120 |
|
121 | 121 | const LEX_RAW_STRING8 = 0xD1;
|
122 | 122 | const LEX_RAW_STRING16 = 0xD2;
|
| 123 | + const LEX_RAW_INT0 = 0xD3; |
| 124 | + const LEX_RAW_INT8 = 0xD4; |
| 125 | + const LEX_RAW_INT16 = 0xD5; |
123 | 126 |
|
124 | 127 | function pretokenise(code, callback) {
|
125 | 128 | callback(tokenise(code));
|
126 | 129 | }
|
127 | 130 |
|
128 | 131 | function tokenise(code) {
|
129 | 132 | var pretokeniseStrings = false; // only works on 2v20.48 and later
|
| 133 | + var pretokeniseInts = false; // only works on 2v25.396 and later |
130 | 134 | var boardData = Espruino.Core.Env.getBoardData();
|
131 |
| - if (Espruino.Config.PRETOKENISE==2) { |
132 |
| - pretokeniseStrings = true; // always |
| 135 | + if (Espruino.Config.PRETOKENISE==2) { // force all options always |
| 136 | + pretokeniseStrings = true; |
| 137 | + pretokeniseInts = true; |
133 | 138 | } else if (boardData && boardData.VERSION) {
|
134 | 139 | var v = parseFloat(boardData.VERSION.replace("v","0"));
|
135 | 140 | if (v >= 2020.48)
|
136 | 141 | pretokeniseStrings = true;
|
| 142 | + if (v >= 2026) |
| 143 | + pretokeniseInts = true; |
137 | 144 | }
|
138 | 145 |
|
139 | 146 | var lex = (function() {
|
|
171 | 178 | var brackets = 0;
|
172 | 179 | var resultCode = "";
|
173 | 180 | var lastIdx = 0;
|
174 |
| - var lastTok = {str:""}; |
| 181 | + var lastTok = {str:""}, lastlastTok = {str:""}; |
175 | 182 | var tok = lex.next();
|
176 | 183 | while (tok!==undefined) {
|
177 | 184 | var previousString = code.substring(lastIdx, tok.startIdx);
|
|
192 | 199 | if (pretokeniseStrings && tok.type == "STRING") {
|
193 | 200 | let str = tok.value; // get string value
|
194 | 201 | lastIdx = tok.endIdx; // get next token
|
| 202 | + lastlastTok = lastTok; |
195 | 203 | lastTok = tok;
|
196 | 204 | tok = lex.next();
|
197 | 205 | let hadAtoB = resultCode.endsWith("atob(") && tok.str==")"; // were we surrounded by 'atob'?
|
|
207 | 215 | else if (length<65536)
|
208 | 216 | resultCode += String.fromCharCode(LEX_RAW_STRING16, length&255, (length>>8)&255)+str;
|
209 | 217 | if (!hadAtoB) continue; // if not atob, we already got the last token ready
|
| 218 | + } else if (pretokeniseInts && tok.type == "NUMBER") { |
| 219 | + let val = tok.value; // get string value |
| 220 | + if (val==Math.round(val)) { // ensure it's an integer |
| 221 | + // Was there a '-' in a place where it's not a subtraction? |
| 222 | + if (lastTok.str=="-" && [",","(",":","?","="].includes(lastlastTok.str)) { |
| 223 | + resultCode = resultCode.slice(0,-1); // remove - |
| 224 | + val = -val; // negate value |
| 225 | + } |
| 226 | + if (val==0) { // it's shorter just to write quotes |
| 227 | + resultCode += String.fromCharCode(LEX_RAW_INT0); |
| 228 | + } else if (val>=-128 && val<128) |
| 229 | + resultCode += String.fromCharCode(LEX_RAW_INT8, val); |
| 230 | + else if (val>=-32768 && val<32768) |
| 231 | + resultCode += String.fromCharCode(LEX_RAW_INT16, val&255, (val>>8)&255); |
| 232 | + else |
| 233 | + resultCode += tokenString; |
| 234 | + } else resultCode += tokenString; |
210 | 235 | } else if (tokenId) {
|
211 | 236 | //console.log(JSON.stringify(tok.str)+" => "+tokenId);
|
212 | 237 | resultCode += String.fromCharCode(tokenId);
|
|
219 | 244 | }
|
220 | 245 | // next
|
221 | 246 | lastIdx = tok.endIdx;
|
| 247 | + lastlastTok = lastTok; |
222 | 248 | lastTok = tok;
|
223 | 249 | tok = lex.next();
|
224 | 250 | }
|
|
0 commit comments