Refactor with new name uclisp

This commit is contained in:
2022-10-28 23:19:19 -04:00
parent 26a0d17074
commit d97be8ec4b
22 changed files with 596 additions and 580 deletions

2
README
View File

@@ -1 +1 @@
A LISP implementation that simply is. A LISP implementation which aspires to target microcontrollers.

View File

@@ -36,7 +36,7 @@ test_env.Append(CPPPATH=test_lib_includes)
lib_objs = [env.Object(p) for p in lib_srcs] lib_objs = [env.Object(p) for p in lib_srcs]
env.Program(build_dir + "nihilispm", lib_objs + program_sources) env.Program(build_dir + "uclisp", lib_objs + program_sources)
# Generate unit test executables # Generate unit test executables

View File

@@ -1,6 +1,6 @@
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_utility.h" #include "utility.h"
#include "builtins.h" #include "builtins.h"
#include <assert.h> #include <assert.h>
@@ -8,131 +8,131 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
LISP_FUNC_0(nl_builtin_hello_world) { LISP_FUNC_0(ucl_builtin_hello_world) {
return nl_string_create(strdup("Hello, world!")); return ucl_string_create(strdup("Hello, world!"));
} }
LISP_FUNC_1(nl_builtin_type, arg) { LISP_FUNC_1(ucl_builtin_type, arg) {
switch (arg->type) { switch (arg->type) {
case NL_TYPE_CELL: case UCL_TYPE_CELL:
return nl_symbol_create(strdup("list")); return ucl_symbol_create(strdup("list"));
case NL_TYPE_SYMBOL: case UCL_TYPE_SYMBOL:
return nl_symbol_create(strdup("symbol")); return ucl_symbol_create(strdup("symbol"));
case NL_TYPE_INT: case UCL_TYPE_INT:
return nl_symbol_create(strdup("int")); return ucl_symbol_create(strdup("int"));
case NL_TYPE_STRING: case UCL_TYPE_STRING:
return nl_symbol_create(strdup("string")); return ucl_symbol_create(strdup("string"));
case NL_TYPE_ERROR: case UCL_TYPE_ERROR:
return nl_symbol_create(strdup("error")); return ucl_symbol_create(strdup("error"));
case NL_TYPE_COUNT: case UCL_TYPE_COUNT:
assert(0); assert(0);
return NULL; return NULL;
} }
} }
LISP_FUNC_1(nl_builtin_error, arg) { LISP_FUNC_1(ucl_builtin_error, arg) {
if (arg->type != NL_TYPE_STRING) { if (arg->type != UCL_TYPE_STRING) {
return nl_error_create("Expected type string passed to 'error'"); return ucl_error_create("Expected type string passed to 'error'");
} }
return nl_error_create(strdup(arg->error)); return ucl_error_create(strdup(arg->error));
} }
LISP_FUNC_1(nl_builtin_symbol_p, arg) { LISP_FUNC_1(ucl_builtin_symbol_p, arg) {
return nl_predicate(arg->type == NL_TYPE_SYMBOL); return ucl_predicate(arg->type == UCL_TYPE_SYMBOL);
} }
LISP_FUNC_1(nl_builtin_string_p, arg) { LISP_FUNC_1(ucl_builtin_string_p, arg) {
return nl_predicate(arg->type == NL_TYPE_STRING); return ucl_predicate(arg->type == UCL_TYPE_STRING);
} }
LISP_FUNC_1(nl_builtin_int_p, arg) { LISP_FUNC_1(ucl_builtin_int_p, arg) {
return nl_predicate(arg->type == NL_TYPE_INT); return ucl_predicate(arg->type == UCL_TYPE_INT);
} }
LISP_FUNC_1(nl_builtin_list_p, arg) { LISP_FUNC_1(ucl_builtin_list_p, arg) {
return nl_predicate(arg->type == NL_TYPE_CELL); return ucl_predicate(arg->type == UCL_TYPE_CELL);
} }
LISP_FUNC_1(nl_builtin_error_p, arg) { LISP_FUNC_1(ucl_builtin_error_p, arg) {
return nl_predicate(arg->type == NL_TYPE_ERROR); return ucl_predicate(arg->type == UCL_TYPE_ERROR);
} }
LISP_FUNC_1(nl_builtin_car, arg) { LISP_FUNC_1(ucl_builtin_car, arg) {
return nl_car(arg); return ucl_car(arg);
} }
LISP_FUNC_1(nl_builtin_cdr, arg) { LISP_FUNC_1(ucl_builtin_cdr, arg) {
return nl_cdr(arg); return ucl_cdr(arg);
} }
LISP_FUNC_2(nl_builtin_add, arg0, arg1) { LISP_FUNC_2(ucl_builtin_add, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'add'"); return ucl_error_create("Invalid type of argument 0 to 'add'");
} }
if (arg1->type != NL_TYPE_INT) { if (arg1->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'add'"); return ucl_error_create("Invalid type of argument 1 to 'add'");
} }
return nl_int_create(arg0->integer + arg1->integer); return ucl_int_create(arg0->integer + arg1->integer);
} }
LISP_FUNC_2(nl_builtin_sub, arg0, arg1) { LISP_FUNC_2(ucl_builtin_sub, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'sub'"); return ucl_error_create("Invalid type of argument 0 to 'sub'");
} }
if (arg1->type != NL_TYPE_INT) { if (arg1->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'sub'"); return ucl_error_create("Invalid type of argument 1 to 'sub'");
} }
return nl_int_create(arg0->integer - arg1->integer); return ucl_int_create(arg0->integer - arg1->integer);
} }
LISP_FUNC_2(nl_builtin_mul, arg0, arg1) { LISP_FUNC_2(ucl_builtin_mul, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mul'"); return ucl_error_create("Invalid type of argument 0 to 'mul'");
} }
if (arg1->type != NL_TYPE_INT) { if (arg1->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mul'"); return ucl_error_create("Invalid type of argument 1 to 'mul'");
} }
return nl_int_create(arg0->integer * arg1->integer); return ucl_int_create(arg0->integer * arg1->integer);
} }
LISP_FUNC_2(nl_builtin_div, arg0, arg1) { LISP_FUNC_2(ucl_builtin_div, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'div'"); return ucl_error_create("Invalid type of argument 0 to 'div'");
} }
if (arg1->type != NL_TYPE_INT) { if (arg1->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'div'"); return ucl_error_create("Invalid type of argument 1 to 'div'");
} }
return nl_int_create(arg0->integer / arg1->integer); return ucl_int_create(arg0->integer / arg1->integer);
} }
LISP_FUNC_2(nl_builtin_mod, arg0, arg1) { LISP_FUNC_2(ucl_builtin_mod, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mod'"); return ucl_error_create("Invalid type of argument 0 to 'mod'");
} }
if (arg1->type != NL_TYPE_INT) { if (arg1->type != UCL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mod'"); return ucl_error_create("Invalid type of argument 1 to 'mod'");
} }
return nl_int_create(arg0->integer % arg1->integer); return ucl_int_create(arg0->integer % arg1->integer);
} }
LISP_FUNC_2(nl_builtin_concat, arg0, arg1) { LISP_FUNC_2(ucl_builtin_concat, arg0, arg1) {
if (arg0->type != NL_TYPE_STRING) { if (arg0->type != UCL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 0 to 'concat'"); return ucl_error_create("Invalid type of argument 0 to 'concat'");
} }
if (arg1->type != NL_TYPE_STRING) { if (arg1->type != UCL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 1 to 'concat'"); return ucl_error_create("Invalid type of argument 1 to 'concat'");
} }
int len = strlen(arg0->string) + strlen(arg1->string); int len = strlen(arg0->string) + strlen(arg1->string);
@@ -141,10 +141,10 @@ LISP_FUNC_2(nl_builtin_concat, arg0, arg1) {
strcat(outstr, arg0->string); strcat(outstr, arg0->string);
strcat(outstr, arg1->string); strcat(outstr, arg1->string);
return nl_string_create(outstr); return ucl_string_create(outstr);
} }
LISP_FUNC_0(nl_builtin_now_millis_mono) { LISP_FUNC_0(ucl_builtin_now_millis_mono) {
// TODO: Implement and move to a 'platform' file // TODO: Implement and move to a 'platform' file
return NULL; return NULL;
} }

View File

@@ -1,61 +1,66 @@
#include "nihilispm_utility.h" #ifndef _UCLISP_BUILTINS_H_
#define _UCLISP_BUILTINS_H_
#include "utility.h"
#define LISP_FUNC_0(func_name) \ #define LISP_FUNC_0(func_name) \
static struct nl_object *func_name##_impl(); \ static struct ucl_object *func_name##_impl(); \
struct nl_object *func_name(struct nl_object *args) { \ struct ucl_object *func_name(struct ucl_object *args) { \
if (args->cell.car != NULL) { \ if (args->cell.car != NULL) { \
return NULL; \ return NULL; \
} \ } \
return func_name##_impl(); \ return func_name##_impl(); \
} \ } \
static struct nl_object *func_name##_impl() static struct ucl_object *func_name##_impl()
#define LISP_FUNC_1(func_name, arg0_name) \ #define LISP_FUNC_1(func_name, arg0_name) \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name); \ static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name); \
struct nl_object *func_name(struct nl_object *args) { \ struct ucl_object *func_name(struct ucl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \ struct ucl_object *len_obj = ucl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \ if (len_obj->type != UCL_TYPE_INT) { \
return NULL; \ return NULL; \
} \ } \
if (len_obj->integer != 1) { \ if (len_obj->integer != 1) { \
return NULL; \ return NULL; \
} \ } \
struct nl_object *arg0 = nl_car(args); \ struct ucl_object *arg0 = ucl_car(args); \
return func_name##_impl(arg0); \ return func_name##_impl(arg0); \
} \ } \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name) static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name)
// TODO: Unroll the args more efficiently, this is O(n^2) // TODO: Unroll the args more efficiently, this is O(n^2)
#define LISP_FUNC_2(func_name, arg0_name, arg1_name) \ #define LISP_FUNC_2(func_name, arg0_name, arg1_name) \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name); \ static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name, struct ucl_object *arg1_name); \
struct nl_object *func_name(struct nl_object *args) { \ struct ucl_object *func_name(struct ucl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \ struct ucl_object *len_obj = ucl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \ if (len_obj->type != UCL_TYPE_INT) { \
return NULL; \ return NULL; \
} \ } \
if (len_obj->integer != 2) { \ if (len_obj->integer != 2) { \
return NULL; \ return NULL; \
} \ } \
struct nl_object *arg0 = nl_list_nth(args, 0); \ struct ucl_object *arg0 = ucl_list_nth(args, 0); \
struct nl_object *arg1 = nl_list_nth(args, 1); \ struct ucl_object *arg1 = ucl_list_nth(args, 1); \
return func_name##_impl(arg0, arg1); \ return func_name##_impl(arg0, arg1); \
} \ } \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name) static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name, struct ucl_object *arg1_name)
struct nl_object *nl_builtin_hello_world(struct nl_object *args); struct ucl_object *ucl_builtin_hello_world(struct ucl_object *args);
struct nl_object *nl_builtin_error(struct nl_object *args); struct ucl_object *ucl_builtin_error(struct ucl_object *args);
struct nl_object *nl_builtin_type(struct nl_object *args); struct ucl_object *ucl_builtin_type(struct ucl_object *args);
struct nl_object *nl_builtin_symbol_p(struct nl_object *args); struct ucl_object *ucl_builtin_symbol_p(struct ucl_object *args);
struct nl_object *nl_builtin_string_p(struct nl_object *args); struct ucl_object *ucl_builtin_string_p(struct ucl_object *args);
struct nl_object *nl_builtin_int_p(struct nl_object *args); struct ucl_object *ucl_builtin_int_p(struct ucl_object *args);
struct nl_object *nl_builtin_list_p(struct nl_object *args); struct ucl_object *ucl_builtin_list_p(struct ucl_object *args);
struct nl_object *nl_builtin_add(struct nl_object *args); struct ucl_object *ucl_builtin_add(struct ucl_object *args);
struct nl_object *nl_builtin_sub(struct nl_object *args); struct ucl_object *ucl_builtin_sub(struct ucl_object *args);
struct nl_object *nl_builtin_mul(struct nl_object *args); struct ucl_object *ucl_builtin_mul(struct ucl_object *args);
struct nl_object *nl_builtin_div(struct nl_object *args); struct ucl_object *ucl_builtin_div(struct ucl_object *args);
struct nl_object *nl_builtin_mod(struct nl_object *args); struct ucl_object *ucl_builtin_mod(struct ucl_object *args);
struct nl_object *nl_builtin_concat(struct nl_object *args); struct ucl_object *ucl_builtin_concat(struct ucl_object *args);
struct nl_object *nl_builtin_now_millis_mono(struct nl_object *args); struct ucl_object *ucl_builtin_now_millis_mono(struct ucl_object *args);
#endif

