Commit fc0ae86f authored by Tracey King's avatar Tracey King 💬
Browse files

Initial commit

parents
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
\ No newline at end of file
--------------------------------------------------------------------------------
-- @class KeyFinder
-- @brief Class used for holding and parsing accepted string formats
-- @member accepted_formats[table<string>]: Table of strings containing format
-- matching
-- @desc Class used for holding and parsing accepted string formats. Can add
-- formats to the accepted_formats to parse files for specific keys
-- or format matching
local KeyFinder = {accepted_formats = {}};
function KeyFinder:new (o)
o = o or {};
setmetatable(o, self);
self.__index = self;
--Set the default formats
-- For pattern matching;
-- Note: %w is alphanumberic characters
-- Note: ^ matches beginning and $ matches the end
-- Note: ? means optional
-- Note: %s is space characters
-- Format = ABCDEABCDEABCDEABCDE
local format_no_spaces = '^%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w%w?%s$';
-- Format = ABCDE-ABCDE-ABCDE-ABCDE
-- Format = ABCDE ABCDE ABCDE ABCDE
local format_hyphen = '^%w%w%w%w%w[%-% ]%w%w%w%w%w[%-% ]%w%w%w%w%w[%-% ]%w%w%w%w%w?%s$';
table.insert(KeyFinder.accepted_formats, format_no_spaces);
table.insert(KeyFinder.accepted_formats, format_hyphen);
return o;
end
--------------------------------------------------------------------------------
-- @func addFormat
-- @brief Adds string to the accepted formats
-- @param format_string[string]: String to add to the accepted formats
-- @return void
-- @desc Function for adding more strings to the accepted_formats table
function KeyFinder:addFormat(format_string)
table.insert(KeyFinder.accepted_formats, format_string);
end
--------------------------------------------------------------------------------
-- @func checkFormat
-- @brief Checks a passed in string against the accepted formats
-- @param input_string[string]: String you want to check the format of
-- @return The input_string if it is found within the accepted formats, or nil
-- @desc Checks the input string against the table of pattern formats and
-- returns the string if the input string is the correct format or
-- returns nil if it is not
function KeyFinder:checkFormat(input_string)
for index, it_format in ipairs(KeyFinder.accepted_formats) do
if(string.match(input_string, it_format)) then
return input_string;
end
end
return nil;
end
--------------------------------------------------------------------------------
-- findKeyFromFile
-- @brief Returns the first key that matches the format from file
-- @param file_name[string]: Name of the file to parse
-- @return key_text[string]: Key parsed from file or nil
-- @desc Given a table of valid formats, the function attempts to match
-- each line with the valid formats. The function stops whenever a
-- match is reached and returns the matched key, or whenever the end
-- of the file is reached. In which case returns nil.
function KeyFinder:findKeyFromFile(file_name)
if(file_name == nil) then
file_name = "testfile.txt";
end
local file = io.open(file_name);
local found = false;
local key_text = nil;
local line = nil;
if(file == nil) then
io.write("File Not Found.");
end
line = file:read("*line");
while(not found and line ~= nil) do
if(KeyFinder:checkFormat(line, KeyFinder.accepted_formats)) then
found = true;
key_text = line;
else
line = file:read("*line");
end
end
file:close();
if(line == nil) then
return "No Key Found.";
end
io.write('Matches Format: ', key_text, '\n');
return key_text;
end
return KeyFinder;
\ No newline at end of file
# About
This code was created in 2018, but never pushed to a repo.
The purpose of this project was to get familiar with Lua. The command_interface is meant to be an embeddable terminal window that I could add to say a video game. Inspiration was drawn from the World of Warcraft chat window where you could issue commands or type in specific chat channels. The command_interface is intended to be the main program to run my misc Lua files and run them.
## Modules
## KeyFinder
The KeyFinder module is a simple program that holds a collection of valid key formats, for example a Windows 7 activation key, and parses a file for the first use that matches that key format. Lua has some interesting string parsing and this was an interesting way to practice these functions.
## Chess
The Chess module is going to be a simple game of chess programmed using Lua. At this moment, it's not finished but will be implemented whenever I have more time to devote to this.
--!! Not actually implemented yet !!--
--@TODO: Finish Implementation
local Piece = {name_ = "P1", type_ = "pawn", controlled_ = "w"};
function Piece:new (o, name, player_controlled)
o = o or {};
setmetatable(o, self);
self.__index = self;
name_ = name;
controlled_ = player_controlled;
return o;
end
function Piece:move ()
end
local Player = {color_, pieces_ = {}};
function Player:new (o, color)
o = o or {};
setmetatable(o, self);
self.__index = self;
color_ = color;
--add pieces here
return o;
end
local Board = {board_, player_one_, player_two_, turn_};
local Chess = {board_ = Board:new(),
player_one_ = Player:new("W"),
player_two_ = Player:new("B"),
turn_ = 0};
--!! Not Actually implemented yet !!--
KeyFinder = require ("KeyFinder");
function main()
running = true;
while(running) do
io.write('----------------------------------------');
io.write('----------------------------------------\n');
io.write("~::");
--note: io.read() gets a line from the user
local input;
input = io.read();
if(string.match(input, "^end$")) then
running = false;
elseif(string.match(input, '^/')) then
--It's a command, try to execute the command
if(not executeCommand(input)) then
io.write("Error.\n");
end
else
io.write("[User] Says:", input, "\n");
end
end
end
function executeCommand(input)
--@TODO: Fill in actual code
--Parse command
input = string.sub(input, 2, #input);
local command;
local commandArgs = {};
for arg in string.gmatch(input, "[^%s]+") do
table.insert(commandArgs, arg);
end
--Find command
if(string.match(commandArgs[1], "commandlist")) then
io.write("To execute commands type \"/\" followed by the command\n\n");
io.write("commandlist\n");
io.write("add [number] [number]\n");
io.write("KeyFinder [filename]\n");
io.write("end\n");
elseif(string.match(commandArgs[1], "add")) then
if(#commandArgs == 3) then
io.write(add(commandArgs[2], commandArgs[3]), "\n");
else
io.write("Invalid number of arguments.\n");
return false;
end
elseif(string.match(commandArgs[1], "KeyFinder")) then
if(#commandArgs == 1) then
findKey = KeyFinder:new();
io.write(KeyFinder.findKeyFromFile(nil), "\n");
elseif(#commandArgs == 2) then
findKey = KeyFinder:new();
io.write(KeyFinder.findKeyFromFile(commandArgs[2]), "\n");
else
io.write("Invalid number of arguments.\n");
return false;
end
else
io.write("Command not found.\n");
return false;
end
--Check type and range for arguments
--Call command
return true;
end
function add(lhs, rhs)
return lhs + rhs;
end
function sub(lhs, rhs)
return lhs - rhs;
end
function div(lhs, rhs)
return lhs / rhs;
end
function mul(lhs, rhs)
return lhs * rhs;
end
--Class to hold function definitions
--function_name; String to hold name of function
--return_type; Return data type
--arguments_type; Array containing the type of the argument list
--
--Note: Will have a function library for user made Lua functions
-- as well as a function library for executable C++ functions.
FunctionContainer = { function_name = nil,
return_type = nil,
arguments_type = {}};
function FunctionContainer:new (o)
o = o or {};
setmetatable(o, self);
self.__index = self;
return o;
end
function FunctionContainer:argumentsCheck (args)
for i, n in arguments_type do
if(typeCheck(n, arg_type)) then
if(rangeCheck()) then
--Then continue
else
return false;
end
else
return false;
end
end
return true;
end
function FunctionContainer:typeCheck (arg, arg_type)
local type_int = '^%d+$';
local type_float = '^&d+$';
local type_double = '^%d+$';
local type_char = '^.+$';
local type_bool = '^[0-1]+$';
local type_hex = '^%x+$';
local type_unsigned_int = '^%d+$';
--Checking with C/C++ formats
if(string.match(arg_type, 'int')) then
return string.match(arg, type_int);
elseif(string.match(arg_type, 'char')) then
return string.match(arg, type_char);
elseif(string.match(arg_type, 'bool')) then
return string.match(arg, type_bool);
elseif(string.match(arg_type, 'hex')) then
return string.match(arg, type_hex);
elseif(string.match(arg_type, 'unsigned_int')) then
return string.match(arg, type_unsigned_int);
elseif(string.match(arg_type, 'float')) then
return string.match(arg, type_float);
elseif(string.match(arg_type, 'double')) then
return string.match(arg, type_double);
end
end
function FunctionContainer:rangeCheck (arg, arg_type)
--@TODO Add function body
return true;
end
--Class to hold collection of Functions and access for searches
FunctionLibrary = {library = {}};
function FunctionLibrary:new (o)
o = o or {};
setmetatable(o, self);
self.__index = self;
return o;
end
function FunctionLibrary:populate ()
--Either put all function definitions in here one by one, or
-- let functions add themselves to the function library.
--@TODO Add functions to library
--table.insert(library,
--FunctionContainer:new(o){function_name = add, return_type = "int",
--arguments_type = {"int", "int"};
--
end
--Input a name and arguments
--Returns a FunctionContainer if exists
function FunctionLibrary:search (name, args)
for i, n in library do --Iterate through library of FC's
if(name == n.function_name and #args == #n.args) then
if(n:typeCheck(args)) then
return n;
end
end
end
return nil;
end
--Input a name and arguments, calls the function if found
--Returns the return of the called function
function FunctionLibrary:execute (name, args)
end
--asdf
main();
This key is too long
abaca-abaca-abaca-abacaz
This key shoudn't work
abaca abaca abaca
This is the most recent key
ABCDE-abcde-12345-Zx1Y0
This key has no spaces
abcde12345ABCDE09zXy
This is an older key
abaca-baaba-cabaa-bacab
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment