Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81281 / c2html / controlStructures.pl
blob: 026c216b9dea6cc56e0d2131e04d72601dbf9b49 (plain) (blame)
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
:- [essentials, tokens].
identifyControlStructures([], []):- !.
identifyControlStructures([H|T], [NewH|R]):-
    \+ append(_, [[ttypedef|_]|_], H),
    append(_, [[Token|_]|_], H),
    member(Token, [tdo, tfor, twhile, tif, telse, tswitch, tfunction, tstruct]),
    T = [T1|T2],
    append(T11, [[tleftBrace, "{"], [execNL, "\n"]], T1),
    \+ ((member(M, T11), M \= [execTAB, "\s\s\s\s"])),
    gatherBody(T2, [], PackedStructBody, Rest, 1),
    identifyControlStructures(PackedStructBody, Result),
    append([H, T1], Result, NewH), !,
    identifyControlStructures(Rest, R).
identifyControlStructures([H|T], [NewH|R]):-
    \+ append(_, [[ttypedef|_]|_], H),
    append(_, [[Token|_]|_], H),
    member(Token, [tfor, twhile, tif, telse]),
    T = [T1|_],
    \+ append(_, [[tleftBrace, "{"], [execNL, "\n"]], T1),
    getPrefixTabsLength(H, N),
    getPrefixTabsLength(T1, M),
    N + 1 =:= M,
    gatherOneLineBody(T, [], PackedOneLineBody, Rest, N),
    identifyControlStructures(PackedOneLineBody, Result),
    append([H], Result, NewH), !,
    identifyControlStructures(Rest, R).
identifyControlStructures([H|T], [H|R]):- !,
    identifyControlStructures(T, R).


gatherOneLineBody([], Res, Res, [], _):- !.
gatherOneLineBody([H|T], Res, Res, [H|T], N):-
    getPrefixTabsLength(H, M),
    N + 1 =\= M, !.
gatherOneLineBody([H|T], Buff, Res, Rest, N):-
    getPrefixTabsLength(H, M),
    N + 1 =:= M,
    append(Buff, [H], NewBuff), !,
    gatherOneLineBody(T, NewBuff, Res, Rest, M).


gatherBody(Rest, Res, Res, Rest, 0):- !.
gatherBody([H|T], Buff, Res, Rest, N):-
    N =\= 0, append(H1, [[tleftBrace, "{"]|_], H),
    \+ ((member(M, H1), M \= [execTAB, "\s\s\s\s"])),
    append(Buff, [H], NewBuff), N1 is N + 1, !,
    gatherBody(T, NewBuff, Res, Rest, N1).
gatherBody([H|T], Buff, Res, Rest, N):-
    N =\= 0, append(H1, [[trightBrace, "}"]|_], H),
    \+ ((member(M, H1), M \= [execTAB, "\s\s\s\s"])),
    append(Buff, [H], NewBuff), N1 is N - 1, !,
    gatherBody(T, NewBuff, Res, Rest, N1).
gatherBody([H|T], Buff, Res, Rest, N):-
    N =\= 0,
    append(Buff, [H], NewBuff), !,
    gatherBody(T, NewBuff, Res, Rest, N).