View File

@@ -1,26 +1,26 @@
#include <assert.h> #include <assert.h>
#include <stddef.h> #include <stddef.h>
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_utility.h" #include "utility.h"
#include "nihilispm_state.h" #include "state.h"
struct nl_object *nl_evaluate_list(struct nl_state *state, struct nl_object *list) { struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object *list) {
// TODO: Recursively eval args // TODO: Recursively eval args
struct nl_object *evaluated_list = nl_nil_create(); struct ucl_object *evaluated_list = ucl_nil_create();
FOREACH_LIST(list, iter, item) { FOREACH_LIST(list, iter, item) {
struct nl_object *obj = nl_evaluate(state, item); struct ucl_object *obj = ucl_evaluate(state, item);
nl_list_append(evaluated_list, obj); ucl_list_append(evaluated_list, obj);
}; };
struct nl_object *fun = nl_car(evaluated_list); struct ucl_object *fun = ucl_car(evaluated_list);
struct nl_object *args = nl_cdr(evaluated_list); struct ucl_object *args = ucl_cdr(evaluated_list);
struct nl_object *result = NULL; struct ucl_object *result = NULL;
assert(fun->type == NL_TYPE_BUILTIN); assert(fun->type == UCL_TYPE_BUILTIN);
if (fun->type == NL_TYPE_BUILTIN) { if (fun->type == UCL_TYPE_BUILTIN) {
result = fun->builtin(args); result = fun->builtin(args);
} }
@@ -31,20 +31,20 @@ struct nl_object *nl_evaluate_list(struct nl_state *state, struct nl_object *lis
return result; return result;
} }
struct nl_object *nl_evaluate(struct nl_state *state, struct nl_object *obj) { struct ucl_object *ucl_evaluate(struct ucl_state *state, struct ucl_object *obj) {
assert(obj != NULL); assert(obj != NULL);
switch (obj->type) { switch (obj->type) {
case NL_TYPE_CELL: case UCL_TYPE_CELL:
return nl_evaluate_list(state, obj); return ucl_evaluate_list(state, obj);
case NL_TYPE_SYMBOL: case UCL_TYPE_SYMBOL:
return nl_state_get(state, obj->symbol); return ucl_state_get(state, obj->symbol);
case NL_TYPE_INT: case UCL_TYPE_INT:
case NL_TYPE_STRING: case UCL_TYPE_STRING:
case NL_TYPE_ERROR: case UCL_TYPE_ERROR:
return obj; return obj;
case NL_TYPE_BUILTIN: case UCL_TYPE_BUILTIN:
case NL_TYPE_COUNT: case UCL_TYPE_COUNT:
assert(0); assert(0);
return NULL; return NULL;
} }

29
src/internal.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef _UCLISP_INTERNAL_H_
#define _UCLISP_INTERNAL_H_
#include "uclisp.h"
#include <stddef.h>
#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0])
#define FOREACH_LIST(list, iter_name, item_name) \
FOREACH_LIST_COND(list, iter_name, item_name, true)
#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \
for (struct ucl_object *iter_name = (list), *item_name = iter_name->cell.car; \
item_name != NULL && iter_name != NULL && (cond); \
iter_name = iter_name->cell.cdr, item_name = (iter_name == NULL) ? NULL : iter_name->cell.car)
struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr);
struct ucl_object *ucl_int_create(int integer);
struct ucl_object *ucl_symbol_create(const char* symbol);
struct ucl_object *ucl_string_create(const char* string);
struct ucl_object *ucl_error_create(const char* error);
struct ucl_object *ucl_builtin_create(ucl_builtin builtin);
void ucl_object_delete(struct ucl_object *obj);
// For testing
struct ucl_object *ucl_token_next(const char **curr_src);
struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom);
#endif

View File

