Skip to content

Commit 5ab9415

Browse files
committed
Allow us to pretokenise ints
1 parent 7ed106b commit 5ab9415

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

plugins/pretokenise.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,27 @@
120120

121121
const LEX_RAW_STRING8 = 0xD1;
122122
const LEX_RAW_STRING16 = 0xD2;
123+
const LEX_RAW_INT0 = 0xD3;
124+
const LEX_RAW_INT8 = 0xD4;
125+
const LEX_RAW_INT16 = 0xD5;
123126

124127
function pretokenise(code, callback) {
125128
callback(tokenise(code));
126129
}
127130

128131
function tokenise(code) {
129132
var pretokeniseStrings = false; // only works on 2v20.48 and later
133+
var pretokeniseInts = false; // only works on 2v25.396 and later
130134
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;
133138
} else if (boardData && boardData.VERSION) {
134139
var v = parseFloat(boardData.VERSION.replace("v","0"));
135140
if (v >= 2020.48)
136141
pretokeniseStrings = true;
142+
if (v >= 2026)
143+
pretokeniseInts = true;
137144
}
138145

139146
var lex = (function() {
@@ -171,7 +178,7 @@
171178
var brackets = 0;
172179
var resultCode = "";
173180
var lastIdx = 0;
174-
var lastTok = {str:""};
181+
var lastTok = {str:""}, lastlastTok = {str:""};
175182
var tok = lex.next();
176183
while (tok!==undefined) {
177184
var previousString = code.substring(lastIdx, tok.startIdx);
@@ -192,6 +199,7 @@
192199
if (pretokeniseStrings && tok.type == "STRING") {
193200
let str = tok.value; // get string value
194201
lastIdx = tok.endIdx; // get next token
202+
lastlastTok = lastTok;
195203
lastTok = tok;
196204
tok = lex.next();
197205
let hadAtoB = resultCode.endsWith("atob(") && tok.str==")"; // were we surrounded by 'atob'?
@@ -207,6 +215,23 @@
207215
else if (length<65536)
208216
resultCode += String.fromCharCode(LEX_RAW_STRING16, length&255, (length>>8)&255)+str;
209217
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;
210235
} else if (tokenId) {
211236
//console.log(JSON.stringify(tok.str)+" => "+tokenId);
212237
resultCode += String.fromCharCode(tokenId);
@@ -219,6 +244,7 @@
219244
}
220245
// next
221246
lastIdx = tok.endIdx;
247+
lastlastTok = lastTok;
222248
lastTok = tok;
223249
tok = lex.next();
224250
}

0 commit comments

Comments
 (0)