From 0048c92cf51b720994348f4585c61428a30071cc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 6 Aug 2022 06:16:14 +0100 Subject: [PATCH] Adjusted some includes --- source/common.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ source/common.h | 20 +++++++++++- source/debug.c | 82 ---------------------------------------------- source/debug.h | 19 ----------- source/lexer.c | 2 -- source/literal.c | 1 - source/parser.c | 1 - source/token.h | 0 8 files changed, 103 insertions(+), 106 deletions(-) create mode 100644 source/common.c delete mode 100644 source/token.h diff --git a/source/common.c b/source/common.c new file mode 100644 index 0000000..e669b1c --- /dev/null +++ b/source/common.c @@ -0,0 +1,84 @@ +#include "common.h" + +#include +#include + +//declare the singleton +Command command; + +void initCommand(int argc, const char* argv[]) { + //default values + command.error = false; + command.help = false; + command.version = false; + command.filename = NULL; + command.source = NULL; + command.verbose = false; + command.optimize = 1; + + for (int i = 1; i < argc; i++) { //start at 1 to skip the program name + if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + command.help = true; + continue; + } + + if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { + command.version = true; + continue; + } + + if ((!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")) && i + 1 < argc) { + command.filename = (char*)argv[i + 1]; + i++; + continue; + } + + if ((!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input")) && i + 1 < argc) { + command.source = (char*)argv[i + 1]; + i++; + continue; + } + + if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + command.verbose = true; + continue; + } + + if (!strncmp(argv[i], "-O", 2)) { + sscanf(argv[i], "-O%d", &command.optimize); + continue; + } + + command.error = true; + } + + //no arguments + if (argc == 1) { + command.error = true; + } +} + +void usageCommand(int argc, const char* argv[]) { + printf("Usage: %s [-h | -v | [-OX][-d][-f filename | -i source]]\n\n", argv[0]); +} + +void helpCommand(int argc, const char* argv[]) { + usageCommand(argc, argv); + + printf("-h | --help\t\tShow this help then exit.\n"); + printf("-v | --version\t\tShow version and copyright information then exit.\n"); + printf("-f | --file filename\tParse and execute the source file.\n"); + printf("-i | --input source\tParse and execute this given string of source code.\n"); + printf("-d | --debug\t\tBe verbose when operating.\n"); + printf("-OX\t\t\tUse level X optimization (default 1)\n"); +} + +void copyrightCommand(int argc, const char* argv[]) { + printf("Toy Programming Language Interpreter Version %d.%d.%d (built on %s)\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD); + printf("Copyright (c) 2020-2022 Kayne Ruse, KR Game Studios\n\n"); + printf("This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.\n\n"); + printf("Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:\n\n"); + printf("1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\n\n"); + printf("2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n"); + printf("3. This notice may not be removed or altered from any source distribution.\n\n"); +} \ No newline at end of file diff --git a/source/common.h b/source/common.h index e7dbf3d..c416fd3 100644 --- a/source/common.h +++ b/source/common.h @@ -7,5 +7,23 @@ #define TOY_VERSION_MAJOR 0 #define TOY_VERSION_MINOR 6 #define TOY_VERSION_PATCH 0 -#define TOY_VERSION_BUILD __DATE__ ";" __TIME__ +#define TOY_VERSION_BUILD __DATE__ " " __TIME__ +//for processing the command line arguments +typedef struct { + bool error; + bool help; + bool version; + char* filename; + char* source; + bool verbose; + int optimize; +} Command; + +extern Command command; + +void initCommand(int argc, const char* argv[]); + +void usageCommand(int argc, const char* argv[]); +void helpCommand(int argc, const char* argv[]); +void copyrightCommand(int argc, const char* argv[]); \ No newline at end of file diff --git a/source/debug.c b/source/debug.c index 5f01c67..f07c8b0 100644 --- a/source/debug.c +++ b/source/debug.c @@ -6,88 +6,6 @@ #include "compiler.h" #include -#include -#include - -//declare the singleton -Command command; - -void initCommand(int argc, const char* argv[]) { - //default values - command.error = false; - command.help = false; - command.version = false; - command.filename = NULL; - command.source = NULL; - command.verbose = false; - command.optimize = 1; - - for (int i = 1; i < argc; i++) { //start at 1 to skip the program name - if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - command.help = true; - continue; - } - - if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { - command.version = true; - continue; - } - - if ((!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")) && i + 1 < argc) { - command.filename = (char*)argv[i + 1]; - i++; - continue; - } - - if ((!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input")) && i + 1 < argc) { - command.source = (char*)argv[i + 1]; - i++; - continue; - } - - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { - command.verbose = true; - continue; - } - - if (!strncmp(argv[i], "-O", 2)) { - sscanf(argv[i], "-O%d", &command.optimize); - continue; - } - - command.error = true; - } - - //no arguments - if (argc == 1) { - command.error = true; - } -} - -void usageCommand(int argc, const char* argv[]) { - printf("Usage: %s [-h | -v | [-OX][-d][-f filename | -i source]]\n\n", argv[0]); -} - -void helpCommand(int argc, const char* argv[]) { - usageCommand(argc, argv); - - printf("-h | --help\t\tShow this help then exit.\n"); - printf("-v | --version\t\tShow version and copyright information then exit.\n"); - printf("-f | --file filename\tParse and execute the source file.\n"); - printf("-i | --input source\tParse and execute this given string of source code.\n"); - printf("-d | --debug\t\tBe verbose when operating.\n"); - printf("-OX\t\t\tUse level X optimization (default 1)\n"); -} - -void copyrightCommand(int argc, const char* argv[]) { - printf("Toy Programming Language Interpreter Version %d.%d.%d (built on %s)\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD); - printf("Copyright (c) 2020-2022 Kayne Ruse, KR Game Studios\n\n"); - printf("This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.\n\n"); - printf("Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:\n\n"); - printf("1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\n\n"); - printf("2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n\n"); - printf("3. This notice may not be removed or altered from any source distribution.\n\n"); -} //utils static unsigned char printByte(const char* tb, int* count) { diff --git a/source/debug.h b/source/debug.h index d45b8de..a797554 100644 --- a/source/debug.h +++ b/source/debug.h @@ -2,23 +2,4 @@ #include "common.h" -//for processing the command line arguments -typedef struct { - bool error; - bool help; - bool version; - char* filename; - char* source; - bool verbose; - int optimize; -} Command; - -extern Command command; - -void initCommand(int argc, const char* argv[]); - -void usageCommand(int argc, const char* argv[]); -void helpCommand(int argc, const char* argv[]); -void copyrightCommand(int argc, const char* argv[]); - void dissectBytecode(const char* tb, int size); \ No newline at end of file diff --git a/source/lexer.c b/source/lexer.c index 267a41e..fc46508 100644 --- a/source/lexer.c +++ b/source/lexer.c @@ -1,8 +1,6 @@ #include "lexer.h" #include "keyword_types.h" -#include "debug.h" - #include #include diff --git a/source/literal.c b/source/literal.c index 24c59af..208bdc1 100644 --- a/source/literal.c +++ b/source/literal.c @@ -1,6 +1,5 @@ #include "literal.h" #include "memory.h" -#include "debug.h" #include #include diff --git a/source/parser.c b/source/parser.c index c02d494..0e6bbf4 100644 --- a/source/parser.c +++ b/source/parser.c @@ -1,6 +1,5 @@ #include "parser.h" -#include "debug.h" #include "common.h" #include "memory.h" diff --git a/source/token.h b/source/token.h deleted file mode 100644 index e69de29..0000000