@@ -1,27 +1,27 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_state.h" #include "state.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_utility.h" #include "utility.h"
#include "builtins.h" #include "builtins.h"
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
(void) argc, (void) argv; (void) argc, (void) argv;
struct nl_state *state = nl_state_create(); struct ucl_state *state = ucl_state_create();
nl_state_put(state, "+", nl_builtin_create(nl_builtin_add)); ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
nl_state_put(state, "-", nl_builtin_create(nl_builtin_sub)); ucl_state_put(state, "-", ucl_builtin_create(ucl_builtin_sub));
nl_state_put(state, "*", nl_builtin_create(nl_builtin_mul)); ucl_state_put(state, "*", ucl_builtin_create(ucl_builtin_mul));
nl_state_put(state, "/", nl_builtin_create(nl_builtin_div)); ucl_state_put(state, "/", ucl_builtin_create(ucl_builtin_div));
nl_state_put(state, "%", nl_builtin_create(nl_builtin_mod)); ucl_state_put(state, "%", ucl_builtin_create(ucl_builtin_mod));
struct nl_object *sexp = nl_parse(argv[1]); struct ucl_object *sexp = ucl_parse(argv[1]);
struct nl_object *result = nl_evaluate(state, nl_car(sexp)); struct ucl_object *result = ucl_evaluate(state, ucl_car(sexp));
assert(result != NULL); assert(result != NULL);
assert(result->type == NL_TYPE_INT); assert(result->type == UCL_TYPE_INT);
printf("%d\n", result->integer); printf("%d\n", result->integer);

View File

