Clean up many strdups
This commit is contained in:
@@ -12,15 +12,15 @@
|
|||||||
LISP_FUNC_1(ucl_builtin_type, state, arg) {
|
LISP_FUNC_1(ucl_builtin_type, state, arg) {
|
||||||
switch (arg->type) {
|
switch (arg->type) {
|
||||||
case UCL_TYPE_CELL:
|
case UCL_TYPE_CELL:
|
||||||
return ucl_symbol_create(strdup("list"));
|
return ucl_symbol_create("list");
|
||||||
case UCL_TYPE_SYMBOL:
|
case UCL_TYPE_SYMBOL:
|
||||||
return ucl_symbol_create(strdup("symbol"));
|
return ucl_symbol_create("symbol");
|
||||||
case UCL_TYPE_INT:
|
case UCL_TYPE_INT:
|
||||||
return ucl_symbol_create(strdup("int"));
|
return ucl_symbol_create("int");
|
||||||
case UCL_TYPE_STRING:
|
case UCL_TYPE_STRING:
|
||||||
return ucl_symbol_create(strdup("string"));
|
return ucl_symbol_create("string");
|
||||||
case UCL_TYPE_ERROR:
|
case UCL_TYPE_ERROR:
|
||||||
return ucl_symbol_create(strdup("error"));
|
return ucl_symbol_create("error");
|
||||||
case UCL_TYPE_COUNT:
|
case UCL_TYPE_COUNT:
|
||||||
assert(0);
|
assert(0);
|
||||||
return NULL;
|
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("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) {
|
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) {
|
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);
|
return ucl_int_create(arg0->integer - arg1->integer);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
|
|
||||||
|
// TODO: remove string.h
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object *list) {
|
struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object *list) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static struct ucl_object *ucl_object_alloc();
|
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();
|
struct ucl_object* obj = ucl_object_alloc();
|
||||||
obj->type = UCL_TYPE_SYMBOL;
|
obj->type = UCL_TYPE_SYMBOL;
|
||||||
obj->symbol = symbol;
|
obj->symbol = strdup(symbol);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ struct ucl_object *ucl_string_create(const char *string)
|
|||||||
{
|
{
|
||||||
struct ucl_object* obj = ucl_object_alloc();
|
struct ucl_object* obj = ucl_object_alloc();
|
||||||
obj->type = UCL_TYPE_STRING;
|
obj->type = UCL_TYPE_STRING;
|
||||||
obj->string = string;
|
obj->string = strdup(string);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ struct ucl_object *ucl_error_create(const char *error)
|
|||||||
{
|
{
|
||||||
struct ucl_object* obj = ucl_object_alloc();
|
struct ucl_object* obj = ucl_object_alloc();
|
||||||
obj->type = UCL_TYPE_ERROR;
|
obj->type = UCL_TYPE_ERROR;
|
||||||
obj->error = error;
|
obj->error = strdup(error);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,11 +113,11 @@ struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
atom = ucl_symbol_create(strdup(maybe_atom->symbol));
|
atom = ucl_symbol_create(maybe_atom->symbol);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case UCL_TYPE_STRING:
|
case UCL_TYPE_STRING:
|
||||||
atom = ucl_string_create(strdup(maybe_atom->string));
|
atom = ucl_string_create(maybe_atom->string);
|
||||||
break;
|
break;
|
||||||
case UCL_TYPE_INT:
|
case UCL_TYPE_INT:
|
||||||
atom = ucl_int_create(maybe_atom->integer);
|
atom = ucl_int_create(maybe_atom->integer);
|
||||||
|
|||||||
@@ -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);
|
struct ucl_object *cell = ucl_state_get_cell(state, name);
|
||||||
if (cell == NULL) {
|
if (cell == NULL) {
|
||||||
if (state->parent == NULL) {
|
if (state->parent == NULL) {
|
||||||
return ucl_error_create(strdup("Unknown error"));
|
return ucl_error_create("Unknown error");
|
||||||
} else {
|
} else {
|
||||||
return ucl_state_get(state->parent, name);
|
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) {
|
if (cell == NULL) {
|
||||||
ucl_list_append(state->list,
|
ucl_list_append(state->list,
|
||||||
ucl_tuple_create(
|
ucl_tuple_create(
|
||||||
ucl_string_create(strdup(name)),
|
ucl_string_create(name),
|
||||||
obj));
|
obj));
|
||||||
} else {
|
} else {
|
||||||
// TODO: Refcounting / cleanup
|
// TODO: Refcounting / cleanup
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ struct ucl_object *ucl_nil_create() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ucl_object *ucl_t_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) {
|
struct ucl_object *ucl_predicate(bool value) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ struct ucl_object* ucl_tuple_create(struct ucl_object *obj0, struct ucl_object *
|
|||||||
#define UCL_COND_OR_RET_ERROR(cond, msg) \
|
#define UCL_COND_OR_RET_ERROR(cond, msg) \
|
||||||
do { \
|
do { \
|
||||||
if (!(cond)) { \
|
if (!(cond)) { \
|
||||||
return ucl_error_create(strdup(msg)); \
|
return ucl_error_create(msg); \
|
||||||
} \
|
} \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
|
|||||||
@@ -103,6 +103,12 @@ static void test_eval_sym_undefined(void) {
|
|||||||
TEST_ASSERT_OBJ_ERROR(response);
|
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) {
|
int main(void) {
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
@@ -117,6 +123,7 @@ int main(void) {
|
|||||||
RUN_TEST(test_eval_string);
|
RUN_TEST(test_eval_string);
|
||||||
RUN_TEST(test_eval_sym_defined);
|
RUN_TEST(test_eval_sym_defined);
|
||||||
RUN_TEST(test_eval_sym_undefined);
|
RUN_TEST(test_eval_sym_undefined);
|
||||||
|
RUN_TEST(test_eval_nested_error);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,20 +188,20 @@ static void test_tokenize_statement(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void test_parse_atom_symbol(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(response->type, UCL_TYPE_SYMBOL);
|
||||||
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
|
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_parse_atom_lparen(void) {
|
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);
|
TEST_ASSERT_NULL(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_parse_atom_rparen(void) {
|
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);
|
TEST_ASSERT_NULL(response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ static void test_put2_get(void) {
|
|||||||
|
|
||||||
static void test_put_modify_get(void) {
|
static void test_put_modify_get(void) {
|
||||||
struct ucl_object *obj = ucl_tuple_create(
|
struct ucl_object *obj = ucl_tuple_create(
|
||||||
ucl_string_create(strdup("bar")),
|
ucl_string_create("bar"),
|
||||||
ucl_string_create(strdup("baz")));
|
ucl_string_create("baz"));
|
||||||
|
|
||||||
ucl_state_put(state, "foo", obj);
|
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");
|
response = ucl_state_get(state, "foo");
|
||||||
|
|
||||||
TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));
|
TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ static void test_cdr_t(void) {
|
|||||||
|
|
||||||
static void test_car_list(void) {
|
static void test_car_list(void) {
|
||||||
input = ucl_tuple_create(
|
input = ucl_tuple_create(
|
||||||
ucl_string_create(strdup("foo")),
|
ucl_string_create("foo"),
|
||||||
ucl_t_create());
|
ucl_t_create());
|
||||||
response = ucl_car(input);
|
response = ucl_car(input);
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ static void test_car_list(void) {
|
|||||||
static void test_cdr_list(void) {
|
static void test_cdr_list(void) {
|
||||||
input = ucl_tuple_create(
|
input = ucl_tuple_create(
|
||||||
ucl_t_create(),
|
ucl_t_create(),
|
||||||
ucl_string_create(strdup("foo")));
|
ucl_string_create("foo"));
|
||||||
response = ucl_cdr(input);
|
response = ucl_cdr(input);
|
||||||
|
|
||||||
TEST_ASSERT_OBJ_STRING(ucl_car(response));
|
TEST_ASSERT_OBJ_STRING(ucl_car(response));
|
||||||
@@ -146,7 +146,7 @@ static void test_list_nth_list_0() {
|
|||||||
response = ucl_list_nth(
|
response = ucl_list_nth(
|
||||||
ucl_tuple_create(
|
ucl_tuple_create(
|
||||||
ucl_t_create(),
|
ucl_t_create(),
|
||||||
ucl_string_create(strdup("foo"))),
|
ucl_string_create("foo")),
|
||||||
0);
|
0);
|
||||||
|
|
||||||
TEST_ASSERT_T(response);
|
TEST_ASSERT_T(response);
|
||||||
@@ -156,7 +156,7 @@ static void test_list_nth_list_1() {
|
|||||||
response = ucl_list_nth(
|
response = ucl_list_nth(
|
||||||
ucl_tuple_create(
|
ucl_tuple_create(
|
||||||
ucl_t_create(),
|
ucl_t_create(),
|
||||||
ucl_string_create(strdup("foo"))),
|
ucl_string_create("foo")),
|
||||||
1);
|
1);
|
||||||
|
|
||||||
TEST_ASSERT_OBJ_STRING(response);
|
TEST_ASSERT_OBJ_STRING(response);
|
||||||
|
|||||||
Reference in New Issue
Block a user