Skip to content

Commit

Permalink
Merge pull request #117 from dekrain/args-clean
Browse files Browse the repository at this point in the history
Improve handling of arguments
  • Loading branch information
adamhutchings authored Oct 28, 2022
2 parents 904c95a + fd8b340 commit 53cdf99
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 111 deletions.
188 changes: 98 additions & 90 deletions src/driver/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ static void yf_check_action(struct yf_args * args, enum yf_info_output out) {
* Internal - add a file. Return 1 if too many files.
*/
static int yf_add_file(struct yf_args * args, char * file) {
if (args->num_files >= 16) {
yf_set_error(args);
return 1;
}
/* No actions allowed. e.g. : no "yfc --version foo.yf" */
if (args->wanted_output != YF_NONE) {
yf_set_error(args);
Expand All @@ -52,7 +48,7 @@ static int yf_add_file(struct yf_args * args, char * file) {
yf_set_error(args);
return 1;
}
args->files[args->num_files++] = file;
yf_list_add(&args->files, file);
return 0;
}

Expand All @@ -65,13 +61,11 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
bool want_compiler_name = false;
bool want_compiler_type = false;

bool has_input_file = false;

/* Zero the args structure. */
memset(args, 0, sizeof *args);
args->wanted_output = YF_NONE;

args->run_c_comp = true;
yf_list_init(&args->files);

/* Start at 1 - avoid program name */
for (i = 1; i < argc; ++i) {
Expand Down Expand Up @@ -112,105 +106,121 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
continue;
}

if (STREQ(arg, "-h") || STREQ(arg, "--help")) {
yf_check_action(args, YF_HELP);
continue;
}
if (arg[0] == '-') {
++arg;
if (!arg[1]) {
if (arg[0] == 'h' || arg[0] == '?') {
yf_check_action(args, YF_HELP);
continue;
}

if (STREQ(arg, "-v") || STREQ(arg, "--version")) {
yf_check_action(args, YF_VERSION);
continue;
}
if (arg[0] == 'v') {
yf_check_action(args, YF_VERSION);
continue;
}
}

if (STREQ(arg, "-native-compiler")) {
want_compiler_name = true;
/* Make sure there actually is a compiler to parse after */
if (i + 1 == argc) {
yf_set_error(args);
return;
if (arg[0] == '-')
++arg;

if (STREQ(arg, "help")) {
yf_check_action(args, YF_HELP);
continue;
}
continue;
}

if (STREQ(arg, "-compiler-type")) {
want_compiler_type = true;
if (i + 1 == argc) {
yf_set_error(args);
return;
if (STREQ(arg, "version")) {
yf_check_action(args, YF_VERSION);
continue;
}
continue;
}

if (STREQ(arg, "--project")) {
args->project = 1;
if (args->num_files) {
/* --project specifies which files to use. */
yf_set_error(args);
return;
if (STREQ(arg, "native-compiler")) {
want_compiler_name = true;
/* Make sure there actually is a compiler to parse after */
if (i + 1 == argc) {
yf_set_error(args);
return;
}
continue;
}
continue;
}

if (STREQ(arg, "--just-gen")) {
args->run_c_comp = false;
continue;
}
if (STREQ(arg, "compiler-type")) {
want_compiler_type = true;
if (i + 1 == argc) {
yf_set_error(args);
return;
}
continue;
}

if (STREQ(arg, "--dump-tokens")) {
if (args->cstdump || args->just_semantics) {
yf_set_error(args);
return;
if (STREQ(arg, "project")) {
args->project = 1;
if (!yf_list_is_empty(&args->files)) {
/* --project specifies which files to use. */
yf_set_error(args);
return;
}
continue;
}
args->tdump = 1;
continue;
}

if (STREQ(arg, "--dump-cst")) {
if (args->tdump || args->just_semantics) {
yf_set_error(args);
return;
if (STREQ(arg, "just-gen")) {
args->run_c_comp = false;
continue;
}
args->cstdump = 1;
continue;
}

if (STREQ(arg, "--just-semantics")) {
if (args->tdump || args->cstdump) {
yf_set_error(args);
return;
if (STREQ(arg, "dump-tokens")) {
if (args->cstdump || args->just_semantics) {
yf_set_error(args);
return;
}
args->tdump = 1;
continue;
}
args->just_semantics = 1;
continue;
}

if (STREQ(arg, "--benchmark")) {
if (args->profile || args->wanted_output != YF_NONE) {
yf_set_error(args);
return;
if (STREQ(arg, "dump-cst")) {
if (args->tdump || args->just_semantics) {
yf_set_error(args);
return;
}
args->cstdump = 1;
continue;
}
args->profile = 1;
continue;
}

if (STREQ(arg, "--dump-projfiles")) {
args->dump_projfiles = 1;
args->project = 1;
continue;
}
if (STREQ(arg, "just-semantics")) {
if (args->tdump || args->cstdump) {
yf_set_error(args);
return;
}
args->just_semantics = 1;
continue;
}

if (STREQ(arg, "--dump-commands")) {
args->dump_commands = 1;
continue;
}
if (STREQ(arg, "benchmark")) {
if (args->profile || args->wanted_output != YF_NONE) {
yf_set_error(args);
return;
}
args->profile = 1;
continue;
}

if (STREQ(arg, "--simulate-run")) {
args->simulate_run = 1;
args->dump_commands = 1;
continue;
}
if (STREQ(arg, "dump-projfiles")) {
args->dump_projfiles = 1;
args->project = 1;
continue;
}

/* No other options are known. Yet. */
if (arg[0] == '-') {
if (STREQ(arg, "dump-commands")) {
args->dump_commands = 1;
continue;
}

if (STREQ(arg, "simulate-run")) {
args->simulate_run = 1;
args->dump_commands = 1;
continue;
}

/* No other options are known. Yet. */
yf_set_error(args);
return;
}
Expand All @@ -221,11 +231,9 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
return;
}

has_input_file = true;

}

if (args->wanted_output == YF_NONE && (!has_input_file && !(args->project))) {
if (args->wanted_output == YF_NONE && !args->project && yf_list_is_empty(&args->files)) {
args->error = 1;
args->wanted_output = YF_ERROR_NO_ARGS;
}
Expand Down
8 changes: 5 additions & 3 deletions src/driver/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <stdbool.h>

#include <util/list.h>

/**
* Any of the possible outputs wanted.
*/
Expand Down Expand Up @@ -51,10 +53,10 @@ struct yf_args {
enum yf_compiler_class compiler_class;

/**
* The indiviidual files to compile, as well as how many files.
* The indiviidual files to compile.
* @item_type char *
*/
char * files[16];
int num_files;
struct yf_list files;

/**
* Project flag, for if we're doing project setup and compiling all files in
Expand Down
8 changes: 4 additions & 4 deletions src/driver/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ int yf_run_compiler(struct yf_args * args) {

yf_cleanup(&compilation);
yf_free((void *)args->selected_compiler);
yf_list_destroy(&args->files, false);

return res;

Expand Down Expand Up @@ -372,7 +373,6 @@ static int yf_compile_project(struct yf_args * args, struct yf_compilation_data
);
}
}
return 0;
}

return yf_create_compiler_jobs(compilation, &data, args);
Expand All @@ -383,17 +383,17 @@ static int yf_compile_files(struct yf_args * args, struct yf_compilation_data *

struct yf_project_compilation_data data;
struct yf_compilation_unit_info * fdata;
int i;
char * fname;

/* No project name */
data.project_name = NULL;

yfh_init(&data.files);

for (i = 0; i < args->num_files; ++i) {
YF_LIST_FOREACH(args->files, fname) {
fdata = malloc(sizeof(struct yf_compilation_unit_info));
memset(fdata, 0, sizeof (struct yf_compilation_unit_info));
fdata->file_name = yf_strdup(args->files[i]);
fdata->file_name = yf_strdup(fname);
fdata->parse_anew = 1;
/* TODO - more data */
yfh_set(&data.files, fdata->file_name, fdata);
Expand Down
27 changes: 13 additions & 14 deletions src/driver/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

const char
* VERSION_MSG = "Version: 0.0.1\n",
* USAGE_MSG = "Flags:\n"
"-h, --help: Display this message.\n"
* USAGE_MSG =
"yfc [options...] <file1> <file2> ...: compile and link the given files.\n"
"Flags (long options can be specified with prefix - or --):\n"
"-h, -?, --help: Display this message.\n"
"-v, --version: Display version.\n"
"-native-compiler <compiler>: specify the native C compiler to use.\n"
"-compiler-type gcc|msvc: specify the class of a native C compiler. (default: gcc)\n"
"yfc <file1> <file2> ...: compile and link the given files, up to 16.\n"
"--project: Compile all files in src/ that need to be compiled. "
"Read documentation for more specifics on this flag.\n"
"--dump-tokens: Test lexer by printing out all tokens.\n"
"--dump-cst: Test parser by printing out the CST.\n"
"--just-semantics: Only verify the code, do not generate it.\n"
"--native-compiler <compiler>: specify the native C compiler to use.\n"
"--compiler-type gcc|msvc: specify the flavor of the native C compiler. (default: gcc)\n"
"--project: Compile project. Read documentation for more specifics on this flag.\n"
"--dump-tokens: Print out all tokens and exit.\n"
"--dump-cst: Print out the CST and exit.\n"
"--just-semantics: Only verify the program, do not run generation.\n"
"--just-gen: Generate the code but don't compile the C.\n"
"--benchmark: Print out time taken for each step.\n"
"--dump-projfiles: Print out all files in a project.\n"
"--dump-commands: Show all compiler invocations.\n"
"--simulate-run: Like --dump-commands, show all compiler invocations, but don't actually execute any of them.\n"
"--dump-commands: Print all compiler invocations.\n"
"--simulate-run: Like --dump-commands, print all compiler invocations, but don't actually do anything.\n"
,
* HELP_HINT_MSG = "Invalid command. "
"Use \"-h\" or \"--help\" for a list of possible commands.\n",
* NO_ARGS_MSG = "yfc: Use \"-h\" or \"--help\" "
"for a list of possible commands.\n"
* NO_ARGS_MSG = "yfc: Use \"-h\" or \"--help\" for a list of possible commands.\n"
;
2 changes: 2 additions & 0 deletions src/util/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ void yf_list_destroy(struct yf_list * list, int free_elements) {
/* External inline function definitions */
extern inline int yf_list_get(struct yf_list_cursor * cur, void ** elem);
extern inline void yf_list_reset_cursor(struct yf_list_cursor * cur, struct yf_list * list);
extern inline bool yf_list_is_empty(struct yf_list *list);
extern inline size_t yf_list_get_count(struct yf_list *list);
25 changes: 25 additions & 0 deletions src/util/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define UTIL_LIST_H

#include <stddef.h>
#include <stdbool.h>

/**
* How it works - each linked list is theoretically a list of nodes with a
Expand Down Expand Up @@ -101,6 +102,30 @@ int yf_list_merge(struct yf_list * dst, struct yf_list * src);
*/
void yf_list_destroy(struct yf_list * list, int free_elements);

/**
* Fast check if list is empty.
*/
inline bool yf_list_is_empty(struct yf_list * list) {
if (list->first == NULL)
return true;

return list->first->numfull == 0;
}

/**
* Gets the total element count of the list
*/
inline size_t yf_list_get_count(struct yf_list * list) {
struct yf_list_cursor cur;
size_t cnt = 0;

if (list->first == NULL || list->first->numfull == 0)
return 0;

yf_list_reset_cursor(&cur, list);
do ++cnt; while (yf_list_next(&cur) == 0);
return cnt;
}

/**
* WARNING! This macro introduces a variable into outer scope and may thus be incompatible with constructs as if ... else ... without block scope.
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
*.c

0 comments on commit 53cdf99

Please sign in to comment.