@@ -1,93 +1,93 @@
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
static struct nl_object *nl_object_alloc(); static struct ucl_object *ucl_object_alloc();
struct nl_object *nl_cell_create(struct nl_object *car, struct nl_object *cdr) struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_CELL; obj->type = UCL_TYPE_CELL;
obj->cell.car = car; obj->cell.car = car;
obj->cell.cdr = cdr; obj->cell.cdr = cdr;
return obj; return obj;
} }
struct nl_object *nl_int_create(int integer) struct ucl_object *ucl_int_create(int integer)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_INT; obj->type = UCL_TYPE_INT;
obj->integer = integer; obj->integer = integer;
return obj; return obj;
} }
struct nl_object *nl_symbol_create(const char *symbol) struct ucl_object *ucl_symbol_create(const char *symbol)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_SYMBOL; obj->type = UCL_TYPE_SYMBOL;
obj->symbol = symbol; obj->symbol = symbol;
return obj; return obj;
} }
struct nl_object *nl_string_create(const char *string) struct ucl_object *ucl_string_create(const char *string)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_STRING; obj->type = UCL_TYPE_STRING;
obj->string = string; obj->string = string;
return obj; return obj;
} }
struct nl_object *nl_error_create(const char *error) struct ucl_object *ucl_error_create(const char *error)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_ERROR; obj->type = UCL_TYPE_ERROR;
obj->error = error; obj->error = error;
return obj; return obj;
} }
struct nl_object *nl_builtin_create(nl_builtin builtin) struct ucl_object *ucl_builtin_create(ucl_builtin builtin)
{ {
struct nl_object* obj = nl_object_alloc(); struct ucl_object* obj = ucl_object_alloc();
obj->type = NL_TYPE_BUILTIN; obj->type = UCL_TYPE_BUILTIN;
obj->builtin = builtin; obj->builtin = builtin;
return obj; return obj;
} }
static struct nl_object* nl_object_alloc() { static struct ucl_object* ucl_object_alloc() {
return malloc(sizeof(struct nl_object)); return malloc(sizeof(struct ucl_object));
} }
void nl_object_delete(struct nl_object *obj) { void ucl_object_delete(struct ucl_object *obj) {
if (obj == NULL) { if (obj == NULL) {
return; return;
} }
switch (obj->type) { switch (obj->type) {
case NL_TYPE_CELL: case UCL_TYPE_CELL:
nl_object_delete(obj->cell.car); ucl_object_delete(obj->cell.car);
obj->cell.car = NULL; obj->cell.car = NULL;
nl_object_delete(obj->cell.cdr); ucl_object_delete(obj->cell.cdr);
obj->cell.cdr = NULL; obj->cell.cdr = NULL;
break; break;
case NL_TYPE_SYMBOL: case UCL_TYPE_SYMBOL:
free((void *) obj->symbol); free((void *) obj->symbol);
obj->symbol = NULL; obj->symbol = NULL;
break; break;
case NL_TYPE_STRING: case UCL_TYPE_STRING:
free((void *) obj->string); free((void *) obj->string);
obj->string = NULL; obj->string = NULL;
break; break;
case NL_TYPE_ERROR: case UCL_TYPE_ERROR:
free((void *) obj->error); free((void *) obj->error);
obj->error = NULL; obj->error = NULL;
break; break;
case NL_TYPE_INT: case UCL_TYPE_INT:
case NL_TYPE_BUILTIN: case UCL_TYPE_BUILTIN:
case NL_TYPE_COUNT: case UCL_TYPE_COUNT:
break; break;
} }
free(obj); free(obj);

View File

@@ -1,46 +0,0 @@
#ifndef _NIHILISPM_H_
#define _NIHILISPM_H_
enum nl_type {
NL_TYPE_CELL = 0,
NL_TYPE_SYMBOL = 1,
NL_TYPE_INT = 2,
NL_TYPE_STRING = 3,
NL_TYPE_ERROR = 4,
NL_TYPE_BUILTIN = 5,
NL_TYPE_COUNT = 6,
};
struct nl_cell {
struct nl_object *car;
struct nl_object *cdr;
};
typedef struct nl_object *(*nl_builtin)(struct nl_object *args);
struct nl_object {
enum nl_type type;
union {
struct nl_cell cell;
const char *symbol;
int integer;
const char *string;
const char *error;
nl_builtin builtin;
};
};
struct nl_parse_result {
int result;
struct nl_cell *statement;
};
struct nl_state; // TODO
struct nl_object *nl_tokenize(const char *source);
struct nl_object *nl_parse(const char *sexp);
struct nl_object *nl_evaluate(struct nl_state *state, struct nl_object *sexp);
#endif

View File

@@ -1,24 +0,0 @@
#include "nihilispm.h"
#include <stddef.h>
#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0])
#define FOREACH_LIST(list, iter_name, item_name) \
FOREACH_LIST_COND(list, iter_name, item_name, true)
#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \
for (struct nl_object *iter_name = (list), *item_name = iter_name->cell.car; \
item_name != NULL && iter_name != NULL && (cond); \
iter_name = iter_name->cell.cdr, item_name = (iter_name == NULL) ? NULL : iter_name->cell.car)
struct nl_object *nl_cell_create(struct nl_object *car, struct nl_object *cdr);
struct nl_object *nl_int_create(int integer);
struct nl_object *nl_symbol_create(const char* symbol);
struct nl_object *nl_string_create(const char* string);
struct nl_object *nl_error_create(const char* error);
struct nl_object *nl_builtin_create(nl_builtin builtin);
void nl_object_delete(struct nl_object *obj);
// For testing
struct nl_object *nl_token_next(const char **curr_src);
struct nl_object *nl_parse_token_atom(struct nl_object *maybe_atom);

View File

@@ -1,11 +0,0 @@
#ifndef _NIHILISPM_STATE_H_
#define _NIHILISPM_STATE_H_
struct nl_state;
struct nl_state *nl_state_create();
void nl_state_delete(struct nl_state *state);
struct nl_object *nl_state_get(struct nl_state *state, const char *name);
void nl_state_put(struct nl_state *state, const char *name, struct nl_object *obj);
#endif

View File

@@ -1,30 +0,0 @@
#include <stdbool.h>
#include "nihilispm.h"
struct nl_object *nl_nil_create();
struct nl_object *nl_t_create();
struct nl_object *nl_predicate(bool value);
struct nl_object *nl_truthy(struct nl_object *arg);
struct nl_object* nl_car(struct nl_object *list);
struct nl_object* nl_cdr(struct nl_object *list);
struct nl_object* nl_list_length(struct nl_object *list);
struct nl_object* nl_list_nth(struct nl_object *list, int n);
struct nl_object* nl_list_append(struct nl_object *list, struct nl_object *obj);
struct nl_object* nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1);
#define NL_RET_IF_ERROR(obj) \
do { \
if ((obj)->type == NL_TYPE_ERROR) { \
return obj; \
} while(0)
#define NL_COND_OR_RET_ERROR(cond, msg) \
do { \
if (!(cond)) { \
return nl_error_create(strdup(msg)); \
} \
} while(0)

View File

@@ -1,5 +1,5 @@
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
// TODO: remove these // TODO: remove these
#include <stdbool.h> #include <stdbool.h>
@@ -21,19 +21,19 @@ static const char *reserved_symbols[] = {
// TODO: remove malloc and strndup calls // TODO: remove malloc and strndup calls
static bool nl_is_whitespace(char c) { static bool ucl_is_whitespace(char c) {
return c == ' ' || c == '\n' || c == '\t'; return c == ' ' || c == '\n' || c == '\t';
} }
static bool nl_is_token(char c) { static bool ucl_is_token(char c) {
return c == START_LIST_CHAR || c == END_LIST_CHAR || c == QUOTE_CHAR; return c == START_LIST_CHAR || c == END_LIST_CHAR || c == QUOTE_CHAR;
} }
static bool nl_is_delimiter(char c) { static bool ucl_is_delimiter(char c) {
return nl_is_whitespace(c) || nl_is_token(c) || c == '\0'; return ucl_is_whitespace(c) || ucl_is_token(c) || c == '\0';
} }
struct nl_object *nl_token_next(const char **curr_src) { struct ucl_object *ucl_token_next(const char **curr_src) {
assert(*curr_src != NULL); assert(*curr_src != NULL);
const char *start = *curr_src; const char *start = *curr_src;
@@ -54,7 +54,7 @@ struct nl_object *nl_token_next(const char **curr_src) {
str[0] = **curr_src; str[0] = **curr_src;
str[1] = '\0'; str[1] = '\0';
(*curr_src)++; (*curr_src)++;
return nl_cell_create(nl_symbol_create(str), NULL); return ucl_cell_create(ucl_symbol_create(str), NULL);
case QUOTE_CHAR: case QUOTE_CHAR:
// skip beginning quote // skip beginning quote
(*curr_src)++; (*curr_src)++;
@@ -66,19 +66,19 @@ struct nl_object *nl_token_next(const char **curr_src) {
(*curr_src)++; (*curr_src)++;
// -2 for removing start/end quotes // -2 for removing start/end quotes
str = strndup(start + 1, *curr_src - start - 2); str = strndup(start + 1, *curr_src - start - 2);
return nl_cell_create(nl_string_create(str), NULL); return ucl_cell_create(ucl_string_create(str), NULL);
case '0'...'9': { case '0'...'9': {
char *end = NULL; char *end = NULL;
long value = strtol(*curr_src, &end, 0); long value = strtol(*curr_src, &end, 0);
*curr_src = end; *curr_src = end;
return nl_cell_create(nl_int_create(value), NULL); return ucl_cell_create(ucl_int_create(value), NULL);
} }
default: default:
while (!nl_is_delimiter(**curr_src)) { while (!ucl_is_delimiter(**curr_src)) {
(*curr_src)++; (*curr_src)++;
} }
str = strndup(start, *curr_src - start); str = strndup(start, *curr_src - start);
return nl_cell_create(nl_symbol_create(str), NULL); return ucl_cell_create(ucl_symbol_create(str), NULL);
} }
} }
@@ -87,11 +87,11 @@ struct nl_object *nl_token_next(const char **curr_src) {
return NULL; return NULL;
} }
struct nl_object *nl_tokenize(const char *source) { struct ucl_object *ucl_tokenize(const char *source) {
struct nl_object *tokens = NULL, *curr_token = NULL, **next_token = &tokens; struct ucl_object *tokens = NULL, *curr_token = NULL, **next_token = &tokens;
const char *curr_src = source; const char *curr_src = source;
while ((curr_token = nl_token_next(&curr_src)) != NULL) { while ((curr_token = ucl_token_next(&curr_src)) != NULL) {
*next_token = curr_token; *next_token = curr_token;
next_token = &curr_token->cell.cdr; next_token = &curr_token->cell.cdr;
} }
@@ -99,78 +99,78 @@ struct nl_object *nl_tokenize(const char *source) {
return tokens; return tokens;
} }
struct nl_object *nl_parse_token_atom(struct nl_object *maybe_atom) { struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom) {
struct nl_object *atom = NULL; struct ucl_object *atom = NULL;
switch (maybe_atom->type) { switch (maybe_atom->type) {
case NL_TYPE_CELL: case UCL_TYPE_CELL:
// Cell types currently are not valid for tokens // Cell types currently are not valid for tokens
assert(false); assert(false);
break; break;
case NL_TYPE_SYMBOL: { case UCL_TYPE_SYMBOL: {
// Check for reserved tokens first, which indicate special, non-atom behavior // Check for reserved tokens first, which indicate special, non-atom behavior
for (int i = 0; i < ARRAY_SIZE(reserved_symbols); i++) { for (int i = 0; i < ARRAY_SIZE(reserved_symbols); i++) {
if (!strcmp(maybe_atom->string, reserved_symbols[i])) { if (!strcmp(maybe_atom->string, reserved_symbols[i])) {
return NULL; return NULL;
} }
} }
atom = nl_symbol_create(strdup(maybe_atom->symbol)); atom = ucl_symbol_create(strdup(maybe_atom->symbol));
break; break;
} }
case NL_TYPE_STRING: case UCL_TYPE_STRING:
atom = nl_string_create(strdup(maybe_atom->string)); atom = ucl_string_create(strdup(maybe_atom->string));
break; break;
case NL_TYPE_INT: case UCL_TYPE_INT:
atom = nl_int_create(maybe_atom->integer); atom = ucl_int_create(maybe_atom->integer);
break; break;
case NL_TYPE_COUNT: case UCL_TYPE_COUNT:
assert(false); assert(false);
} }
return atom; return atom;
} }
static struct nl_object *nl_parse_tokens_recursive(struct nl_object **token_iter) { static struct ucl_object *ucl_parse_tokens_recursive(struct ucl_object **token_iter) {
// Invariants: // Invariants:
// - This function returns NULL if it encounters the first unmatched END_LIST_CHAR // - This function returns NULL if it encounters the first unmatched END_LIST_CHAR
// - This function returns NULL if it encounters end-of-list without first finding a START_LIST_CHAR // - This function returns NULL if it encounters end-of-list without first finding a START_LIST_CHAR
// - This function returns an nl_object of the atom or list at **token_iter // - This function returns an ucl_object of the atom or list at **token_iter
assert(token_iter != NULL); assert(token_iter != NULL);
if (*token_iter == NULL) { if (*token_iter == NULL) {
return NULL; return NULL;
} }
assert((*token_iter)->type == NL_TYPE_CELL); assert((*token_iter)->type == UCL_TYPE_CELL);
struct nl_object *token = (*token_iter)->cell.car; struct ucl_object *token = (*token_iter)->cell.car;
struct nl_object *next_sexp = nl_parse_token_atom(token); struct ucl_object *next_sexp = ucl_parse_token_atom(token);
if (next_sexp != NULL) { if (next_sexp != NULL) {
*token_iter = (*token_iter)->cell.cdr; *token_iter = (*token_iter)->cell.cdr;
return next_sexp; return next_sexp;
} }
assert(token->type == NL_TYPE_SYMBOL); assert(token->type == UCL_TYPE_SYMBOL);
if (token->symbol[0] == START_LIST_CHAR) { if (token->symbol[0] == START_LIST_CHAR) {
struct nl_object *list = NULL; struct ucl_object *list = NULL;
struct nl_object **next_node = &list; struct ucl_object **next_node = &list;
// Consume the START_LIST_CHAR // Consume the START_LIST_CHAR
*token_iter = (*token_iter)->cell.cdr; *token_iter = (*token_iter)->cell.cdr;
while (1) { while (1) {
token = (*token_iter)->cell.car; token = (*token_iter)->cell.car;
if (token->type == NL_TYPE_SYMBOL && token->symbol[0] == END_LIST_CHAR) { if (token->type == UCL_TYPE_SYMBOL && token->symbol[0] == END_LIST_CHAR) {
*token_iter = (*token_iter)->cell.cdr; *token_iter = (*token_iter)->cell.cdr;
if (list == NULL) { if (list == NULL) {
list = nl_cell_create(NULL, NULL); list = ucl_cell_create(NULL, NULL);
} }
return list; return list;
} }
next_sexp = nl_parse_tokens_recursive(token_iter); next_sexp = ucl_parse_tokens_recursive(token_iter);
if (next_sexp == NULL) { if (next_sexp == NULL) {
// Error somewhere in the recursive parsing // Error somewhere in the recursive parsing
nl_object_delete(list); ucl_object_delete(list);
return NULL; return NULL;
} }
*next_node = nl_cell_create(next_sexp, NULL); *next_node = ucl_cell_create(next_sexp, NULL);
next_node = &(*next_node)->cell.cdr; next_node = &(*next_node)->cell.cdr;
} }
} else if (token->symbol[0] == END_LIST_CHAR) { } else if (token->symbol[0] == END_LIST_CHAR) {
@@ -185,42 +185,42 @@ static struct nl_object *nl_parse_tokens_recursive(struct nl_object **token_iter
// parse_tokens -> doesn't care about quotes, terminates on EOF // parse_tokens -> doesn't care about quotes, terminates on EOF
// parse_tokens_recursive -> error on EOF) // parse_tokens_recursive -> error on EOF)
struct nl_object *nl_parse_tokens(struct nl_object *tokens) { struct ucl_object *ucl_parse_tokens(struct ucl_object *tokens) {
struct nl_object* resultl = NULL; struct ucl_object* resultl = NULL;
struct nl_object** token_iter = &tokens; struct ucl_object** token_iter = &tokens;
struct nl_object** next_cell = &resultl; struct ucl_object** next_cell = &resultl;
while (*token_iter != NULL) { while (*token_iter != NULL) {
struct nl_object *new_sexp = nl_parse_tokens_recursive(token_iter); struct ucl_object *new_sexp = ucl_parse_tokens_recursive(token_iter);
if (new_sexp == NULL) { if (new_sexp == NULL) {
goto error; goto error;
} }
*next_cell = nl_cell_create(new_sexp, NULL); *next_cell = ucl_cell_create(new_sexp, NULL);
next_cell = &(*next_cell)->cell.cdr; next_cell = &(*next_cell)->cell.cdr;
} }
if (resultl == NULL) { if (resultl == NULL) {
return nl_cell_create(NULL, NULL); return ucl_cell_create(NULL, NULL);
} }
return resultl; return resultl;
error: error:
nl_object_delete(resultl); ucl_object_delete(resultl);
return NULL; return NULL;
} }
// TODO: Should the parse a single sexp (return the last-parsed position), or // TODO: Should the parse a single sexp (return the last-parsed position), or
// all sexps in the source (return a list of sexps)? // all sexps in the source (return a list of sexps)?
struct nl_object *nl_parse(const char *source) { struct ucl_object *ucl_parse(const char *source) {
struct nl_object *tokens = nl_tokenize(source); struct ucl_object *tokens = ucl_tokenize(source);
struct nl_object *sexp = nl_parse_tokens(tokens); struct ucl_object *sexp = ucl_parse_tokens(tokens);
nl_object_delete(tokens); ucl_object_delete(tokens);
return sexp; return sexp;
} }
/* struct ParseResult *nl_parse(const char *source) { */ /* struct ParseResult *ucl_parse(const char *source) { */
/* struct Cell *tokens = nl_tokenize(source); */ /* struct Cell *tokens = ucl_tokenize(source); */
/* struct Cell *sexp = n */ /* struct Cell *sexp = n */
/* } */ /* } */

View File

@@ -1,7 +1,7 @@
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_state.h" #include "state.h"
#include "nihilispm_utility.h" #include "utility.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@@ -10,18 +10,18 @@
// Implements state as a giant alist // Implements state as a giant alist
// TODO: Consider generalizing the alist concept // TODO: Consider generalizing the alist concept
struct nl_state { struct ucl_state {
struct nl_object *list; struct ucl_object *list;
}; };
#define NAME_POSITION 0 #define NAME_POSITION 0
#define DATA_POSITION 1 #define DATA_POSITION 1
static struct nl_object *nl_state_get_cell(struct nl_state *state, const char *name) { static struct ucl_object *ucl_state_get_cell(struct ucl_state *state, const char *name) {
FOREACH_LIST(state->list, iter, item) { FOREACH_LIST(state->list, iter, item) {
assert(item->type == NL_TYPE_CELL); assert(item->type == UCL_TYPE_CELL);
const char *item_name = nl_list_nth(item, NAME_POSITION)->string; const char *item_name = ucl_list_nth(item, NAME_POSITION)->string;
if (!strcmp(name, item_name)) { if (!strcmp(name, item_name)) {
return item; return item;
} }
@@ -30,18 +30,18 @@ static struct nl_object *nl_state_get_cell(struct nl_state *state, const char *n
return NULL; return NULL;
} }
struct nl_object *nl_state_get(struct nl_state *state, const char *name) { struct ucl_object *ucl_state_get(struct ucl_state *state, const char *name) {
struct nl_object *cell = nl_state_get_cell(state, name); struct ucl_object *cell = ucl_state_get_cell(state, name);
NL_COND_OR_RET_ERROR(cell != NULL, "Unknown name"); UCL_COND_OR_RET_ERROR(cell != NULL, "Unknown name");
return nl_list_nth(cell, DATA_POSITION); return ucl_list_nth(cell, DATA_POSITION);
} }
void nl_state_put(struct nl_state *state, const char *name, struct nl_object *obj) { void ucl_state_put(struct ucl_state *state, const char *name, struct ucl_object *obj) {
struct nl_object *cell = nl_state_get_cell(state, name); struct ucl_object *cell = ucl_state_get_cell(state, name);
if (cell == NULL) { if (cell == NULL) {
nl_list_append(state->list, ucl_list_append(state->list,
nl_tuple_create( ucl_tuple_create(
nl_string_create(strdup(name)), ucl_string_create(strdup(name)),
obj)); obj));
} else { } else {
// TODO: Refcounting / cleanup // TODO: Refcounting / cleanup
@@ -49,12 +49,12 @@ void nl_state_put(struct nl_state *state, const char *name, struct nl_object *ob
} }
} }
struct nl_state *nl_state_create() { struct ucl_state *ucl_state_create() {
struct nl_state *state = malloc(sizeof(struct nl_state)); struct ucl_state *state = malloc(sizeof(struct ucl_state));
state->list = nl_nil_create(); state->list = ucl_nil_create();
return state; return state;
} }
void nl_state_delete(struct nl_state *state) { void ucl_state_delete(struct ucl_state *state) {
nl_object_delete(state->list); ucl_object_delete(state->list);
} }

12
src/state.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef _UCLISP_STATE_H_
#define _UCLISP_STATE_H_
struct ucl_state;
struct ucl_state *ucl_state_create();
void ucl_state_delete(struct ucl_state *state);
struct ucl_object *ucl_state_get(struct ucl_state *state, const char *name);
void ucl_state_put(struct ucl_state *state, const char *name, struct ucl_object *obj);
#endif

46
src/uclisp.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef _UCLISP_H_
#define _UCLISP_H_
enum ucl_type {
UCL_TYPE_CELL = 0,
UCL_TYPE_SYMBOL = 1,
UCL_TYPE_INT = 2,
UCL_TYPE_STRING = 3,
UCL_TYPE_ERROR = 4,
UCL_TYPE_BUILTIN = 5,
UCL_TYPE_COUNT = 6,
};
struct ucl_cell {
struct ucl_object *car;
struct ucl_object *cdr;
};
typedef struct ucl_object *(*ucl_builtin)(struct ucl_object *args);
struct ucl_object {
enum ucl_type type;
union {
struct ucl_cell cell;
const char *symbol;
int integer;
const char *string;
const char *error;
ucl_builtin builtin;
};
};
struct ucl_parse_result {
int result;
struct ucl_cell *statement;
};
struct ucl_state; // TODO
struct ucl_object *ucl_tokenize(const char *source);
struct ucl_object *ucl_parse(const char *sexp);
struct ucl_object *ucl_evaluate(struct ucl_state *state, struct ucl_object *sexp);
#endif

View File

@@ -1,61 +1,61 @@
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_utility.h" #include "utility.h"
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
struct nl_object *nl_car(struct nl_object *list) { struct ucl_object *ucl_car(struct ucl_object *list) {
NL_COND_OR_RET_ERROR( UCL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL, list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_car'"); "Invalid type of argument 0 to 'ucl_car'");
struct nl_object *car = list->cell.car; struct ucl_object *car = list->cell.car;
if (car == NULL) { if (car == NULL) {
return nl_nil_create(); return ucl_nil_create();
} }
return car; return car;
} }
struct nl_object *nl_cdr(struct nl_object *list) { struct ucl_object *ucl_cdr(struct ucl_object *list) {
NL_COND_OR_RET_ERROR( UCL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL, list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_cdr'"); "Invalid type of argument 0 to 'ucl_cdr'");
struct nl_object *cdr = list->cell.cdr; struct ucl_object *cdr = list->cell.cdr;
if (cdr == NULL) { if (cdr == NULL) {
return nl_nil_create(); return ucl_nil_create();
} }
return cdr; return cdr;
} }
struct nl_object *nl_nil_create() { struct ucl_object *ucl_nil_create() {
return nl_cell_create(NULL, NULL); return ucl_cell_create(NULL, NULL);
} }
struct nl_object *nl_t_create() { struct ucl_object *ucl_t_create() {
return nl_symbol_create(strdup("t")); return ucl_symbol_create(strdup("t"));
} }
struct nl_object *nl_predicate(bool value) { struct ucl_object *ucl_predicate(bool value) {
if (value) { if (value) {
return nl_t_create(); return ucl_t_create();
} else { } else {
return nl_nil_create(); return ucl_nil_create();
} }
} }
struct nl_object *nl_list_length(struct nl_object *list) { struct ucl_object *ucl_list_length(struct ucl_object *list) {
NL_COND_OR_RET_ERROR( UCL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL, list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_length'"); "Invalid type of argument 0 to 'ucl_list_length'");
struct nl_object *node = list; struct ucl_object *node = list;
if (list->cell.car == NULL) { if (list->cell.car == NULL) {
return nl_int_create(0); return ucl_int_create(0);
} }
int length = 1; int length = 1;
@@ -64,18 +64,18 @@ struct nl_object *nl_list_length(struct nl_object *list) {
length++; length++;
} }
return nl_int_create(length); return ucl_int_create(length);
} }
struct nl_object *nl_list_nth(struct nl_object *list, int n) { struct ucl_object *ucl_list_nth(struct ucl_object *list, int n) {
NL_COND_OR_RET_ERROR( UCL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL, list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_'"); "Invalid type of argument 0 to 'ucl_list_'");
int length = nl_list_length(list)->integer; int length = ucl_list_length(list)->integer;
NL_COND_OR_RET_ERROR(length > n, "Position n >= list length in nl_list_nth"); UCL_COND_OR_RET_ERROR(length > n, "Position n >= list length in ucl_list_nth");
struct nl_object *node = list; struct ucl_object *node = list;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
node = node->cell.cdr; node = node->cell.cdr;
} }
@@ -83,19 +83,19 @@ struct nl_object *nl_list_nth(struct nl_object *list, int n) {
return node->cell.car; return node->cell.car;
} }
struct nl_object *nl_truthy(struct nl_object *obj) { struct ucl_object *ucl_truthy(struct ucl_object *obj) {
// TODO: Implement me // TODO: Implement me
return nl_error_create("Unimplemented function 'nl_truthy'"); return ucl_error_create("Unimplemented function 'ucl_truthy'");
} }
struct nl_object *nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1) { struct ucl_object *ucl_tuple_create(struct ucl_object *obj0, struct ucl_object *obj1) {
struct nl_object *tuple = nl_cell_create(obj0, NULL); struct ucl_object *tuple = ucl_cell_create(obj0, NULL);
nl_list_append(tuple, obj1); ucl_list_append(tuple, obj1);
return tuple; return tuple;
} }
struct nl_object *nl_list_append(struct nl_object *list, struct nl_object *obj) { struct ucl_object *ucl_list_append(struct ucl_object *list, struct ucl_object *obj) {
struct nl_object *iter = list; struct ucl_object *iter = list;
if (list->cell.car == NULL) { if (list->cell.car == NULL) {
list->cell.car = obj; list->cell.car = obj;
@@ -105,7 +105,7 @@ struct nl_object *nl_list_append(struct nl_object *list, struct nl_object *obj)
while (iter->cell.cdr != NULL) { while (iter->cell.cdr != NULL) {
iter = iter->cell.cdr; iter = iter->cell.cdr;
} }
iter->cell.cdr = nl_cell_create(obj, NULL); iter->cell.cdr = ucl_cell_create(obj, NULL);
return list; return list;
} }

35
src/utility.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef _UCLISP_UTILITY_H_
#define _UCLISP_UTILITY_H_
#include <stdbool.h>
#include "uclisp.h"
struct ucl_object *ucl_nil_create();
struct ucl_object *ucl_t_create();
struct ucl_object *ucl_predicate(bool value);
struct ucl_object *ucl_truthy(struct ucl_object *arg);
struct ucl_object* ucl_car(struct ucl_object *list);
struct ucl_object* ucl_cdr(struct ucl_object *list);
struct ucl_object* ucl_list_length(struct ucl_object *list);
struct ucl_object* ucl_list_nth(struct ucl_object *list, int n);
struct ucl_object* ucl_list_append(struct ucl_object *list, struct ucl_object *obj);
struct ucl_object* ucl_tuple_create(struct ucl_object *obj0, struct ucl_object *obj1);
#define UCL_RET_IF_ERROR(obj) \
do { \
if ((obj)->type == UCL_TYPE_ERROR) { \
return obj; \
} while(0)
#define UCL_COND_OR_RET_ERROR(cond, msg) \
do { \
if (!(cond)) { \
return ucl_error_create(strdup(msg)); \
} \
} while(0)
#endif

View File

@@ -2,25 +2,25 @@
#include <unity.h> #include <unity.h>
#include <string.h> #include <string.h>
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
/* static struct nl_parse_result *result; */ /* static struct ucl_parse_result *result; */
static struct nl_object *response; static struct ucl_object *response;
void setUp(void) { void setUp(void) {
response = NULL; response = NULL;
} }
void tearDown(void) { void tearDown(void) {
nl_object_delete(response); ucl_object_delete(response);
} }
static void test_token_next_empty_str(void) { static void test_token_next_empty_str(void) {
const char *input = ""; const char *input = "";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NULL(response); TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
@@ -30,7 +30,7 @@ static void test_token_next_only_whitespace(void) {
const char *input = " \n"; const char *input = " \n";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NULL(response); TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
@@ -40,11 +40,11 @@ static void test_token_next_lparen(void) {
const char *input = "("; const char *input = "(";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
} }
@@ -53,11 +53,11 @@ static void test_token_next_rparen(void) {
const char *input = ")"; const char *input = ")";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string); TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
} }
@@ -66,20 +66,20 @@ static void test_token_next_lrparen(void) {
const char *input = "()"; const char *input = "()";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL(')', *curr); TEST_ASSERT_EQUAL(')', *curr);
nl_object_delete(response); ucl_object_delete(response);
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string); TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
} }
@@ -88,11 +88,11 @@ static void test_token_next_string(void) {
const char *input = "\"foo\""; const char *input = "\"foo\"";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr); TEST_ASSERT_EQUAL('\0', *curr);
} }
@@ -101,11 +101,11 @@ static void test_token_next_string_w_whitespace(void) {
const char *input = " \"foo\" "; const char *input = " \"foo\" ";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr); TEST_ASSERT_EQUAL_STRING(" ", curr);
} }
@@ -114,11 +114,11 @@ static void test_token_next_symbol(void) {
const char *input = "foo"; const char *input = "foo";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING("", curr); TEST_ASSERT_EQUAL_STRING("", curr);
} }
@@ -127,206 +127,206 @@ static void test_token_next_symbol_w_whitespace(void) {
const char *input = " foo "; const char *input = " foo ";
const char *curr = input; const char *curr = input;
response = nl_token_next(&curr); response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr); TEST_ASSERT_EQUAL_STRING(" ", curr);
} }
static void test_tokenize_empty_str(void) { static void test_tokenize_empty_str(void) {
response = nl_tokenize(""); response = ucl_tokenize("");
TEST_ASSERT_NULL(response); TEST_ASSERT_NULL(response);
} }
static void test_tokenize_nil(void) { static void test_tokenize_nil(void) {
response = nl_tokenize("()"); response = ucl_tokenize("()");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
const struct nl_object *token1 = response; const struct ucl_object *token1 = response;
const struct nl_object *token2 = response->cell.cdr; const struct ucl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token1->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string); TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2); TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token2->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token2->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token2->cell.car->string); TEST_ASSERT_EQUAL_STRING(")", token2->cell.car->string);
TEST_ASSERT_NULL(token2->cell.cdr); TEST_ASSERT_NULL(token2->cell.cdr);
} }
static void test_tokenize_statement(void) { static void test_tokenize_statement(void) {
response = nl_tokenize("(foo)"); response = ucl_tokenize("(foo)");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
const struct nl_object *token1 = response; const struct ucl_object *token1 = response;
const struct nl_object *token2 = response->cell.cdr; const struct ucl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token1->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string); TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2); TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token2->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token2->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", token2->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", token2->cell.car->string);
const struct nl_object *token3 = token2->cell.cdr; const struct ucl_object *token3 = token2->cell.cdr;
TEST_ASSERT_NOT_NULL(token3); TEST_ASSERT_NOT_NULL(token3);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token3->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token3->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token3->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token3->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token3->cell.car->string); TEST_ASSERT_EQUAL_STRING(")", token3->cell.car->string);
} }
static void test_parse_atom_symbol(void) { static void test_parse_atom_symbol(void) {
response = nl_parse_token_atom(nl_symbol_create(strdup("foo"))); response = ucl_parse_token_atom(ucl_symbol_create(strdup("foo")));
TEST_ASSERT_EQUAL(response->type, NL_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 = nl_parse_token_atom(nl_symbol_create(strdup("("))); response = ucl_parse_token_atom(ucl_symbol_create(strdup("(")));
TEST_ASSERT_NULL(response); TEST_ASSERT_NULL(response);
} }
static void test_parse_atom_rparen(void) { static void test_parse_atom_rparen(void) {
response = nl_parse_token_atom(nl_symbol_create(strdup(")"))); response = ucl_parse_token_atom(ucl_symbol_create(strdup(")")));
TEST_ASSERT_NULL(response); TEST_ASSERT_NULL(response);
} }
static void test_parse_empty_str(void) { static void test_parse_empty_str(void) {
response = nl_parse(""); response = ucl_parse("");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NULL(response->cell.car); TEST_ASSERT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr); TEST_ASSERT_NULL(response->cell.cdr);
} }
static void test_parse_symbol(void) { static void test_parse_symbol(void) {
response = nl_parse("foo"); response = ucl_parse("foo");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(response->cell.car->type, NL_TYPE_SYMBOL); TEST_ASSERT_EQUAL(response->cell.car->type, UCL_TYPE_SYMBOL);
TEST_ASSERT_EQUAL_STRING(response->cell.car->symbol, "foo"); TEST_ASSERT_EQUAL_STRING(response->cell.car->symbol, "foo");
} }
static void test_parse_nil(void) { static void test_parse_nil(void) {
response = nl_parse("()"); response = ucl_parse("()");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr); TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.car); TEST_ASSERT_NULL(response->cell.car->cell.car);
TEST_ASSERT_NULL(response->cell.car->cell.cdr); TEST_ASSERT_NULL(response->cell.car->cell.cdr);
} }
static void test_parse_list_1elem(void) { static void test_parse_list_1elem(void) {
response = nl_parse("(foo)"); response = ucl_parse("(foo)");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr); TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.cdr); TEST_ASSERT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol);
} }
static void test_parse_list_2elem(void) { static void test_parse_list_2elem(void) {
response = nl_parse("(foo bar)"); response = ucl_parse("(foo bar)");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr); TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NOT_NULL(response->cell.car->cell.cdr); TEST_ASSERT_NOT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->cell.cdr->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->cell.cdr->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->cell.cdr->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.car->cell.cdr->cell.car->string); TEST_ASSERT_EQUAL_STRING("bar", response->cell.car->cell.cdr->cell.car->string);
} }
static void test_parse_2elem(void) { static void test_parse_2elem(void) {
response = nl_parse("foo bar"); response = ucl_parse("foo bar");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->symbol); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->symbol);
TEST_ASSERT_NOT_NULL(response->cell.cdr); TEST_ASSERT_NOT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.cdr->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.cdr->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.cdr->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->symbol); TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->symbol);
} }
static void test_parse_2elem_str(void) { static void test_parse_2elem_str(void) {
response = nl_parse("\"foo\" \"bar\""); response = ucl_parse("\"foo\" \"bar\"");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_NOT_NULL(response->cell.cdr); TEST_ASSERT_NOT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.cdr->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.cdr->type);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.cdr->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->string); TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->string);
} }
static void test_parse_nested(void) { static void test_parse_nested(void) {
response = nl_parse("((foo))"); response = ucl_parse("((foo))");
TEST_ASSERT_NOT_NULL(response); TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car); TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr); TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.cdr); TEST_ASSERT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->cell.car->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.car->cell.cdr); TEST_ASSERT_NULL(response->cell.car->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->cell.car->cell.car->type); TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->cell.car->symbol); TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->cell.car->symbol);
} }

