diff options
Removed old homeworks
13 files changed, 0 insertions, 1096 deletions
| -rw-r--r-- | homework/81568/c2html/.gitignore | 2 | ||||
| -rw-r--r-- | homework/81568/c2html/Makefile | 2 | ||||
| -rw-r--r-- | homework/81568/c2html/README.md | 8 | ||||
| -rw-r--r-- | homework/81568/c2html/cyclomatic.cpp | 95 | ||||
| -rw-r--r-- | homework/81568/c2html/cyclomatic.h | 19 | ||||
| -rw-r--r-- | homework/81568/c2html/html_utils.cpp | 87 | ||||
| -rw-r--r-- | homework/81568/c2html/html_utils.h | 18 | ||||
| -rw-r--r-- | homework/81568/c2html/lexcer.cpp | 327 | ||||
| -rw-r--r-- | homework/81568/c2html/lexcer.h | 76 | ||||
| -rw-r--r-- | homework/81568/c2html/lexconst.h | 42 | ||||
| -rw-r--r-- | homework/81568/c2html/main.cpp | 114 | ||||
| -rw-r--r-- | homework/81568/c2html/test.c | 114 | ||||
| -rw-r--r-- | homework/81568/c2html/test.html | 192 |
diff --git a/homework/81568/c2html/.gitignore b/homework/81568/c2html/.gitignore deleted file mode 100644 index c5a122e..0000000 --- a/homework/81568/c2html/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -lexcer -TAGS diff --git a/homework/81568/c2html/Makefile b/homework/81568/c2html/Makefile deleted file mode 100644 index 2856500..0000000 --- a/homework/81568/c2html/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -lexcer: main.cpp lexcer.cpp cyclomatic.cpp html_utils.cpp - g++ -o lexcer main.cpp lexcer.cpp cyclomatic.cpp html_utils.cpp diff --git a/homework/81568/c2html/README.md b/homework/81568/c2html/README.md deleted file mode 100644 index 8c31e72..0000000 --- a/homework/81568/c2html/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# c2HTML - Kind of tokenizer for the 'C' Programming Language. - -# Install - Pretty simple. - ```bash - make - ``` diff --git a/homework/81568/c2html/cyclomatic.cpp b/homework/81568/c2html/cyclomatic.cpp deleted file mode 100644 index b7b0c17..0000000 --- a/homework/81568/c2html/cyclomatic.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "cyclomatic.h" -#include "html_utils.h" -#include "lexcer.h" -#include "lexconst.h" -#include <stack> - -using HTMLUtils::writeToOutput; -using LexCer::TokenClass; -using LexCer::Tokenizer; - -// Writing a full parser would give us the change of -// making the cyclomatic graph of the function. -// #courseproject -unsigned ParCer::outputWithCyclomaticComplexity(FILE *in, FILE *out) { - Tokenizer tokenizer (in, out); - - std::string lexeme; - double number; - TokenClass type; - Token currentToken; - std::stack<short> history; - bool inFunction = false; - - #define PAREN false - std::stack<bool> openedBlocks; - std::string className = "a"; - unsigned counter = 1; - unsigned complexity = 1; - - do { - currentToken = tokenizer.nextToken(&lexeme, &number); - type = currentToken.type; - - if (!inFunction) { - history.push(type); - } else { - if (type == TokenClass::RightCurly) { - if (openedBlocks.empty()) { // end of a function. What one does to avoid parsing 'C' - inFunction = false; - fputs("<!-- ", out); - char closingDiv[100]; - sprintf(closingDiv, "%d --></div>", complexity); - fputs(closingDiv, out); - complexity = 1; - } else { - openedBlocks.pop(); - } - } - else if (type == TokenClass::LeftCurly) { - openedBlocks.push(PAREN); - } - else if (type == TokenClass::Keyword) { - if (!lexeme.compare("if") || - !lexeme.compare("while") || - !lexeme.compare("for") || - !lexeme.compare("case") || - !lexeme.compare("default")) - { - ++complexity; - } - } - } - - if (!inFunction && type == TokenClass::LeftCurly) { - history.pop(); - while (!history.empty() && history.top() == TokenClass::Skip) { - history.pop(); - } - - if (history.top() == TokenClass::RightParen) { - while (!history.empty() && history.top() != TokenClass::LeftParen) - history.pop(); - if (!history.empty()) - history.pop(); - while (!history.empty() && history.top() == TokenClass::Skip) - history.pop(); - - if (!history.empty() && history.top() == TokenClass::Identifier) { // We are in a function - char openingDiv[100]; - sprintf(openingDiv, "<div class=\"func\">", className.c_str()); - fputs(openingDiv, out); - inFunction = true; - history = std::stack<short>{}; - } - } - } - - if (!writeToOutput(out, currentToken)) - break; - lexeme = ""; - number = 0.0; - } while (type != TokenClass::Eof || - type != TokenClass::Invalid); - return counter - 1; -} diff --git a/homework/81568/c2html/cyclomatic.h b/homework/81568/c2html/cyclomatic.h deleted file mode 100644 index 29fdc1c..0000000 --- a/homework/81568/c2html/cyclomatic.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef CYCLOMATIC_H -#define CYCLOMATIC_H - -#include <cstdio> - -// Given this -// is 20,000 LOC, I'll probably leave parsing -// 'C' as a course project -// #courseproject - -// class Parser; - -namespace ParCer { - -unsigned outputWithCyclomaticComplexity(FILE *fpIn, FILE *fpOut); - -} /* Namespace ParCer */ - -#endif /* CYCLOMATIC_H */ diff --git a/homework/81568/c2html/html_utils.cpp b/homework/81568/c2html/html_utils.cpp deleted file mode 100644 index 6ca91c6..0000000 --- a/homework/81568/c2html/html_utils.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "html_utils.h" -#include "lexconst.h" - -namespace HTMLUtils { - -std::string escapeHTML(const std::string &lexeme) { - std::string result; - for (auto c : lexeme) { - switch(c) { - case '<': - result.append("<"); - break; - case '>': - result.append(">"); - break; - case '&': - result.append("&"); - break; - default: - result.push_back(c); - break; - } - } - return result; -} - -bool writeToOutput(FILE *out, Token token) { - if (token.type == TokenClass::Eof) { - printf("End of file!\n"); - fputc('\n', out); - return false; - } - - if (token.type == TokenClass::Skip) { - if (fputs(token.lexeme.c_str(), out) == EOF) { - printf("Error while writing to output"); - return false; - } - return true; - } - - printf("Token of type %s at line %d: ", LexCer::tokenToClass.at(token.type).c_str(), token.line); - if (token.type == TokenClass::Number) - printf("%f\n", token.number); - else - printf("%s\n", token.lexeme.c_str()); - - auto lexeme = escapeHTML(token.lexeme); - - std::string output; - output.append(LexCer::spanBegin).append(LexCer::tokenToClass.at(token.type)).append("\">").append(lexeme).append(LexCer::spanEnd); - - if (fputs(output.c_str(), out) == EOF) { - printf("Error while writing to output"); - return false; - } - return true; -} - -std::string getOutputFileName(const std::string &name) { - auto pos = name.find_last_of('.'); - if (pos == std::string::npos) { - return name + ".html"; - } - - return name.substr(0, pos) + ".html"; -} - -void lexCit(FILE *in, FILE *out) { - Tokenizer hl {in, out}; - - std::string lexeme; - double number; - TokenClass type; - Token token; - do { - token = hl.nextToken(&lexeme, &number); - type = token.type; - - if (!writeToOutput(out, token)) - break; - lexeme = ""; - number = 0.0; - } while(type != TokenClass::Eof || type != TokenClass::Invalid); -} - -} /* Namespace HTMLUtils */ diff --git a/homework/81568/c2html/html_utils.h b/homework/81568/c2html/html_utils.h deleted file mode 100644 index 8d8cf0d..0000000 --- a/homework/81568/c2html/html_utils.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef HTML_UTILS_H -#define HTML_UTILS_H - -#include "lexcer.h" -using LexCer::Token; -using LexCer::TokenClass; -using LexCer::Tokenizer; - -namespace HTMLUtils { - -std::string escapeHTML(const std::string &lexeme); -bool writeToOutput(FILE *out, Token token); -std::string getOutputFileName(const std::string &name); -void lexCit(FILE *in, FILE *out); - -} /* Namespace HMTLUtils */ - -#endif /* HTML_UTILS_H */ diff --git a/homework/81568/c2html/lexcer.cpp b/homework/81568/c2html/lexcer.cpp deleted file mode 100644 index df28e6f..0000000 --- a/homework/81568/c2html/lexcer.cpp +++ /dev/null @@ -1,327 +0,0 @@ -#include "lexcer.h" -#include "lexconst.h" - -#include <cctype> -#include <cstdlib> - -namespace LexCer { - -Tokenizer::Tokenizer(FILE *in, FILE *out) - : in(in), out(out) { - token.type = TokenClass::Invalid; - // The idea behind having modifiable typenames structure was to read #defines, but - // maybe it is better if a whole preproccessor is written. - typenames = {"void", "char", "short", "int", "long", "float", "double", "signed", "unsigned", "FILE"}; -} - -Token Tokenizer::makeToken(TokenClass type, std::string lexeme, double number) { - token.type = type; - token.line = line; - token.lexeme = lexeme; - token.number = number; - return token; -} - -bool Tokenizer::readNextDirective(std::string *lexeme) { - char c; - while (isalpha((c = fgetc(in)))) - lexeme->push_back(c); - // we may need that last bit - ungetc((unsigned) c, in); - auto directive = lexeme->substr(1, lexeme->size() - 1); - if (!preprocessor.count(directive)) - return false; - if (directive.compare("define") == 0) { - //readDefine(); - } - return true; -} - -bool Tokenizer::readNextString(std::string *lexeme, char oldc) { - char c, prevc = oldc; - while (((c = fgetc(in)) != '"' || prevc == '\\') && c != '\n' && c != EOF) { - lexeme->push_back(c); - prevc = c; - } - - if (c == '\n' || c == EOF) { - ungetc((unsigned) c, in); - return false; - } - lexeme->push_back(c); - return true; -} - -bool Tokenizer::readNextIdentifier(std::string *lexeme) { - char c; - bool valid = true; - while (isalnum(c = fgetc(in)) || c == '_') { - lexeme->push_back(c); - } - ungetc((unsigned) c, in); - return valid; -} - -bool Tokenizer::readNextNumber(char beg, double *number, std::string *numberRepr) { - bool valid = true; - num_t base = DEC; - char c; - if (beg == '0') { - c = fgetc(in); - numberRepr->push_back(c); - if (c == 'b') - base = BIN; - else if (c == 'x' || c == 'X') - base = HEX; - else if (isdigit(c)) - base = OCT; - else - ungetc((unsigned) c, in); - } - - while (isValidDigit(c = fgetc(in), base)) { - numberRepr->push_back(c); - } - - if (c == '.') { - numberRepr->push_back(c); - while (isValidDigit(c = fgetc(in), base)) - numberRepr->push_back(c); - } - if (tolower(c) == 'e') { - numberRepr->push_back(c); - if (base != DEC) - return false; - if (!isdigit(c = fgetc(in)) && c != '-' && c != '+') { - ungetc((unsigned) c, in); - return false; - } - numberRepr->push_back(c); - while (isValidDigit(c = fgetc(in), base)) - numberRepr->push_back(c); - } - - char lower = tolower(c); - if (lower == 'u') { - numberRepr->push_back(c); - lower = tolower(c = fgetc(in)); - } - if (lower == 'f' || lower == 'l') - numberRepr->push_back(c); - else - ungetc((unsigned) c, in); - - *number = this->atod(*numberRepr); // not working ... - return true; -} - -bool Tokenizer::isValidDigit(char c, num_t numType) const { - bool valid; - switch (numType) { - case DEC: - return isdigit(c); - case BIN: - return (c == '0' || c == '1'); - case OCT: - return (c >= '0' || c <= '7'); - case HEX: - return (isdigit(c) || tolower(c) >= 'a' || tolower(c) <= 'f'); - default: - return false; - } -} - -bool Tokenizer::readLineComment(std::string *lexeme) { - char c; - while ((c = fgetc(in)) != '\n' && c != EOF) - lexeme->push_back(c); - ungetc((unsigned) c, in); - return true; // for consistency -} - -bool Tokenizer::readMultilineComment(std::string *lexeme) { - char c, prevc = (char) -1; - while (((c = fgetc(in)) != '/' && prevc != '*') && c != EOF) - lexeme->push_back(c); - - if (c == EOF) { - return false; - } - lexeme->push_back(c); // closing slash - return true; -} - -bool Tokenizer::readInclude(std::string *lexeme) { - char c; - bool valid = true; - while ((c = fgetc(in)) != '>') { - lexeme->push_back(c); - if (!isalpha(c) && c != '_' && c != '.') - valid = false; - } - lexeme->push_back(c); - return valid; -} - -bool Tokenizer::readUntilBlank(std::string *lexeme) { - char c; - while (!isblank(c = fgetc(in)) && c != EOF) ; - ungetc((unsigned) c, in); - return true; -} - -Token Tokenizer::nextToken(std::string *lexeme, double *number) { - TokenClass type; - if (token.type == TokenClass::Eof) { - return makeToken(TokenClass::Invalid, *lexeme, *number); - } - - char c = fgetc(in); - if (c == EOF) { - return makeToken(TokenClass::Eof, *lexeme, *number); - } - lexeme->push_back(c); - // operator || symbol || string || preprocessor directive - // probably should check for errors like '5 * % 4;', but... - switch(c) { - case '\n': - ++line; - case '\t': - case ' ': - case ';': - case ',': - return makeToken(TokenClass::Skip, *lexeme, *number); - case '(': - return makeToken(TokenClass::LeftParen, *lexeme, *number); - case ')': - return makeToken(TokenClass::RightParen, *lexeme, *number); - case '{': - return makeToken(TokenClass::LeftCurly, *lexeme, *number); - case '}': - return makeToken(TokenClass::RightCurly, *lexeme, *number); - case '[': - case ']': - case '?': - case ':': - return makeToken(TokenClass::Operator, *lexeme, *number); - case '*': - c = fgetc(in); - if (c == '/') { - return makeToken(TokenClass::Invalid, *lexeme, *number); - } - ungetc((unsigned) c, in); - c = '*'; - case '^': - case '%': - case '=': - case '!': - case '~': - c = fgetc(in); - if (c != '=') ungetc((unsigned) c, in); - else lexeme->push_back(c); - return makeToken(TokenClass::Operator, *lexeme, *number); - case '<': - case '>': - c = fgetc(in); - if (c == lexeme->at(lexeme->size() - 1)) { - lexeme->push_back(c); - c = fgetc(in); - if (c == '=') - lexeme->push_back(c); - else - ungetc((unsigned) c, in); - } else if (lexeme->at(lexeme->size() - 1) == '<' && isalpha(c)) { - lexeme->push_back(c); - if (!readInclude(lexeme)) - return makeToken(TokenClass::Invalid, *lexeme, *number); - return makeToken(TokenClass::String, *lexeme, *number); - } else - ungetc((unsigned) c, in); - return makeToken(TokenClass::Operator, *lexeme, *number); - case '&': - case '|': - case '+': - c = fgetc(in); - if (c == lexeme->at(lexeme->size() - 1) || c == '=') - lexeme->push_back(c); - else - ungetc((unsigned) c, in); - return makeToken(TokenClass::Operator, *lexeme, *number); - case '-': - c = fgetc(in); - if (c == '-' || c == '=' || c == '>') - lexeme->push_back(c); - else - ungetc((unsigned) c, in); - return makeToken(TokenClass::Operator, *lexeme, *number); - case '#': - if (readNextDirective(lexeme)) - return makeToken(TokenClass::PreprocessorDirective, *lexeme, *number); - return makeToken(TokenClass::Invalid, *lexeme, *number); - case '\'': - lexeme->push_back(c = fgetc(in)); - if (c == '\\') { - lexeme->push_back(c = fgetc(in)); - } - lexeme->push_back(c = fgetc(in)); // the closing mark - if (c != '\'') - return makeToken(TokenClass::Invalid, *lexeme, *number); - return makeToken(TokenClass::String, *lexeme, *number); - case '"': - if (readNextString(lexeme, c)) - return makeToken(TokenClass::String, *lexeme, *number); - return makeToken(TokenClass::Invalid, *lexeme, *number); - case '/': - c = fgetc(in); - lexeme->push_back(c); - switch (c) { - case '=': - return makeToken(TokenClass::Operator, *lexeme, *number); - case '/': - if (!readLineComment(lexeme)) - return makeToken(TokenClass::Invalid, *lexeme, *number); - return makeToken(TokenClass::Comment, *lexeme, *number); - case '*': - if (!readMultilineComment(lexeme)) - return makeToken(TokenClass::Invalid, *lexeme, *number); - return makeToken(TokenClass::Comment, *lexeme, *number); - default: - if (!isblank(c) || !isdigit(c)) { - ungetc((unsigned) c, in); - lexeme->pop_back(); - return makeToken(TokenClass::Invalid, *lexeme, *number); - } - return makeToken(TokenClass::Operator, *lexeme, *number); - } - case '\\': - c = fgetc(in); - ungetc((unsigned) c, in); - if (c != '\n') - return makeToken(TokenClass::Invalid, *lexeme, *number); // stray '\' - return makeToken(TokenClass::Operator, *lexeme, *number); - } - - // identifier || keyword || typename - if (isalpha(c) || c == '_') { - if (readNextIdentifier(lexeme)) { - if (keywords.count(*lexeme) != 0) - return makeToken(TokenClass::Keyword, *lexeme, *number); - else if (typenames.count(*lexeme) != 0) - return makeToken(TokenClass::Typename, *lexeme, *number); - return makeToken(TokenClass::Identifier, *lexeme, *number); - } else { - return makeToken(TokenClass::Invalid, *lexeme, *number); - } - } - - if (!isdigit(c)) { - return makeToken(TokenClass::Invalid, *lexeme, *number); - } - - // number - if (readNextNumber(c, number, lexeme)) - return makeToken(TokenClass::Number, *lexeme, *number); - return makeToken(TokenClass::Invalid, *lexeme, *number); -} - -} /* Namespace Lexcer */ diff --git a/homework/81568/c2html/lexcer.h b/homework/81568/c2html/lexcer.h deleted file mode 100644 index eb8afe0..0000000 --- a/homework/81568/c2html/lexcer.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef FMI_IPL_LEXCER_H -#define FMI_IPL_LEXCER_H - -#include <unordered_set> -#include <unordered_map> -#include <string> - -#include <cstdio> - -namespace LexCer { - -enum TokenClass : short { - Operator, - Keyword, - PreprocessorDirective, - Typename, - Identifier, - String, - Comment, - Number, - LeftParen, - RightParen, - LeftCurly, - RightCurly, - - Skip, - Eof, - Invalid -}; - -struct Token { - TokenClass type; - unsigned line; - std::string lexeme; - double number; -}; - - -class Tokenizer { -private: - enum num_t { - BIN = 2, - OCT = 8, - DEC = 10, - HEX = 16 - }; - -public: - Tokenizer(FILE *in, FILE *out); - - Token nextToken(std::string *lexeme, double *number); - -private: - Token makeToken(TokenClass type, std::string lexeme = std::string(), double number = 0.0); - bool readNextDirective(std::string *lexeme); - bool readNextString(std::string *lexeme, char oldc); - bool readNextIdentifier(std::string *lexeme); - bool readNextNumber(char beg, double *number, std::string *numberRepr); - bool isValidDigit(char c, num_t numType) const ; - bool readLineComment(std::string *lexeme); - bool readMultilineComment(std::string *lexeme); - bool readInclude(std::string *lexeme); - bool readUntilBlank(std::string *lexeme); - - double atod(const std::string &number) const { return 0.0; } -private: - FILE *in; - FILE *out; - Token token; // current token - unsigned line = 1; - std::unordered_set<std::string> typenames; -}; - -} /* Namespace LexCer */ - -#endif diff --git a/homework/81568/c2html/lexconst.h b/homework/81568/c2html/lexconst.h deleted file mode 100644 index cfb9e6d..0000000 --- a/homework/81568/c2html/lexconst.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef LEXCER_CONST_H -#define LEXCER_CONST_H - -namespace LexCer { - -static const std::unordered_set<std::string> keywords = - {"auto", "register", "static", "extern", "typedef", "struct", "union", "enum", - "if", "else", "for", "while", "switch", "case", "default", "const", "break", - "continue", "goto", "sizeof", "alignof", "volatile", "return"}; - -static const std::unordered_set<std::string> preprocessor = - {"include", "define", "undef", "if", "ifdef", "ifndef", "else", "elif", "endif", - "line", "error", "pragma"}; - -static const std::unordered_map<TokenClass, std::string> tokenToClass - { - { - {TokenClass::Operator, "operator"}, - {TokenClass::Keyword, "keyword"}, - {TokenClass::Typename, "typename"}, - {TokenClass::PreprocessorDirective, "preproc"}, - {TokenClass::Identifier, "identifier"}, - {TokenClass::String, "string"}, - {TokenClass::Comment, "comment"}, - {TokenClass::Number, "number"}, - {TokenClass::Invalid, "invalid"}, - {TokenClass::LeftParen, "identifier"}, - {TokenClass::LeftCurly, "identifier"}, - {TokenClass::RightParen, "identifier"}, - {TokenClass::RightCurly, "identifier"}, - {TokenClass::RightParen, "identifier"} - } - }; - -static const std::string spanBegin = "<span class=\""; -static const std::string spanEnd = "</span>"; - -static constexpr int MAX_EXP_LEN = 4; - -} /* Namespace LexCer */ - -#endif diff --git a/homework/81568/c2html/main.cpp b/homework/81568/c2html/main.cpp deleted file mode 100644 index d4dbed3..0000000 --- a/homework/81568/c2html/main.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include <cstdio> -#include <cstring> - -#include "cyclomatic.h" -#include "html_utils.h" -#include "lexcer.h" - -int main(int argc, char *argv[]) { - - if (argc != 2) { - printf("Usage: %s <*.{c,h} file>\n", argv[0]); - return 1; /* wrong usage */ - } - - auto fpIn = fopen(argv[1], "r"); - if (!fpIn) { - return 2; /* unable to open file or inavlid file */ - } - - auto outputName = HTMLUtils::getOutputFileName(argv[1]); - auto fpOut = fopen(outputName.c_str(), "w"); - if (!fpOut) { - return 2; - } - - - fputs( - "<!DOCTYPE html>\n" - "<html>\n" - "<head>\n" - " <title>LexCed</title>\n" - " <style>\n" - " .keyword {\n" - " color: purple;\n" - " }\n" - " .number {\n" - " color: darkblue;\n" - " }\n" - " .string {\n" - " color: green;\n" - " }\n" - " .operator {\n" - " font-style: bold;\n" - " }\n" - " .invalid {\n" - " text-decoration: underline;\n" - " text-decoration-color: red;\n" - " text-decoration-style: wavy;\n" - " }\n" - " .typename {\n" - " color: blue;\n" - " }\n" - " .preproc {\n" - " color: purple;\n" - " }\n" - " .identifier {\n" - " color: black;\n" - " }\n" - " .string {\n" - " color: brown;\n" - " }\n" - " .comment {\n" - " color: green;\n" - " }\n" - " .complexity-level-one {\n" - " background-color: PaleGreen;\n" - " }\n" - " .complexity-level-two {\n" - " background-color: DarkGreen;\n" - " }\n" - " .complexity-level-three {\n" - " background-color: SaddleBrown;\n" - " }\n" - " .complexity-level-four {\n" - " background-color: Brown;\n" - " }\n" - " .complexity-level-five {\n" - " background-color: Maroon;\n" - " }\n" - " </style>\n" - "</head>\n" - "<body onload=\"toggleComplexity()\">\n" - "<pre class=\"code\">\n", - fpOut - ); - ParCer::outputWithCyclomaticComplexity(fpIn, fpOut); - fputs("</pre>\n" - "<script>\n" - "function toggleComplexity() {\n" - " var funcs = document.getElementsByClassName(\"func\");" - " if (funcs.length == 0) return;\n" - " var len = funcs.length;\n" - " for (var i = 0; i < len; ++i) {\n" - " var f = document.getElementsByClassName(\"func\")[0];\n" - " var complexity = f.innerHTML.match(\"(<!-- )([0-9]+)( -->)\")[2];\n" - " if (complexity < 3)\n" - " f.className = \"complexity-level-one\";\n" - " else if (complexity < 7)\n" - " f.className = \"complexity-level-two\";\n" - " else if (complexity < 15)\n" - " f.className = \"complexity-level-three\";\n" - " else if (complexity < 20)\n" - " f.className = \"complexity-level-four\";\n" - " else\n" - " f.className = \"complexity-level-five\";\n" - " }\n" - "}\n", - fpOut); - fputs("</script></body></html>\n", fpOut); - - printf("Highlighted text written in %s!\n", outputName.c_str()); - - return 0; -} diff --git a/homework/81568/c2html/test.c b/homework/81568/c2html/test.c deleted file mode 100644 index 0aafa72..0000000 --- a/homework/81568/c2html/test.c +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef SAMPLE_CODE_C -#define SAMPLE_CODE_C - -#define ONE_LINE_MACRO 5 - -#include <stdio.h> - -int bitcount(long long x) { - int cnt = 0; - while (x) { - ++cnt; - x &= (x-1); - } - return cnt; -} - -void shift_to_pos(int *num, int p, int n) { - *num = p > n ? *num << (p - n + 1) : *num >> (n - p + 1); -} - -int setbits(x, p, n, y) { - int mask = (1 << n) - 1; - int to_set = y & mask; - shift_to_pos(&mask, p, n); - shift_to_pos(&to_set, p, n); - return (x & ~mask) | to_set; -} - -unsigned invert(unsigned x, unsigned p, unsigned n) { - ++p; // in case p < n - while(n-- && p--) { - if (!(x & (1 << n))) { - x |= (1 << n); - } else { - x ^= (1 << n); - } - } - return x; -} - -int rightrot(long long x, int n) { - int bitcnt = bitcount(x); - x <<= n; - return (x & ( (1 << (bitcnt)) - 1) ) | (x & ~( (1 << bitcnt) - 1 )) >> bitcnt; -} - -char num[32]; - -void to_binary(unsigned x) { - int i = 31; - num[32] = '\0'; - while (x) { - if (x & 1) num[i] = '1'; - else num[i] = '0'; - --i; - x >>= 1; - } - for (; i >= 0; --i) num[i] = '0'; - for (; i < 32; ++i) { - printf("%c%s", num[i], !((i+1) % 4) ? " " : ""); - } -} - -void switch_why_not(void) { - char c = 'X'; - switch(c) { - case 'x': - printf("hex\n"); - break; - case 'X': - printf("why break stuff\n"); - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - default: - break; - } - - return; -} - -main() { - long long x = 0b110110; - printf("rightrot:\n"); - to_binary(x); - printf("\n"); - to_binary(rightrot(x, 3)); - printf("\n"); - - unsigned i = 0b101010; - printf("invert:\n"); - to_binary(i); - printf("\n"); - to_binary(invert(i, 4, 4)); - printf("\n"); - - long long a = 0b111001010011; - long long b = 0b11111010; - printf("bits of: "); to_binary(a); printf("\n"); - printf("bits to: "); to_binary(b); printf("\n"); - to_binary(setbits(a, 6, 4, b)); - - - int testInt = 12345678; - float testFloat = 1234.5678f; - double testDouble = 1234.5678; - - return 0; -} - -#endif /* SAMPLE_CODE_C */ diff --git a/homework/81568/c2html/test.html b/homework/81568/c2html/test.html deleted file mode 100644 index e39aec0..0000000 --- a/homework/81568/c2html/test.html +++ /dev/null @@ -1,192 +0,0 @@ -<!DOCTYPE html> -<html> -<head> - <title>LexCed</title> - <style> - .keyword { - color: purple; - } - .number { - color: darkblue; - } - .string { - color: green; - } - .operator { - font-style: bold; - } - .invalid { - text-decoration: underline; - text-decoration-color: red; - text-decoration-style: wavy; - } - .typename { - color: blue; - } - .preproc { - color: purple; - } - .identifier { - color: black; - } - .string { - color: brown; - } - .comment { - color: green; - } - .complexity-level-one { - background-color: PaleGreen; - } - .complexity-level-two { - background-color: DarkGreen; - } - .complexity-level-three { - background-color: SaddleBrown; - } - .complexity-level-four { - background-color: Brown; - } - .complexity-level-five { - background-color: Maroon; - } - </style> -</head> -<body onload="toggleComplexity()"> -<pre class="code"> -<span class="preproc">#ifndef</span> <span class="identifier">SAMPLE_CODE_C</span> -<span class="preproc">#define</span> <span class="identifier">SAMPLE_CODE_C</span> - -<span class="preproc">#define</span> <span class="identifier">ONE_LINE_MACRO</span> <span class="number">5</span> - -<span class="preproc">#include</span> <span class="string"><stdio.h></span> - -<span class="typename">int</span> <span class="identifier">bitcount</span><span class="identifier">(</span><span class="typename">long</span> <span class="typename">long</span> <span class="identifier">x</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">int</span> <span class="identifier">cnt</span> <span class="operator">=</span> <span class="number">0;</span>; - <span class="keyword">while</span> <span class="identifier">(</span><span class="identifier">x</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="operator">++</span><span class="identifier">cnt</span>; - <span class="identifier">x</span> <span class="operator">&=</span> <span class="identifier">(</span><span class="identifier">x</span><span class="operator">-</span><span class="number">1</span><span class="identifier">)</span>; - <span class="identifier">}</span> - <span class="keyword">return</span> <span class="identifier">cnt</span>; -<!-- 2 --></div><span class="identifier">}</span> - -<span class="typename">void</span> <span class="identifier">shift_to_pos</span><span class="identifier">(</span><span class="typename">int</span> <span class="operator">*</span><span class="identifier">num</span>, <span class="typename">int</span> <span class="identifier">p</span>, <span class="typename">int</span> <span class="identifier">n</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="operator">*</span><span class="identifier">num</span> <span class="operator">=</span> <span class="identifier">p</span> <span class="operator">></span> <span class="identifier">n</span> <span class="operator">?</span> <span class="operator">*</span><span class="identifier">num</span> <span class="operator"><<</span> <span class="identifier">(</span><span class="identifier">p</span> <span class="operator">-</span> <span class="identifier">n</span> <span class="operator">+</span> <span class="number">1</span><span class="identifier">)</span> <span class="operator">:</span> <span class="operator">*</span><span class="identifier">num</span> <span class="operator">>></span> <span class="identifier">(</span><span class="identifier">n</span> <span class="operator">-</span> <span class="identifier">p</span> <span class="operator">+</span> <span class="number">1</span><span class="identifier">)</span>; -<!-- 1 --></div><span class="identifier">}</span> - -<span class="typename">int</span> <span class="identifier">setbits</span><span class="identifier">(</span><span class="identifier">x</span>, <span class="identifier">p</span>, <span class="identifier">n</span>, <span class="identifier">y</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">int</span> <span class="identifier">mask</span> <span class="operator">=</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">n</span><span class="identifier">)</span> <span class="operator">-</span> <span class="number">1</span>; - <span class="typename">int</span> <span class="identifier">to_set</span> <span class="operator">=</span> <span class="identifier">y</span> <span class="operator">&</span> <span class="identifier">mask</span>; - <span class="identifier">shift_to_pos</span><span class="identifier">(</span><span class="operator">&</span><span class="identifier">mask</span>, <span class="identifier">p</span>, <span class="identifier">n</span><span class="identifier">)</span>; - <span class="identifier">shift_to_pos</span><span class="identifier">(</span><span class="operator">&</span><span class="identifier">to_set</span>, <span class="identifier">p</span>, <span class="identifier">n</span><span class="identifier">)</span>; - <span class="keyword">return</span> <span class="identifier">(</span><span class="identifier">x</span> <span class="operator">&</span> <span class="operator">~</span><span class="identifier">mask</span><span class="identifier">)</span> <span class="operator">|</span> <span class="identifier">to_set</span>; -<!-- 1 --></div><span class="identifier">}</span> - -<span class="typename">unsigned</span> <span class="identifier">invert</span><span class="identifier">(</span><span class="typename">unsigned</span> <span class="identifier">x</span>, <span class="typename">unsigned</span> <span class="identifier">p</span>, <span class="typename">unsigned</span> <span class="identifier">n</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="operator">++</span><span class="identifier">p</span>; <span class="comment">// in case p < n</span> - <span class="keyword">while</span><span class="identifier">(</span><span class="identifier">n</span><span class="operator">--</span> <span class="operator">&&</span> <span class="identifier">p</span><span class="operator">--</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="keyword">if</span> <span class="identifier">(</span><span class="operator">!</span><span class="identifier">(</span><span class="identifier">x</span> <span class="operator">&</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">n</span><span class="identifier">)</span><span class="identifier">)</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="identifier">x</span> <span class="operator">|=</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">n</span><span class="identifier">)</span>; - <span class="identifier">}</span> <span class="keyword">else</span> <span class="identifier">{</span> - <span class="identifier">x</span> <span class="operator">^=</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">n</span><span class="identifier">)</span>; - <span class="identifier">}</span> - <span class="identifier">}</span> - <span class="keyword">return</span> <span class="identifier">x</span>; -<!-- 3 --></div><span class="identifier">}</span> - -<span class="typename">int</span> <span class="identifier">rightrot</span><span class="identifier">(</span><span class="typename">long</span> <span class="typename">long</span> <span class="identifier">x</span>, <span class="typename">int</span> <span class="identifier">n</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">int</span> <span class="identifier">bitcnt</span> <span class="operator">=</span> <span class="identifier">bitcount</span><span class="identifier">(</span><span class="identifier">x</span><span class="identifier">)</span>; - <span class="identifier">x</span> <span class="operator"><<=</span> <span class="identifier">n</span>; - <span class="keyword">return</span> <span class="identifier">(</span><span class="identifier">x</span> <span class="operator">&</span> <span class="identifier">(</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">(</span><span class="identifier">bitcnt</span><span class="identifier">)</span><span class="identifier">)</span> <span class="operator">-</span> <span class="number">1</span><span class="identifier">)</span> <span class="identifier">)</span> <span class="operator">|</span> <span class="identifier">(</span><span class="identifier">x</span> <span class="operator">&</span> <span class="operator">~</span><span class="identifier">(</span> <span class="identifier">(</span><span class="number">1</span> <span class="operator"><<</span> <span class="identifier">bitcnt</span><span class="identifier">)</span> <span class="operator">-</span> <span class="number">1</span> <span class="identifier">)</span><span class="identifier">)</span> <span class="operator">>></span> <span class="identifier">bitcnt</span>; -<!-- 1 --></div><span class="identifier">}</span> - -<span class="typename">char</span> <span class="identifier">num</span><span class="operator">[</span><span class="number">32</span><span class="operator">]</span>; - -<span class="typename">void</span> <span class="identifier">to_binary</span><span class="identifier">(</span><span class="typename">unsigned</span> <span class="identifier">x</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">int</span> <span class="identifier">i</span> <span class="operator">=</span> <span class="number">31</span>; - <span class="identifier">num</span><span class="operator">[</span><span class="number">32</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">'\0'</span>; - <span class="keyword">while</span> <span class="identifier">(</span><span class="identifier">x</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="keyword">if</span> <span class="identifier">(</span><span class="identifier">x</span> <span class="operator">&</span> <span class="number">1</span><span class="identifier">)</span> <span class="identifier">num</span><span class="operator">[</span><span class="identifier">i</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">'1'</span>; - <span class="keyword">else</span> <span class="identifier">num</span><span class="operator">[</span><span class="identifier">i</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">'0'</span>; - <span class="operator">--</span><span class="identifier">i</span>; - <span class="identifier">x</span> <span class="operator">>>=</span> <span class="number">1</span>; - <span class="identifier">}</span> - <span class="keyword">for</span> <span class="identifier">(</span>; <span class="identifier">i</span> <span class="operator">></span><span class="operator">=</span> <span class="number">0;</span>; <span class="operator">--</span><span class="identifier">i</span><span class="identifier">)</span> <span class="identifier">num</span><span class="operator">[</span><span class="identifier">i</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">'0'</span>; - <span class="keyword">for</span> <span class="identifier">(</span>; <span class="identifier">i</span> <span class="operator"><</span> <span class="number">32</span>; <span class="operator">++</span><span class="identifier">i</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"%c%s"</span>, <span class="identifier">num</span><span class="operator">[</span><span class="identifier">i</span><span class="operator">]</span>, <span class="operator">!</span><span class="identifier">(</span><span class="identifier">(</span><span class="identifier">i</span><span class="operator">+</span><span class="number">1</span><span class="identifier">)</span> <span class="operator">%</span> <span class="number">4</span><span class="identifier">)</span> <span class="operator">?</span> <span class="string">" "</span> <span class="operator">:</span> <span class="string">""</span><span class="identifier">)</span>; - <span class="identifier">}</span> -<!-- 5 --></div><span class="identifier">}</span> - -<span class="typename">void</span> <span class="identifier">switch_why_not</span><span class="identifier">(</span><span class="typename">void</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">char</span> <span class="identifier">c</span> <span class="operator">=</span> <span class="string">'X'</span>; - <span class="keyword">switch</span><span class="identifier">(</span><span class="identifier">c</span><span class="identifier">)</span> <span class="identifier">{</span> - <span class="keyword">case</span> <span class="string">'x'</span><span class="operator">:</span> - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"hex\n"</span><span class="identifier">)</span>; - <span class="keyword">break</span>; - <span class="keyword">case</span> <span class="string">'X'</span><span class="operator">:</span> - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"why break stuff\n"</span><span class="identifier">)</span>; - <span class="keyword">case</span> <span class="number">1</span><span class="operator">:</span> - <span class="keyword">case</span> <span class="number">2</span><span class="operator">:</span> - <span class="keyword">case</span> <span class="number">3</span><span class="operator">:</span> - <span class="keyword">case</span> <span class="number">4</span><span class="operator">:</span> - <span class="keyword">case</span> <span class="number">5</span><span class="operator">:</span> - <span class="keyword">case</span> <span class="number">6</span><span class="operator">:</span> - <span class="keyword">default</span><span class="operator">:</span> - <span class="keyword">break</span>; - <span class="identifier">}</span> - - <span class="keyword">return</span>; -<!-- 10 --></div><span class="identifier">}</span> - -<span class="identifier">main</span><span class="identifier">(</span><span class="identifier">)</span> <div class="func"><span class="identifier">{</span> - <span class="typename">long</span> <span class="typename">long</span> <span class="identifier">x</span> <span class="operator">=</span> <span class="number">0b110110</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"rightrot:\n"</span><span class="identifier">)</span>; - <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">x</span><span class="identifier">)</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">rightrot</span><span class="identifier">(</span><span class="identifier">x</span>, <span class="number">3</span><span class="identifier">)</span><span class="identifier">)</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - - <span class="typename">unsigned</span> <span class="identifier">i</span> <span class="operator">=</span> <span class="number">0b101010</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"invert:\n"</span><span class="identifier">)</span>; - <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">i</span><span class="identifier">)</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">invert</span><span class="identifier">(</span><span class="identifier">i</span>, <span class="number">4</span>, <span class="number">4</span><span class="identifier">)</span><span class="identifier">)</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - - <span class="typename">long</span> <span class="typename">long</span> <span class="identifier">a</span> <span class="operator">=</span> <span class="number">0b111001010011</span>; - <span class="typename">long</span> <span class="typename">long</span> <span class="identifier">b</span> <span class="operator">=</span> <span class="number">0b11111010</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"bits of: "</span><span class="identifier">)</span>; <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">a</span><span class="identifier">)</span>; <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"bits to: "</span><span class="identifier">)</span>; <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">b</span><span class="identifier">)</span>; <span class="identifier">printf</span><span class="identifier">(</span><span class="string">"\n"</span><span class="identifier">)</span>; - <span class="identifier">to_binary</span><span class="identifier">(</span><span class="identifier">setbits</span><span class="identifier">(</span><span class="identifier">a</span>, <span class="number">6</span>, <span class="number">4</span>, <span class="identifier">b</span><span class="identifier">)</span><span class="identifier">)</span>; - - - <span class="typename">int</span> <span class="identifier">testInt</span> <span class="operator">=</span> <span class="number">12345678</span>; - <span class="typename">float</span> <span class="identifier">testFloat</span> <span class="operator">=</span> <span class="number">1234.5678f</span>; - <span class="typename">double</span> <span class="identifier">testDouble</span> <span class="operator">=</span> <span class="number">1234.5678</span>; - - <span class="keyword">return</span> <span class="number">0;</span>; -<!-- 1 --></div><span class="identifier">}</span> - -<span class="preproc">#endif</span> <span class="comment">/* SAMPLE_CODE_C */</span> - -</pre> -<script> -function toggleComplexity() { - var funcs = document.getElementsByClassName("func"); if (funcs.length == 0) return; - var len = funcs.length; - for (var i = 0; i < len; ++i) { - var f = document.getElementsByClassName("func")[0]; - var complexity = f.innerHTML.match("(<!-- )([0-9]+)( -->)")[2]; - if (complexity < 3) - f.className = "complexity-level-one"; - else if (complexity < 7) - f.className = "complexity-level-two"; - else if (complexity < 15) - f.className = "complexity-level-three"; - else if (complexity < 20) - f.className = "complexity-level-four"; - else - f.className = "complexity-level-five"; - } -} -</script></body></html> |
