Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions homework/0MI0800028/Homework 02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
in="example.lisp"
out="highlighted.html"

CC=gcc
CFLAGS=-lfl -g
LEX=flex
YACC=bison -t -d

.PHONY: all
all: build-all webpage

.PHONY: webpage
webpage:
cat ./page_top.html > $(out)
./a.out < $(in) >> $(out)
cat ./page_bottom.html >> $(out)

build-all: build-bison build-flex
$(CC) lex.yy.c commonlisp2html.tab.c $(CFLAGS)

.PHONY: build-flex
build-flex: commonlisp2html.l
$(LEX) commonlisp2html.l

.PHONY: build-bison
build-bison: commonlisp2html.y
$(YACC) commonlisp2html.y

.PHONY: clean
clean:
rm -f a.out *.tab.* lex.yy.c
61 changes: 61 additions & 0 deletions homework/0MI0800028/Homework 02/commonlisp2html.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
%{
#include "commonlisp2html.tab.h"
%}

DIGIT [0-9]
LEFTBRACKET \(
RIGHTBRACKET \)

STR \"([^\"]|\\\")*([^\\]|\\\\)\"
NUMBERS [^A-z](\+|-)?{DIGIT}+(\.{DIGIT}+(e{DIGIT}+|d{DIGIT}+|e-{DIGIT}+)?|\/{DIGIT}+)?

COMMENT ;.*$
PARAMETER_TYPE &[0-9A-z]+
KEYWORD :[0-9A-z]+
CONSTANTS T|NIL|pi|t

SPECIAL_FORM block|function|if|let|quote|return-from|defun|demacro

/* Functions */
F_ARITHMETIC "+ "|"- "|"* "|\/|1\+|1-|conjugate|gcd|lcm
F_NUM_COMPARISON =|\/=|<|>|<=|>=|max|min
F_TRIGONOMETRIC sin|cos|tan|cis|asin|acos|atan|sinh|cosh|tanh|asinh|acosh|atanh
F_OTHER_MATH abs|sqrt|phase|signum
F_LOGICAL not
F_LISTS list
F_OTHER format

/* Macros */
M_ARITHMETIC incf|decf
M_LOGICAL and|or
M_CONDITIONAL when|unless|cond|case|typecase
M_DECLARE defvar|defparameter|defconstant|defun

/* Not in spec, but commonly followed */
GLOBAL_VAR \*([0-9A-z.?-_])+\*
CONST_VAR \+([0-9A-z.?-_])+\+

%%

{LEFTBRACKET} { return LBRACKET; }
{RIGHTBRACKET} { return RBRACKET; }
{STR} { yylval = strdup(yytext); return STR; }
{NUMBERS} { yylval = strdup(yytext); return NUM; }
{COMMENT} { yylval = strdup(yytext); return COMMENT;}
{PARAMETER_TYPE} { yylval = strdup(yytext); return PARAMETER_TYPE; }
{KEYWORD} { yylval = strdup(yytext); return KEYWORD; }
{CONSTANTS} { yylval = strdup(yytext); return CONSTANT; }

{SPECIAL_FORM} { yylval = strdup(yytext); return SPECIAL_FORM; }
{F_ARITHMETIC}|{F_NUM_COMPARISON}|{F_TRIGONOMETRIC}|{F_TRIGONOMETRIC}|{F_OTHER_MATH}|{F_LOGICAL}|{F_LISTS}|{F_OTHER} { yylval = strdup(yytext); return FUNCTION; }
{M_ARITHMETIC}|{M_LOGICAL}|{M_CONDITIONAL}|{M_DECLARE} { yylval = strdup(yytext); return MACRO; }

{GLOBAL_VAR}|{CONST_VAR} { yylval = strdup(yytext); return VARIABLE; }

\n { return NEWLINE; }

