Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / cToHtml
diff options
context:
space:
mode:
Commit990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675 (patch)
Author
 Georgi Lyubenov <godzbanebane@gmail.com>
2018-11-07 19:34:16 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-07 19:34:16 +0200
Filesd7f3f8cdc90ea9cc405b7c353d93893c80f15e4d /homework/81248/cToHtml
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.tar
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.tar.gz
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.zip
Parent76b849fbaa276566fa8838d745c0e5fbd2f138bc (diff)
Предаване на първо домашно ("XXX2html") (#12)
* Initial commit of clex new file: .gitignore new file: Setup.hs new file: app/Main.hs new file: keyword-strings.txt new file: package.yaml new file: sample.c new file: src/Boilerplate.hs new file: src/Lex.hs new file: src/Lib.hs new file: stack.yaml new file: test/Spec.hs Need to implement ctohtml functionality now. And maybe tests for the lexer. * Clean-up modified: app/Main.hs modified: package.yaml modified: src/Boilerplate.hs modified: src/Lex.hs deleted: src/Lib.hs Clean-up warnings (unused stuff). Remove unused Lib.hs * Add swap files to gitignore modified: .gitignore * Add comments, fix keyword parsing modified: src/Lex.hs Previously keyword parsing would only work if you followed the keyword with a whitespace. Now we just include all non-alphanumerir characters as ends of a keyword. * Fix escaped character parsing in strings modified: src/Lex.hs Had previously only fixed it for characters. * Add explicit exports and comments to Boilerplate.hs modified: src/Boilerplate.hs * Remove chars written as numbers modified: src/Lex.hs * Rename Boilerplate to Types modified: Lex.hs renamed: Boilerplate.hs -> Types.hs * Fix forgotten shift assigns, implement tokenToBS modified: src/Types.hs * Add generic tests, fix a couple of bugs modified: package.yaml renamed: src/Lex.hs -> src/Clex/Lex.hs renamed: src/Types.hs -> src/Clex/Types.hs modified: stack.yaml new file: test/Clex/LexSpec.hs new file: test/Clex/TypesSpec.hs modified: test/Spec.hs Added pretty basic tests that generate tokens and then lex to and from bytestrings. Fixed a bug where I didn't realise unsigned could be 0 or more instances, and not a single char. Fixed a bug where (tokenToBS Not) == "!=", instead of "!". Fixed a bug where I was parsing Numbers before Floating numbers, and as a result "0.123" would show up as [Number _, DotFloat _] instead of [LeadingDot _]. * Remove executable info from clex lib deleted: app/Main.hs modified: package.yaml * Add cToHtml tool new file: .gitignore new file: Setup.hs new file: app/Main.hs new file: package.yaml new file: sample.c new file: src/Html.hs new file: stack.yaml * Add instructions and global stack.yaml for ease of use new file: .gitignore new file: README.md new file: stack.yaml * Add a bit of docs to cToHtml new file: README.md modified: package.yaml * Add comment on comments and the CPP modified: README.md * Apply hlint, remove ChangeLog.md, add *.swp modified: .gitignore new file: cToHtml/app/.Main.hs.swp modified: cToHtml/app/Main.hs modified: cToHtml/package.yaml modified: clex/src/Clex/Lex.hs modified: clex/test/Clex/LexSpec.hs modified: clex/test/Clex/TypesSpec.hs * Don't need Lucid for lexer modified: clex/package.yaml * Fix comma typo modified: README.md * Fix typo and misinformation modified: cToHtml/package.yaml modified: clex/package.yaml
diff --git a/homework/81248/cToHtml/.gitignore b/homework/81248/cToHtml/.gitignore
new file mode 100644
index 0000000..37d0663
--- /dev/null
+++ b/homework/81248/cToHtml/.gitignore
@@ -0,0 +1,3 @@
+.stack-work/
+cToHtml.cabal
+*~ \ No newline at end of file
diff --git a/homework/81248/cToHtml/README.md b/homework/81248/cToHtml/README.md
new file mode 100644
index 0000000..d56ce26
--- /dev/null
+++ b/homework/81248/cToHtml/README.md
@@ -0,0 +1,8 @@
+# cToHtml
+
+При подаден C файл го конвертира до html файл, в който всичките литерали,
+ключови думи и оператори са оцветени в някакъв подаден цвят. Мързеше ме да оцветявам
+всичко различно, но принципно става много лесно.
+
+Не обработвам препроцесорски директиви (те не са част от езика) и коментари
+(бих предположил, че препроцесорът ги маха и тях).
diff --git a/homework/81248/cToHtml/Setup.hs b/homework/81248/cToHtml/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/homework/81248/cToHtml/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/homework/81248/cToHtml/app/.Main.hs.swp b/homework/81248/cToHtml/app/.Main.hs.swp
new file mode 100644
index 0000000..d6ddc14
--- /dev/null
+++ b/homework/81248/cToHtml/app/.Main.hs.swp
Binary files differ
diff --git a/homework/81248/cToHtml/app/Main.hs b/homework/81248/cToHtml/app/Main.hs
new file mode 100644
index 0000000..b504b82
--- /dev/null
+++ b/homework/81248/cToHtml/app/Main.hs
@@ -0,0 +1,29 @@
+module Main
+ ( main
+ ) where
+
+import qualified Data.ByteString as BS
+
+import System.Environment (getArgs)
+
+import Lucid (renderToFile)
+
+import Clex.Lex (lexC)
+import Html (cToHtml)
+
+main :: IO ()
+main = do
+ xs <- getArgs
+ if length xs /= 3
+ then putStrLn usage
+ else do
+ let [inFile, outFile, col] = xs
+ file <- BS.readFile inFile
+ renderToFile outFile $ cToHtml col $ either fail id $ lexC file
+
+usage :: String
+usage = " \nUsage: cToHtml inFile.c outFile.html <colour>\n\
+ \ Converts inFile.c to a colourised version in which all\n\
+ \ keywords, identifiers and literals are coloured in <colour>.\n\
+ \ We rely on <colour> working in a style tag.\n\
+ \ (we basically set \"color:<colour>\" as a style)\n"
diff --git a/homework/81248/cToHtml/package.yaml b/homework/81248/cToHtml/package.yaml
new file mode 100644
index 0000000..f34147c
--- /dev/null
+++ b/homework/81248/cToHtml/package.yaml
@@ -0,0 +1,34 @@
+name: cToHtml
+version: 0.0.0.1
+author: "Georgi Lyubenov"
+maintainer: "godzbanebane@gmail.com"
+
+description: Lex c using clex and then output coloured C.
+
+extra-source-files:
+- README.md
+
+dependencies:
+- base >= 4.7 && < 5
+- clex
+- lucid
+- bytestring
+- text
+
+library:
+ source-dirs: src
+
+ghc-options:
+- -Wall
+- -Werror
+
+executables:
+ cToHtml:
+ main: Main.hs
+ source-dirs: app
+ ghc-options:
+ - -threaded
+ - -rtsopts
+ - -with-rtsopts=-N
+ dependencies:
+ - cToHtml
diff --git a/homework/81248/cToHtml/sample.c b/homework/81248/cToHtml/sample.c
new file mode 100644
index 0000000..dad89ad
--- /dev/null
+++ b/homework/81248/cToHtml/sample.c
@@ -0,0 +1,95 @@
+float xs = 0.5e123213f;
+float xss = .5f;
+
+typedef struct word_struct {
+ const char* begin;
+ int length;
+} word;
+
+const word get_word(const char* begin){
+ const char* current = begin;
+ short len = 0;
+ while( (*current != ' ') && (*current != 0) && (*current != '\n') ) {
+ current++;
+ len++;
+ }
+ word res = {
+ .begin = begin,
+ .length = len
+ };
+ return res;
+}
+
+void print_op(const char symb, FILE* file_write) {
+ fprintf(file_write, "%c", symb);
+}
+
+const int single_char(char *line, int *count, FILE* file_write) {
+ char letter = *(line + *count);
+ switch(letter) {
+ case ';': print_op(';', file_write); return 1;
+ case '{': print_op('{', file_write); return 1;
+ case '}': print_op('}', file_write); return 1;
+ case ',': print_op(',', file_write); return 1;
+ case ':': print_op(':', file_write); return 1;
+ case '=': print_op('=', file_write); return 1;
+ case '(': print_op('(', file_write); return 1;
+ case ')': print_op(')', file_write); return 1;
+ case '[': print_op('[', file_write); return 1;
+ case ']': print_op(']', file_write); return 1;
+ case '.': print_op('.', file_write); return 1;
+ case '&': print_op('&', file_write); return 1;
+ case '!': print_op('!', file_write); return 1;
+ case '~': print_op('~', file_write); return 1;
+ case '-': print_op('-', file_write); return 1;
+ case '+': print_op('+', file_write); return 1;
+ case '*': print_op('*', file_write); return 1;
+ case '/': print_op('/', file_write); return 1;
+ case '%': print_op('%', file_write); return 1;
+ case '<': print_op('<', file_write); return 1;
+ case '>': print_op('>', file_write); return 1;
+ case '^': print_op('^', file_write); return 1;
+ case '|': print_op('|', file_write); return 1;
+ case '?': print_op('?', file_write); return 1;
+ default: print_op('n', file_write); return 1;
+
+ }
+}
+
+const int number(char *line, int *count, FILE* file_write){
+
+}
+
+int main (void) {
+
+ FILE *file_read;
+ FILE *file_write;
+
+ file_read = fopen("example_code.c", "r");
+ file_write = fopen("output", "w");
+
+ char *line = NULL;
+ size_t len = 0;
+ ssize_t nread;
+ printf("------------------------------\n\tWRITING\n------------------------------\n");
+ while((nread = getline(&line, &len, file_read)) != -1) {
+ int counter = 0;
+ while(*(line + counter) != 0 ) {
+ if(*(line+counter) == '\n') {
+ print_op('\n', file_write);
+ }
+ if(single_char(line, &counter, file_write)) {
+ counter++;
+ continue;
+ }
+ if(number(line, &counter, file_write)) {
+ continue;
+ }
+
+
+
+ }
+ }
+
+
+}
diff --git a/homework/81248/cToHtml/src/Html.hs b/homework/81248/cToHtml/src/Html.hs
new file mode 100644
index 0000000..49c6eb1
--- /dev/null
+++ b/homework/81248/cToHtml/src/Html.hs
@@ -0,0 +1,30 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE ExtendedDefaultRules #-}
+
+module Html
+ ( cToHtml
+ ) where
+
+import qualified Data.Text as T
+
+import Lucid
+
+import Clex.Types
+
+cToHtml :: String -> [Token] -> Html ()
+cToHtml col toks = pre_ $ foldMap (colourize col) toks
+
+colourize :: String -> Token -> Html ()
+colourize _ (Whitespace bs) = toHtmlRaw bs
+colourize _ (HexNumber bs) = toHtml bs
+colourize _ (OctNumber bs) = toHtml bs
+colourize _ (Number bs) = toHtml bs
+colourize _ (CharLit bs) = toHtml bs
+colourize _ (Scientific bs) = toHtml bs
+colourize _ (DotFloat bs) = toHtml bs
+colourize _ (LeadingDot bs) = toHtml bs
+colourize _ (StringLit bs) = toHtml bs
+colourize _ (Identifier bs) = toHtml bs
+colourize col x = span_ [style_ $ "color:" <> col'] $ toHtml $ tokenToBS x
+ where col' = T.pack col
diff --git a/homework/81248/cToHtml/stack.yaml b/homework/81248/cToHtml/stack.yaml
new file mode 100644
index 0000000..80ae9b2
--- /dev/null
+++ b/homework/81248/cToHtml/stack.yaml
@@ -0,0 +1,5 @@
+resolver: lts-12.13
+
+packages:
+- .
+- ../clex