/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- MessageArgument
- create_message_argument
- search_argument
- format_message
- self_check
- crb_compile_error
- throw_runtime_exception
- crb_runtime_error
- yyerror
- CRB_error
- CRB_check_argument_count_func
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "MEM.h"
#include "DBG.h"
#include "crowbar.h"
extern char *yytext;
extern CRB_MessageFormat crb_compile_error_message_format[];
extern CRB_MessageFormat crb_runtime_error_message_format[];
typedef struct {
CRB_MessageArgumentType type;
char *name;
union {
int int_val;
double double_val;
char *string_val;
void *pointer_val;
int character_val;
} u;
} MessageArgument;
static void
create_message_argument(MessageArgument *arg, va_list ap)
{
int index = 0;
CRB_MessageArgumentType type;
while ((type = va_arg(ap, CRB_MessageArgumentType))
!= CRB_MESSAGE_ARGUMENT_END) {
arg[index].type = type;
arg[index].name = va_arg(ap, char*);
switch (type) {
case CRB_INT_MESSAGE_ARGUMENT:
arg[index].u.int_val = va_arg(ap, int);
break;
case CRB_DOUBLE_MESSAGE_ARGUMENT:
arg[index].u.double_val = va_arg(ap, double);
break;
case CRB_STRING_MESSAGE_ARGUMENT:
arg[index].u.string_val = va_arg(ap, char*);
break;
case CRB_POINTER_MESSAGE_ARGUMENT:
arg[index].u.pointer_val = va_arg(ap, void*);
break;
case CRB_CHARACTER_MESSAGE_ARGUMENT:
arg[index].u.character_val = va_arg(ap, int);
break;
case CRB_MESSAGE_ARGUMENT_END:
assert(0);
break;
default:
assert(0);
}
index++;
assert(index < MESSAGE_ARGUMENT_MAX);
}
}
static void
search_argument(MessageArgument *arg_list,
char *arg_name, MessageArgument *arg)
{
int i;
for (i = 0; arg_list[i].type != CRB_MESSAGE_ARGUMENT_END; i++) {
if (!strcmp(arg_list[i].name, arg_name)) {
*arg = arg_list[i];
return;
}
}
assert(0);
}
static void
format_message(CRB_MessageFormat *format, VString *v, va_list ap)
{
int i;
char buf[LINE_BUF_SIZE];
int arg_name_index;
char arg_name[LINE_BUF_SIZE];
MessageArgument arg[MESSAGE_ARGUMENT_MAX];
MessageArgument cur_arg;
create_message_argument(arg, ap);
for (i = 0; format->format[i] != '\0'; i++) {
if (format->format[i] != '$') {
crb_vstr_append_character(v, format->format[i]);
continue;
}
assert(format->format[i+1] == '(');
i += 2;
for (arg_name_index = 0; format->format[i] != ')';
arg_name_index++, i++) {
arg_name[arg_name_index] = format->format[i];
}
arg_name[arg_name_index] = '\0';
assert(format->format[i] == ')');
search_argument(arg, arg_name, &cur_arg);
switch (cur_arg.type) {
case CRB_INT_MESSAGE_ARGUMENT:
sprintf(buf, "%d", cur_arg.u.int_val);
crb_vstr_append_string(v, buf);
break;
case CRB_DOUBLE_MESSAGE_ARGUMENT:
sprintf(buf, "%f", cur_arg.u.double_val);
crb_vstr_append_string(v, buf);
break;
case CRB_STRING_MESSAGE_ARGUMENT:
strcpy(buf, cur_arg.u.string_val);
crb_vstr_append_string(v, cur_arg.u.string_val);
break;
case CRB_POINTER_MESSAGE_ARGUMENT:
sprintf(buf, "%p", cur_arg.u.pointer_val);
crb_vstr_append_string(v, buf);
break;
case CRB_CHARACTER_MESSAGE_ARGUMENT:
sprintf(buf, "%c", cur_arg.u.character_val);
crb_vstr_append_string(v, buf);
break;
case CRB_MESSAGE_ARGUMENT_END:
assert(0);
break;
default:
assert(0);
}
}
}
void
self_check()
{
if (strcmp(crb_compile_error_message_format[0].format, "dummy") != 0) {
DBG_panic(("compile error message format error.\n"));
}
if (strcmp(crb_compile_error_message_format
[COMPILE_ERROR_COUNT_PLUS_1].format,
"dummy") != 0) {
DBG_panic(("compile error message format error. "
"COMPILE_ERROR_COUNT_PLUS_1..%d\n",
COMPILE_ERROR_COUNT_PLUS_1));
}
if (strcmp(crb_runtime_error_message_format[0].format, "dummy") != 0) {
DBG_panic(("runtime error message format error.\n"));
}
if (strcmp(crb_runtime_error_message_format
[RUNTIME_ERROR_COUNT_PLUS_1].format,
"dummy") != 0) {
DBG_panic(("runtime error message format error. "
"RUNTIME_ERROR_COUNT_PLUS_1..%d\n",
RUNTIME_ERROR_COUNT_PLUS_1));
}
}
void
crb_compile_error(CompileError id, ...)
{
va_list ap;
VString message;
int line_number;
self_check();
va_start(ap, id);
line_number = crb_get_current_interpreter()->current_line_number;
crb_vstr_clear(&message);
format_message(&crb_compile_error_message_format[id],
&message, ap);
fprintf(stderr, "%3d:%s\n", line_number, message.string);
va_end(ap);
exit(1);
}
void
throw_runtime_exception(CRB_Interpreter *inter, CRB_LocalEnvironment *env,
int line_number, char *message)
{
CRB_Value exception_value;
CRB_Object *exception;
CRB_Value message_value;
CRB_Value stack_trace;
CRB_Object *new_line;
CRB_Value *stack_trace_top;
int stack_count = 0;
exception = CRB_create_assoc(inter);
exception_value.type = CRB_ASSOC_VALUE;
exception_value.u.object = exception;
CRB_push_value(inter, &exception_value);
stack_count++;
message_value.type = CRB_STRING_VALUE;
message_value.u.object = CRB_create_crowbar_string(inter, message);
CRB_push_value(inter, &message_value);
stack_count++;
CRB_add_assoc_member(inter, exception, EXCEPTION_MEMBER_MESSAGE,
&message_value);
stack_trace.type = CRB_ARRAY_VALUE;
stack_trace.u.object = CRB_create_array(inter, 1);
CRB_push_value(inter, &stack_trace);
stack_count++;
CRB_add_assoc_member(inter, exception, EXCEPTION_MEMBER_STACK_TRACE,
&stack_trace);
new_line = crb_create_stack_trace_line(inter, env, line_number);
stack_trace_top = CRB_array_get(inter, env, stack_trace.u.object, 0);
stack_trace_top->type = CRB_ASSOC_VALUE;
stack_trace_top->u.object = new_line;
inter->current_exception = exception;
CRB_shrink_stack(inter, stack_count);
longjmp(inter->current_recovery_environment.environment, LONGJMP_ARG);
}
void
crb_runtime_error(CRB_Interpreter *inter, CRB_LocalEnvironment *env,
int line_number, RuntimeError id, ...)
{
va_list ap;
VString message;
self_check();
va_start(ap, id);
crb_vstr_clear(&message);
format_message(&crb_runtime_error_message_format[id],
&message, ap);
va_end(ap);
throw_runtime_exception(inter, env, line_number, message.string);
}
int
yyerror(char const *str)
{
crb_compile_error(PARSE_ERR,
CRB_STRING_MESSAGE_ARGUMENT, "token", yytext,
CRB_MESSAGE_ARGUMENT_END);
return 0;
}
void
CRB_error(CRB_Interpreter *inter, CRB_LocalEnvironment *env,
CRB_NativeLibInfo *info, int line_number, int error_code, ...)
{
va_list ap;
VString message;
va_start(ap, error_code);
crb_vstr_clear(&message);
format_message(&info->message_format[error_code],
&message, ap);
throw_runtime_exception(inter, env, line_number, message.string);
}
void
CRB_check_argument_count_func(CRB_Interpreter *inter,
CRB_LocalEnvironment *env,
int line_number,
int arg_count, int expected_count)
{
if (arg_count < expected_count) {
crb_runtime_error(inter, env, line_number, ARGUMENT_TOO_FEW_ERR,
CRB_MESSAGE_ARGUMENT_END);
} else if (arg_count > expected_count) {
crb_runtime_error(inter, env, line_number, ARGUMENT_TOO_MANY_ERR,
CRB_MESSAGE_ARGUMENT_END);
}
}