View File

@@ -2,57 +2,57 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_state.h" #include "state.h"
#include "nihilispm_utility.h" #include "utility.h"
#include "testing_helpers.h" #include "testing_helpers.h"
/* static struct nl_parse_result *result; */ /* static struct ucl_parse_result *result; */
static struct nl_state *state; static struct ucl_state *state;
static struct nl_object *response; static struct ucl_object *response;
void setUp(void) { void setUp(void) {
state = nl_state_create(); state = ucl_state_create();
} }
void tearDown(void) { void tearDown(void) {
nl_state_delete(state); ucl_state_delete(state);
state = NULL; state = NULL;
} }
static void test_get_empty(void) { static void test_get_empty(void) {
response = nl_state_get(state, "foo"); response = ucl_state_get(state, "foo");
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_put_get(void) { static void test_put_get(void) {
nl_state_put(state, "foo", nl_t_create()); ucl_state_put(state, "foo", ucl_t_create());
response = nl_state_get(state, "foo"); response = ucl_state_get(state, "foo");
TEST_ASSERT_T(response); TEST_ASSERT_T(response);
} }
static void test_put2_get(void) { static void test_put2_get(void) {
nl_state_put(state, "foo1", nl_t_create()); ucl_state_put(state, "foo1", ucl_t_create());
nl_state_put(state, "foo2", nl_nil_create()); ucl_state_put(state, "foo2", ucl_nil_create());
response = nl_state_get(state, "foo1"); response = ucl_state_get(state, "foo1");
TEST_ASSERT_T(response); TEST_ASSERT_T(response);
} }
static void test_put_modify_get(void) { static void test_put_modify_get(void) {
struct nl_object *obj = nl_tuple_create( struct ucl_object *obj = ucl_tuple_create(
nl_string_create(strdup("bar")), ucl_string_create(strdup("bar")),
nl_string_create(strdup("baz"))); ucl_string_create(strdup("baz")));
nl_state_put(state, "foo", obj); ucl_state_put(state, "foo", obj);
nl_list_append(obj, nl_string_create(strdup("quux"))); ucl_list_append(obj, ucl_string_create(strdup("quux")));
response = nl_state_get(state, "foo"); response = ucl_state_get(state, "foo");
TEST_ASSERT_OBJ_STRING(nl_list_nth(response, 2)); TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));
TEST_ASSERT_EQUAL_STRING(nl_list_nth(response, 2)->string, "quux"); TEST_ASSERT_EQUAL_STRING(ucl_list_nth(response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(nl_list_nth(obj, 2)); TEST_ASSERT_OBJ_STRING(ucl_list_nth(obj, 2));
TEST_ASSERT_EQUAL_STRING(nl_list_nth(obj, 2)->string, "quux"); TEST_ASSERT_EQUAL_STRING(ucl_list_nth(obj, 2)->string, "quux");
} }

View File

@@ -2,13 +2,13 @@
#include <unity.h> #include <unity.h>
#include <string.h> #include <string.h>
#include "nihilispm.h" #include "uclisp.h"
#include "nihilispm_internal.h" #include "internal.h"
#include "nihilispm_utility.h" #include "utility.h"
#include "testing_helpers.h" #include "testing_helpers.h"
static struct nl_object *input; static struct ucl_object *input;
static struct nl_object *response; static struct ucl_object *response;
void setUp(void) { void setUp(void) {
input = NULL; input = NULL;
@@ -17,99 +17,99 @@ void setUp(void) {
void tearDown(void) { void tearDown(void) {
// TODO: Implement GC so we can clean these both up // TODO: Implement GC so we can clean these both up
//nl_object_delete(input); //ucl_object_delete(input);
input = NULL; input = NULL;
nl_object_delete(response); ucl_object_delete(response);
response = NULL; response = NULL;
} }
static void test_nil_create(void) { static void test_nil_create(void) {
response = nl_nil_create(); response = ucl_nil_create();
TEST_ASSERT_NIL(response); TEST_ASSERT_NIL(response);
} }
static void test_t_create(void) { static void test_t_create(void) {
response = nl_t_create(); response = ucl_t_create();
TEST_ASSERT_T(response); TEST_ASSERT_T(response);
} }
static void test_predicate_true(void) { static void test_predicate_true(void) {
response = nl_predicate(true); response = ucl_predicate(true);
TEST_ASSERT_T(response); TEST_ASSERT_T(response);
} }
static void test_predicate_false(void) { static void test_predicate_false(void) {
response = nl_predicate(false); response = ucl_predicate(false);
TEST_ASSERT_NIL(response); TEST_ASSERT_NIL(response);
} }
static void test_car_nil(void) { static void test_car_nil(void) {
response = nl_car(nl_nil_create()); response = ucl_car(ucl_nil_create());
TEST_ASSERT_NIL(response); TEST_ASSERT_NIL(response);
} }
static void test_cdr_nil(void) { static void test_cdr_nil(void) {
response = nl_cdr(nl_nil_create()); response = ucl_cdr(ucl_nil_create());
TEST_ASSERT_NIL(response); TEST_ASSERT_NIL(response);
} }
static void test_car_t(void) { static void test_car_t(void) {
response = nl_car(nl_t_create()); response = ucl_car(ucl_t_create());
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_cdr_t(void) { static void test_cdr_t(void) {
response = nl_car(nl_t_create()); response = ucl_car(ucl_t_create());
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_car_list(void) { static void test_car_list(void) {
input = nl_tuple_create( input = ucl_tuple_create(
nl_string_create(strdup("foo")), ucl_string_create(strdup("foo")),
nl_t_create()); ucl_t_create());
response = nl_car(input); response = ucl_car(input);
TEST_ASSERT_OBJ_STRING(response); TEST_ASSERT_OBJ_STRING(response);
} }
static void test_cdr_list(void) { static void test_cdr_list(void) {
input = nl_tuple_create( input = ucl_tuple_create(
nl_t_create(), ucl_t_create(),
nl_string_create(strdup("foo"))); ucl_string_create(strdup("foo")));
response = nl_cdr(input); response = ucl_cdr(input);
TEST_ASSERT_OBJ_STRING(nl_car(response)); TEST_ASSERT_OBJ_STRING(ucl_car(response));
} }
static void test_list_length_nil() { static void test_list_length_nil() {
response = nl_list_length(nl_nil_create()); response = ucl_list_length(ucl_nil_create());
TEST_ASSERT_OBJ_INT(response); TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 0); TEST_ASSERT_EQUAL(response->integer, 0);
} }
static void test_list_length_1() { static void test_list_length_1() {
response = nl_list_length( response = ucl_list_length(
nl_cell_create( ucl_cell_create(
nl_t_create(), NULL)); ucl_t_create(), NULL));
TEST_ASSERT_OBJ_INT(response); TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 1); TEST_ASSERT_EQUAL(response->integer, 1);
} }
static void test_list_length_2() { static void test_list_length_2() {
response = nl_list_length( response = ucl_list_length(
nl_tuple_create( ucl_tuple_create(
nl_t_create(), ucl_t_create(),
nl_t_create())); ucl_t_create()));
TEST_ASSERT_OBJ_INT(response); TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2); TEST_ASSERT_EQUAL(response->integer, 2);
@@ -117,67 +117,67 @@ static void test_list_length_2() {
static void test_list_append_nil() { static void test_list_append_nil() {
input = nl_nil_create(); input = ucl_nil_create();
nl_list_append(input, nl_t_create()); ucl_list_append(input, ucl_t_create());
TEST_ASSERT_EQUAL(nl_list_length(input)->integer, 1); TEST_ASSERT_EQUAL(ucl_list_length(input)->integer, 1);
} }
static void test_list_append_list() { static void test_list_append_list() {
input = nl_tuple_create(nl_t_create(), nl_t_create()); input = ucl_tuple_create(ucl_t_create(), ucl_t_create());
nl_list_append(input, nl_t_create()); ucl_list_append(input, ucl_t_create());
TEST_ASSERT_EQUAL(nl_list_length(input)->integer, 3); TEST_ASSERT_EQUAL(ucl_list_length(input)->integer, 3);
} }
static void test_list_nth_nil_0() { static void test_list_nth_nil_0() {
response = nl_list_nth(nl_nil_create(), 0); response = ucl_list_nth(ucl_nil_create(), 0);
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_list_nth_nil_1() { static void test_list_nth_nil_1() {
response = nl_list_nth(nl_nil_create(), 1); response = ucl_list_nth(ucl_nil_create(), 1);
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_list_nth_list_0() { static void test_list_nth_list_0() {
response = nl_list_nth( response = ucl_list_nth(
nl_tuple_create( ucl_tuple_create(
nl_t_create(), ucl_t_create(),
nl_string_create(strdup("foo"))), ucl_string_create(strdup("foo"))),
0); 0);
TEST_ASSERT_T(response); TEST_ASSERT_T(response);
} }
static void test_list_nth_list_1() { static void test_list_nth_list_1() {
response = nl_list_nth( response = ucl_list_nth(
nl_tuple_create( ucl_tuple_create(
nl_t_create(), ucl_t_create(),
nl_string_create(strdup("foo"))), ucl_string_create(strdup("foo"))),
1); 1);
TEST_ASSERT_OBJ_STRING(response); TEST_ASSERT_OBJ_STRING(response);
} }
static void test_list_nth_t_0() { static void test_list_nth_t_0() {
response = nl_list_nth(nl_t_create(), 1); response = ucl_list_nth(ucl_t_create(), 1);
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_list_nth_bounds_0() { static void test_list_nth_bounds_0() {
response = nl_list_nth(nl_nil_create(), 0); response = ucl_list_nth(ucl_nil_create(), 0);
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);
} }
static void test_list_nth_bounds_1() { static void test_list_nth_bounds_1() {
response = nl_list_nth( response = ucl_list_nth(
nl_cell_create( ucl_cell_create(
nl_nil_create(), ucl_nil_create(),
NULL), NULL),
1); 1);
@@ -185,10 +185,10 @@ static void test_list_nth_bounds_1() {
} }
static void test_list_nth_bounds_2() { static void test_list_nth_bounds_2() {
response = nl_list_nth( response = ucl_list_nth(
nl_tuple_create( ucl_tuple_create(
nl_nil_create(), ucl_nil_create(),
nl_nil_create()), ucl_nil_create()),
2); 2);
TEST_ASSERT_OBJ_ERROR(response); TEST_ASSERT_OBJ_ERROR(response);

View File

@@ -3,27 +3,27 @@
#include <unity.h> #include <unity.h>
#include "nihilispm.h" #include "uclisp.h"
#define TEST_ASSERT_OBJ_ERROR(obj) \ #define TEST_ASSERT_OBJ_ERROR(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_ERROR) TEST_ASSERT_EQUAL(obj->type, UCL_TYPE_ERROR)
#define TEST_ASSERT_OBJ_SYMBOL(obj) \ #define TEST_ASSERT_OBJ_SYMBOL(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_SYMBOL) TEST_ASSERT_EQUAL(obj->type, UCL_TYPE_SYMBOL)
#define TEST_ASSERT_OBJ_STRING(obj) \ #define TEST_ASSERT_OBJ_STRING(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_STRING) TEST_ASSERT_EQUAL(obj->type, UCL_TYPE_STRING)
#define TEST_ASSERT_OBJ_INT(obj) \ #define TEST_ASSERT_OBJ_INT(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_INT) TEST_ASSERT_EQUAL(obj->type, UCL_TYPE_INT)
#define TEST_ASSERT_OBJ_LIST(obj) \ #define TEST_ASSERT_OBJ_LIST(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_CELL) TEST_ASSERT_EQUAL(obj->type, UCL_TYPE_CELL)
#define TEST_ASSERT_LIST_LEN(list, len) \ #define TEST_ASSERT_LIST_LEN(list, len) \
do { \ do { \
TEST_ASSERT_OBJ_LIST(list); \ TEST_ASSERT_OBJ_LIST(list); \
TEST_ASSERT_EQUAL(nl_list_length(list)->integer, len); \ TEST_ASSERT_EQUAL(ucl_list_length(list)->integer, len); \
} while(0) } while(0)
#define TEST_ASSERT_NIL(obj) \ #define TEST_ASSERT_NIL(obj) \