Skip to content

Commit 52e4988

Browse files
committed
Added aliases for some number formats. Added tests for preprocessor keywords.
1 parent d52a5e2 commit 52e4988

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

misc/quickTest.lua2p

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ for _, token in ipairs{
101101
newToken("keyword", "while"),
102102
-- newToken("keyword", "continue"), -- Error! :'(
103103
newToken("number", 42),
104-
newToken("number", 42, "integer"),
104+
newToken("number", 44, "integer"),
105+
newToken("number", 48, "int"),
105106
newToken("number", 5.7, "float"),
106107
newToken("number", 0, "float"),
107108
newToken("number", 5.7, "scientific"),
108-
newToken("number", .031, "scientific"),
109+
newToken("number", .031, "SCIENTIFIC"),
110+
newToken("number", 5.7, "e"),
111+
newToken("number", .031, "E"),
109112
newToken("number", 42, "hexadecimal"),
113+
newToken("number", 42, "HEX"),
110114
newToken("number", 5.7, "auto"),
111115
newToken("number", math.huge),
112116
newToken("number", -math.huge),

misc/runTests.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,25 @@ doTest("Output values of different types", function()
195195
assert(not luaOut)
196196
end)
197197

198-
-- @Incomplete: Add tests for @insert and the other pp keywords.
199-
-- @Incomplete: Add test for params.onInsert().
198+
doTest("Preprocessor keywords", function()
199+
local pp = assert(loadfile"preprocess.lua")()
200+
201+
local luaOut = assert(pp.processString{ code=[[ filename = @file ]]})
202+
assertCodeOutput(luaOut, [[filename = "<code>"]]) -- Note: The dummy value when we have no real path may change in the future.
203+
204+
local luaOut = assert(pp.processString{ code=[[ ln = @line ]]})
205+
assertCodeOutput(luaOut, [[ln = 1]])
206+
207+
local luaOut = assert(pp.processString{
208+
code = [[
209+
v = @insert "foo"
210+
]],
211+
onInsert = function(name)
212+
return '"bar"'
213+
end,
214+
})
215+
assertCodeOutput(luaOut, [[v = "bar"]])
216+
end)
200217

201218

202219

@@ -208,7 +225,7 @@ doTest("Get useful tokens", function()
208225

209226
pp.removeUselessTokens(tokens)
210227

211-
assert(#tokens == 4, "Unexpected amount of tokens.")
228+
assert(#tokens == 4, "Unexpected token count.")
212229
assert(tokens[1].type == "keyword", "Unexpected token type 1.")
213230
assert(tokens[1].value == "local", "Unexpected token value 1.")
214231
assert(tokens[2].type == "identifier", "Unexpected token type 2.")

preprocess-cl.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ exec lua "$0" "$@"
130130
info: Info about the processed file. (See 'ProcessInfo' in preprocess.lua)
131131
132132
"fileerror"
133-
Sent if an error happens while processing a file (right before the program quits).
133+
Sent if an error happens while processing a file (right before the program exits).
134134
Arguments:
135135
path: The file being processed.
136136
error: The error message.

preprocess.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ setmetatable(_G, {__newindex=function(_G, k, v)
122122
end})
123123
--]]
124124

125-
local VERSION = "1.11.2"
125+
local VERSION = "1.12.0"
126126

127127
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
128128

@@ -252,7 +252,7 @@ function printTraceback(message, level)
252252
print(message)
253253
print("stack traceback:")
254254

255-
for level = 1+(level or 1), math.huge do
255+
for level = 1+(level or 1), 1/0 do
256256
local info = debug.getinfo(level, "nSl")
257257
if not info then break end
258258

@@ -799,9 +799,9 @@ function serialize(buffer, v)
799799

800800
table.insert(buffer, '"'..s..'"')
801801

802-
elseif v == math.huge then
802+
elseif v == 1/0 then
803803
table.insert(buffer, "(1/0)")
804-
elseif v == -math.huge then
804+
elseif v == -1/0 then
805805
table.insert(buffer, "(-1/0)")
806806
elseif v ~= v then
807807
table.insert(buffer, "(0/0)") -- NaN.
@@ -956,8 +956,8 @@ function getNextUsableToken(tokens, i, iLimit, dir)
956956

957957
iLimit
958958
= dir < 0
959-
and math.max((iLimit or 1), 1)
960-
or math.min((iLimit or math.huge), #tokens)
959+
and math.max((iLimit or 1 ), 1)
960+
or math.min((iLimit or 1/0), #tokens)
961961

962962
for i = i, iLimit, dir do
963963
if not USELESS_TOKENS[tokens[i].type] then
@@ -1275,11 +1275,16 @@ end
12751275
--
12761276
-- Number formats:
12771277
-- "integer" E.g. 42
1278+
-- "int" Same as integer, e.g. 42
12781279
-- "float" E.g. 3.14
12791280
-- "scientific" E.g. 0.7e+12
12801281
-- "SCIENTIFIC" E.g. 0.7E+12 (upper case)
1282+
-- "e" Same as scientific, e.g. 0.7e+12
1283+
-- "E" Same as SCIENTIFIC, e.g. 0.7E+12 (upper case)
12811284
-- "hexadecimal" E.g. 0x19af
12821285
-- "HEXADECIMAL" E.g. 0x19AF (upper case)
1286+
-- "hex" Same as hexadecimal, e.g. 0x19af
1287+
-- "HEX" Same as HEXADECIMAL, e.g. 0x19AF (upper case)
12831288
-- "auto" Note: Infinite numbers and NaN always get automatic format.
12841289
--
12851290
function metaFuncs.newToken(tokType, ...)
@@ -1335,15 +1340,20 @@ function metaFuncs.newToken(tokType, ...)
13351340
-- @Incomplete: Hexadecimal floats.
13361341
local numStr
13371342
= n ~= n and "(0/0)"
1338-
or n == math.huge and "(1/0)"
1339-
or n == -math.huge and "(-1/0)"
1343+
or n == 1/0 and "(1/0)"
1344+
or n == -1/0 and "(-1/0)"
13401345
or numberFormat == "auto" and tostring(n)
13411346
or numberFormat == "integer" and F("%d", n)
1347+
or numberFormat == "int" and F("%d", n)
13421348
or numberFormat == "float" and F("%f", n):gsub("(%d)0+$", "%1")
13431349
or numberFormat == "scientific" and F("%e", n):gsub("(%d)0+e", "%1e"):gsub("0+(%d+)$", "%1")
13441350
or numberFormat == "SCIENTIFIC" and F("%E", n):gsub("(%d)0+E", "%1E"):gsub("0+(%d+)$", "%1")
1351+
or numberFormat == "e" and F("%e", n):gsub("(%d)0+e", "%1e"):gsub("0+(%d+)$", "%1")
1352+
or numberFormat == "E" and F("%E", n):gsub("(%d)0+E", "%1E"):gsub("0+(%d+)$", "%1")
13451353
or numberFormat == "hexadecimal" and (n == math.floor(n) and F("0x%x", n) or error("Hexadecimal floats not supported yet."))
13461354
or numberFormat == "HEXADECIMAL" and (n == math.floor(n) and F("0x%X", n) or error("Hexadecimal floats not supported yet."))
1355+
or numberFormat == "hex" and (n == math.floor(n) and F("0x%x", n) or error("Hexadecimal floats not supported yet."))
1356+
or numberFormat == "HEX" and (n == math.floor(n) and F("0x%X", n) or error("Hexadecimal floats not supported yet."))
13471357
or error(F("Invalid number format '%s'.", numberFormat))
13481358

13491359
return {type="number", representation=numStr, value=n}

0 commit comments

Comments
 (0)