summaryrefslogtreecommitdiff
path: root/Data/Libraries/Penlight/examples/testglobal.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-30 11:32:16 +0800
committerchai <chaifix@163.com>2021-10-30 11:32:16 +0800
commit42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce (patch)
tree24bc7009457a8d7500f264e89946dc20d069294f /Data/Libraries/Penlight/examples/testglobal.lua
parent164885fd98d48703bd771f802d79557b7db97431 (diff)
+ Penlight
Diffstat (limited to 'Data/Libraries/Penlight/examples/testglobal.lua')
-rw-r--r--Data/Libraries/Penlight/examples/testglobal.lua28
1 files changed, 28 insertions, 0 deletions
diff --git a/Data/Libraries/Penlight/examples/testglobal.lua b/Data/Libraries/Penlight/examples/testglobal.lua
new file mode 100644
index 0000000..0baaaef
--- /dev/null
+++ b/Data/Libraries/Penlight/examples/testglobal.lua
@@ -0,0 +1,28 @@
+-- very simple lexer program which looks at all identifiers in a Lua
+-- file and checks whether they're in the global namespace.
+-- At the end, we dump out the result of count_map, which will give us
+-- unique identifiers with their usage count.
+-- (an example of a program which itself needs to be careful about what
+-- goes into the global namespace)
+
+local utils = require 'pl.utils'
+local file = require 'pl.file'
+local lexer = require 'pl.lexer'
+local List = require 'pl.List'
+local pretty = require 'pl.pretty'
+local seq = require 'pl.seq'
+local path = require 'pl.path'
+
+utils.on_error 'quit'
+
+local txt = file.read(arg[1] or path.normpath('examples/testglobal.lua'))
+local globals = List()
+for t,v in lexer.lua(txt) do
+ if t == 'iden' and rawget(_G,v) then
+ globals:append(v)
+ end
+end
+
+pretty.dump(seq.count_map(globals))
+
+