/* flex catches the longest possibility, so if some variable name contains a known word (function, macro, etc.), this will prevent it from highlighting */
[A-z!-'*-@]+|" "|\t { yylval = strdup(yytext); return OTHER; }


%%
108 changes: 108 additions & 0 deletions homework/0MI0800028/Homework 02/commonlisp2html.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
%{
#include <stdio.h>
#include <string.h>

int yylex();
int yyerror(char *s);

#define LBRACKET_SPAN "<span class=\"bracket\">(</span>"
#define RBRACKET_SPAN "<span class=\"bracket\">)</span>"

char* span(char *c, char *s);
char* details(char *summary, char *rest);
char* concat2(char *first, char *second);
char* concatperm2(char *first, char *second);
char* concatperm3(char *first, char *second, char *third);
%}

%token LBRACKET RBRACKET OTHER
%token NEWLINE SPACE TAB
%token STR NUM COMMENT PARAMETER_TYPE KEYWORD CONSTANT
%token SPECIAL_FORM FUNCTION MACRO VARIABLE

%define api.value.type {char*}

%%

start:
| start stlist { printf("%s", $2); free($2); }
| start terminal { printf("%s", $2); free($2); }
| start NEWLINE { printf("\n"); }

stlist: LBRACKET RBRACKET { $$ = concat2(LBRACKET_SPAN, RBRACKET_SPAN); }
| LBRACKET list RBRACKET { $$ = concatperm3(LBRACKET_SPAN, $2, RBRACKET_SPAN); }
| LBRACKET NEWLINE list RBRACKET { $$ = details(concat2(LBRACKET_SPAN, ""), $3); }
| LBRACKET list NEWLINE postlist RBRACKET { $$ = details($2, $4); }
;

postlist: { $$ = ""; }
| postlist stlist { $$ = concat2($1, $2); free($2); }
| postlist terminal { $$ = concat2($1, $2); free($2); }
| postlist NEWLINE { $$ = concat2($1, "\n"); }
;

list: terminal
| list terminal { $$ = concatperm2($1, $2); }
| stlist
| list stlist { $$ = concatperm2($1, $2); }
;

terminal: STR { $$ = span("string", $1); }
| NUM { $$ = span("number", $1); }
| COMMENT { $$ = span("comment", $1); }
| PARAMETER_TYPE { $$ = span("parameter-type", $1); }
| KEYWORD { $$ = span("keyword", $1); }
| CONSTANT { $$ = span("constant", $1); }
| SPECIAL_FORM { $$ = span("special-operator", $1); }
| FUNCTION { $$ = span("function", $1); }
| MACRO { $$ = span("macro", $1); }
| VARIABLE { $$ = span("variable", $1); }
| OTHER { $$ = span("", $1); }
;

%%


char* span(char *c, char *s) {
char* str = (char*)malloc(strlen(c) + strlen(s) + 1 + 22);
sprintf(str, "<span class=\"%s\">%s</span>", c, s);
free(s);
return str;
}
char* details(char *summary, char *rest) {
if (*rest == '\0') rest = concat2("", ""); // Зверски грозен фикс, но не е толкова голям проблем

char* str = (char*)malloc(strlen(LBRACKET_SPAN) + strlen(summary) + strlen(rest) + strlen(RBRACKET_SPAN) + 1 + 39);
sprintf(str, "<details><summary>%s%s</summary>\n%s%s</details>", LBRACKET_SPAN, summary, rest, RBRACKET_SPAN);

free(summary); free(rest);
return str;
}

char* concat2(char* first, char* second) {
char* concat = (char*)malloc(strlen(first) + strlen(second) + 1);
sprintf(concat, "%s%s", first, second);
return concat;
}
char* concatperm2(char *first, char *second) {
char* concat = concat2(first, second);
free(first); free(second);
return concat;
}

char* concatperm3(char *first, char *second, char *third) {
char* concat = (char*)malloc(strlen(first) + strlen(second) + strlen(third) + 1);
sprintf(concat, "%s%s%s", first, second, third);
free(second);
Comment on lines +94 to +96

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So manual memory management ...

return concat;
}


int yyerror(char *s) {
printf("Error: %s\n", s);
return 0;
}

int main(int argc, char **argv) {
yyparse();
}
21 changes: 21 additions & 0 deletions homework/0MI0800028/Homework 02/example.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(defvar *NAME* "John")
(defvar *AGE* 23)
(defvar *ABOUT* " T NIL cond decf block")

;;; This is a stupid funciton
(defun adult? ()
(>= *AGE* 18))

(defun will-I-graduate? ()
NIL)

(defun distance (p1 p2)
(sqrt (* p1 p1) (* p2 p2)))

(defun bar (a b)
(if (and (> b a) (<= 10 b))
(format t "Valid")
(format t "Invalid")))

(defun foo (&key ((:apple a)) ((:box b) 0) ((:charlie c) 0 c-supplied-p))
(list a b c c-supplied-p))
3 changes: 3 additions & 0 deletions homework/0MI0800028/Homework 02/page_bottom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
</pre></code>
</body>
</html>
59 changes: 59 additions & 0 deletions homework/0MI0800028/Homework 02/page_top.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<title>Highlighted code</title>
<style>
body {
font-size: 20px;
}
pre, code {
color: #ffd7af;
background-color: #1c1c1c;
padding-left: 1.5em;
}
code summary {
margin-left: -1.05em;
}
code details {
display: inline-block;
}
code details[open], code details > summary {
margin-bottom: -1em;
}
.bracket {
color: #d47408;
}
.string {
color: #afaf00;
}
.number {
color: #d787af;
}
.comment {
color: #8a8a8a;
}
.parameter-type {
color: #87afaf;
}
.keyword {
color: #87af87;
}
.constant {
color: #9045ff;
}
.special-operator {
color: #5f8fd7;
}
.function {
color: #d75f5f;
}
.macro {
color: #73d75f;
}
.variable {
color: #ffaf00;
}
</style>
</head>
<body>
<pre><code>