Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit7c8f2a8aa04603d5bc83441353184589640e8c41 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-08 19:45:50 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-08 19:45:50 +0200
Files7443dae4cc342e194c486aa964f3e15e18ae3265
uni-projects-7c8f2a8aa04603d5bc83441353184589640e8c41.tar
uni-projects-7c8f2a8aa04603d5bc83441353184589640e8c41.tar.gz
uni-projects-7c8f2a8aa04603d5bc83441353184589640e8c41.zip
Parentd462bad3f4ed30d2c9e4562968a0b8a2ad9e3db7 (diff)
[IPL/HW02] Copied work from HW01, added a help message to lexer.sh, added left margin to pre tag
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l b/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l
new file mode 100644
index 0000000..46447b1
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/commonlisp2html.l
@@ -0,0 +1,56 @@
+%{
+
+%}
+
+DIGIT [0-9]
+BRACKET \(|\)
+
+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.?-_])+\+
+
+%%
+
+{BRACKET} { printf("<span class=\"bracket\">%s</span>", yytext); }
+{STR} { printf("<span class=\"string\">%s</span>", yytext); }
+{NUMBERS} { printf("<span class=\"number\">%s</span>", yytext); }
+{COMMENT} { printf("<span class=\"comment\">%s</span>", yytext); }
+{PARAMETER_TYPE} { printf("<span class=\"parameter-type\">%s</span>", yytext); }
+{KEYWORD} { printf("<span class=\"keyword\">%s</span>", yytext); }
+{CONSTANTS} { printf("<span class=\"constant\">%s</span>", yytext); }
+
+{SPECIAL_FORM} { printf("<span class=\"special-operator\">%s</span>", yytext); }
+{F_ARITHMETIC}|{F_NUM_COMPARISON}|{F_TRIGONOMETRIC}|{F_TRIGONOMETRIC}|{F_OTHER_MATH}|{F_LOGICAL}|{F_LISTS}|{F_OTHER} { printf("<span class=\"function\">%s</span>", yytext); }
+{M_ARITHMETIC}|{M_LOGICAL}|{M_CONDITIONAL}|{M_DECLARE} { printf("<span class=\"macro\">%s</span>", yytext); }
+
+{GLOBAL_VAR}|{CONST_VAR} { printf("<span class=\"variable\">%s</span>", yytext); }
+
+ /* 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]+ { printf("%s", yytext); }
+
+%%
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/example.lisp b/Year 2/Implementation of Programming Languages/Homework 02/example.lisp
new file mode 100644
index 0000000..cd7a577
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/example.lisp
@@ -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))
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/highlighted.html b/Year 2/Implementation of Programming Languages/Homework 02/highlighted.html
new file mode 100644
index 0000000..6dde455
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/highlighted.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Highlighted code</title>
+ <style>
+ body {
+ font-size: 20px;
+ }
+ pre, code {
+ color: #ffd7af;
+ background-color: #1c1c1c;
+ padding-left: 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>
+<span class="bracket">(</span><span class="macro">defvar</span> <span class="variable">*NAME*</span> <span class="string">"John"</span><span class="bracket">)</span>
+<span class="bracket">(</span><span class="macro">defvar</span> <span class="variable">*AGE*</span><span class="number"> 23</span><span class="bracket">)</span>
+<span class="bracket">(</span><span class="macro">defvar</span> <span class="variable">*ABOUT*</span> <span class="string">" T NIL cond decf block"</span><span class="bracket">)</span>
+
+<span class="comment">;;; This is a stupid funciton</span>
+<span class="bracket">(</span><span class="special-operator">defun</span> adult? <span class="bracket">(</span><span class="bracket">)</span>
+ <span class="bracket">(</span><span class="function">>=</span> <span class="variable">*AGE*</span><span class="number"> 18</span><span class="bracket">)</span><span class="bracket">)</span>
+
+<span class="bracket">(</span><span class="special-operator">defun</span> will-I-graduate? <span class="bracket">(</span><span class="bracket">)</span>
+ <span class="constant">NIL</span><span class="bracket">)</span>
+
+<span class="bracket">(</span><span class="special-operator">defun</span> distance <span class="bracket">(</span>p1 p2<span class="bracket">)</span>
+ <span class="bracket">(</span><span class="function">sqrt</span> <span class="bracket">(</span><span class="function">* </span>p1 p1<span class="bracket">)</span> <span class="bracket">(</span><span class="function">* </span>p2 p2<span class="bracket">)</span><span class="bracket">)</span><span class="bracket">)</span>
+
+<span class="bracket">(</span><span class="special-operator">defun</span> bar <span class="bracket">(</span>a b<span class="bracket">)</span>
+ <span class="bracket">(</span><span class="special-operator">if</span> <span class="bracket">(</span><span class="macro">and</span> <span class="bracket">(</span><span class="function">></span> b a<span class="bracket">)</span> <span class="bracket">(</span><span class="function"><=</span><span class="number"> 10</span> b<span class="bracket">)</span><span class="bracket">)</span>
+ <span class="bracket">(</span><span class="function">format</span> <span class="constant">t</span> <span class="string">"Valid"</span><span class="bracket">)</span>
+ <span class="bracket">(</span><span class="function">format</span> <span class="constant">t</span> <span class="string">"Invalid"</span><span class="bracket">)</span><span class="bracket">)</span><span class="bracket">)</span>
+
+<span class="bracket">(</span><span class="special-operator">defun</span> foo <span class="bracket">(</span><span class="parameter-type">&key</span> <span class="bracket">(</span><span class="bracket">(</span><span class="keyword">:apple</span> a<span class="bracket">)</span><span class="bracket">)</span> <span class="bracket">(</span><span class="bracket">(</span><span class="keyword">:box</span> b<span class="bracket">)</span><span class="number"> 0</span><span class="bracket">)</span> <span class="bracket">(</span><span class="bracket">(</span><span class="keyword">:charlie</span> c<span class="bracket">)</span><span class="number"> 0</span> c-supplied-p<span class="bracket">)</span><span class="bracket">)</span>
+ <span class="bracket">(</span><span class="function">list</span> a b c c-supplied-p<span class="bracket">)</span><span class="bracket">)</span>
+ </pre></code>
+ </body>
+</html>
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/lexer.sh b/Year 2/Implementation of Programming Languages/Homework 02/lexer.sh
new file mode 100755
index 0000000..5a07c15
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/lexer.sh
@@ -0,0 +1,40 @@
+#! /bin/bash
+
+if [ "$1" = "-h" ] || [ "$1" = "-?" ] || [ "$1" = "--help" ]; then
+ cat << EOF
+lexer.sh [-h|-?|--help|-b|-r|-p] <OPTION>
+
+-h
+-?
+--help Brings up this help message
+
+-b <FILES> Puts <FILES> through flex and compiles with gcc
+ Outputed executable is "a.out"
+-r <FILES> Runs <FILES> through compiled lexer and prints results
+ Expects "a.out" as the compiled lexer (in the current folder)
+-p <FILES> Runs <FILES> through compiled lexer and creates webpage with highlighting
+ Expects "a.out" as the compiled lexer (in the current folder)
+ Webpage is always named "highlighted.html". It is created by putting lexer
+ output between "page_top.html" and "page_bottom.html"
+EOF
+ exit
+fi
+
+if [ "$1" = "-b" ]; then
+ shift
+ flex $@
+ gcc lex.yy.c -lfl
+fi
+
+if [ "$1" = "-r" ]; then
+ shift
+ ./a.out < $@
+fi
+
+if [ "$1" = "-p" ]; then
+ filename="highlighted.html"
+ cat ./page_top.html > $filename
+ shift
+ ./a.out < $@ >> $filename
+ cat ./page_bottom.html >> $filename
+fi
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/page_bottom.html b/Year 2/Implementation of Programming Languages/Homework 02/page_bottom.html
new file mode 100644
index 0000000..073ae29
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/page_bottom.html
@@ -0,0 +1,3 @@
+ </pre></code>
+ </body>
+</html>
diff --git a/Year 2/Implementation of Programming Languages/Homework 02/page_top.html b/Year 2/Implementation of Programming Languages/Homework 02/page_top.html
new file mode 100644
index 0000000..63de125
--- /dev/null
+++ b/Year 2/Implementation of Programming Languages/Homework 02/page_top.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Highlighted code</title>
+ <style>
+ body {
+ font-size: 20px;
+ }
+ pre, code {
+ color: #ffd7af;
+ background-color: #1c1c1c;
+ padding-left: 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>