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
110 changes: 110 additions & 0 deletions homework/81842/Lua.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* scanner for a toy JavaScript-like language */

%{
/* need this for the call to atof() below */
#include <string.h>
#include <math.h>
#define YY_NO_UNISTD_H

void functionOrReserved(char* className, char* yytext);
%}

DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9_]*
FIELDSET ,|;
COLON :|::
DOT \.|\.{2}|\.{3}
BRACKET [\(\)\[\]{}]
STRQ '([^\']|(\\\'))*'
STRDQ \"([^\"]|(\\\"))*\"
COMMENT --\[\[([^\]]|\][^\]])*\]?\]\]|-{2}.*
BOOLEAN true|false

RESERVED_KEYWORD and|break|do|else|elseif|end|for|function|goto|if|in|local|nil|not|or|repeat|return|then|until|while
NUMBER -?{DIGIT}+(\.[0-9]+)?([eE][-+]?[0-9]*)?|0x({DIGIT}|[a-fA-F])+

%%

{NUMBER} {
printf("<span class=number>%s</span>", yytext);
}

{BOOLEAN} {
printf("<span class=boolean>%s</span>", yytext);
}

{RESERVED_KEYWORD}[ ]*[\(|\{|\[] {
functionOrReserved("reserved_keyword",yytext);
}
{RESERVED_KEYWORD} {
printf( "<span class=reserved_keyword>%s</span>", yytext );
}

{ID}[ ]*[\(|\{|\[] {
functionOrReserved("function_or_table",yytext);
}
{ID} {
printf( "<span class=identifier>%s</span>", yytext );
}

{STRQ}|{STRDQ} {
printf( "<span class=string>%s</span>", yytext );
}
{COMMENT} {
printf( "<span class=comment>%s</span>", yytext );
}

"+"|"-"|"*"|"/"|"%"|"^"|"#"|"&"|"~"|"|"|"<<"|">>"|"//"|"=="|"~="|"<="|">="|"<"|">"|"=" {
printf( "<span class=operator>%s</span>", yytext );
}

{FIELDSET}|{COLON}|{DOT} {
printf( "<span class=complimentary_symbol>%s</span>", yytext );
}

{BRACKET} {
printf( "<span class=bracket>%s</span>",yytext );
}

\n printf("<br>");
[ ] printf("&nbsp;");
[\t] printf("&nbsp;&nbsp;&nbsp;&nbsp;");

. {
printf( "Unrecognized character: %s\n", yytext );
}

%%

int yywrap()
{
return 1;
}

void functionOrReserved(char* className, char* yytext) {

const int f_length = strlen(yytext);
const char last_char = yytext[f_length-1];
char* yytext_without_bracket = strdup(yytext);
yytext_without_bracket[f_length-1] = '\0';

printf( "<span class=%s>%s</span>",className, yytext_without_bracket);
printf( "<span class=bracket>%c</span>",last_char);
free(yytext_without_bracket);
}


int main(int argc, const char* argv[])
{
++argv, --argc;
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;

printf("<!DOCTYPE html>\n<html>\n\t<head> <link rel=\"stylesheet\" href=\"forest.css\"> \n\t</head>\n\t<body>\n\t");
yylex();
printf("</body>\n\t </html>");

return 0;
}
51 changes: 51 additions & 0 deletions homework/81842/colorfulHtml/forest.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
body {
font-family: monospace;
background-color: rgb(36, 42, 45);
color:white
}

span {
white-space: pre-wrap;
}

.number {
color:lawngreen
}

.identifier {
color:rgb(243, 255, 158)
}

.function_or_table {
color:rgb(235, 152, 8)
}

.reserved_keyword {
color:rgba(11, 144, 171, 0.962)
}

.operator {
color: rgb(200, 75, 40);
}

.complimentary_symbol {
color:rgb(226, 168, 147)
}

.boolean {
color:rgb(46, 113, 35)
}

.bracket {
color:rgb(106, 138, 50)
}

.string {
color:rgb(128, 126, 63)
}

.comment {
color:rgb(209, 195, 1)
}


7 changes: 7 additions & 0 deletions homework/81842/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

flex_file=$1
code_file=$2
flex $flex_file
gcc -o a.exe lex.yy.c
./a.exe $code_file > colorfulHtml/lua.html
45 changes: 45 additions & 0 deletions homework/81842/lua-examples/class-impl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- class.lua
-- Compatible with Lua 5.1 (not 5.0).
function class(base, init)
local c = {} -- a new class instance
if not init and type(base) == 'function' then
init = base
base = nil
elseif type(base) == 'table' then
-- our new class is a shallow copy of the base class!
for i,v in pairs(base) do
c[i] = v
end
c._base = base
end
-- the class will be the metatable for all its objects,
-- and they will look up their methods in it.
c.__index = c

-- expose a constructor which can be called by <classname>(<args>)
local mt = {}
mt.__call = function(class_tbl, ...)
local obj = {}
setmetatable(obj,c)
if init then
init(obj,...)
else
-- make sure that any stuff from the base class is initialized!
if base and base.init then
base.init(obj, ...)
end
end
return obj
end
c.init = init
c.is_a = function(self, klass)
local m = getmetatable(self)
while m do
if m == klass then return true end
m = m._base
end
return false
end
setmetatable(c, mt)
return c
end
58 changes: 58 additions & 0 deletions homework/81842/lua-examples/comments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--[[
LUA 5.1 compatible

Ordered Table
keys added will be also be stored in a metatable to recall the insertion oder
metakeys can be seen with for i,k in ( <this>:ipairs() or ipairs( <this>._korder ) ) do
ipairs( ) is a bit faster

variable names inside __index shouldn't be added, if so you must delete these again to access the metavariable
or change the metavariable names, except for the 'del' command. thats the reason why one cannot change its value
]]--
function newT( t )
local mt = {}
-- set methods
mt.__index = {
-- set key order table inside __index for faster lookup
_korder = {},
-- traversal of hidden values
hidden = function() return pairs( mt.__index ) end,
-- traversal of table ordered: returning index, key
ipairs = function( self ) return ipairs( self._korder ) end,
-- traversal of table
pairs = function( self ) return pairs( self ) end,
-- traversal of table ordered: returning key,value
opairs = function( self )
local i = 0
local function iter( self )
i = i + 1
local k = self._korder[i]
if k then
return k,self[k]
end
end
return iter,self
end,
-- to be able to delete entries we must write a delete function
del = function( self,key )
if self[key] then
self[key] = nil
for i,k in ipairs( self._korder ) do
if k == key then
table.remove( self._korder, i )
return
end
end
end
end,
}
-- set new index handling
mt.__newindex = function( self,k,v )
if k ~= "del" and v then
rawset( self,k,v )
table.insert( self._korder, k )
end
end
return setmetatable( t or {},mt )
end
-- CHILLCODE™
Loading