This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Shura1oplot's profile picture

cclint - lua linter with cc-specified features

Started by Shura1oplot, 11 March 2014 - 12:49 PM
Shura1oplot #1
Posted 11 March 2014 - 01:49 PM
cclint is an outside MC lua linter with CC-specified features. It uses luac's bytecode listing and reports all accesses to global variables, which catches many typing errors in variable names.

Examples
SpoilerExample 1
lua code:

local function f()
	count = 0
	for i = 1, 10 do
		-- do some useful
		count = count + 1
	end
end
cclint output:

W:2: global set of 'count'
W:5: global get of 'count'
W:5: global set of 'count'

Example 2
lua code:

local function doSomeUseful()
end
local function main()
	doSoneUseful()
end
cclint outout:

W:4: global get of 'doSoneUseful'

Example 3 (CC-specified)
lua code:

local function main()
	lama.forward()
end
cclint outout:

W:2: global get of 'lama'
solution:

os.loadAPI("apis/lama")
local function main()
	lama.forward()
end

If luac cannot compile code, cclint reports error.
SpoilerExample 4
lua code:

local function f(a, B)/>/>/>/>/>
	if a > b
		return true
	end
	return false
end
cclint outout:

E:3: 'then' expected near 'return'

Global variable can be declared by

os.loadAPI("path/NAME")
bapil.loadAPI("path/NAME")

Linter supports some control directives:
  • lint-ignore-global-get: foo,bar,baz - ignore global get for listed names,
  • lint-ignore-global-set: foo,bar,baz - ignore global set for listed names,
  • lint-ignore-global: foo,bar,baz - ignore global get or set for listed names,
  • lint-set-globals-in-main-chunk - ignore globals set in main chunk,
  • lint-check-globals-cached - report on getting globals outside main chunk.
Directives can be added as comment into the code.
Spoiler

-- lint-ignore-global-set: varX
varX = 0


-- lint-set-globals-in-main-chunk
function f1()
end
function f2()
end


-- lint-check-globals-cached
local function main(...)
	local n = select("#", ...)
end
cclint output:

W:3: global get of 'select'
solution:

-- lint-check-globals-cached
local select = select
local function main(...)
	local n = select("#", ...)
end

Download: https://github.com/Shura1oplot/cclint

How to install
On windows:
  1. create C:\cclint
  2. copy cclint.py to C:\cclint
  3. install python >= 3.3 (http://python.org/downloads/)
  4. download lua 5.1 windows binaries (http://luabinaries.s...t/download.html)
  5. copy luac5.1.exe to C:\cclint
  6. run it in windows terminal by typing "C:\cclint\cclint.py lua_file"
On linux:
  1. install lua 5.1 from repository (for debian based distros: apt-get install lua5.1)
  2. copy cclint.py to ~/bin
  3. ensure ~/bin is in PATH environment variable
  4. use it in terminal emulator by typing "cclint.py lua_file"
On OSX:
  1. install lua 5.1 somehow (I have never use OSX)
  2. copy cclint.py to ~/bin
  3. ensure ~/bin is in PATH variable
  4. if directory with luac5.1 is not in PATH environment variable, set LUAC51 environment variable to full path to luac5.1.
  5. use it in terminal emulator by typing "cclint.py lua_file"
cclint looks for luac5.1 binary:
  1. in LUAC51 environment variable,
  2. in the directory with cclint.py [only on Windows],
  3. in directories in PATH environment variable.

Similar projects:
There is an experimental SublimeText 3 plugin: https://github.com/S...-contrib-cclint
It can be installed only by git clone to ~/.config/sublime-text-3/Packages directory.
Tested on linux, may work on OSX and should not work on windows without some changes in plugin code.
If windows support is needed, add issue or pull request in github project page.
SpoilerScreenshot 1
Edited on 11 March 2014 - 02:01 PM
tesla1889 #2
Posted 15 March 2014 - 03:08 PM
http://en.wikipedia.org/wiki/Lint_%28software%29

pretty ambitious from what i've read
i'm glad someone has implemented this