diff options
| Commit | 9557d0cb5a1b3aa9466ddc504be004b5449ce9f2 (patch) | |
|---|---|---|
| Author | 2018-11-03 15:22:53 +0200 | |
| Committer | 2018-11-03 15:22:53 +0200 | |
| Files | 296b007f8913f4ecda3b790af94b04cc329407ce | |
| IPL-9557d0cb5a1b3aa9466ddc504be004b5449ce9f2.tar IPL-9557d0cb5a1b3aa9466ddc504be004b5449ce9f2.tar.gz IPL-9557d0cb5a1b3aa9466ddc504be004b5449ce9f2.zip | ||
| Parent | 113598bd4636b65720df6af87ea5a94661907c8c (diff) | |
Added prettifier and control structure collapse. (#19)
* Create README.md
* Initial commit
* Initial commit
* Create README.md
* Rename homework/81281/c2html.pl to homework/81281/c2html/c2html.pl
* Rename homework/81281/test.c to homework/81281/c2html/test.c
* Update README.md
* Update README.md
* Update c2html.pl
* Update c2html.pl
* Update c2html.pl
* Update README.md
* Update c2html.pl
* Update test.c
* Create executeProgram.sh
* Update README.md
* Update c2html.pl
* Initial commit of updated code + prettify C
* Rename secret.c to messyCode.c
* Update and rename test.c to initialTest.c
* Update README.md
* Update README.md
* Update README.md
* Update README.md
* Rename executeProgram.sh to c2htmlrun.sh
* Update README.md
* Update prettify.pl
* Update prettify.pl
* Update messyCode.c
* Fixed some bugs.
* Update README.md
* Update c2html.pl
* Added collapse and fixed matrix representation.
* Updated flatten and added predicate for function identification.
* Update initialTest.c
* Update messyCode.c
* Bug fixing, added is_functionAndBody predicate.
* Update print.pl
* Result after applying prettify and added collapse functions
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Can identify structures and collapse them too
* Update messyCode.c
* Update README.md
* Update prettify.pl
* Update README.md
* Update prettify.pl
* Update messyCode.c
* Update messyCode.c
* Update page.html
* Fix bug typedef
* Update page.html
* Added choices
* Update c2html.pl
* Update prettify.pl
* Update prettify.pl
* Update README.md
* Update README.md
* Update c2html.pl
* Update c2html.pl
* Added forgotten do-while structure format
* Update messyCode.c
* Removing unknown tokens from stream
* Rename homework/81281/c2html/page.html to homework/81281/c2html/exampleCodeAndTestPage/page.html
* Rename homework/81281/c2html/messyCode.c to homework/81281/c2html/exampleCodeAndTestPage/messyCode.c
* Rename homework/81281/c2html/initialTest.c to homework/81281/c2html/exampleCodeAndTestPage/initialTest.c
* Update prettify.pl
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Can collapse all control structures and fixed comment /**/
* Fixed named of predicates
* Update messyCode.c
* Update page.html
* Update README.md
* Changed execTAB to "\s\s\s\s"
* Changed execTAB to "\s\s\s\s"
* Changed execTAB to "\s\s\s\s"
* Update page.html
* Added new tokens (forgotten operators) and added rules for them
* Added new tokens (forgotten operators) and fixed some old predicates
* Fixed bug with "else '\n' if (..)" in addNewLines predicate
* Fixed regular expression for a string and removed token ttypesPrintF
* Added clauses for handling enum and fixed handling of chars
* Fixed typos
* Added forgotten token to clause
* Fixed "case smt :'\n'" where smt is more than one char
* Fixed some bugs and divided code in modules
* New code module and fixed bugs
* Fixed some bugs with tabulation
* Divided code in modules
11 files changed, 1418 insertions, 321 deletions
| -rw-r--r-- | homework/81281/c2html/README.md | 23 | ||||
| -rw-r--r-- | homework/81281/c2html/c2html.pl | 413 | ||||
| -rw-r--r-- | homework/81281/c2html/c2htmlrun.sh | 13 | ||||
| -rw-r--r-- | homework/81281/c2html/controlStructures.pl | 56 | ||||
| -rw-r--r-- | homework/81281/c2html/essentials.pl | 14 | ||||
| -rw-r--r-- | homework/81281/c2html/exampleCodeAndTestPage/initialTest.c | 99 | ||||
| -rw-r--r-- | homework/81281/c2html/exampleCodeAndTestPage/messyCode.c | 139 | ||||
| -rw-r--r-- | homework/81281/c2html/exampleCodeAndTestPage/page.html | 266 | ||||
| -rw-r--r-- | homework/81281/c2html/prettify.pl | 384 | ||||
| -rw-r--r-- | homework/81281/c2html/print.pl | 103 | ||||
| -rw-r--r-- | homework/81281/c2html/tokens.pl | 229 |
diff --git a/homework/81281/c2html/README.md b/homework/81281/c2html/README.md index d5d6983..f57d94f 100644 --- a/homework/81281/c2html/README.md +++ b/homework/81281/c2html/README.md @@ -1,5 +1,16 @@ -# Homework 1 -Create a xxx2html syntax highlighter for a language of your choice. +# Homework 1 + 2 +## Create a xxx2html syntax highlighter for a language of your choice. ++ ~~In this case it is C.~~ +## Extend the xxx2html tool to: + ++ ~~Produce pretty-printed html.~~ + ++ **TODO:** bonus: support line length limit + ++ **TODO:** Produce html where functions have their background colored depending on cyclomatic complexity ranging from light green to light read. + ++ ~~Produce html where control structures can be collapsed.~~ + ### How to install and use SWI-Prolog @@ -10,13 +21,15 @@ Create a xxx2html syntax highlighter for a language of your choice. + [Command line options](http://www.swi-prolog.org/pldoc/man?section=cmdline) ### Added bash script for executing program after successful installation of SWI-Prolog on a Unix or Unix-like variants -[Script](./executeProgram.sh) + +[Script](./c2htmlrun.sh) ### If you are going to install SWI-Prolog on Windows -+ You can omit lines 4 to 16 from c2html.pl. ++ You can omit lines 6 to 17 from c2html.pl. + Afterward in order to execute the program you can call the main predicate and give it a valid string name of file. -For example **main("test.c").** +For example **mainy("test.c").** + ### For the curious who want to find about DCG Grammar rules : diff --git a/homework/81281/c2html/c2html.pl b/homework/81281/c2html/c2html.pl index 82e8840..9479b96 100644 --- a/homework/81281/c2html/c2html.pl +++ b/homework/81281/c2html/c2html.pl @@ -1,351 +1,132 @@ :- use_module(library(dcg/basics)). :- use_module(library(readutil)). - +:- [tokens, prettify, print, essentials, controlStructures]. :- set_prolog_flag(verbose, silent). :- initialization(main, main). main :- current_prolog_flag(argv, Argv), - Argv = [H|_], - atom_string(H, File), - mainy(File), + Argv = [H1, H2|_], + atom_string(H1, File), + atom_string(H2, Number), + mainy(File, Number), format('You can see the result in the html file.~n'), halt. main :- halt(1). -mainy(File):- +mainy(File, Number):- + open(File, read, RFile), read_stream_to_codes(RFile, CSource), close(RFile), phrase(tokens(Tokens), CSource), -%% write(Tokens), + flattenMine(Tokens, TokenStream), + ((Number == "0", PrettyCode = TokenStream); + (Number == "1", prettify(TokenStream, Raw), + flattenMine(Raw, PrettyCode)); + (Number == "2", prettify(TokenStream, Raw), + identifyControlStructures(Raw, RawF), + flattenMine(RawF, PrettyCode))), open('page.html', write, WFile), - write(WFile,"<!doctype> + write(WFile,'<!doctype> <html> - <head> - <title>hello.c</title> - <style> - .keyword { - color: blue; - } - .number { - color: violet; - } - .identifier { - color: green; - } - .operator { - font-style: bold; - color: blue; - } - .comment { - color: lime; - } - .string { - color: orange; - } - .parenthese { - font-style: bold; - color: brown; - } - .quotation { - font-style: bold; - color: black; - } - .function { - color: purple; - } - </style> - </head> - <body> - <pre class=\"code\">"), - tohtmlfile(WFile,Tokens), - close(WFile). - -%% Definitions of tokens -token([tauto, "auto" ]) --> "auto", !. -token([tbreak, "break" ]) --> "break", !. -token([tcase, "case" ]) --> "case", !. -token([tchar, "char" ]) --> "char", !. -token([tconst, "const" ]) --> "const", !. -token([tcontinue, "continue" ]) --> "continue", !. -token([tdefault, "default" ]) --> "default", !. -token([tdo, "do" ]) --> "do", !. -token([tdouble, "double" ]) --> "double", !. -token([telse, "else" ]) --> "else", !. -token([tenum, "enum" ]) --> "enum", !. -token([textern, "extern" ]) --> "extern", !. -token([tfloat, "float" ]) --> "float", !. -token([tfor, "for" ]) --> "for", !. -token([tgoto, "goto" ]) --> "goto", !. -token([tif, "if" ]) --> "if", !. -token([tint, "int" ]) --> "int", !. -token([tlong, "long" ]) --> "long", !. -token([tregister, "register" ]) --> "register", !. -token([treturn, "return" ]) --> "return", !. -token([tshort, "short" ]) --> "short", !. -token([tsigned, "signed" ]) --> "signed", !. -token([tsizeof, "sizeof" ]) --> "sizeof", !. -token([tstatic, "static" ]) --> "static", !. -token([tstruct, "struct" ]) --> "struct", !. -token([tswitch, "switch"]) --> "switch", !. -token([ttypedef, "switch" ]) --> "typedef", !. -token([tunion, "union" ]) --> "union", !. -token([tunsigned, "unsigned" ]) --> "unsigned", !. -token([tvoid, "void" ]) --> "void", !. -token([tvolatile, "volatile" ]) --> "volatile", !. -token([twhile, "while" ]) --> "while", !. -token([tinclude, "#include" ]) --> "#include", !. - -token([tcomment1, IA]) --> comment1(I), !, - {atom_chars(IA,I)}. - -%% Handling windows written files with "\r\n" -token([[tcomment2, IA], [execNL, '\n']]) --> comment2(I), !, - {append(I1, [C1, C2], I), - ((char_code('\r', C1), char_code('\n', C2), atom_chars(IA,I1)); - (\+ char_code('\r', C1), char_code('\n', C2), - append(I1, [C1], I2), atom_chars(IA,I2)))}. - -token([ttypesPrintF, IA]) --> prFT(I), !, - {atom_chars(IA,I)}. -token([tstring, IA]) --> stringy(I), !, - {atom_chars(IA,I)}. -token([theader, IA]) --> header(I), !, - {append(IO, [_], I), append([_], II, IO), atom_chars(IA,II)}. -token([[tfunction, IA], [tleftParen, "("]]) --> func(I), !, - {append(IO, [_], I), IO \= [], atom_chars(IA, IO)}. - -token([tmultiply, "*"]) --> "*", !. -token([tdivide, "/"]) --> "/", !. -token([tmod, "%"]) --> "%", !. -token([tadd, "+"]) --> "+", !. -token([tsubtract, "-"]) --> "-", !. -token([tless, "<"]) --> "<", !. -token([tlessEqual, "<="]) --> "<=", !. -token([tgreater, ">"]) --> ">", !. -token([tgreaterEqual,">=" ]) --> ">=", !. -token([tequal, "=="]) --> "==", !. -token([tnotequal,"!=" ]) --> "!=", !. -token([tnot, "!"]) --> "!", !. -token([tassign,"="]) --> "=", !. -token([tlogicalAnd, "&&"]) --> "&&", !. -token([tlogicalOr, "||"]) --> "||", !. -token([tbitwiseAnd, "&"]) --> "&", !. -token([tbitwiseOr, "|"]) --> "|", !. - -token([tspace , " "]) --> "\s", !. -token([ttab , "\t"]) --> "\t", !. -token([execTAB , "\\t"]) --> "\\t", !. -token([execNL, "\n"]) --> ( "\n"; "\r\n"), !. -token([tnl, "\\n"]) --> "\\n", !. - -token([tleftParen,"("]) --> "(", !. -token([trightParen, ")"]) --> ")", !. -token([tleftBrace, "{"]) --> "{", !. -token([trightBrace, "}"]) --> "}", !. -token([tleftSqParen, "["]) --> "[", !. -token([trightSqParen, "]" ]) --> "]", !. -token([tsemicolon, ";"]) --> ";", !. -token([ttwodots, ":"]) --> ":", !. -token([tquestion, "?"]) --> "?", !. - -token([tcomma, "," ]) --> ",", !. -token([tquot, "\'"]) --> "\'", !. -token([tdoubleQuot, "\""]) --> "\"", !. -token([tdot, "."]) --> ".", !. - -token([tidentifier, IA]) --> identifier(I), !, - {atom_chars(IA,I)}. -token([tnumber, I]) --> number(I), !. - -token([tunknown, IA]) --> allThatIsNotAToken(I), !, - {I \= [], atom_chars(IA, I)}. - - -%% Main predicate -tokens([Token | Tail]) --> token(Token), !, tokens(Tail). -tokens([]) --> !, [], !. - - -%% Helper predicates -identifier([C|Cs]) --> [C], {char_type(C, csymf)}, !, - identifierHelper(Cs). - -identifierHelper([C|Cs]) --> [C], {char_type(C, csym)}, !, - identifierHelper(Cs). -identifierHelper([C]) --> [C], {char_type(C, csym), \+ char_code('_', C)}, !. -identifierHelper([]) --> [], !. - -func([C|Cs]) --> [C], {char_type(C, csymf)}, !, - funkHelper(Cs). - -funkHelper([C|Cs]) --> [C], {char_type(C, csym)}, !, - funkHelper(Cs). -funkHelper([C]) --> [C], {char_code("(", C)}, !. - -stringy([C|Cs]) --> [C], {char_code('\"', C)}, !, - stringyHelper(Cs). - -stringyHelper([C|Cs]) --> [C], {char_type(C, csymf); char_code('.', C)}, - stringyHelper(Cs). -stringyHelper([C]) --> [C], {char_code('\"', C)}. -header([C|Cs]) --> [C], {char_code('<', C)}, !, - hederHelper(Cs). + <head> + <title>hello.c</title> + <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> + <style> + .keyword { + color: blue; + } -hederHelper([C|Cs]) --> [C], - {char_type(C, csymf); char_code('.', C); char_code('/', C); char_code('+', C)}, - hederHelper(Cs). -hederHelper([C]) --> [C], {char_code('>', C)}. + .number { + color: violet; + } -prFT([C|Cs]) --> [C], {char_code('%', C)}, !, - pr(Cs). + .identifier { + color: green; + } -pr([C]) --> [C], {char_type(C, csym)}. + .operator { + font-style: bold; + color: blue; + } -comment1([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('*', C2)}, !, - anything(Cs), - {append(_,[C3,C4],Cs), char_code('*', C3), char_code('/', C4)}. + .comment { + color: lime; + } -comment2([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('/', C2)}, !, - anytingButNL(Cs). + .string { + color: orange; + } -anything([C|Cs]) --> [C], anything(Cs). -anything([]) --> []. + .parenthese { + font-style: bold; + color: brown; + } -allThatIsNotAToken([C|Cs]) --> [C|Cs], \+ tokens(C), \+ tokens([C|Cs]), - allThatIsNotAToken(Cs). -allThatIsNotAToken([]) --> []. + .quotation { + font-style: bold; + color: black; + } -anytingButNL([C|Cs]) --> [C], - {\+ char_code('\r', C), \+ char_code('\n', C)}, anytingButNL(Cs). -anytingButNL([C1,C2|_]) --> [C1,C2], - {(char_code('\r', C1), char_code('\n', C2)); - (\+ char_code('\r', C1), char_code('\n', C2))}. + .unknown { + font-style: bold; + color: red; + } + .function { + color: purple; + } -%% Definitions of lists of different types of tokens -is_keyWord([H,_]):- member(H, [tauto, tbreak, tcase, tchar, tconst, tcontinue, - tdefault, tdo, tdouble, telse, tenum, textern, tfloat, tfor, tgoto, tif, - tint, tlong, tregister, treturn, tshort, tsigned, tsizeof, tstatic, tstruct, - tswitch, ttypedef, tunion, tunsigned, twhile, tvoid, tvolatile, tinclude]). + .collapsible { + background-color: PaleGreen; + cursor: pointer; + padding: 10px; + width: 100%; + border: none; + text-align: left; + outline: none; + font-size: 15px; + } -is_operator([H,_]):- member(H, [tmultiply, tdivide, tmod, tadd, tsubtract, tless, - tlessEqual, tgreater, tgreaterEqual, tequal, tnotequal, tnot, tassign, tlogicalOr, - tlogicalAnd, tbitwiseOr, tbitwiseAnd]). + .active, + .collapsible:hover { + background-color: PaleGoldenRod; + } -is_parenth([H,_]):- member(H, [tleftParen, trightParen, tleftBrace, trightBrace, - tleftSqParen, trightSqParen, tsemicolon, ttwodots]). + .content { + padding: 0 18px; + display: none; + overflow: hidden; + background-color: #f1f1f1; + } + </style> + </head> -is_quotation([H,_]):- member(H, [tquot, tdoubleQuot, tcomma, tdot, tquestion]). + <body> + <pre class=\"code\">'), + tohtmlfile(WFile, PrettyCode), + write(WFile, ' <script> + var coll = document.getElementsByClassName(\"collapsible\"); + var i; -is_unknown([H,_]):- member(H, [tunknown]). + for (i = 0; i < coll.length; i++) { + coll[i].addEventListener(\"click\", function() { + this.classList.toggle(\"active\"); + var content = this.nextElementSibling; + if (content.style.display === \"block\") { + content.style.display = \"none\"; + } else { + content.style.display = \"block\"; + } + }); + } + </script> + </pre> + </body> -is_number([H,_]):- member(H, [tnumber, ttypesPrintF]). - -is_function([[H,_],_]):- member(H, [tfunction]). - -is_identifier([H,_]):- member(H, [tidentifier, tnl, ttab]). - -is_blank([H,_]):- member(H, [execTAB, tspace, execNL]). - -is_comment1([H,_]):-member(H, [tcomment1]). - -is_comment2([[H,_],_]):-member(H, [tcomment2]). - -is_string([H,_]):- member(H, [tstring]). - -is_header([H,_]):- member(H, [theader]). - - -%% Writing to html file the stream of tokens -tohtmlfile(Stream, []):- !, write(Stream, '</pre></body></html>'), !. - -tohtmlfile(Stream, [H|T]) :- is_identifier(H), H = [_,Lexem], - write(Stream, '<span class=\"identifier\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_string(H), H = [_,Lexem], - write(Stream, '<span class=\"string\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_function(H), H = [[_, Lexem1],[_,Lexem2]], - write(Stream, '<span class=\"function\">'), - write(Stream, Lexem1), - write(Stream,'</span>'), - write(Stream, '<span class=\"parenthese\">'), - write(Stream, Lexem2), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_header(H), H = [_,Lexem], - write(Stream, '<span class=\"string\"><'), - write(Stream, Lexem), - write(Stream,'></span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_keyWord(H), H = [_,Lexem], - write(Stream, '<span class=\"keyword\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_number(H), H = [_,Lexem], - write(Stream, '<span class=\"number\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_operator(H), H = [_,Lexem], - write(Stream, '<span class=\"operator\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_parenth(H), H = [_,Lexem], - write(Stream, '<span class=\"parenthese\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- (is_quotation(H); is_unknown(H)), H = [_,Lexem], - write(Stream, '<span class=\"quotation\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_comment1(H), H = [_,Lexem], - write(Stream, '<span class=\"comment\">'), - write(Stream, Lexem), - write(Stream,'</span>'), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_comment2(H), H = [[_, Lexem1],[_,Lexem2]], - write(Stream, '<span class=\"comment\">'), - write(Stream, Lexem1), - write(Stream,'</span>'), - write(Stream, Lexem2), - !, - tohtmlfile(Stream, T). - -tohtmlfile(Stream, [H|T]) :- is_blank(H), H = [_,Lexem], - write(Stream, Lexem), - !, - tohtmlfile(Stream, T). + </html>'), + close(WFile). diff --git a/homework/81281/c2html/c2htmlrun.sh b/homework/81281/c2html/c2htmlrun.sh new file mode 100644 index 0000000..dec4b69 --- /dev/null +++ b/homework/81281/c2html/c2htmlrun.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +echo -n "Please enter the name of the C file you wish to highlight: " +read fileName +echo -ne "Please enter:\n\t0 for highlighting\n\t1 highlighting and prettification\n\t2 for highlighting and prettification and control structure collapse: " +read number +if ! [[ $fileName == *.c\n ]]; then + base=`pwd`"/c2html.pl" + PL=swipl + exec $PL -q -f "$base" "$fileName" "$number" +else + echo "Not a correct C filename!" +fi diff --git a/homework/81281/c2html/controlStructures.pl b/homework/81281/c2html/controlStructures.pl new file mode 100644 index 0000000..026c216 --- /dev/null +++ b/homework/81281/c2html/controlStructures.pl @@ -0,0 +1,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). diff --git a/homework/81281/c2html/essentials.pl b/homework/81281/c2html/essentials.pl new file mode 100644 index 0000000..0a792fa --- /dev/null +++ b/homework/81281/c2html/essentials.pl @@ -0,0 +1,14 @@ +% From [[[a, b], [a, c]], [b, c]] -> [[a, b], [a, c], [b, c]] +% From [[[a, b], [a, c, [a]]] -> [[a, b], [a, c, [a]], [b, c]]. +flattenMine([], []):- !. +flattenMine([H|T], R):- flattenElem(H, RH), flattenMine(T, RT), append(RH, RT, R), !. + +flattenElem([H|T], [[H|T]]):- \+ is_list(H), !. +flattenElem([H|T], [H|T]):- is_list(H), H = [H1|_], \+ is_list(H1), !. +flattenElem([H|T], [[H|T]]):- is_list(H), H = [H1|_], is_list(H1), !. + +getPrefixTabsLength(L, N):- append(A, B, L), + \+((member(AA, A), AA \= [execTAB, "\s\s\s\s"])), \+((member(BB, B), BB == [execTAB, "\s\s\s\s"])), length(A, N). + +removeNTabs(L, 0, L):- !. +removeNTabs([_|T], N, R):- N > 0, N1 is N - 1, !, removeNTabs(T, N1, R). diff --git a/homework/81281/c2html/exampleCodeAndTestPage/initialTest.c b/homework/81281/c2html/exampleCodeAndTestPage/initialTest.c new file mode 100644 index 0000000..7f1b4cd --- /dev/null +++ b/homework/81281/c2html/exampleCodeAndTestPage/initialTest.c @@ -0,0 +1,99 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +int N = 0; +int a[750001] = {0}; +/*Problem Secrect DAA + */ +//// mauuu + +int matrix[3][3] = {{0, 0, 0}, {0, 1, 0}, {0, 1, 1}}; +int arr[3] = {0, 0, 0}; +char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}}; +void swap(int* i, int* j) +{ + int t = *i; + *i = *j; + (*i)->go(); + *j = t; +} +int partition(int l, int r) +{ + int randIdx = l + rand() % (r - l); + + int pivot = a[randIdx]; + + swap(&a[l], &a[randIdx]); + int ind = l; + for (int i = l + 1; i < r; i++) + { + if (a[i] <= pivot) + { + ind++; + swap(&a[i], &a[ind]); + } + } + + swap(&a[l], &a[ind]); + switch (allala) + { + case 0: + o = 9; + break; + default: + break; + } + return ind; +} +void quickSort(int l, int r) +{ + if (l < r) + { + int idx = partition(l, r); + quickSort(l, idx); + quickSort(idx + 1, r); + } +} +int binarySearch(int k) +{ + + int middle, left = 0, right = N - 1; + if (k < a[left]) + return (-1); + if (k == a[left]) + return (left + 1); + if (k > a[right]) + return (-1); + + while (right - left > 1) + { + middle = (left + right) / 2; + if (k <= a[middle]) + right = middle; + else + left = middle; + } + int j = 4 * 5; + int k = 6 * j; + return ((right <= N - 1 && a[right] == k) ? (right + 1) : (-1)); +} + +int main() +{ + srand(time(0)); + scanf("%d", &N); + for (int i = 0; i < N; ++i) + { + scanf("%d ", &a[i]); + } + quickSort(0, N); + + int tmp = 0; + scanf("%d", &tmp); + while (tmp != 0) + { + printf("%d\n", binarySearch(tmp)); + scanf("%d", &tmp); + } + return 0; +} diff --git a/homework/81281/c2html/exampleCodeAndTestPage/messyCode.c b/homework/81281/c2html/exampleCodeAndTestPage/messyCode.c new file mode 100644 index 0000000..4ac99b4 --- /dev/null +++ b/homework/81281/c2html/exampleCodeAndTestPage/messyCode.c @@ -0,0 +1,139 @@ +#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+int N = 0;
+int a[750001] = {0};
+struct Books
+{
+ char title[50];
+ char author[50];
+ char subject[100];
+ int book_id;
+ void dummy()
+ {
+ printf("Hello\n");
+ for (size_t i = 0; i < count; i++)
+ {
+ /*yes*/
+ }
+ }
+} book;
+
+typedef struct temp
+{
+ int a;
+
+} classTemp;
+
+void classTemp_GetData(classTemp* pThis)
+{
+ printf("Enter value of a : ");
+ scanf("%d", &(pThis->a));
+}
+
+classTemp T;
+
+struct point* biggest_point(size_t size,
+ struct point* points,
+ struct point* (*point_compare)(struct point* a,
+ struct point* b))
+{
+ int i;
+ struct point* biggest = NULL;
+
+ for (i = 0; i < size; i++)
+ {
+ biggest = point_compare(biggest, points + i);
+ }
+ return biggest;
+}
+
+int matrix[3][3] = {{0, 0, 0}, {0, 1, 0}, {0, 1, 1}};
+int arr[3] = {0, 0, 0};
+char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}};
+/*Problem Secrect DAA
+/// lalalallala
+*/
+//// mauuu
+int a, b;
+void swap(int* i, int* j)
+{
+int t = *i;
+*i = *j;(*i)->go();
+*j = t;
+do
+{
+printf("mau\n");
+ } while (i > 1);
+}
+int partition(int l, int r)
+{
+int randIdx = l + rand() % (r - l);
+int pivot = a[randIdx];
+
+swap(&a[l], &a[randIdx]);
+int ind = l;
+for (int i = l + 1; i < r; i++)
+{
+if (a[i] <= pivot)
+{
+ind++;
+swap(&a[i], &a[ind]);
+switch (allala)
+ {
+ case 0:
+ o = 9;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ swap(&a[l], &a[ind]);
+ return ind;
+}
+void quickSort(int l, int r)
+{
+ if (l < r)
+ {
+ int idx = partition(l, r);
+ quickSort(l, idx);
+ quickSort(idx + 1, r);
+ }
+}
+int binarySearch(int k)
+{
+ int middle, left = 0, right = N - 1;
+ if (k < a[left])
+ return (-1);
+ if (k == a[left])
+ return (left + 1);
+ if (k > a[right])
+ return (-1); while (right - left > 1)
+ {
+ middle = (left + right) / 2;
+ if (k <= a[middle])
+right = middle;
+ else
+ left = middle;
+ }
+ return ((right <= N - 1 && a[right] == k) ? (right + 1) : (-1));
+}
+
+int main()
+{
+ srand(time(0));
+ scanf("%d", &N);
+ for (int i = 0; i < N; ++i)
+ {
+ scanf("%d ", &a[i]);
+ }
+ quickSort(0, N);
+ int tmp = 0; scanf("%d", &tmp);while (tmp != 0)
+ {
+printf("%d\n", binarySearch(tmp));
+ scanf("%d", &tmp);
+ }
+ return 0;
+}
diff --git a/homework/81281/c2html/exampleCodeAndTestPage/page.html b/homework/81281/c2html/exampleCodeAndTestPage/page.html new file mode 100644 index 0000000..15ebd7a --- /dev/null +++ b/homework/81281/c2html/exampleCodeAndTestPage/page.html @@ -0,0 +1,266 @@ +<!doctype>
+<html>
+
+ <head>
+ <title>hello.c</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <style>
+ .keyword {
+ color: blue;
+ }
+
+ .number {
+ color: violet;
+ }
+
+ .identifier {
+ color: green;
+ }
+
+ .operator {
+ font-style: bold;
+ color: blue;
+ }
+
+ .comment {
+ color: lime;
+ }
+
+ .string {
+ color: orange;
+ }
+
+ .parenthese {
+ font-style: bold;
+ color: brown;
+ }
+
+ .quotation {
+ font-style: bold;
+ color: black;
+ }
+
+ .unknown {
+ font-style: bold;
+ color: red;
+ }
+
+ .function {
+ color: purple;
+ }
+
+ .collapsible {
+ background-color: PaleGreen;
+ cursor: pointer;
+ padding: 10px;
+ width: 100%;
+ border: none;
+ text-align: left;
+ outline: none;
+ font-size: 15px;
+ }
+
+ .active,
+ .collapsible:hover {
+ background-color: PaleGoldenRod;
+ }
+
+ .content {
+ padding: 0 18px;
+ display: none;
+ overflow: hidden;
+ background-color: #f1f1f1;
+ }
+ </style>
+ </head>
+
+ <body>
+ <pre class="code"><span class="keyword">#include</span> <span class="string"><stdio.h></span>
+<span class="keyword">#include</span> <span class="string"><stdlib.h></span>
+<span class="keyword">#include</span> <span class="string"><time.h></span>
+<span class="keyword">int</span> <span class="identifier">N</span> <span class="operator">=</span> <span class="number">0</span><span class="parenthese">;</span>
+<span class="keyword">int</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="number">750001</span><span class="parenthese">]</span> <span class="operator">=</span> <span class="parenthese">{</span><span class="number">0</span><span class="parenthese">}</span><span class="parenthese">;</span>
+<button class="collapsible"><span class="keyword">struct</span> <span class="identifier">Books</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="keyword">char</span> <span class="identifier">title</span><span class="parenthese">[</span><span class="number">50</span><span class="parenthese">]</span><span class="parenthese">;</span>
+ <span class="keyword">char</span> <span class="identifier">author</span><span class="parenthese">[</span><span class="number">50</span><span class="parenthese">]</span><span class="parenthese">;</span>
+ <span class="keyword">char</span> <span class="identifier">subject</span><span class="parenthese">[</span><span class="number">100</span><span class="parenthese">]</span><span class="parenthese">;</span>
+ <span class="keyword">int</span> <span class="identifier">book_id</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">void</span> <span class="function">dummy</span><span class="parenthese">(</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="function">printf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="identifier">Hello</span><span class="identifier">\n</span><span class="quotation">"</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">for</span> <span class="parenthese">(</span><span class="identifier">size_t</span> <span class="identifier">i</span> <span class="operator">=</span> <span class="number">0</span><span class="parenthese">;</span> <span class="identifier">i</span> <span class="operator"><</span> <span class="identifier">count</span><span class="parenthese">;</span> <span class="identifier">i</span><span class="operator">++</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="comment">/*yes*/</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="parenthese">}</span>
+
+</div><span class="parenthese">}</span> <span class="identifier">book</span><span class="parenthese">;</span>
+
+</div><span class="keyword">typedef</span> <span class="keyword">struct</span> <span class="identifier">temp</span>
+<span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">a</span><span class="parenthese">;</span>
+<span class="parenthese">}</span> <span class="identifier">classTemp</span><span class="parenthese">;</span>
+
+<button class="collapsible"><span class="keyword">void</span> <span class="function">classTemp_GetData</span><span class="parenthese">(</span><span class="identifier">classTemp</span> <span class="operator">*</span> <span class="identifier">pThis</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="function">printf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="identifier">Enter</span><span class="identifier">value</span><span class="identifier">of</span><span class="identifier">a</span><span class="parenthese">:</span><span class="quotation">"</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="function">scanf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="quotation">"</span><span class="quotation">,</span> <span class="operator">&</span> <span class="parenthese">(</span><span class="identifier">pThis</span><span class="operator">-></span><span class="identifier">a</span><span class="parenthese">)</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<span class="parenthese">}</span>
+
+</div><span class="identifier">classTemp</span> <span class="identifier">T</span><span class="parenthese">;</span>
+<button class="collapsible"><span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="function">biggest_point</span><span class="parenthese">(</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="quotation">,</span> <span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="identifier">points</span><span class="quotation">,</span> <span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="parenthese">(</span><span class="operator">*</span><span class="identifier">point_compare</span><span class="parenthese">)</span> <span class="parenthese">(</span><span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="identifier">a</span><span class="quotation">,</span> <span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="identifier">b</span><span class="parenthese">)</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">i</span><span class="parenthese">;</span>
+ <span class="keyword">struct</span> <span class="identifier">point</span> <span class="operator">*</span> <span class="identifier">biggest</span> <span class="operator">=</span> <span class="identifier">NULL</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">for</span> <span class="parenthese">(</span><span class="identifier">i</span> <span class="operator">=</span> <span class="number">0</span><span class="parenthese">;</span> <span class="identifier">i</span> <span class="operator"><</span> <span class="identifier">size</span><span class="parenthese">;</span> <span class="identifier">i</span><span class="operator">++</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="identifier">biggest</span> <span class="operator">=</span> <span class="function">point_compare</span><span class="parenthese">(</span><span class="identifier">biggest</span><span class="quotation">,</span> <span class="identifier">points</span> <span class="operator">+</span> <span class="identifier">i</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="keyword">return</span> <span class="identifier">biggest</span><span class="parenthese">;</span>
+<span class="parenthese">}</span>
+
+</div><span class="keyword">int</span> <span class="identifier">matrix</span><span class="parenthese">[</span><span class="number">3</span><span class="parenthese">]</span><span class="parenthese">[</span><span class="number">3</span><span class="parenthese">]</span> <span class="operator">=</span> <span class="parenthese">{</span><span class="parenthese">{</span><span class="number">0</span><span class="quotation">,</span> <span class="number">0</span><span class="quotation">,</span> <span class="number">0</span><span class="parenthese">}</span><span class="quotation">,</span> <span class="parenthese">{</span><span class="number">0</span><span class="quotation">,</span> <span class="number">1</span><span class="quotation">,</span> <span class="number">0</span><span class="parenthese">}</span><span class="quotation">,</span> <span class="parenthese">{</span><span class="number">0</span><span class="quotation">,</span> <span class="number">1</span><span class="quotation">,</span> <span class="number">1</span><span class="parenthese">}</span><span class="parenthese">}</span><span class="parenthese">;</span>
+<span class="keyword">int</span> <span class="identifier">arr</span><span class="parenthese">[</span><span class="number">3</span><span class="parenthese">]</span> <span class="operator">=</span> <span class="parenthese">{</span><span class="number">0</span><span class="quotation">,</span> <span class="number">0</span><span class="quotation">,</span> <span class="number">0</span><span class="parenthese">}</span><span class="parenthese">;</span>
+<span class="keyword">char</span> <span class="identifier">matrix</span><span class="parenthese">[</span><span class="number">3</span><span class="parenthese">]</span><span class="parenthese">[</span><span class="number">3</span><span class="parenthese">]</span> <span class="operator">=</span> <span class="parenthese">{</span><span class="parenthese">{</span><span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="parenthese">}</span><span class="quotation">,</span> <span class="parenthese">{</span><span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">1</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="parenthese">}</span><span class="quotation">,</span> <span class="parenthese">{</span><span class="quotation">'</span><span class="number">0</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">1</span><span class="quotation">'</span><span class="quotation">,</span> <span class="quotation">'</span><span class="number">1</span><span class="quotation">'</span><span class="parenthese">}</span><span class="parenthese">}</span><span class="parenthese">;</span>
+<span class="comment">/*Problem Secrect DAA
+/// lalalallala
+*/</span>
+<span class="comment">//// mauuu</span>
+<span class="keyword">int</span> <span class="identifier">a</span><span class="quotation">,</span> <span class="identifier">b</span><span class="parenthese">;</span>
+<button class="collapsible"><span class="keyword">void</span> <span class="function">swap</span><span class="parenthese">(</span><span class="keyword">int</span> <span class="operator">*</span><span class="identifier">i</span><span class="quotation">,</span> <span class="keyword">int</span> <span class="operator">*</span><span class="identifier">j</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">t</span> <span class="operator">=</span> <span class="operator">*</span><span class="identifier">i</span><span class="parenthese">;</span>
+ <span class="operator">*</span><span class="identifier">i</span><span class="operator">=</span> <span class="operator">*</span><span class="identifier">j</span><span class="parenthese">;</span>
+ <span class="parenthese">(</span><span class="operator">*</span><span class="identifier">i</span><span class="parenthese">)</span><span class="operator">-></span><span class="function">go</span><span class="parenthese">(</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="operator">*</span><span class="identifier">j</span><span class="operator">=</span> <span class="identifier">t</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">do</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="function">printf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="identifier">mau</span><span class="identifier">\n</span><span class="quotation">"</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span> <span class="keyword">while</span> <span class="parenthese">(</span><span class="identifier">i</span> <span class="operator">></span> <span class="number">1</span><span class="parenthese">)</span><span class="parenthese">;</span>
+
+</div><span class="parenthese">}</span>
+
+</div><button class="collapsible"><span class="keyword">int</span> <span class="function">partition</span><span class="parenthese">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="quotation">,</span> <span class="keyword">int</span> <span class="identifier">r</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">randIdx</span> <span class="operator">=</span> <span class="identifier">l</span> <span class="operator">+</span> <span class="function">rand</span><span class="parenthese">(</span><span class="parenthese">)</span> <span class="operator">%</span> <span class="parenthese">(</span><span class="identifier">r</span> <span class="operator">-</span> <span class="identifier">l</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">int</span> <span class="identifier">pivot</span> <span class="operator">=</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">randIdx</span><span class="parenthese">]</span><span class="parenthese">;</span>
+ <span class="function">swap</span><span class="parenthese">(</span><span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">l</span><span class="parenthese">]</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">randIdx</span><span class="parenthese">]</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">int</span> <span class="identifier">ind</span> <span class="operator">=</span> <span class="identifier">l</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">for</span> <span class="parenthese">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="operator">=</span> <span class="identifier">l</span> <span class="operator">+</span> <span class="number">1</span><span class="parenthese">;</span> <span class="identifier">i</span> <span class="operator"><</span> <span class="identifier">r</span><span class="parenthese">;</span> <span class="identifier">i</span><span class="operator">++</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+<button class="collapsible"> <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">i</span><span class="parenthese">]</span> <span class="operator"><=</span> <span class="identifier">pivot</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="identifier">ind</span><span class="operator">++</span><span class="parenthese">;</span>
+ <span class="function">swap</span><span class="parenthese">(</span><span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">i</span><span class="parenthese">]</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">ind</span><span class="parenthese">]</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">switch</span> <span class="parenthese">(</span><span class="identifier">allala</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="keyword">case</span> <span class="number">0</span> <span class="parenthese">:</span>
+ <span class="identifier">o</span> <span class="operator">=</span> <span class="number">9</span><span class="parenthese">;</span>
+ <span class="keyword">break</span><span class="parenthese">;</span>
+ <span class="keyword">default</span> <span class="parenthese">:</span>
+ <span class="keyword">break</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="parenthese">}</span>
+
+</div> <span class="parenthese">}</span>
+
+</div> <span class="function">swap</span><span class="parenthese">(</span><span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">l</span><span class="parenthese">]</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">ind</span><span class="parenthese">]</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">return</span> <span class="identifier">ind</span><span class="parenthese">;</span>
+<span class="parenthese">}</span>
+
+</div><button class="collapsible"><span class="keyword">void</span> <span class="function">quickSort</span><span class="parenthese">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="quotation">,</span> <span class="keyword">int</span> <span class="identifier">r</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+<button class="collapsible"> <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">l</span> <span class="operator"><</span> <span class="identifier">r</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">idx</span> <span class="operator">=</span> <span class="function">partition</span><span class="parenthese">(</span><span class="identifier">l</span><span class="quotation">,</span> <span class="identifier">r</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="function">quickSort</span><span class="parenthese">(</span><span class="identifier">l</span><span class="quotation">,</span> <span class="identifier">idx</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="function">quickSort</span><span class="parenthese">(</span><span class="identifier">idx</span> <span class="operator">+</span> <span class="number">1</span><span class="quotation">,</span> <span class="identifier">r</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div><span class="parenthese">}</span>
+
+</div><button class="collapsible"><span class="keyword">int</span> <span class="function">binarySearch</span><span class="parenthese">(</span><span class="keyword">int</span> <span class="identifier">k</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="keyword">int</span> <span class="identifier">middle</span><span class="quotation">,</span> <span class="identifier">left</span> <span class="operator">=</span> <span class="number">0</span><span class="quotation">,</span> <span class="identifier">right</span> <span class="operator">=</span> <span class="identifier">N</span> <span class="operator">-</span> <span class="number">1</span><span class="parenthese">;</span>
+ <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">k</span> <span class="operator"><</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">left</span><span class="parenthese">]</span><span class="parenthese">)</span>
+ <span class="keyword">return</span> <span class="parenthese">(</span><span class="number">-1</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">k</span> <span class="operator">==</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">left</span><span class="parenthese">]</span><span class="parenthese">)</span>
+ <span class="keyword">return</span> <span class="parenthese">(</span><span class="identifier">left</span> <span class="operator">+</span> <span class="number">1</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">k</span> <span class="operator">></span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">right</span><span class="parenthese">]</span><span class="parenthese">)</span>
+ <span class="keyword">return</span> <span class="parenthese">(</span><span class="number">-1</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">while</span> <span class="parenthese">(</span><span class="identifier">right</span> <span class="operator">-</span> <span class="identifier">left</span> <span class="operator">></span> <span class="number">1</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="identifier">middle</span> <span class="operator">=</span> <span class="parenthese">(</span><span class="identifier">left</span> <span class="operator">+</span> <span class="identifier">right</span><span class="parenthese">)</span> <span class="operator">/</span> <span class="number">2</span><span class="parenthese">;</span>
+ <span class="keyword">if</span> <span class="parenthese">(</span><span class="identifier">k</span> <span class="operator"><=</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">middle</span><span class="parenthese">]</span><span class="parenthese">)</span>
+ <span class="identifier">right</span> <span class="operator">=</span> <span class="identifier">middle</span><span class="parenthese">;</span>
+ <span class="keyword">else</span>
+ <span class="identifier">left</span> <span class="operator">=</span> <span class="identifier">middle</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="keyword">return</span> <span class="parenthese">(</span><span class="parenthese">(</span><span class="identifier">right</span> <span class="operator"><=</span> <span class="identifier">N</span> <span class="operator">-</span> <span class="number">1</span> <span class="operator">&&</span> <span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">right</span><span class="parenthese">]</span> <span class="operator">==</span> <span class="identifier">k</span><span class="parenthese">)</span> <span class="quotation">?</span> <span class="parenthese">(</span><span class="identifier">right</span> <span class="operator">+</span> <span class="number">1</span><span class="parenthese">)</span> <span class="parenthese">:</span> <span class="parenthese">(</span><span class="number">-1</span><span class="parenthese">)</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<span class="parenthese">}</span>
+
+</div><button class="collapsible"><span class="keyword">int</span> <span class="function">main</span><span class="parenthese">(</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"><span class="parenthese">{</span>
+ <span class="function">srand</span><span class="parenthese">(</span><span class="function">time</span><span class="parenthese">(</span><span class="number">0</span><span class="parenthese">)</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="function">scanf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="quotation">"</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">N</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">for</span> <span class="parenthese">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="operator">=</span> <span class="number">0</span><span class="parenthese">;</span> <span class="identifier">i</span> <span class="operator"><</span> <span class="identifier">N</span><span class="parenthese">;</span> <span class="operator">++</span><span class="identifier">i</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="function">scanf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="quotation">"</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">a</span><span class="parenthese">[</span><span class="identifier">i</span><span class="parenthese">]</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="function">quickSort</span><span class="parenthese">(</span><span class="number">0</span><span class="quotation">,</span> <span class="identifier">N</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="keyword">int</span> <span class="identifier">tmp</span> <span class="operator">=</span> <span class="number">0</span><span class="parenthese">;</span>
+ <span class="function">scanf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="quotation">"</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">tmp</span><span class="parenthese">)</span><span class="parenthese">;</span>
+<button class="collapsible"> <span class="keyword">while</span> <span class="parenthese">(</span><span class="identifier">tmp</span> <span class="operator">!=</span> <span class="number">0</span><span class="parenthese">)</span>
+</span></button>
+<div class="content"> <span class="parenthese">{</span>
+ <span class="function">printf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="identifier">\n</span><span class="quotation">"</span><span class="quotation">,</span> <span class="function">binarySearch</span><span class="parenthese">(</span><span class="identifier">tmp</span><span class="parenthese">)</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="function">scanf</span><span class="parenthese">(</span><span class="quotation">"</span><span class="number">%d</span><span class="quotation">"</span><span class="quotation">,</span> <span class="operator">&</span><span class="identifier">tmp</span><span class="parenthese">)</span><span class="parenthese">;</span>
+ <span class="parenthese">}</span>
+
+</div> <span class="keyword">return</span> <span class="number">0</span><span class="parenthese">;</span>
+<span class="parenthese">}</span>
+
+</div> <script>
+ var coll = document.getElementsByClassName("collapsible");
+ var i;
+
+ for (i = 0; i < coll.length; i++) {
+ coll[i].addEventListener("click", function() {
+ this.classList.toggle("active");
+ var content = this.nextElementSibling;
+ if (content.style.display === "block") {
+ content.style.display = "none";
+ } else {
+ content.style.display = "block";
+ }
+ });
+ }
+ </script>
+ </pre>
+ </body>
+
+</html>
diff --git a/homework/81281/c2html/prettify.pl b/homework/81281/c2html/prettify.pl new file mode 100644 index 0000000..70626cf --- /dev/null +++ b/homework/81281/c2html/prettify.pl @@ -0,0 +1,384 @@ +prettify(StreamTokens, PlainWithNLnSnT):- + removeAllWhites(StreamTokens, Plain), + % write(Plain),nl,write("Plain ------------"), nl, + addNewLines(Plain, PlainWithNL), + % write(PlainWithNL),nl,write("PlainWithNL ------------"), nl, + addSpaces(PlainWithNL, PlainWithNLnS), + % write(PlainWithNLnS),nl,write("PlainWithNLnS ------------"), nl, + addTabs(PlainWithNLnS, PlainWithNLnSnT). + % write(PlainWithNLnSnT),nl,write("PlainWithNLnSnT ------------"), nl. + % write(PrettyStream). + % write(PrettyStream), nl. + +% Removes all white characters +removeAllWhites([], []):- !. +removeAllWhites([[H,_]|T], R):- member(H, [execTAB, execSpace, execNL, tunknown]), + removeAllWhites(T, R), !. +removeAllWhites([[H, B]|T], [[H, B]|R]):- \+ member(H, [execTAB, execSpace, execNL, tunknown]), + removeAllWhites(T, R), !. + +% Pack Till ; for cases like char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}}; +packTillsemiColon([[tsemicolon, ";"]|T], Buff, Buff, [[tsemicolon, ";"]|T]). +packTillsemiColon([H|T], Buff, Res, Rest):- + append(Buff, [H], NewBuff), !, + packTillsemiColon(T, NewBuff, Res, Rest). + +% Pack Till : and include them in result too. For case smt: where smt if more than 1 character +packTillTwoDots([[ttwodots, ":"]|T], Buff, NBuff, T):- append(Buff, [[ttwodots, ":"]], NBuff). +packTillTwoDots([H|T], Buff, Res, Rest):- + append(Buff, [H], NewBuff), !, + packTillTwoDots(T, NewBuff, Res, Rest). + +% Pack Till ; for cases like char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}}; +packTillsemiColonOnlyIdentifiers([[tsemicolon, ";"]|T], Buff, Buff, [[tsemicolon, ";"]|T]). +packTillsemiColonOnlyIdentifiers([H1, H2|T], Buff, Res, Rest):- + H1 = [H11|_], member(H11, [tcomma]), + H2 = [H21|_], member(H21, [tidentifier]), + append(Buff, [H1, H2], NewBuff), !, + packTillsemiColonOnlyIdentifiers(T, NewBuff, Res, Rest). + +% Pack for while, if, for and function arguments +packTillRightParen(Rest, Buff, Res, Rest, 0):- + append(Buff, [[execNL, '\n']], Res). +packTillRightParen([H|T], Buff, Res, Rest, N):- + N =\= 0, H = [H1|_], H1 == tleftParen, + append(Buff, [H], NewBuff), N1 is N + 1, + packTillRightParen(T, NewBuff, Res, Rest, N1). +packTillRightParen([H|T], Buff, Res, Rest, N):- + N =\= 0, H = [H1|_], H1 == trightParen, + append(Buff, [H], NewBuff), N1 is N - 1, + packTillRightParen(T, NewBuff, Res, Rest, N1). +packTillRightParen([H|T], Buff, Res, Rest, N):- + N =\= 0, H = [H1|_], \+ member(H1, [trightParen, tleftParen]), + append(Buff, [H], NewBuff), + packTillRightParen(T, NewBuff, Res, Rest, N). + + +% Base +addNewLines([], [], []):- !. +% if ; // /**/, do +addNewLines([H|T], Buff, [NewBuff|R]):- + H = [H1|_], member(H1, [tsemicolon, tcomment1, tcomment2, tdo]), + append(Buff, [H, [execNL, "\n"]], NewBuff), !, + addNewLines(T, [], R). +% if else and no following if +addNewLines([H1, H2|T], Buff, [NewBuff|R]):- + H1 = [H11|_], member(H11, [telse]), + H2 = [H21|_], \+ member(H21, [tif]), + append(Buff, [H1, [execNL, "\n"]], NewBuff), !, + addNewLines([H2|T], [], R). +% if } while(..) +addNewLines([H1, H2|T], Buff, R):- + H1 = [H11|_], member(H11, [trightBrace]), + H2 = [H21|_], member(H21, [twhile]), + packTillsemiColon(T, [], Res, Rest), + append(Buff, [H1, H2], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% % if typedef ... +% addNewLines([H1|T], Buff, R):- +% H1 = [H11|_], member(H11, [ttypedef]), +% packTillsemiColon(T, [], Res, Rest), +% append(Buff, [H1], NewBuf), +% append(NewBuf, Res, NewBuff), !, +% addNewLines(Rest, NewBuff, R). +% if struct +addNewLines([H1, H2, H3|T], Buff, [NewBuff|R]):- + H1 = [H11|_], member(H11, [tstruct]), + H2 = [H21|_], member(H21, [tidentifier]), + H3 = [H31|_], member(H31, [tleftBrace]), + append(Buff, [H1, H2, [execNL, "\n"]], NewBuff), !, + addNewLines([H3|T], [], R). +% if struct{ } id; +addNewLines([H1, H2|T], Buff, R):- + H1 = [H11|_], member(H11, [trightBrace]), + H2 = [H21|_], member(H21, [tidentifier]), + packTillsemiColonOnlyIdentifiers(T, [], Res, Rest), + append(Buff, [H1, H2], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% if struct{ }; +addNewLines([H1, H2|T], Buff, R):- + H1 = [H11|_], member(H11, [trightBrace]), + H2 = [H21|_], member(H21, [tsemicolon]), + append(Buff, [H1, H2, [execNL, "\n"]], NewBuff), !, + addNewLines(T, NewBuff, R). +% if char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}}; +addNewLines([H1, H2, H3|T], Buff, R):- + H1 = [H11|_], member(H11, [tchar, tint, tdouble, tidentifier, tshort, tsigned, tunsigned, tenum, tfloat]), + H2 = [H21|_], member(H21, [tidentifier]), + H3 = [H31|_], member(H31, [tleftSqParen]), + packTillsemiColon(T, [], Res, Rest), + append(Buff, [H1, H2, H3], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% if const char matrix[3][3] = {{'0', '0', '0'}, {'0', '1', '0'}, {'0', '1', '1'}}; +addNewLines([H1, H2, H3, H4|T], Buff, R):- + H1 = [H11|_], member(H11, [tconst]), + H2 = [H21|_], member(H21, [tchar, tint, tdouble, tidentifier, tshort, tsigned, tunsigned, tenum, tfloat]), + H3 = [H31|_], member(H31, [tidentifier]), + H4 = [H41|_], member(H41, [tleftSqParen]), + packTillsemiColon(T, [], Res, Rest), + append(Buff, [H1, H2, H3, H4], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% if const tenum {}; +addNewLines([H1, H2, H3|T], Buff, R):- + H1 = [H11|_], member(H11, [tconst]), + H2 = [H21|_], member(H21, [tenum]), + H3 = [H31|_], member(H31, [tidentifier]), + packTillsemiColon(T, [], Res, Rest), + append(Buff, [H1, H2, H3], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% if const tenum {}; +addNewLines([H1, H2|T], Buff, R):- + H1 = [H11|_], member(H11, [tenum]), + H2 = [H21|_], member(H21, [tidentifier]), + packTillsemiColon(T, [], Res, Rest), + append(Buff, [H1, H2], NewBuf), + append(NewBuf, Res, NewBuff), !, + addNewLines(Rest, NewBuff, R). +% if case smt: +addNewLines([H|T], Buff, [NewBuff|R]):- + H = [H1|_], member(H1, [tcase]), + packTillTwoDots([H|T], [], Res, Rest), + append(Buff, Res, NewBuf), + append(NewBuf, [[execNL, "\n"]], NewBuff), !, + addNewLines(Rest, [], R). +% if default : +addNewLines([H|T], Buff, [NewBuff|R]):- + H = [H1|_], member(H1, [tdefault]), + T = [[ttwodots, ":"]|T1], + append(Buff, [H, [ttwodots, ":"], [execNL, "\n"]], NewBuff), !, + addNewLines(T1, [], R). +% if }EOF +addNewLines([H|[]], Buff, [NewBuff|R]):- + H = [H1|_], member(H1, [trightBrace]), + append(Buff, [H, [execNL, "\n"]], NewBuff), !, + addNewLines([], [], R). +% if {0,{;,{,,}0,};}, +addNewLines([H1, H2|T], Buff, R):- + H1 = [H11|_], member(H11, [tleftBrace, trightBrace]), + H2 = [H21|_], member(H21, [tnumber, tsemicolon, tcomma]), + append(Buff, [H1], NewBuff), !, + addNewLines([H2|T], NewBuff, R). +% if not {0,{;,{,,}0,};}, +addNewLines([H1, H2|T], Buff, [NewBuff|R]):- + H1 = [H11|_], member(H11, [tleftBrace, trightBrace]), + H2 = [H21|_], \+ member(H21, [tnumber, tsemicolon, tcomma]), + append(Buff, [H1, [execNL, "\n"]], NewBuff), !, + addNewLines([H2|T], [], R). +% if function, while, for, if +addNewLines([H1, H2|T], Buff, [NewBuff|R]):- + H1 = [H11|_], member(H11, [tif, twhile, tfor, tswitch]), + packTillRightParen(T, [], MyPart, Rest, 1), + append(Buff, [H1, H2], NewB1), + append(NewB1, MyPart, NewBuff), !, + addNewLines(Rest, [], R). +% if function void m(){...} +addNewLines([H, H2|T], Buff, [NewBuff|R]):- + H = [H1|_], member(H1, [tfunction]), + packTillRightParen(T, [], MyPart, Rest, 1), + Rest \= [], Rest= [[M1|_]|_], M1 == tleftBrace, + append(Buff, [H, H2], NewB1), + append(NewB1, MyPart, NewBuff), !, + addNewLines(Rest, [], R). +% if function like scanf("%d", &n); +addNewLines([H, H2|T], Buff, R):- + H = [H1|_], member(H1, [tfunction]), + packTillRightParen(T, [], _, Rest, 1), + (Rest == [] ; Rest= [[M1|_]|_], M1 \= tleftBrace), + append(Buff, [H], NewBuff), !, + addNewLines([H2|T], NewBuff, R). +% if #inclide +addNewLines([H1, H2|T], Buff, [NewBuff|R]):- + H1 = [H11|_], member(H11, [tinclude]), + append(Buff, [H1, H2, [execNL, "\n"]], NewBuff), !, + addNewLines(T, [], R). +% if not any above just append them +addNewLines([H|T], Buff, R):- + append(Buff, [H], NewBuff), !, + addNewLines(T, NewBuff, R). + +%%Main predicate +addNewLines(Tokens, PackedLines):- addNewLines(Tokens, [], PackedLines), !. + + +% Base +addSpaces([], Buff, Buff):- !. +% no spaces before and after '\n' or ')' +addSpaces([H|T], Buff, Res):- + H = [H1|_], member(H1, [execNL, trightParen]), + ((append(NewBuf, [[execSpace, "\s"]], Buff), + append(NewBuf, [H], NewBuff)); + (\+ append(_, [[execSpace, "\s"]], Buff), + append(Buff, [H], NewBuff))), !, + addSpaces(T, NewBuff, Res). +% Before ',', ';' no space +addSpaces([H|T], Buff, Res):- + H = [H1|_], member(H1, [tcomma, tsemicolon]), + append(NewBuf, [[execSpace, "\s"]], Buff), + append(NewBuf, [H, [execSpace, "\s"]], NewBuff), !, + addSpaces(T, NewBuff, Res). +% No space after (, [, {, function name, tsizeof +addSpaces([H1|T], Buff, Res):- + H1 = [H11|_], member(H11, [tleftParen, tfunction, tleftBrace, tleftSqParen, + tsizeof]), + append(Buff, [H1], NewBuff), !, + addSpaces(T, NewBuff, Res). +% i->mau();-- +addSpaces([H1|T], Buff, Res):- + H1 = [H11|_], member(H11, [tptr, execNL]), + append(NewBuf, [_], Buff), + append(NewBuf, [H1], NewBuff), !, + addSpaces(T, NewBuff, Res). +% some tokens before *i, &i +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [tmultiply, tbitwiseAnd]), + H2 = [H21|_], member(H21, [tidentifier]), + length(Buff, N), N >= 2, + append(_, [H3, H4], Buff), + H3 = [H31|_], \+ member(H31, [tnumber, tidentifier, trightParen, trightSqParen]), + H4 = [H41|_], member(H41, [execSpace, tleftParen, tleftBrace, tleftSqParen]), + append(Buff, [H1, H2], NewBuff), !, + addSpaces(T, NewBuff, Res). +% new line (*i, (&i +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [tmultiply, tbitwiseAnd]), + H2 = [H21|_], member(H21, [tidentifier]), + length(Buff, 1), + Buff = [[H31|_]], member(H31, [tleftParen, tleftBrace, tleftSqParen]), + append(Buff, [H1, H2], NewBuff), !, + addSpaces(T, NewBuff, Res). +% none before *i, &i +addSpaces([H1, H2|T], [], Res):- + H1 = [H11|_], member(H11, [tmultiply, tbitwiseAnd]), + H2 = [H21|_], member(H21, [tidentifier]), + append([], [H1, H2], NewBuff), !, + addSpaces(T, NewBuff, Res). +% i++, i-- +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [tidentifier]), + H2 = [H21|_], member(H21, [tpp, tmm]), + append(Buff, [H1, H2], NewBuff), !, + addSpaces(T, NewBuff, Res). +% ++i, --i +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [tpp, tmm]), + H2 = [H21|_], member(H21, [tidentifier]), + append(Buff, [H1], NewBuff), !, + addSpaces([H2|T], NewBuff, Res). +% pack strings line '0' +addSpaces([H1|T], Buff, Res):- + H1 = [H11, Lex], member(H11, [tquot]), + append(M, [[H11,Lex]|Rest], T), + append(Buff, [H1|M], NewBuf), + append(NewBuf, [[H11,Lex], [execSpace, "\s"]], NewBuff), !, + addSpaces(Rest, NewBuff, Res). +% })]] +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [trightParen, trightBrace, trightSqParen]), + H2 = [H21|_], member(H21, [trightParen, trightBrace, trightSqParen]), + append(Buff, [H1], NewBuff), !, + addSpaces([H2|T], NewBuff, Res). +% 0], 0}, a], a}, 1), a), a][ +addSpaces([H1, H2, H3|T], Buff, Res):- + H1 = [H11|_], member(H11, [tnumber, tidentifier]), + H2 = [H21|_], member(H21, [trightParen, trightBrace, trightSqParen]), + H3 = [H31|_], + ((member(H31, [trightParen, trightBrace, trightSqParen, tleftSqParen]), + append(Buff, [H1, H2], NewBuff)); + (\+ member(H31, [trightParen, trightBrace, trightSqParen]), + append(Buff, [H1, H2, [execSpace, "\s"]], NewBuff))), !, + addSpaces([H3|T], NewBuff, Res). +% a[.. +addSpaces([H1, H2|T], Buff, Res):- + H1 = [H11|_], member(H11, [tidentifier]), + H2 = [H21|_], member(H21, [tleftSqParen]), + append(Buff, [H1], NewBuff), !, + addSpaces([H2|T], NewBuff, Res). +% Else +addSpaces([H|T], Buff, Res):- + append(Buff, [H, [execSpace, "\s"]], NewBuff), !, + addSpaces(T, NewBuff, Res). + +% Main predicate +addSpaces([], []):- !. +addSpaces([HT|TT], Res):- addSpaces(HT, [], RHT), !, + addSpaces(TT, RTT), + append([RHT], RTT, Res). + + +% Base +addTabs([], _, Buff, Buff, _, _):- !. +% Openning tleftBrace +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + H = [[H1|_]|_], + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), + H1 == tleftBrace, + append(Tabs, [[execTAB, "\s\s\s\s"]], NewTabs), !, + addTabs(T, NewTabs, NewBuff, Res, Bit1, Bit2). +% Closing trightBrace and a little newline for readability +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + H = [[H1|_]|_], + H1 == trightBrace, + append(NewTabs, [[execTAB, "\s\s\s\s"]], Tabs), + append(NewTabs, H, NewH1), + append(NewH1, [[execNL, "\n"]], NewH), + append(Buff,[NewH], NewBuff), !, + addTabs(T, NewTabs, NewBuff, Res, Bit1, Bit2). +% Special case when one line only and maybe nested ifs, fors, etc +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + H = [[H1|_]|_], + member(H1, [tfor, twhile, tif, telse]), + T = [[H2|_]|_], + H2 = [H21|_], + H21 \= tleftBrace, + append(Tabs, H, NewH), + append(Tabs, [[execTAB, "\s\s\s\s"]], NewTabs), + append(Buff,[NewH], NewBuff), !, + NewBit2 is Bit2 + 1, + addTabs(T, NewTabs, NewBuff, Res, Bit1, NewBit2). +% Ordinary case +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + H = [[H1|_]|_], + member(H1, [tfor, twhile, tif, telse, tswitch]), + T = [[[H2|_]|_]|_], + H2 == tleftBrace, + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), !, + addTabs(T, Tabs, NewBuff, Res, Bit1, Bit2). +% case Bit1 helpes us to identify where if starts and ends +addTabs([H|T], Tabs, Buff, Res, 0, Bit2):- + H = [[H1|_]|_], + member(H1, [tcase, tdefault]), + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), + append(Tabs, [[execTAB, "\s\s\s\s"]], NewTabs), !, + addTabs(T, NewTabs, NewBuff, Res, 1, Bit2). +% Ordinary case +addTabs([H|T], Tabs, Buff, Res, 1, Bit2):- + H = [[H1|_]|_], + member(H1, [tbreak]), + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), + append(NewTabs, [[execTAB, "\s\s\s\s"]], Tabs), !, + addTabs(T, NewTabs, NewBuff, Res, 0, Bit2). +% All other tokens and Bit2 isn't 0 +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + Bit2 \= 0, + removeNTabs(Tabs, Bit2, NewTabs), + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), !, + addTabs(T, NewTabs, NewBuff, Res, Bit1, 0). +% All other tokens +addTabs([H|T], Tabs, Buff, Res, Bit1, Bit2):- + append(Tabs, H, NewH), + append(Buff,[NewH], NewBuff), !, + addTabs(T, Tabs, NewBuff, Res, Bit1, Bit2). + +% Main predicate +addTabs(PackedLinesWithSpaces, PackedLinesWithSpacesAndTabs):- + addTabs(PackedLinesWithSpaces, [], [], PackedLinesWithSpacesAndTabs, 0, 0), !. diff --git a/homework/81281/c2html/print.pl b/homework/81281/c2html/print.pl new file mode 100644 index 0000000..d115e46 --- /dev/null +++ b/homework/81281/c2html/print.pl @@ -0,0 +1,103 @@ +%% Writing to html file the stream of tokens +tohtmlfile(_, []):- !. + +tohtmlfile(Stream, [H|T]) :- is_identifier(H), H = [_,Lexem], + write(Stream, '<span class=\"identifier\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_string(H), H = [_,Lexem], + write(Stream, '<span class=\"string\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_function(H), H = [_, Lexem], + write(Stream, '<span class=\"function\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_header(H), H = [_,Lexem], + write(Stream, '<span class=\"string\"><'), + write(Stream, Lexem), + write(Stream,'></span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_keyWord(H), H = [_,Lexem], + write(Stream, '<span class=\"keyword\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_number(H), H = [_,Lexem], + write(Stream, '<span class=\"number\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_operator(H), H = [_,Lexem], + write(Stream, '<span class=\"operator\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_parenth(H), H = [_,Lexem], + write(Stream, '<span class=\"parenthese\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_quotation(H), H = [_,Lexem], + write(Stream, '<span class=\"quotation\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_unknown(H), H = [_,Lexem], + write(Stream, '<span class=\"unknown\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_comment(H), H = [_,Lexem], + write(Stream, '<span class=\"comment\">'), + write(Stream, Lexem), + write(Stream,'</span>'), + !, + tohtmlfile(Stream, T). + +tohtmlfile(Stream, [H|T]) :- is_controlStructureBody(H), + write(Stream, '<button class=\"collapsible\">'), + H = [H1|T1], + tohtmlfile(Stream, H1), + write(Stream, '</span></button>'), nl(Stream), + write(Stream, '<div class=\"content\">'), + helper(Stream, T1), + write(Stream,'</div>'), + !, + tohtmlfile(Stream, T). + + +tohtmlfile(Stream, [H|T]) :- is_blank(H), H = [_,Lexem], + write(Stream, Lexem), + !, + tohtmlfile(Stream, T). + + + +helper(_, []). +helper(Stream, L):- append(A, [H|B], L), is_controlStructureBody(H), + helper(Stream,A), tohtmlfile(Stream, [H]), helper(Stream,B). +helper(Stream, [H|T]):- tohtmlfile(Stream, H), helper(Stream, T). diff --git a/homework/81281/c2html/tokens.pl b/homework/81281/c2html/tokens.pl new file mode 100644 index 0000000..1a41933 --- /dev/null +++ b/homework/81281/c2html/tokens.pl @@ -0,0 +1,229 @@ +%% Definitions of tokens +token([tauto, "auto" ]) --> "auto", !. +token([tbreak, "break" ]) --> "break", !. +token([tcase, "case" ]) --> "case", !. +token([tchar, "char" ]) --> "char", !. +token([tconst, "const" ]) --> "const", !. +token([tcontinue, "continue" ]) --> "continue", !. +token([tdefault, "default" ]) --> "default", !. +token([tdo, "do" ]) --> "do", !. +token([tdouble, "double" ]) --> "double", !. +token([telse, "else" ]) --> "else", !. +token([tenum, "enum" ]) --> "enum", !. +token([textern, "extern" ]) --> "extern", !. +token([tfloat, "float" ]) --> "float", !. +token([tfor, "for" ]) --> "for", !. +token([tgoto, "goto" ]) --> "goto", !. +token([tif, "if" ]) --> "if", !. +token([tint, "int" ]) --> "int", !. +token([tlong, "long" ]) --> "long", !. +token([tregister, "register" ]) --> "register", !. +token([treturn, "return" ]) --> "return", !. +token([tshort, "short" ]) --> "short", !. +token([tsigned, "signed" ]) --> "signed", !. +token([tsizeof, "sizeof" ]) --> "sizeof", !. +token([tstatic, "static" ]) --> "static", !. +token([tstruct, "struct" ]) --> "struct", !. +token([tswitch, "switch"]) --> "switch", !. +token([ttypedef, "typedef" ]) --> "typedef", !. +token([tunion, "union" ]) --> "union", !. +token([tunsigned, "unsigned" ]) --> "unsigned", !. +token([tvoid, "void" ]) --> "void", !. +token([tvolatile, "volatile" ]) --> "volatile", !. +token([twhile, "while" ]) --> "while", !. +token([tinclude, "#include" ]) --> "#include", !. + +token([tcomment1, IA]) --> comment1(I), !, + { atom_chars(IA,I)}. + +%% Handling windows written files with "\r\n" +token([[tcomment2, IA], [execNL, '\n']]) --> comment2(I), !, + {append(I1, [C1, C2], I), + ((char_code('\r', C1), char_code('\n', C2), atom_chars(IA,I1)); + (\+ char_code('\r', C1), char_code('\n', C2), + append(I1, [C1], I2), atom_chars(IA,I2)))}. + +token([tstring, IA]) --> stringy(I), !, + {atom_chars(IA,I)}. +token([theader, IA]) --> header(I), !, + {append(IO, [_], I), append([_], II, IO), atom_chars(IA,II)}. +token([[tfunction, IA], [tleftParen, "("]]) --> func(I), !, + {append(IO, [_], I), IO \= [], atom_chars(IA, IO)}. +token([tnumber, I]) --> number(I), !. + +token([tbinShiftLeftAssign, "<<="]) --> "<<=", !. +token([tbinShiftRightAssign, ">>="]) --> ">>=", !. +token([tpp, "++"]) --> "++", !. +token([tmm, "--"]) --> "--", !. +token([tptr, "->"]) --> "->", !. +token([tgreaterEqual,">=" ]) --> ">=", !. +token([tequal, "=="]) --> "==", !. +token([tnotequal,"!=" ]) --> "!=", !. +token([tlessEqual, "<="]) --> "<=", !. +token([tplusAssign, "+="]) --> "+=", !. +token([tminusAssign, "-="]) --> "-=", !. +token([tmultAssign, "*="]) --> "*=", !. +token([tdivAssign, "/="]) --> "/=", !. +token([tlessEqual, "<="]) --> "<=", !. +token([tlogicalAnd, "&&"]) --> "&&", !. +token([tlogicalOr, "||"]) --> "||", !. +token([tbinShiftLeft, "<<"]) --> "<<", !. +token([tbinShiftRight, ">>"]) --> ">>", !. +token([tbinAndAssign, "&="]) --> "&=", !. +token([tbinXOrAssign, "^="]) --> "^=", !. +token([tbinOrAssign, "|="]) --> "|=", !. + +token([tmultiply, "*"]) --> "*", !. +token([tdivide, "/"]) --> "/", !. +token([tmod, "%"]) --> "%", !. +token([tadd, "+"]) --> "+", !. +token([tsubtract, "-"]) --> "-", !. +token([tless, "<"]) --> "<", !. +token([tgreater, ">"]) --> ">", !. +token([tnot, "!"]) --> "!", !. +token([tassign,"="]) --> "=", !. +token([tbitwiseAnd, "&"]) --> "&", !. +token([tbitwiseOr, "|"]) --> "|", !. +token([tbitwiseXOr, "^"]) --> "^", !. +token([tbinCompliment, "~"]) --> "~", !. + + +token([tspace , "\\s"]) --> "\\s", !. +token([ttab , "\\t"]) --> "\\t", !. +token([tnl, "\\n"]) --> "\\n", !. +token([execSpace , "\s"]) --> "\s", !. +token([execTAB , "\s\s\s\s"]) --> "\t", !. +token([execNL, "\n"]) --> ( "\n"; "\r\n"), !. + + +token([tleftParen,"("]) --> "(", !. +token([trightParen, ")"]) --> ")", !. +token([tleftBrace, "{"]) --> "{", !. +token([trightBrace, "}"]) --> "}", !. +token([tleftSqParen, "["]) --> "[", !. +token([trightSqParen, "]" ]) --> "]", !. +token([tsemicolon, ";"]) --> ";", !. +token([ttwodots, ":"]) --> ":", !. +token([tquestion, "?"]) --> "?", !. + +token([tcomma, "," ]) --> ",", !. +token([tquot, "\'"]) --> "\'", !. +token([tdoubleQuot, "\""]) --> "\"", !. +token([tdot, "."]) --> ".", !. + +token([tidentifier, IA]) --> identifier(I), !, + {atom_chars(IA,I)}. + +token([tunknown, IA]) --> allThatIsNotAToken(I), !, + {I \= [], atom_chars(IA, I)}. + + +%% Main predicate +tokens([Token | Tail]) --> token(Token), !, tokens(Tail). +tokens([]) --> !, [], !. + + +%% Helper predicates +identifier([C|Cs]) --> [C], {char_type(C, csymf)}, !, + identifierHelper(Cs). + +identifierHelper([C|Cs]) --> [C], {char_type(C, csym)}, !, + identifierHelper(Cs). +identifierHelper([C]) --> [C], {char_type(C, csym), \+ char_code('_', C)}, !. +identifierHelper([]) --> [], !. + +func([C|Cs]) --> [C], {char_type(C, csymf)}, !, + funkHelper(Cs). + +funkHelper([C|Cs]) --> [C], {char_type(C, csym)}, !, + funkHelper(Cs). +funkHelper([C]) --> [C], {char_code("(", C)}, !. + +stringy([C|Cs]) --> [C], {char_code('\"', C)}, !, + stringyHelper(Cs). + +stringyHelper([C]) --> [C], { char_code('\n', C), fail}, !. +stringyHelper([C1, C2]) --> [C1, C2], { char_code('\\', C1), char_code('\s', C2), fail}, !. +stringyHelper([C1, C2|Cs]) --> [C1, C2], { char_code('\\', C1), char_code('\"', C2)}, !, + stringyHelper(Cs). +stringyHelper([C]) --> [C], { char_code('\"', C)}. +stringyHelper([C|Cs]) --> [C], stringyHelper(Cs). + +header([C|Cs]) --> [C], {char_code('<', C)}, !, + headerHelper(Cs). + +headerHelper([C|Cs]) --> [C], + {char_type(C, csymf); char_code('.', C); char_code('/', C); char_code('+', C)}, + headerHelper(Cs). +headerHelper([C]) --> [C], {char_code('>', C)}. + + +comment1([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('*', C2)}, !, + anythingButStarSlash(Cs). + +comment2([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('/', C2)}, !, + anythingButNL(Cs). + +anything([C|Cs]) --> [C], anything(Cs). +anything([]) --> []. + +allThatIsNotAToken([C|Cs]) --> [C|Cs], \+ tokens(C), \+ tokens([C|Cs]), + allThatIsNotAToken(Cs). +allThatIsNotAToken([]) --> []. + +anythingButNL([C|Cs]) --> [C], + {\+ char_code('\r', C), \+ char_code('\n', C)}, + anythingButNL(Cs). +anythingButNL([C1,C2]) --> [C1,C2], + {(char_code('\r', C1), char_code('\n', C2)); + (\+ char_code('\r', C1), char_code('\n', C2))}. + +anythingButStarSlash([C1,C2]) --> [C1, C2], + {char_code('*', C1), char_code('/', C2)}. +anythingButStarSlash([C|Cs]) --> [C], + anythingButStarSlash(Cs). + + + +%% Definitions of lists of different types of tokens +is_keyWord([H,_]):- member(H, [tauto, tbreak, tcase, tchar, tconst, tcontinue, + tdefault, tdo, tdouble, telse, tenum, textern, tfloat, tfor, tgoto, tif, + tint, tlong, tregister, treturn, tshort, tsigned, tstatic, tstruct, + tswitch, ttypedef, tunion, tunsigned, twhile, tvoid, tvolatile, tinclude]). + +is_operator([H,_]):- member(H, [tmultiply, tdivide, tmod, tadd, tsubtract, tless, + tlessEqual, tgreater, tgreaterEqual, tequal, tnotequal, tnot, tassign, tlogicalOr, + tlogicalAnd, tbitwiseOr, tbitwiseAnd, tpp, tmm, tptr, tsizeof, tbinShiftLeftAssign, + tbinShiftRightAssign, tplusAssign, tminusAssign, tmultAssign, tdivAssign, tbinShiftLeft, + tbinShiftRight, tbinAndAssign, tbinXOrAssign, tbinOrAssign, tbitwiseXOr, tbinCompliment]). + +is_parenth([H,_]):- member(H, [tleftParen, trightParen, tleftBrace, trightBrace, + tleftSqParen, trightSqParen, tsemicolon, ttwodots]). + +is_quotation([H,_]):- member(H, [tquot, tdoubleQuot, tcomma, tdot, tquestion]). + +is_unknown([H,_]):- member(H, [tunknown]). + +is_number([H,_]):- member(H, [tnumber]). + +is_function([H,_]):- member(H, [tfunction]). + +is_controlStructureBody([H|_]):- + member(Token, [tdo, tfor, twhile, tif, telse, tswitch, tfunction, tstruct]), + append(_, [[Token|_]|_], H). + +is_packedFunction([H|_]):- append(_, [[tfunction|_]|_], H). + +is_packedStructure([H|_]):- append(_, [[tstruct|_]|_], H). + +% experimental is_functionWithCC([H|_]):- H = [[tCC]|_]. + +is_identifier([H,_]):- member(H, [tidentifier, tnl, ttab, tspace]). + +is_blank([H,_]):- member(H, [execTAB, execSpace, execNL]). + +is_comment([H,_]):-member(H, [tcomment1, tcomment2]). + +is_string([H,_]):- member(H, [tstring]). + +is_header([H,_]):- member(H, [theader]). |
