Clean up many strdups

This commit is contained in:
2022-11-02 20:36:07 -04:00
parent 9c1a81811c
commit 3b7bef779b
11 changed files with 39 additions and 31 deletions

View File

@@ -12,15 +12,15 @@
LISP_FUNC_1(ucl_builtin_type, state, arg) {
switch (arg->type) {
case UCL_TYPE_CELL:
return ucl_symbol_create(strdup("list"));
return ucl_symbol_create("list");
case UCL_TYPE_SYMBOL:
return ucl_symbol_create(strdup("symbol"));
return ucl_symbol_create("symbol");
case UCL_TYPE_INT:
return ucl_symbol_create(strdup("int"));
return ucl_symbol_create("int");
case UCL_TYPE_STRING:
return ucl_symbol_create(strdup("string"));
return ucl_symbol_create("string");
case UCL_TYPE_ERROR:
return ucl_symbol_create(strdup("error"));
return ucl_symbol_create("error");
case UCL_TYPE_COUNT:
assert(0);
return NULL;
@@ -32,7 +32,7 @@ LISP_FUNC_1(ucl_builtin_error, state, arg) {
return ucl_error_create("Expected type string passed to 'error'");
}
return ucl_error_create(strdup(arg->error));
return ucl_error_create(arg->error);
}
LISP_FUNC_1(ucl_builtin_symbol_p, state, arg) {
@@ -81,7 +81,7 @@ LISP_FUNC_2(ucl_builtin_sub, state, arg0, arg1) {
}
if (arg1->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 1 to 'sub'");
return ucl_error_create(("Invalid type of argument 1 to 'sub'"));
}
return ucl_int_create(arg0->integer - arg1->integer);

View File

@@ -6,6 +6,7 @@
#include "utility.h"
#include "state.h"
// TODO: remove string.h
#include <string.h>
struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object *list) {

View File

@@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
static struct ucl_object *ucl_object_alloc();
@@ -28,7 +28,7 @@ struct ucl_object *ucl_symbol_create(const char *symbol)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_SYMBOL;
obj->symbol = symbol;
obj->symbol = strdup(symbol);
return obj;
}
@@ -36,7 +36,7 @@ struct ucl_object *ucl_string_create(const char *string)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_STRING;
obj->string = string;
obj->string = strdup(string);
return obj;
}
@@ -44,7 +44,7 @@ struct ucl_object *ucl_error_create(const char *error)
{
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_ERROR;
obj->error = error;
obj->error = strdup(error);
return obj;
}

View File

@@ -113,11 +113,11 @@ struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom) {
return NULL;
}
}
atom = ucl_symbol_create(strdup(maybe_atom->symbol));
atom = ucl_symbol_create(maybe_atom->symbol);
break;
}
case UCL_TYPE_STRING:
atom = ucl_string_create(strdup(maybe_atom->string));
atom = ucl_string_create(maybe_atom->string);
break;
case UCL_TYPE_INT:
atom = ucl_int_create(maybe_atom->integer);

View File

@@ -34,7 +34,7 @@ struct ucl_object *ucl_state_get(struct ucl_state *state, const char *name) {
struct ucl_object *cell = ucl_state_get_cell(state, name);
if (cell == NULL) {
if (state->parent == NULL) {
return ucl_error_create(strdup("Unknown error"));
return ucl_error_create("Unknown error");
} else {
return ucl_state_get(state->parent, name);
}
@@ -47,7 +47,7 @@ void ucl_state_put(struct ucl_state *state, const char *name, struct ucl_object
if (cell == NULL) {
ucl_list_append(state->list,
ucl_tuple_create(
ucl_string_create(strdup(name)),
ucl_string_create(name),
obj));
} else {
// TODO: Refcounting / cleanup

View File

@@ -37,7 +37,7 @@ struct ucl_object *ucl_nil_create() {
}
struct ucl_object *ucl_t_create() {
return ucl_symbol_create(strdup("t"));
return ucl_symbol_create("t");
}
struct ucl_object *ucl_predicate(bool value) {

View File

@@ -25,11 +25,11 @@ struct ucl_object* ucl_tuple_create(struct ucl_object *obj0, struct ucl_object *
return obj; \
} while(0)
#define UCL_COND_OR_RET_ERROR(cond, msg) \
do { \
if (!(cond)) { \
return ucl_error_create(strdup(msg)); \
} \
#define UCL_COND_OR_RET_ERROR(cond, msg) \
do { \
if (!(cond)) { \
return ucl_error_create(msg); \
} \
} while(0)
#endif