Skip to content

Commit 1aa2355

Browse files
committed
Fixed newToken("pp_keyword",...) not accepting all preprocessor keywords. Huge numbers are now outputted as '1/0' instead of 'math.huge'.
1 parent 6ce2ab2 commit 1aa2355

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

misc/quickTest.lua2p

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ for i = 1, 3 do
7777
!outputLua("break") -- Just a comment.
7878
end
7979

80+
local HUGE_POSITIVE = !(math.huge)
81+
local HUGE_NEGATIVE = !(-math.huge)
82+
local NAN = !(0/0)
83+
8084
print("The end.")
8185

8286
!(

preprocess.lua

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ local KEYWORDS = {
133133
"goto",
134134
} for i, v in ipairs(KEYWORDS) do KEYWORDS[v], KEYWORDS[i] = true, nil end
135135

136+
local PREPROCESSOR_KEYWORDS = {
137+
"file","insert","line",
138+
} for i, v in ipairs(PREPROCESSOR_KEYWORDS) do PREPROCESSOR_KEYWORDS[v], PREPROCESSOR_KEYWORDS[i] = true, nil end
139+
136140
local PUNCTUATION = {
137141
"+", "-", "*", "/", "%", "^", "#",
138142
"==", "~=", "<=", ">=", "<", ">", "=",
@@ -775,11 +779,11 @@ function serialize(buffer, v)
775779
table.insert(buffer, '"'..s..'"')
776780

777781
elseif v == math.huge then
778-
table.insert(buffer, "math.huge")
782+
table.insert(buffer, "(1/0)")
779783
elseif v == -math.huge then
780-
table.insert(buffer, " -math.huge") -- The space prevents an accidental comment if a "-" is right before.
784+
table.insert(buffer, "(-1/0)")
781785
elseif v ~= v then
782-
table.insert(buffer, "0/0") -- NaN.
786+
table.insert(buffer, "(0/0)") -- NaN.
783787
elseif v == 0 then
784788
table.insert(buffer, "0") -- In case it's actually -0 for some reason, which would be silly to output.
785789
elseif vType == "number" then
@@ -1236,7 +1240,7 @@ end
12361240
-- stringToken = newToken( "string", contents [, longForm=false ] )
12371241
-- whitespaceToken = newToken( "whitespace", contents )
12381242
-- ppEntryToken = newToken( "pp_entry", isDouble )
1239-
-- ppKeywordToken = newToken( "pp_keyword", keyword )
1243+
-- ppKeywordToken = newToken( "pp_keyword", ppKeyword )
12401244
--
12411245
-- commentToken = { type="comment", representation=string, value=string, long=isLongForm }
12421246
-- identifierToken = { type="identifier", representation=string, value=string }
@@ -1260,7 +1264,8 @@ end
12601264
function metaFuncs.newToken(tokType, ...)
12611265
if tokType == "comment" then
12621266
local comment, long = ...
1263-
long = not not (long or comment:find"[\r\n]")
1267+
long = not not (long or comment:find"[\r\n]")
1268+
assert(type(comment) == "string")
12641269

12651270
local repr
12661271
if long then
@@ -1280,6 +1285,7 @@ function metaFuncs.newToken(tokType, ...)
12801285

12811286
elseif tokType == "identifier" then
12821287
local ident = ...
1288+
assert(type(ident) == "string")
12831289

12841290
if ident == "" then
12851291
error("Identifier length is 0.")
@@ -1291,6 +1297,7 @@ function metaFuncs.newToken(tokType, ...)
12911297

12921298
elseif tokType == "keyword" then
12931299
local keyword = ...
1300+
assert(type(keyword) == "string")
12941301

12951302
if not KEYWORDS[keyword] then
12961303
error(F("Bad keyword '%s'.", keyword))
@@ -1300,14 +1307,15 @@ function metaFuncs.newToken(tokType, ...)
13001307

13011308
elseif tokType == "number" then
13021309
local n, numberFormat = ...
1303-
numberFormat = numberFormat or "auto"
1310+
numberFormat = numberFormat or "auto"
1311+
assert(type(n) == "number")
13041312

13051313
-- Some of these are technically multiple other tokens. We could trigger an error but ehhh...
13061314
-- @Incomplete: Hexadecimal floats.
13071315
local numStr
1308-
= n ~= n and "0/0"
1309-
or n == math.huge and "math.huge"
1310-
or n == -math.huge and " -math.huge" -- The space prevents an accidental comment if a "-" is right before.
1316+
= n ~= n and "(0/0)"
1317+
or n == math.huge and "(1/0)"
1318+
or n == -math.huge and "(-1/0)"
13111319
or numberFormat == "auto" and tostring(n)
13121320
or numberFormat == "integer" and F("%d", n)
13131321
or numberFormat == "float" and F("%f", n):gsub("(%d)0+$", "%1")
@@ -1321,6 +1329,7 @@ function metaFuncs.newToken(tokType, ...)
13211329

13221330
elseif tokType == "punctuation" then
13231331
local symbol = ...
1332+
assert(type(symbol) == "string")
13241333

13251334
-- Note: "!" and "!!" are of a different token type (pp_entry).
13261335
if not PUNCTUATION[symbol] then
@@ -1331,7 +1340,8 @@ function metaFuncs.newToken(tokType, ...)
13311340

13321341
elseif tokType == "string" then
13331342
local s, long = ...
1334-
long = not not long
1343+
long = not not long
1344+
assert(type(s) == "string")
13351345

13361346
local repr
13371347
if long then
@@ -1351,6 +1361,7 @@ function metaFuncs.newToken(tokType, ...)
13511361

13521362
elseif tokType == "whitespace" then
13531363
local whitespace = ...
1364+
assert(type(whitespace) == "string")
13541365

13551366
if whitespace == "" then
13561367
error("String is empty.")
@@ -1361,14 +1372,18 @@ function metaFuncs.newToken(tokType, ...)
13611372
return {type="whitespace", representation=whitespace, value=whitespace}
13621373

13631374
elseif tokType == "pp_entry" then
1364-
local double = not not ...
1375+
local double = ...
1376+
assert(type(double) == "boolean")
1377+
13651378
local symbol = double and "!!" or "!"
1379+
13661380
return {type="pp_entry", representation=symbol, value=symbol, double=double}
13671381

13681382
elseif tokType == "pp_keyword" then
13691383
local keyword = ...
1384+
assert(type(keyword) == "string")
13701385

1371-
if keyword ~= "insert" then
1386+
if not PREPROCESSOR_KEYWORDS[keyword] then
13721387
error(F("Bad preprocessor keyword '%s'.", keyword))
13731388
end
13741389

0 commit comments

Comments
 (0)