Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance hashmap implementation #133

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
34 changes: 17 additions & 17 deletions src/driver/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
static void yf_set_error(struct yf_args * args) {
args->error = 1;
args->wanted_output = YF_ERROR;
args->wanted_output = YF_INFO_ERROR;
}

/**
Expand All @@ -27,7 +27,7 @@ static void yf_set_error(struct yf_args * args) {
* Check if the wanted action is not already set.
*/
static void yf_check_action(struct yf_args * args, enum yf_info_output out) {
if (args->wanted_output != YF_NONE) {
if (args->wanted_output != YF_INFO_NONE) {
yf_set_error(args);
} else {
args->wanted_output = out;
Expand All @@ -39,7 +39,7 @@ static void yf_check_action(struct yf_args * args, enum yf_info_output out) {
*/
static int yf_add_file(struct yf_args * args, char * file) {
/* No actions allowed. e.g. : no "yfc --version foo.yf" */
if (args->wanted_output != YF_NONE) {
if (args->wanted_output != YF_INFO_NONE) {
yf_set_error(args);
return 1;
}
Expand All @@ -63,7 +63,7 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {

/* Zero the args structure. */
memset(args, 0, sizeof *args);
args->wanted_output = YF_NONE;
args->wanted_output = YF_INFO_NONE;
args->run_c_comp = true;
yf_list_init(&args->files);

Expand Down Expand Up @@ -110,12 +110,12 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
++arg;
if (!arg[1]) {
if (arg[0] == 'h' || arg[0] == '?') {
yf_check_action(args, YF_HELP);
yf_check_action(args, YF_INFO_HELP);
continue;
}

if (arg[0] == 'v') {
yf_check_action(args, YF_VERSION);
yf_check_action(args, YF_INFO_VERSION);
continue;
}
}
Expand All @@ -124,12 +124,12 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
++arg;

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

if (STREQ(arg, "version")) {
yf_check_action(args, YF_VERSION);
yf_check_action(args, YF_INFO_VERSION);
continue;
}

Expand Down Expand Up @@ -195,7 +195,7 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {
}

if (STREQ(arg, "benchmark")) {
if (args->profile || args->wanted_output != YF_NONE) {
if (args->profile || args->wanted_output != YF_INFO_NONE) {
yf_set_error(args);
return;
}
Expand Down Expand Up @@ -233,32 +233,32 @@ void yf_parse_args(int argc, char ** argv, struct yf_args * args) {

}

if (args->wanted_output == YF_NONE && !args->project && yf_list_is_empty(&args->files)) {
if (args->wanted_output == YF_INFO_NONE && !args->project && yf_list_is_empty(&args->files)) {
args->error = 1;
args->wanted_output = YF_ERROR_NO_ARGS;
args->wanted_output = YF_INFO_ERROR_NO_ARGS;
}

}

bool yf_should_compile(struct yf_args * args) {
return args->wanted_output == YF_NONE;
return args->wanted_output == YF_INFO_NONE;
}

int yf_output_info(struct yf_args * args) {

switch (args->wanted_output) {
case YF_NONE:
case YF_INFO_NONE:
return 0;
case YF_VERSION:
case YF_INFO_VERSION:
printf("%s", VERSION_MSG);
return 0;
case YF_HELP:
case YF_INFO_HELP:
printf("%s", USAGE_MSG);
return 0;
case YF_ERROR:
case YF_INFO_ERROR:
printf("%s", HELP_HINT_MSG);
return 1;
case YF_ERROR_NO_ARGS:
case YF_INFO_ERROR_NO_ARGS:
printf("%s", NO_ARGS_MSG);
return 1;
}
Expand Down
10 changes: 5 additions & 5 deletions src/driver/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* Any of the possible outputs wanted.
*/
enum yf_info_output {
YF_NONE,
YF_VERSION,
YF_HELP,
YF_ERROR,
YF_ERROR_NO_ARGS, /* SPECIFICALLY if no arguments are given. */
YF_INFO_NONE,
YF_INFO_VERSION,
YF_INFO_HELP,
YF_INFO_ERROR,
YF_INFO_ERROR_NO_ARGS, /* SPECIFICALLY if no arguments are given. */
};

enum yf_compiler_class {
Expand Down
15 changes: 9 additions & 6 deletions src/driver/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static int yf_create_compiler_jobs(
yf_list_init(&compilation->garbage);

struct yfh_cursor cursor;
for (yfh_cursor_init(&cursor, &data->files); !yfh_cursor_next(&cursor); ) {
for (yfh_cursor_init(&cursor, &data->files); yfh_cursor_next(&cursor) == YF_OK; ) {
yfh_cursor_get(&cursor, NULL, (void **)&fdata);

ujob = malloc(sizeof(struct yf_compile_analyse_job));
Expand All @@ -189,7 +189,7 @@ static int yf_create_compiler_jobs(
yf_list_add(&compilation->jobs, ujob);
}

for (yfh_cursor_init(&cursor, &data->files); !yfh_cursor_next(&cursor); ) {
for (yfh_cursor_init(&cursor, &data->files); yfh_cursor_next(&cursor) == YF_OK; ) {
yfh_cursor_get(&cursor, NULL, (void **)&ujob);
if (ujob->stage < YF_COMPILE_ANALYSEONLY)
continue;
Expand Down Expand Up @@ -355,7 +355,7 @@ static int yf_compile_project(struct yf_args * args, struct yf_compilation_data
if (args->dump_projfiles) {
YF_PRINT_DEFAULT("Project files: (green = needs to be recompiled):");
struct yfh_cursor cursor;
for (yfh_cursor_init(&cursor, &data.files); !yfh_cursor_next(&cursor); ) {
for (yfh_cursor_init(&cursor, &data.files); yfh_cursor_next(&cursor) == YF_OK; ) {
yfh_cursor_get(&cursor, NULL, (void **)&fdata);
if (fdata->parse_anew) {
YF_PRINT_WITH_COLOR(
Expand Down Expand Up @@ -396,7 +396,8 @@ static int yf_compile_files(struct yf_args * args, struct yf_compilation_data *
fdata->file_name = yf_strdup(fname);
fdata->parse_anew = 1;
/* TODO - more data */
yfh_set(&data.files, fdata->file_name, fdata);
if (yfh_set(&data.files, fdata->file_name, fdata) != YF_OK)
abort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we print some error message here indicating what's happening? Or does abort do something similar?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abort lets us break if we're debugging. For a regular user, the error would be meaningless since there's not anything that they can do anything about it.

}

return yf_create_compiler_jobs(compilation, &data, args);
Expand Down Expand Up @@ -482,8 +483,10 @@ static int yfc_run_frontend_build_symtable(
retval = yf_do_cst_dump(&data->parse_tree);
} else {
retval = yf_build_symtab(data);
if (!retval && data->unit_info->file_prefix)
yfh_set(&compilation->symtables, data->unit_info->file_prefix, &data->symtab);
if (!retval && data->unit_info->file_prefix) {
if (yfh_set(&compilation->symtables, data->unit_info->file_prefix, &data->symtab) != YF_OK)
abort();
}
}
return retval;
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/compiler-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ int yf_ensure_entry_point(
void * dummy;

struct yfh_cursor cursor;
for (yfh_cursor_init(&cursor, &pdata->symtables); !yfh_cursor_next(&cursor); ) {
for (yfh_cursor_init(&cursor, &pdata->symtables); yfh_cursor_next(&cursor) == YF_OK; ) {

yfh_cursor_get(&cursor, NULL, (void **)&fsymtab);

Expand Down
6 changes: 4 additions & 2 deletions src/semantics/symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ static int yfs_add_var(struct yf_hashmap * symtab, struct yf_parse_node * n) {
return 1;
}

yfh_set(symtab, v->name.name, vsym);
if (yfh_set(symtab, v->name.name, vsym) != YF_OK)
abort();

return 0;

Expand Down Expand Up @@ -102,7 +103,8 @@ static int yfs_add_fn(struct yf_hashmap * symtab, struct yf_parse_node * f) {

}

yfh_set(symtab, fsym->fn.name, fsym);
if (yfh_set(symtab, fsym->fn.name, fsym) != YF_OK)
abort();

return 0;

Expand Down
4 changes: 3 additions & 1 deletion src/semantics/validate/validate-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ struct yfs_type * yfv_get_type_s(
* Get a value from the hashmap.
*/
struct yfs_type * result = NULL;
yfh_get(&udata->types.table, typestr, (void **)&result);
if (yfh_get(&udata->types.table, typestr, (void **)&result) != YF_OK) {
/* result is null */
}
return result;
}
3 changes: 2 additions & 1 deletion src/semantics/validate/validate-var.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ int validate_vardecl(
/* The global scope symtab is already set up. */
if (!global) {
a->name->var.name = c->name.name;
yfh_set(&validator->current_scope->table, c->name.name, a->name);
if (yfh_set(&validator->current_scope->table, c->name.name, a->name) != YF_OK)
abort();
} else {
/* Free the name, since it was only needed for type checking. */
free(a->name);
Expand Down
2 changes: 2 additions & 0 deletions src/util/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void * yf_malloc(size_t size) {

if (ret == NULL) {
YF_PRINT_ERROR("Allocation of %zu bytes failed", size);
abort();
}

/* Return anyway, the error will be printed out before. */
Expand All @@ -24,6 +25,7 @@ void * yf_calloc(size_t num_elems, size_t size) {

if (ret == NULL) {
YF_PRINT_ERROR("Allocation of %zu %zu-byte sized elements failed", num_elems, size);
abort();
}

return ret;
Expand Down
Loading