Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 21 additions & 19 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 @@ -48,7 +48,8 @@ static int yf_add_file(struct yf_args * args, char * file) {
yf_set_error(args);
return 1;
}
yf_list_add(&args->files, file);
if (yf_list_add(&args->files, file) != YF_OK)
abort();
return 0;
}

Expand All @@ -63,9 +64,10 @@ 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);
if (yf_list_init(&args->files) != YF_OK)
abort();

/* Start at 1 - avoid program name */
for (i = 1; i < argc; ++i) {
Expand Down Expand Up @@ -110,12 +112,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 +126,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 +197,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 +235,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
40 changes: 24 additions & 16 deletions src/driver/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,19 @@ static int yf_create_compiler_jobs(
yf_backend_find_compiler(args);

struct yf_list link_objs;
yf_list_init(&link_objs);
if (yf_list_init(&link_objs) != YF_OK)
abort();

/* Fill project info */
compilation->project_name = data->project_name;
yf_list_init(&compilation->jobs);
if (yf_list_init(&compilation->jobs) != YF_OK)
abort();
yfh_init(&compilation->symtables);
yf_list_init(&compilation->garbage);
if (yf_list_init(&compilation->garbage) != YF_OK)
abort();

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

ujob = malloc(sizeof(struct yf_compile_analyse_job));
memset(ujob, 0, sizeof(struct yf_compile_analyse_job));
Expand All @@ -186,22 +188,25 @@ static int yf_create_compiler_jobs(
YF_COMPILE_FULL;

yfh_cursor_set(&cursor, ujob); // Set the job for further stages
yf_list_add(&compilation->jobs, ujob);
if (yf_list_add(&compilation->jobs, ujob) != YF_OK)
abort();
}

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

if (ujob->stage < YF_COMPILE_ANALYSEONLY)
continue;

cjob = malloc(sizeof(struct yf_compile_compile_job));
cjob->job.type = YF_COMPILATION_COMPILE;
cjob->unit = ujob;
yf_list_add(&compilation->jobs, cjob);
if (yf_list_add(&compilation->jobs, cjob) != YF_OK)
abort();

if (ujob->stage >= YF_COMPILE_CODEGENONLY) {
char * object_file = yf_backend_add_compile_job(compilation, args, ujob->unit_info);
yf_list_add(&link_objs, object_file);
if (yf_list_add(&link_objs, object_file) != YF_OK)
abort();
has_compiled_files = true;
}
}
Expand All @@ -210,7 +215,8 @@ static int yf_create_compiler_jobs(
yf_backend_add_link_job(compilation, args, &link_objs);
}

yf_list_merge(&compilation->garbage, &link_objs);
if (yf_list_merge(&compilation->garbage, &link_objs) != YF_OK)
abort();
yfh_destroy(&data->files, NULL);

return 0;
Expand Down Expand Up @@ -355,8 +361,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); ) {
yfh_cursor_get(&cursor, NULL, (void **)&fdata);
for (yfh_cursor_init(&cursor, &data.files); yfh_cursor_next_get(&cursor, NULL, (void **)&fdata) == YF_OK; ) {
if (fdata->parse_anew) {
YF_PRINT_WITH_COLOR(
YF_CODE_YELLOW,
Expand Down Expand Up @@ -396,7 +401,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 +488,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
17 changes: 10 additions & 7 deletions src/driver/compiler-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ char * yf_backend_add_compile_job(
cjob->command[5] = "-fdollars-in-identifiers";
cjob->command[6] = NULL;

yf_list_add(&compilation->jobs, cjob);
if (yf_list_add(&compilation->jobs, cjob) != YF_OK)
abort();

return object_file;

Expand Down Expand Up @@ -214,8 +215,11 @@ int yf_backend_add_link_job(
struct yf_list_cursor link_objs_cur;
yf_list_reset_cursor(&link_objs_cur, link_objs);
for (obj_it = 0; obj_it < num_objs; ++obj_it) {
yf_list_get(&link_objs_cur, (void **)it);
yf_list_next(&link_objs_cur);
if (yf_list_get(&link_objs_cur, (void **)it) != YF_OK ||
yf_list_next(&link_objs_cur) != YF_OK)

abort();

++it;
}

Expand All @@ -234,7 +238,8 @@ int yf_backend_add_link_job(
ljob->job.type = YF_COMPILATION_EXEC;
ljob->command = link_cmd;

yf_list_add(&compilation->jobs, ljob);
if (yf_list_add(&compilation->jobs, ljob) != YF_OK)
abort();

return 0;

Expand Down Expand Up @@ -277,9 +282,7 @@ int yf_ensure_entry_point(
void * dummy;

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

yfh_cursor_get(&cursor, NULL, (void **)&fsymtab);
for (yfh_cursor_init(&cursor, &pdata->symtables); yfh_cursor_next_get(&cursor, NULL, (void **)&fsymtab) == YF_OK; ) {

/* If a lookup for "main" succeeds, that's another entry point. */
if (yfh_get(&fsymtab->table, "main", &dummy) == 0) {
Expand Down
6 changes: 4 additions & 2 deletions src/parser/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ int yfp_funcdecl(struct yf_parse_node * node, struct yf_lexer * lexer) {
node->funcdecl.extc = false;

/* Start arg list for writing */
yf_list_init(&node->funcdecl.params);
if (yf_list_init(&node->funcdecl.params) != YF_OK)
abort();
argct = 0;

for (;;) {
Expand Down Expand Up @@ -61,7 +62,8 @@ int yfp_funcdecl(struct yf_parse_node * node, struct yf_lexer * lexer) {
++argct;

/* Add to arg list */
yf_list_add(&node->funcdecl.params, argp);
if (yf_list_add(&node->funcdecl.params, argp) != YF_OK)
abort();

}

Expand Down
12 changes: 8 additions & 4 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ int yfp_program(struct yf_parse_node * node, struct yf_lexer * lexer) {
node->loc.line = node->loc.column = -1;

node->type = YFCS_PROGRAM;
yf_list_init(&node->program.decls);
if (yf_list_init(&node->program.decls) != YF_OK)
abort();

for (;;) {

Expand Down Expand Up @@ -78,7 +79,8 @@ int yfp_program(struct yf_parse_node * node, struct yf_lexer * lexer) {
}

/* Now, we have a node - add it to the list. */
yf_list_add(&node->program.decls, decl);
if (yf_list_add(&node->program.decls, decl) != YF_OK)
abort();

}

Expand Down Expand Up @@ -258,7 +260,8 @@ int yfp_bstmt(struct yf_parse_node * node, struct yf_lexer * lexer) {
P_GETCT(node, tok);

node->type = YFCS_BSTMT;
yf_list_init(&node->bstmt.stmts);
if (yf_list_init(&node->bstmt.stmts) != YF_OK)
abort();

for (;;) {
P_PEEK(lexer, &tok);
Expand All @@ -272,7 +275,8 @@ int yfp_bstmt(struct yf_parse_node * node, struct yf_lexer * lexer) {
free(stmt);
return 1;
}
yf_list_add(&node->bstmt.stmts, stmt);
if (yf_list_add(&node->bstmt.stmts, stmt) != YF_OK)
abort();
}

}
6 changes: 4 additions & 2 deletions src/parser/stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ int yfp_funccall(struct yf_parse_node * node, struct yf_lexer * lexer) {
*/

/* Start arg list for writing */
yf_list_init(&node->expr.call.args);
if (yf_list_init(&node->expr.call.args) != YF_OK)
abort();
argct = 0;

for (;;) {
Expand Down Expand Up @@ -143,7 +144,8 @@ int yfp_funccall(struct yf_parse_node * node, struct yf_lexer * lexer) {
++argct;

/* Add to arg list */
yf_list_add(&node->expr.call.args, argp);
if (yf_list_add(&node->expr.call.args, argp) != YF_OK)
abort();

}

Expand Down
Loading