#include #include #include #include #include #include "mlp_priv.h" static mlp_value *mlp_value_new(mlp_type t){ mlp_value *v = MLP_MALLOC(mlp_value) ; v->type = t ; v->stringy = NULL ; return v ; } mlp_value *mlp_value_new_bool(char val){ mlp_value *v = mlp_value_new(MLP_BOOL) ; v->v.c = (val ? 1 : 0) ; return v ; } mlp_value *mlp_value_new_long(long val){ mlp_value *v = mlp_value_new(MLP_LONG) ; v->v.l = val ; return v ; } mlp_value *mlp_value_new_double(double val){ mlp_value *v = mlp_value_new(MLP_DOUBLE) ; v->v.d = val ; return v ; } mlp_value *mlp_value_new_char(char val){ mlp_value *v = mlp_value_new(MLP_CHAR) ; v->v.c = val ; return v ; } mlp_value *mlp_value_new_string(const char *val){ mlp_value *v = mlp_value_new(MLP_STRING) ; v->v.s = (val == NULL ? NULL : strdup(val)) ; return v ; } mlp_value *mlp_value_new_object(mlp_object *val){ mlp_value *v = mlp_value_new(MLP_OBJECT) ; v->v.o = val ; return v ; } mlp_value *mlp_value_new_exception(mlp_exception *val){ mlp_value *v = mlp_value_new(MLP_EXCEPTION) ; v->v.e = val ; return v ; } void mlp_value_delete(mlp_context *ctx, mlp_value *val){ mlp_debug(2, "-> Value delete: %s", mlp_value_to_string(val)) ; switch (val->type){ case MLP_STRING: if (val->v.s != NULL){ free(val->v.s) ; } break ; case MLP_OBJECT: if (val->v.o != NULL){ mlp_object_delete(ctx, val->v.o) ; } break ; case MLP_EXCEPTION: if (val->v.e != NULL){ mlp_exception_delete(ctx, val->v.e) ; } break ; } if (val->stringy != NULL){ free(val->stringy) ; } free(val) ; mlp_debug(2, "<- Value delete") ; } mlp_type mlp_value_get_type(const mlp_value *val){ return val->type ; } char mlp_value_get_bool(const mlp_value *val){ assert(val->type == MLP_BOOL) ; return (val->v.c ? 1 : 0) ; } long mlp_value_get_long(const mlp_value *val){ assert(val->type == MLP_LONG) ; return val->v.l ; } double mlp_value_get_double(const mlp_value *val){ assert(val->type == MLP_DOUBLE) ; return val->v.d ; } char mlp_value_get_char(const mlp_value *val){ assert(val->type == MLP_CHAR) ; return val->v.c ; } const char *mlp_value_get_string(const mlp_value *val){ assert(val->type == MLP_STRING) ; return val->v.s ; } char *mlp_value_take_string(mlp_value *val){ const char *s = mlp_value_get_string(val) ; val->v.s = NULL ; return (char *)s ; } /* mlp_object *mlp_value_get_object(const mlp_value *val){ assert(val->type == MLP_OBJECT) ; return val->v.o ; } */ mlp_object *mlp_value_take_object(mlp_value *val){ assert(val->type == MLP_OBJECT) ; mlp_object *o = val->v.o ; val->v.o = NULL ; return o ; } /* mlp_exception *mlp_value_get_exception(const mlp_value *val){ assert(val->type == MLP_EXCEPTION) ; return val->v.e ; } */ mlp_exception *mlp_value_take_exception(mlp_value *val){ assert(val->type == MLP_EXCEPTION) ; mlp_exception *e = val->v.e ; val->v.e = NULL ; return e ; } const char *mlp_value_to_string(mlp_value *val){ if (val->stringy != NULL){ return val->stringy ; } switch (val->type){ case MLP_BOOL: { char b[32] ; snprintf(b, 31, "BOOL(%s)", val->v.l ? "true" : "false") ; val->stringy = strdup(b) ; } case MLP_LONG: { char num[1024] ; snprintf(num, 1023, "LONG(%ld)", val->v.l) ; val->stringy = strdup(num) ; return val->stringy ; } case MLP_DOUBLE: { char num[1024] ; snprintf(num, 1023, "DOUBLE(%.15lf)", val->v.d) ; val->stringy = strdup(num) ; return val->stringy ; } case MLP_CHAR: { char c[8] ; snprintf(c, 1, "CHAR(%c)", val->v.c) ; val->stringy = strdup(c) ; return val->stringy ; } case MLP_STRING: { if (val->v.s != NULL){ int len = strlen(val->v.s) + 32 ; val->stringy = (char *)malloc(len * sizeof(char)) ; snprintf(val->stringy, len-1, "STRING(%s)", val->v.s) ; } else { val->stringy = strdup("STRING(NULL)") ; } return val->stringy ; } case MLP_OBJECT: { if (val->v.o != NULL){ const char *ostr = mlp_object_to_string(val->v.o) ; int len = strlen(ostr) + 32 ; val->stringy = (char *)malloc(len * sizeof(char)) ; snprintf(val->stringy, len-1, "OBJECT(%s)", ostr) ; } else { val->stringy = strdup("OBJECT(NULL)") ; } return val->stringy ; } case MLP_EXCEPTION: { if (val->v.e != NULL){ const char *estr = mlp_exception_to_string(val->v.e) ; int len = strlen(estr) + 32 ; val->stringy = (char *)malloc(len * sizeof(char)) ; snprintf(val->stringy, len-1, "EXCEPTION(%s)", estr) ; } else { val->stringy = strdup("EXCEPTION(NULL)") ; } return val->stringy ; } } return val->stringy ; } mlp_value **mlp_value_array_new(int n){ return MLP_CALLOC_N(mlp_value *, n+1) ; } void mlp_value_array_delete(mlp_context *ctx, mlp_value **vals){ int i = 0 ; while (vals[i] != NULL){ mlp_value_delete(ctx, vals[i]) ; i++ ; } free(vals) ; }