197 lines
6.5 KiB
C
197 lines
6.5 KiB
C
|
|
#include "repl_output.h"
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static const char* type_name(RavaValueType_e type) {
|
||
|
|
switch (type) {
|
||
|
|
case RAVA_VAL_INT: return "int";
|
||
|
|
case RAVA_VAL_LONG: return "long";
|
||
|
|
case RAVA_VAL_FLOAT: return "float";
|
||
|
|
case RAVA_VAL_DOUBLE: return "double";
|
||
|
|
case RAVA_VAL_BOOLEAN: return "boolean";
|
||
|
|
case RAVA_VAL_CHAR: return "char";
|
||
|
|
case RAVA_VAL_NULL: return "null";
|
||
|
|
case RAVA_VAL_OBJECT: return "Object";
|
||
|
|
case RAVA_VAL_ARRAY: return "Array";
|
||
|
|
case RAVA_VAL_STRING: return "String";
|
||
|
|
case RAVA_VAL_ARRAYLIST: return "ArrayList";
|
||
|
|
case RAVA_VAL_HASHMAP: return "HashMap";
|
||
|
|
case RAVA_VAL_SOCKET: return "Socket";
|
||
|
|
case RAVA_VAL_STREAM: return "Stream";
|
||
|
|
case RAVA_VAL_METHOD_REF: return "MethodRef";
|
||
|
|
default: return "unknown";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_value(RavaValue_t value, RavaVM_t *vm) {
|
||
|
|
(void)vm;
|
||
|
|
switch (value.type) {
|
||
|
|
case RAVA_VAL_INT:
|
||
|
|
printf("%ld\n", value.data.int_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_LONG:
|
||
|
|
printf("%ldL\n", value.data.long_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_FLOAT:
|
||
|
|
printf("%gf\n", value.data.float_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_DOUBLE:
|
||
|
|
printf("%g\n", value.data.double_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_BOOLEAN:
|
||
|
|
printf("%s\n", value.data.bool_val ? "true" : "false");
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_CHAR:
|
||
|
|
printf("'%c'\n", value.data.char_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_STRING:
|
||
|
|
if (value.data.string_val) {
|
||
|
|
printf("\"%s\"\n", value.data.string_val);
|
||
|
|
} else {
|
||
|
|
printf("null\n");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_OBJECT:
|
||
|
|
if (value.data.object_val) {
|
||
|
|
RavaObject_t *obj = value.data.object_val;
|
||
|
|
printf("%s@%p\n", obj->class_name ? obj->class_name : "Object", (void*)obj);
|
||
|
|
} else {
|
||
|
|
printf("null\n");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_ARRAY:
|
||
|
|
if (value.data.array_val) {
|
||
|
|
RavaArray_t *arr = value.data.array_val;
|
||
|
|
printf("[%s array]: size=%zu\n", type_name(arr->element_type), arr->length);
|
||
|
|
} else {
|
||
|
|
printf("null\n");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_ARRAYLIST:
|
||
|
|
if (value.data.arraylist_val) {
|
||
|
|
printf("[ArrayList]: size=%zu\n", rava_arraylist_size(value.data.arraylist_val));
|
||
|
|
} else {
|
||
|
|
printf("null\n");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_HASHMAP:
|
||
|
|
if (value.data.hashmap_val) {
|
||
|
|
printf("[HashMap]: size=%zu\n", rava_hashmap_size(value.data.hashmap_val));
|
||
|
|
} else {
|
||
|
|
printf("null\n");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_NULL:
|
||
|
|
printf("null\n");
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
printf("<%s>\n", type_name(value.type));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_variable(RavaREPLVariable_t *var) {
|
||
|
|
if (!var) return;
|
||
|
|
printf("%-20s %-15s ", var->name, var->type_name ? var->type_name : "?");
|
||
|
|
if (!var->is_initialized) {
|
||
|
|
printf("<uninitialized>");
|
||
|
|
} else {
|
||
|
|
switch (var->type) {
|
||
|
|
case RAVA_VAL_INT:
|
||
|
|
printf("%ld", var->value.data.int_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_LONG:
|
||
|
|
printf("%ldL", var->value.data.long_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_FLOAT:
|
||
|
|
printf("%gf", var->value.data.float_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_DOUBLE:
|
||
|
|
printf("%g", var->value.data.double_val);
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_BOOLEAN:
|
||
|
|
printf("%s", var->value.data.bool_val ? "true" : "false");
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_STRING:
|
||
|
|
if (var->value.data.string_val) {
|
||
|
|
printf("\"%s\"", var->value.data.string_val);
|
||
|
|
} else {
|
||
|
|
printf("null");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_OBJECT:
|
||
|
|
if (var->value.data.object_val) {
|
||
|
|
printf("%s@%p", var->type_name ? var->type_name : "Object",
|
||
|
|
(void*)var->value.data.object_val);
|
||
|
|
} else {
|
||
|
|
printf("null");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case RAVA_VAL_ARRAY:
|
||
|
|
if (var->value.data.array_val) {
|
||
|
|
printf("[array]: size=%zu", var->value.data.array_val->length);
|
||
|
|
} else {
|
||
|
|
printf("null");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
printf("0");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
printf("\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_variables(RavaREPLSession_t *session) {
|
||
|
|
if (!session || session->variable_count == 0) {
|
||
|
|
printf("No variables defined.\n");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
printf("%-20s %-15s %s\n", "Variable", "Type", "Value");
|
||
|
|
printf("%-20s %-15s %s\n", "--------", "----", "-----");
|
||
|
|
for (size_t i = 0; i < session->variable_count; i++) {
|
||
|
|
rava_repl_output_variable(&session->variables[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_methods(RavaREPLSession_t *session) {
|
||
|
|
if (!session || session->method_count == 0) {
|
||
|
|
printf("No methods defined.\n");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
printf("%-20s %-15s\n", "Method", "Return Type");
|
||
|
|
printf("%-20s %-15s\n", "------", "-----------");
|
||
|
|
for (size_t i = 0; i < session->method_count; i++) {
|
||
|
|
printf("%-20s %-15s\n",
|
||
|
|
session->methods[i].name,
|
||
|
|
session->methods[i].return_type ? session->methods[i].return_type : "void");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_classes(RavaREPLSession_t *session) {
|
||
|
|
if (!session || session->class_count == 0) {
|
||
|
|
printf("No classes defined.\n");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
printf("Defined classes:\n");
|
||
|
|
for (size_t i = 0; i < session->class_count; i++) {
|
||
|
|
printf(" %s\n", session->classes[i].name);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_error(const char *type, const char *message) {
|
||
|
|
if (type) {
|
||
|
|
printf("%s: %s\n", type, message ? message : "Unknown error");
|
||
|
|
} else {
|
||
|
|
printf("Error: %s\n", message ? message : "Unknown error");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void rava_repl_output_timing(double elapsed_ms) {
|
||
|
|
if (elapsed_ms >= 1000.0) {
|
||
|
|
printf("# Executed in %.2f s\n", elapsed_ms / 1000.0);
|
||
|
|
} else {
|
||
|
|
printf("# Executed in %.1f ms\n", elapsed_ms);
|
||
|
|
}
|
||
|
|
}
|