diff --git a/homework/81842/Lua.l b/homework/81842/Lua.l new file mode 100644 index 00000000..9a564d35 --- /dev/null +++ b/homework/81842/Lua.l @@ -0,0 +1,110 @@ +/* scanner for a toy JavaScript-like language */ + +%{ +/* need this for the call to atof() below */ +#include +#include +#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("%s", yytext); +} + +{BOOLEAN} { + printf("%s", yytext); +} + +{RESERVED_KEYWORD}[ ]*[\(|\{|\[] { + functionOrReserved("reserved_keyword",yytext); +} +{RESERVED_KEYWORD} { + printf( "%s", yytext ); +} + +{ID}[ ]*[\(|\{|\[] { + functionOrReserved("function_or_table",yytext); +} +{ID} { + printf( "%s", yytext ); +} + +{STRQ}|{STRDQ} { + printf( "%s", yytext ); +} +{COMMENT} { + printf( "%s", yytext ); +} + +"+"|"-"|"*"|"/"|"%"|"^"|"#"|"&"|"~"|"|"|"<<"|">>"|"//"|"=="|"~="|"<="|">="|"<"|">"|"=" { + printf( "%s", yytext ); +} + +{FIELDSET}|{COLON}|{DOT} { + printf( "%s", yytext ); +} + +{BRACKET} { + printf( "%s",yytext ); +} + +\n printf("
"); +[ ] printf(" "); +[\t] printf("    "); + +. { + 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( "%s",className, yytext_without_bracket); + printf( "%c",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("\n\n\t \n\t\n\t\n\t"); + yylex(); + printf("\n\t "); + + return 0; +} diff --git a/homework/81842/colorfulHtml/forest.css b/homework/81842/colorfulHtml/forest.css new file mode 100644 index 00000000..57ad52e2 --- /dev/null +++ b/homework/81842/colorfulHtml/forest.css @@ -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) +} + + diff --git a/homework/81842/create.sh b/homework/81842/create.sh new file mode 100644 index 00000000..660e41e6 --- /dev/null +++ b/homework/81842/create.sh @@ -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 diff --git a/homework/81842/lua-examples/class-impl.lua b/homework/81842/lua-examples/class-impl.lua new file mode 100644 index 00000000..3aacb77a --- /dev/null +++ b/homework/81842/lua-examples/class-impl.lua @@ -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 () + 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 \ No newline at end of file diff --git a/homework/81842/lua-examples/comments.lua b/homework/81842/lua-examples/comments.lua new file mode 100644 index 00000000..b2e1a4e7 --- /dev/null +++ b/homework/81842/lua-examples/comments.lua @@ -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 ( :ipairs() or ipairs( ._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ā„¢ \ No newline at end of file diff --git a/homework/81842/lua-examples/dumper.lua b/homework/81842/lua-examples/dumper.lua new file mode 100644 index 00000000..9c00d35c --- /dev/null +++ b/homework/81842/lua-examples/dumper.lua @@ -0,0 +1,230 @@ + --[[ DataDumper.lua + Copyright (c) 2007 Olivetti-Engineering SA + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + ]] + + local dumplua_closure = [[ + local closures = {} + local function closure(t) + closures[#closures+1] = t + t[1] = assert(loadstring(t[1])) + return t[1] + end + + for _,t in pairs(closures) do + for i = 2,#t do + debug.setupvalue(t[1], i-1, t[i]) + end + end + ]] + + local lua_reserved_keywords = { + 'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', + 'function', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', + 'return', 'then', 'true', 'until', 'while' } + + local function keys(t) + local res = {} + local oktypes = { stringstring = true, numbernumber = true } + local function cmpfct(a,b) + if oktypes[type(a)..type(b)] then + return a < b + else + return type(a) < type(b) + end + end + for k in pairs(t) do + res[#res+1] = k + end + table.sort(res, cmpfct) + return res + end + + local c_functions = {} + for _,lib in pairs{'_G', 'string', 'table', 'math', + 'io', 'os', 'coroutine', 'package', 'debug'} do + local t = _G[lib] or {} + lib = lib .. "." + if lib == "_G." then lib = "" end + for k,v in pairs(t) do + if type(v) == 'function' and not pcall(string.dump, v) then + c_functions[v] = lib..k + end + end + end + + function DataDumper(value, varname, fastmode, ident) + local defined, dumplua = {} + -- Local variables for speed optimization + local string_format, type, string_dump, string_rep = + string.format, type, string.dump, string.rep + local tostring, pairs, table_concat = + tostring, pairs, table.concat + local keycache, strvalcache, out, closure_cnt = {}, {}, {}, 0 + setmetatable(strvalcache, {__index = function(t,value) + local res = string_format('%q', value) + t[value] = res + return res + end}) + local fcts = { + string = function(value) return strvalcache[value] end, + number = function(value) return value end, + boolean = function(value) return tostring(value) end, + ['nil'] = function(value) return 'nil' end, + ['function'] = function(value) + return string_format("loadstring(%q)", string_dump(value)) + end, + userdata = function() error("Cannot dump userdata") end, + thread = function() error("Cannot dump threads") end, + } + local function test_defined(value, path) + if defined[value] then + if path:match("^getmetatable.*%)$") then + out[#out+1] = string_format("s%s, %s)\n", path:sub(2,-2), defined[value]) + else + out[#out+1] = path .. " = " .. defined[value] .. "\n" + end + return true + end + defined[value] = path + end + local function make_key(t, key) + local s + if type(key) == 'string' and key:match('^[_%a][_%w]*$') then + s = key .. "=" + else + s = "[" .. dumplua(key, 0) .. "]=" + end + t[key] = s + return s + end + for _,k in ipairs(lua_reserved_keywords) do + keycache[k] = '["'..k..'"] = ' + end + if fastmode then + fcts.table = function (value) + -- Table value + local numidx = 1 + out[#out+1] = "{" + for key,val in pairs(value) do + if key == numidx then + numidx = numidx + 1 + else + out[#out+1] = keycache[key] + end + local str = dumplua(val) + out[#out+1] = str.."," + end + if string.sub(out[#out], -1) == "," then + out[#out] = string.sub(out[#out], 1, -2); + end + out[#out+1] = "}" + return "" + end + else + fcts.table = function (value, ident, path) + if test_defined(value, path) then return "nil" end + -- Table value + local sep, str, numidx, totallen = " ", {}, 1, 0 + local meta, metastr = (debug or getfenv()).getmetatable(value) + if meta then + ident = ident + 1 + metastr = dumplua(meta, ident, "getmetatable("..path..")") + totallen = totallen + #metastr + 16 + end + for _,key in pairs(keys(value)) do + local val = value[key] + local s = "" + local subpath = path or "" + if key == numidx then + subpath = subpath .. "[" .. numidx .. "]" + numidx = numidx + 1 + else + s = keycache[key] + if not s:match "^%[" then subpath = subpath .. "." end + subpath = subpath .. s:gsub("%s*=%s*$","") + end + s = s .. dumplua(val, ident+1, subpath) + str[#str+1] = s + totallen = totallen + #s + 2 + end + if totallen > 80 then + sep = "\n" .. string_rep(" ", ident+1) + end + str = "{"..sep..table_concat(str, ","..sep).." "..sep:sub(1,-3).."}" + if meta then + sep = sep:sub(1,-3) + return "setmetatable("..sep..str..","..sep..metastr..sep:sub(1,-3)..")" + end + return str + end + fcts['function'] = function (value, ident, path) + if test_defined(value, path) then return "nil" end + if c_functions[value] then + return c_functions[value] + elseif debug == nil or debug.getupvalue(value, 1) == nil then + return string_format("loadstring(%q)", string_dump(value)) + end + closure_cnt = closure_cnt + 1 + local res = {string.dump(value)} + for i = 1,math.huge do + local name, v = debug.getupvalue(value,i) + if name == nil then break end + res[i+1] = v + end + return "closure " .. dumplua(res, ident, "closures["..closure_cnt.."]") + end + end + function dumplua(value, ident, path) + return fcts[type(value)](value, ident, path) + end + if varname == nil then + varname = "return " + elseif varname:match("^[%a_][%w_]*$") then + varname = varname .. " = " + end + if fastmode then + setmetatable(keycache, {__index = make_key }) + out[1] = varname + table.insert(out,dumplua(value, 0)) + return table.concat(out) + else + setmetatable(keycache, {__index = make_key }) + local items = {} + for i=1,10 do items[i] = '' end + items[3] = dumplua(value, ident or 0, "t") + if closure_cnt > 0 then + items[1], items[6] = dumplua_closure:match("(.*\n)\n(.*)") + out[#out+1] = "" + end + if #out > 0 then + items[2], items[4] = "local t = ", "\n" + items[5] = table.concat(out) + items[7] = varname .. "t" + else + items[2] = varname + end + return table.concat(items) + end + end + diff --git a/homework/81842/lua-examples/factorial.lua b/homework/81842/lua-examples/factorial.lua new file mode 100644 index 00000000..baa27d81 --- /dev/null +++ b/homework/81842/lua-examples/factorial.lua @@ -0,0 +1,12 @@ +-- defines a factorial function ---sh create.sh Lua.l factorial.lua +function fact (n) + if n == 0 then + return 1 + else + return n * fact(n-1) + end + end + + print("enter a number:") + a = io.read("*number") -- read a number + print(fact(a)) \ No newline at end of file diff --git a/homework/81842/lua-examples/numbers.lua b/homework/81842/lua-examples/numbers.lua new file mode 100644 index 00000000..f47ad7d8 --- /dev/null +++ b/homework/81842/lua-examples/numbers.lua @@ -0,0 +1,7 @@ +543.21E8 +3.1415927 +0.86602540378444 +5e+20 +0x1F4 +0xFfa3 + diff --git a/homework/81842/lua-examples/quick_sort.lua b/homework/81842/lua-examples/quick_sort.lua new file mode 100644 index 00000000..87aa9c57 --- /dev/null +++ b/homework/81842/lua-examples/quick_sort.lua @@ -0,0 +1,33 @@ +-- Ensure that that the element at i is in the right position, +-- and return a closure which can be used for continuing the sort. +function quicksorter(i, vec, low, high) + if low >= high then + return quicksorter + else -- low < high + -- partition the vector and initialize the child closures + local middle = partition(vec, low, high) + local left, right = quicksorter + + -- Create the promise + local function self(i, vec, low, high) + if i < middle then + left = left(i, vec, low, middle-1) + return self + elseif i > middle then + right = right(i, vec, middle+1, high) + return self + end + end + + -- Force the promise until i is in the right position + return self(i, vec, low, high) + end + end + + function lazysort(vec, low, high) + local sorter = quicksorter + return function(i) + sorter = sorter(i, vec, low, high) + return vec[i] + end + end \ No newline at end of file diff --git a/homework/81842/lua-examples/words.lua b/homework/81842/lua-examples/words.lua new file mode 100644 index 00000000..758c5cc2 --- /dev/null +++ b/homework/81842/lua-examples/words.lua @@ -0,0 +1,19 @@ +true +false +function +function(a) +function(a) { + a = "string"; +} +notReserved(a) { + a = "string"; +} +"string" +'string' +nil += | == +Uppercase = {} +--[[random things here to say]] +-- to do is not to do +a = {1,2,3,4} +a[1] = "dumb" \ No newline at end of file