Skip to content

Commit f0a3c08

Browse files
committed
Added evaluate().
Argument expressions for macros are no longer validated (as it was inconsistent).
1 parent 94b7e49 commit f0a3c08

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

preprocess.lua

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[============================================================
22
--=
3-
--= LuaPreprocess library
3+
--= LuaPreprocess v1.17-dev - preprocessing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
66
--= License: MIT (see the bottom of this file)
@@ -21,7 +21,7 @@
2121
- printf
2222
- run
2323
- tokenize, newToken, concatTokens, removeUselessTokens, eachToken, isToken, getNextUsefulToken
24-
- toLua, serialize
24+
- toLua, serialize, evaluate
2525
Only during processing:
2626
- getCurrentPathIn, getCurrentPathOut
2727
- getOutputSoFar, getOutputSizeSoFar, getCurrentLineNumberInOutput
@@ -127,7 +127,7 @@
127127

128128

129129

130-
local PP_VERSION = "1.17.0"
130+
local PP_VERSION = "1.17.0-dev"
131131

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

@@ -238,7 +238,7 @@ local loadLuaString, loadLuaFile
238238
local outputLineNumber, maybeOutputLineNumber
239239
local pack, unpack
240240
local printf, printTokens, printError, printfError, printErrorTraceback
241-
local serialize, toLua
241+
local serialize, toLua, evaluate
242242
local tableInsert, tableRemove, tableInsertFormat
243243
local utf8GetCharLength, utf8GetCodepointAndLength
244244

@@ -1059,8 +1059,6 @@ function serialize(buffer, v)
10591059
return true
10601060
end
10611061

1062-
1063-
10641062
-- luaString = toLua( value )
10651063
-- Returns nil and a message on error.
10661064
function toLua(v)
@@ -1072,6 +1070,21 @@ function toLua(v)
10721070
return table.concat(buffer)
10731071
end
10741072

1073+
-- value = evaluate( expression )
1074+
-- Returns nil and a message on error.
1075+
function evaluate(expression)
1076+
local chunk, err = loadLuaString("return "..expression, "@", getfenv(2))
1077+
if not chunk then
1078+
return nil, F("Invalid expression '%s'. (%s)", expression, (err:gsub("^:%d+: ", "")))
1079+
end
1080+
1081+
if expression:find(",", 1, true) and not loadLuaString("return("..expression.."\n)", "@") then
1082+
return nil, F("Ambiguous expression '%s'. (Comma-separated list?)", expression)
1083+
end
1084+
1085+
return (chunk())
1086+
end
1087+
10751088

10761089

10771090
function escapePattern(s)
@@ -1374,6 +1387,12 @@ metaFuncs.toLua = toLua
13741387
-- This could avoid allocating unnecessary strings.
13751388
metaFuncs.serialize = serialize
13761389

1390+
-- evaluate()
1391+
-- value = evaluate( expression )
1392+
-- Evaluate an expression. Returns nil and a message on error.
1393+
-- Note that nil or false can also be returned if that's the value the expression results in!
1394+
metaFuncs.evaluate = evaluate
1395+
13771396
-- escapePattern()
13781397
-- escapedString = escapePattern( string )
13791398
-- Escape a string so it can be used in a pattern as plain text.
@@ -2458,6 +2477,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
24582477
end
24592478
local argStr = insertTokensAsStringLiteral(tokens, argNonPpTokens, argNonPpStartTok)
24602479

2480+
--[[ :NoInconsistentMacroArgumentValidation
24612481
if isFirstPart then
24622482
local chunk, err = loadLuaString("return ("..argStr..")", "@")
24632483
@@ -2467,6 +2487,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
24672487
-- errorAtToken(fileBuffers, argNonPpStartTok, nil, "Macro", "Syntax error: Invalid table constructor expression. (%s)", err)
24682488
end
24692489
end
2490+
]]
24702491
end
24712492

24722493
-- Add ')' for end of call.
@@ -2585,6 +2606,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
25852606
end
25862607
local argStr = insertTokensAsStringLiteral(tokens, argNonPpTokens, argNonPpStartTok)
25872608

2609+
--[[ :NoInconsistentMacroArgumentValidation
25882610
if isFirstPart then
25892611
local chunk, err = loadLuaString("return ("..argStr..")", "@")
25902612
@@ -2594,6 +2616,7 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
25942616
-- errorAtToken(fileBuffers, argNonPpStartTok, nil, "Macro", "Syntax error: Invalid expression for argument #%d. (%s)", argNum, err)
25952617
end
25962618
end
2619+
]]
25972620

25982621
elseif isFirstPart then
25992622
-- There were no useful tokens for the argument!
@@ -3398,7 +3421,7 @@ return pp
33983421

33993422
--[[!===========================================================
34003423
3401-
Copyright © 2018-2021 Marcus 'ReFreezed' Thunström
3424+
Copyright © 2018-2022 Marcus 'ReFreezed' Thunström
34023425
34033426
Permission is hereby granted, free of charge, to any person obtaining a copy
34043427
of this software and associated documentation files (the "Software"), to deal

tests/quickTest.lua2p

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ other = 500921
154154
!local lua = stopInterceptingOutput():gsub("%a+", "%0Derp")
155155
!!(lua)
156156

157+
!local function CALL_WITH_EVALUATED(func, value)
158+
$func(!(assert(evaluate(value))))
159+
!end
160+
@@CALL_WITH_EVALUATED(print, table.concat{"Large number: ",59^15})
161+
157162

158163

159164
-- Predefined macros.

tests/quickTest.output.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ local y = x
109109
aDerp = someDerp
110110
otherDerp = 500921
111111

112+
print("Large number: 3.6540978656062e+026")
113+
112114

113115

114116
-- Symbols

0 commit comments

Comments
 (0)