-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
doc_complexity.pro
59 lines (51 loc) · 1.49 KB
/
doc_complexity.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
; docformat = 'rst'
;+
; Computes the cyclomatic complexity (or conditional complexity) of a section
; of code.
;
; The complexity is::
;
; p - s + 2
;
; where p is the number of decision points and s is the number of exit points.
;
; For more information, see::
;
; http://en.wikipedia.org/wiki/Cyclomatic_complexity
;
; :Todo:
; * should ignore comments and string literals
; * should add the number of non-else clauses in CASE and SWITCH statements
;
; :Returns:
; long
;
; :Params:
; lines : in, optional, type=strarr
; lines of code to analyze
;-
function doc_complexity, lines
compile_opt strictarr
pattern = '[[:>:]]'
;pattern = '[[:space:](),-\+]'
tokenizer = obj_new('MGffTokenizer', lines, /string_array, pattern=pattern)
complexity = 1L
while (~tokenizer->done()) do begin
tok = tokenizer->next(pre_delim=pre, post_delim=post, newline=newline)
case strlowcase(strtrim(tok, 2)) of
'if': complexity++
'case': complexity++ ; really should add the number of non-else cases
'switch': complexity++ ; really should add the number of non-else cases
'while': complexity++
'repeat': complexity++
'for': complexity++
'return': complexity--
else:
endcase
endwhile
return, complexity > 1
end
; main-level example program
print, doc_complexity(['if(1)then print, 1 else if-1 gt 2 then print, 2 else if+2 gt 3 then print, 3'])
print, doc_complexity(['case 1 of', 'a:print, a', 'b: print, b', 'else:', 'endcase'])
end