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

Let var temp constness #63

Merged
merged 9 commits into from
Nov 29, 2023
Merged
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
2 changes: 2 additions & 0 deletions back/nev.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ static int nev_compile_prog(const char * input, program * prog)
set_line_no(1);
parse_result = 0;

set_msg_buffer(&prog->msg_count, &prog->msg_array_size, &prog->msg_array);

yyparse(&module_main);
if ((ret = parse_result) == 0)
{
Expand Down
12 changes: 12 additions & 0 deletions back/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ program * program_new()
value->params = NULL;
value->params_count = 0;
value->entry_addr = 0;
value->msg_count = 0;
value->msg_array_size = 0;
value->msg_array = NULL;
value->module_value = module_new();

return value;
Expand All @@ -42,5 +45,14 @@ void program_delete(program * value)
{
module_delete(value->module_value);
}
if (value->msg_array != NULL)
{
unsigned int i = 0;
for (i = 0; i < value->msg_count; i++)
{
free(value->msg_array[i]);
}
free(value->msg_array);
}
free(value);
}
78 changes: 63 additions & 15 deletions back/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
* THE SOFTWARE.
*/
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

const char * utils_file_name = "<stdin>";
#define MAX_MSG_SIZE 1024

static const char * utils_file_name = "<stdin>";
static unsigned int * utils_msg_count = NULL;
static unsigned int * utils_msg_array_size = NULL;
static char *** utils_msg_array = NULL;

const char * get_utils_file_name()
{
Expand All @@ -35,38 +42,79 @@ void set_utils_file_name(const char * file_name)
utils_file_name = file_name;
}

void print_error_msg(int line_no, const char * format, ...)
void set_msg_buffer(unsigned int * msg_count, unsigned int * msg_array_size, char *** msg_array)
{
va_list args;
utils_msg_count = msg_count;
utils_msg_array_size = msg_array_size;
utils_msg_array = msg_array;
}

va_start(args, format);
void print_msg_buffer(unsigned int msg_count, char ** msg_array)
{
if (msg_array == NULL)
{
return;
}
unsigned int i = 0;
for (i = 0; i < msg_count; i++)
{
printf("%s\n", msg_array[i]);
}
}

fprintf(stderr, "%s:%d: error: ", utils_file_name, line_no);
vfprintf(stderr, format, args);
static void print_msg(const char * type, int line_no, const char * format, va_list args)
{
va_list args_copy;

va_copy(args_copy, args);
fprintf(stderr, "%s:%d: %s: ", utils_file_name, line_no, type);
vfprintf(stderr, format, args_copy);
va_end(args_copy);

#ifndef NO_FFI
fprintf(stderr, "\n");
#else
fprintf(stderr, "\r\n");
#endif

if (utils_msg_count != NULL)
{
unsigned int msg_len = 0;
char msg_buf[MAX_MSG_SIZE] = { 0 };

if (*utils_msg_count >= *utils_msg_array_size)
{
*utils_msg_array_size = *utils_msg_array_size + 10;
*utils_msg_array = realloc(*utils_msg_array, *utils_msg_array_size * sizeof(char *));
}

va_copy(args_copy, args);
msg_len = snprintf(msg_buf, MAX_MSG_SIZE, "%s:%d: %s: ", utils_file_name, line_no, type);
msg_len += vsnprintf(msg_buf + msg_len, MAX_MSG_SIZE, format, args_copy);
va_end(args_copy);

(*utils_msg_array)[*utils_msg_count] = strdup(msg_buf);
(*utils_msg_count)++;
}
}

void print_error_msg(int line_no, const char * format, ...)
{
va_list args;
va_start(args, format);

print_msg("error", line_no, format, args);

va_end(args);
}

void print_warning_msg(int line_no, const char * format, ...)
{
va_list args;

va_start(args, format);

fprintf(stderr,"%s:%d: warning: ", utils_file_name, line_no);
vfprintf(stderr, format, args);

#ifndef NO_FFI
fprintf(stderr, "\n");
#else
fprintf(stderr, "\r\n");
#endif
print_msg("warning", line_no, format, args);

va_end(args);
}

4 changes: 3 additions & 1 deletion back/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
const char * get_utils_file_name();
void set_utils_file_name(const char * file_name);

void print_error_msg(int line_no, const char * format, ...);
void set_msg_buffer(unsigned int * msg_count, unsigned int * msg_array_size, char *** msg_array);
void print_msg_buffer(unsigned int msg_count, char ** msg_array);

void print_error_msg(int line_no, const char * format, ...);
void print_warning_msg(int line_no, const char * format, ...);

#endif /* __UTILS_H__ */
Loading
Loading