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

View File

@@ -103,6 +103,12 @@ static void test_eval_sym_undefined(void) {
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_eval_nested_error(void) {
response = eval("(+ (error \"foo\") 3)");
TEST_ASSERT_OBJ_ERROR(response);
}
int main(void) {
UNITY_BEGIN();
@@ -117,6 +123,7 @@ int main(void) {
RUN_TEST(test_eval_string);
RUN_TEST(test_eval_sym_defined);
RUN_TEST(test_eval_sym_undefined);
RUN_TEST(test_eval_nested_error);
return UNITY_END();
}

View File

@@ -188,20 +188,20 @@ static void test_tokenize_statement(void) {
}
static void test_parse_atom_symbol(void) {
response = ucl_parse_token_atom(ucl_symbol_create(strdup("foo")));
response = ucl_parse_token_atom(ucl_symbol_create("foo"));
TEST_ASSERT_EQUAL(response->type, UCL_TYPE_SYMBOL);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
}
static void test_parse_atom_lparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create(strdup("(")));
response = ucl_parse_token_atom(ucl_symbol_create("("));
TEST_ASSERT_NULL(response);
}
static void test_parse_atom_rparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create(strdup(")")));
response = ucl_parse_token_atom(ucl_symbol_create(")"));
TEST_ASSERT_NULL(response);
}

View File

@@ -41,11 +41,11 @@ static void test_put2_get(void) {
static void test_put_modify_get(void) {
struct ucl_object *obj = ucl_tuple_create(
ucl_string_create(strdup("bar")),
ucl_string_create(strdup("baz")));
ucl_string_create("bar"),
ucl_string_create("baz"));
ucl_state_put(state, "foo", obj);
ucl_list_append(obj, ucl_string_create(strdup("quux")));
ucl_list_append(obj, ucl_string_create("quux"));
response = ucl_state_get(state, "foo");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));

View File

@@ -73,7 +73,7 @@ static void test_cdr_t(void) {
static void test_car_list(void) {
input = ucl_tuple_create(
ucl_string_create(strdup("foo")),
ucl_string_create("foo"),
ucl_t_create());
response = ucl_car(input);
@@ -83,7 +83,7 @@ static void test_car_list(void) {
static void test_cdr_list(void) {
input = ucl_tuple_create(
ucl_t_create(),
ucl_string_create(strdup("foo")));
ucl_string_create("foo"));
response = ucl_cdr(input);
TEST_ASSERT_OBJ_STRING(ucl_car(response));
@@ -146,7 +146,7 @@ static void test_list_nth_list_0() {
response = ucl_list_nth(
ucl_tuple_create(
ucl_t_create(),
ucl_string_create(strdup("foo"))),
ucl_string_create("foo")),
0);
TEST_ASSERT_T(response);
@@ -156,7 +156,7 @@ static void test_list_nth_list_1() {
response = ucl_list_nth(
ucl_tuple_create(
ucl_t_create(),
ucl_string_create(strdup("foo"))),
ucl_string_create("foo")),
1);
TEST_ASSERT_OBJ_STRING(response);