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

View File

@@ -1,6 +1,6 @@
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "uclisp.h"
#include "internal.h"
#include "utility.h"
#include "builtins.h"
#include <assert.h>
@@ -8,131 +8,131 @@
#include <string.h>
#include <stdlib.h>
LISP_FUNC_0(nl_builtin_hello_world) {
return nl_string_create(strdup("Hello, world!"));
LISP_FUNC_0(ucl_builtin_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) {
case NL_TYPE_CELL:
return nl_symbol_create(strdup("list"));
case NL_TYPE_SYMBOL:
return nl_symbol_create(strdup("symbol"));
case NL_TYPE_INT:
return nl_symbol_create(strdup("int"));
case NL_TYPE_STRING:
return nl_symbol_create(strdup("string"));
case NL_TYPE_ERROR:
return nl_symbol_create(strdup("error"));
case NL_TYPE_COUNT:
case UCL_TYPE_CELL:
return ucl_symbol_create(strdup("list"));
case UCL_TYPE_SYMBOL:
return ucl_symbol_create(strdup("symbol"));
case UCL_TYPE_INT:
return ucl_symbol_create(strdup("int"));
case UCL_TYPE_STRING:
return ucl_symbol_create(strdup("string"));
case UCL_TYPE_ERROR:
return ucl_symbol_create(strdup("error"));
case UCL_TYPE_COUNT:
assert(0);
return NULL;
}
}
LISP_FUNC_1(nl_builtin_error, arg) {
if (arg->type != NL_TYPE_STRING) {
return nl_error_create("Expected type string passed to 'error'");
LISP_FUNC_1(ucl_builtin_error, arg) {
if (arg->type != UCL_TYPE_STRING) {
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) {
return nl_predicate(arg->type == NL_TYPE_SYMBOL);
LISP_FUNC_1(ucl_builtin_symbol_p, arg) {
return ucl_predicate(arg->type == UCL_TYPE_SYMBOL);
}
LISP_FUNC_1(nl_builtin_string_p, arg) {
return nl_predicate(arg->type == NL_TYPE_STRING);
LISP_FUNC_1(ucl_builtin_string_p, arg) {
return ucl_predicate(arg->type == UCL_TYPE_STRING);
}
LISP_FUNC_1(nl_builtin_int_p, arg) {
return nl_predicate(arg->type == NL_TYPE_INT);
LISP_FUNC_1(ucl_builtin_int_p, arg) {
return ucl_predicate(arg->type == UCL_TYPE_INT);
}
LISP_FUNC_1(nl_builtin_list_p, arg) {
return nl_predicate(arg->type == NL_TYPE_CELL);
LISP_FUNC_1(ucl_builtin_list_p, arg) {
return ucl_predicate(arg->type == UCL_TYPE_CELL);
}
LISP_FUNC_1(nl_builtin_error_p, arg) {
return nl_predicate(arg->type == NL_TYPE_ERROR);
LISP_FUNC_1(ucl_builtin_error_p, arg) {
return ucl_predicate(arg->type == UCL_TYPE_ERROR);
}
LISP_FUNC_1(nl_builtin_car, arg) {
return nl_car(arg);
LISP_FUNC_1(ucl_builtin_car, arg) {
return ucl_car(arg);
}
LISP_FUNC_1(nl_builtin_cdr, arg) {
return nl_cdr(arg);
LISP_FUNC_1(ucl_builtin_cdr, arg) {
return ucl_cdr(arg);
}
LISP_FUNC_2(nl_builtin_add, arg0, arg1) {
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'add'");
LISP_FUNC_2(ucl_builtin_add, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'add'");
}
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'add'");
if (arg1->type != UCL_TYPE_INT) {
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) {
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'sub'");
LISP_FUNC_2(ucl_builtin_sub, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'sub'");
}
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'sub'");
if (arg1->type != UCL_TYPE_INT) {
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) {
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mul'");
LISP_FUNC_2(ucl_builtin_mul, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'mul'");
}
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mul'");
if (arg1->type != UCL_TYPE_INT) {
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) {
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'div'");
LISP_FUNC_2(ucl_builtin_div, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'div'");
}
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'div'");
if (arg1->type != UCL_TYPE_INT) {
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) {
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mod'");
LISP_FUNC_2(ucl_builtin_mod, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'mod'");
}
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mod'");
if (arg1->type != UCL_TYPE_INT) {
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) {
if (arg0->type != NL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 0 to 'concat'");
LISP_FUNC_2(ucl_builtin_concat, arg0, arg1) {
if (arg0->type != UCL_TYPE_STRING) {
return ucl_error_create("Invalid type of argument 0 to 'concat'");
}
if (arg1->type != NL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 1 to 'concat'");
if (arg1->type != UCL_TYPE_STRING) {
return ucl_error_create("Invalid type of argument 1 to 'concat'");
}
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, 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
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) \
static struct nl_object *func_name##_impl(); \
struct nl_object *func_name(struct nl_object *args) { \
static struct ucl_object *func_name##_impl(); \
struct ucl_object *func_name(struct ucl_object *args) { \
if (args->cell.car != NULL) { \
return NULL; \
} \
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) \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name); \
struct nl_object *func_name(struct nl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \
static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name); \
struct ucl_object *func_name(struct ucl_object *args) { \
struct ucl_object *len_obj = ucl_list_length(args); \
if (len_obj->type != UCL_TYPE_INT) { \
return NULL; \
} \
if (len_obj->integer != 1) { \
return NULL; \
} \
struct nl_object *arg0 = nl_car(args); \
struct ucl_object *arg0 = ucl_car(args); \
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)
#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); \
struct nl_object *func_name(struct nl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \
static struct ucl_object *func_name##_impl(struct ucl_object *arg0_name, struct ucl_object *arg1_name); \
struct ucl_object *func_name(struct ucl_object *args) { \
struct ucl_object *len_obj = ucl_list_length(args); \
if (len_obj->type != UCL_TYPE_INT) { \
return NULL; \
} \
if (len_obj->integer != 2) { \
return NULL; \
} \
struct nl_object *arg0 = nl_list_nth(args, 0); \
struct nl_object *arg1 = nl_list_nth(args, 1); \
struct ucl_object *arg0 = ucl_list_nth(args, 0); \
struct ucl_object *arg1 = ucl_list_nth(args, 1); \
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 nl_object *nl_builtin_error(struct nl_object *args);
struct nl_object *nl_builtin_type(struct nl_object *args);
struct nl_object *nl_builtin_symbol_p(struct nl_object *args);
struct nl_object *nl_builtin_string_p(struct nl_object *args);
struct nl_object *nl_builtin_int_p(struct nl_object *args);
struct nl_object *nl_builtin_list_p(struct nl_object *args);
struct ucl_object *ucl_builtin_hello_world(struct ucl_object *args);
struct ucl_object *ucl_builtin_error(struct ucl_object *args);
struct ucl_object *ucl_builtin_type(struct ucl_object *args);
struct ucl_object *ucl_builtin_symbol_p(struct ucl_object *args);
struct ucl_object *ucl_builtin_string_p(struct ucl_object *args);
struct ucl_object *ucl_builtin_int_p(struct ucl_object *args);
struct ucl_object *ucl_builtin_list_p(struct ucl_object *args);
struct nl_object *nl_builtin_add(struct nl_object *args);
struct nl_object *nl_builtin_sub(struct nl_object *args);
struct nl_object *nl_builtin_mul(struct nl_object *args);
struct nl_object *nl_builtin_div(struct nl_object *args);
struct nl_object *nl_builtin_mod(struct nl_object *args);
struct nl_object *nl_builtin_concat(struct nl_object *args);
struct ucl_object *ucl_builtin_add(struct ucl_object *args);
struct ucl_object *ucl_builtin_sub(struct ucl_object *args);
struct ucl_object *ucl_builtin_mul(struct ucl_object *args);
struct ucl_object *ucl_builtin_div(struct ucl_object *args);
struct ucl_object *ucl_builtin_mod(struct ucl_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 <stddef.h>
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "nihilispm_state.h"
#include "uclisp.h"
#include "internal.h"
#include "utility.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
struct nl_object *evaluated_list = nl_nil_create();
struct ucl_object *evaluated_list = ucl_nil_create();
FOREACH_LIST(list, iter, item) {
struct nl_object *obj = nl_evaluate(state, item);
nl_list_append(evaluated_list, obj);
struct ucl_object *obj = ucl_evaluate(state, item);
ucl_list_append(evaluated_list, obj);
};
struct nl_object *fun = nl_car(evaluated_list);
struct nl_object *args = nl_cdr(evaluated_list);
struct nl_object *result = NULL;
struct ucl_object *fun = ucl_car(evaluated_list);
struct ucl_object *args = ucl_cdr(evaluated_list);
struct ucl_object *result = NULL;
assert(fun->type == NL_TYPE_BUILTIN);
if (fun->type == NL_TYPE_BUILTIN) {
assert(fun->type == UCL_TYPE_BUILTIN);
if (fun->type == UCL_TYPE_BUILTIN) {
result = fun->builtin(args);
}
@@ -31,20 +31,20 @@ struct nl_object *nl_evaluate_list(struct nl_state *state, struct nl_object *lis
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);
switch (obj->type) {
case NL_TYPE_CELL:
return nl_evaluate_list(state, obj);
case NL_TYPE_SYMBOL:
return nl_state_get(state, obj->symbol);
case NL_TYPE_INT:
case NL_TYPE_STRING:
case NL_TYPE_ERROR:
case UCL_TYPE_CELL:
return ucl_evaluate_list(state, obj);
case UCL_TYPE_SYMBOL:
return ucl_state_get(state, obj->symbol);
case UCL_TYPE_INT:
case UCL_TYPE_STRING:
case UCL_TYPE_ERROR:
return obj;
case NL_TYPE_BUILTIN:
case NL_TYPE_COUNT:
case UCL_TYPE_BUILTIN:
case UCL_TYPE_COUNT:
assert(0);
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 <stdio.h>
#include "nihilispm.h"
#include "nihilispm_state.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "uclisp.h"
#include "state.h"
#include "internal.h"
#include "utility.h"
#include "builtins.h"
int main(int argc, const char **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));
nl_state_put(state, "-", nl_builtin_create(nl_builtin_sub));
nl_state_put(state, "*", nl_builtin_create(nl_builtin_mul));
nl_state_put(state, "/", nl_builtin_create(nl_builtin_div));
nl_state_put(state, "%", nl_builtin_create(nl_builtin_mod));
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
ucl_state_put(state, "-", ucl_builtin_create(ucl_builtin_sub));
ucl_state_put(state, "*", ucl_builtin_create(ucl_builtin_mul));
ucl_state_put(state, "/", ucl_builtin_create(ucl_builtin_div));
ucl_state_put(state, "%", ucl_builtin_create(ucl_builtin_mod));
struct nl_object *sexp = nl_parse(argv[1]);
struct nl_object *result = nl_evaluate(state, nl_car(sexp));
struct ucl_object *sexp = ucl_parse(argv[1]);
struct ucl_object *result = ucl_evaluate(state, ucl_car(sexp));
assert(result != NULL);
assert(result->type == NL_TYPE_INT);
assert(result->type == UCL_TYPE_INT);
printf("%d\n", result->integer);

View File

@@ -1,93 +1,93 @@
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "uclisp.h"
#include "internal.h"
#include <stddef.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();
obj->type = NL_TYPE_CELL;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_CELL;
obj->cell.car = car;
obj->cell.cdr = cdr;
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();
obj->type = NL_TYPE_INT;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_INT;
obj->integer = integer;
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();
obj->type = NL_TYPE_SYMBOL;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_SYMBOL;
obj->symbol = symbol;
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();
obj->type = NL_TYPE_STRING;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_STRING;
obj->string = string;
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();
obj->type = NL_TYPE_ERROR;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_ERROR;
obj->error = error;
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();
obj->type = NL_TYPE_BUILTIN;
struct ucl_object* obj = ucl_object_alloc();
obj->type = UCL_TYPE_BUILTIN;
obj->builtin = builtin;
return obj;
}
static struct nl_object* nl_object_alloc() {
return malloc(sizeof(struct nl_object));
static struct ucl_object* ucl_object_alloc() {
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) {
return;
}
switch (obj->type) {
case NL_TYPE_CELL:
nl_object_delete(obj->cell.car);
case UCL_TYPE_CELL:
ucl_object_delete(obj->cell.car);
obj->cell.car = NULL;
nl_object_delete(obj->cell.cdr);
ucl_object_delete(obj->cell.cdr);
obj->cell.cdr = NULL;
break;
case NL_TYPE_SYMBOL:
case UCL_TYPE_SYMBOL:
free((void *) obj->symbol);
obj->symbol = NULL;
break;
case NL_TYPE_STRING:
case UCL_TYPE_STRING:
free((void *) obj->string);
obj->string = NULL;
break;
case NL_TYPE_ERROR:
case UCL_TYPE_ERROR:
free((void *) obj->error);
obj->error = NULL;
break;
case NL_TYPE_INT:
case NL_TYPE_BUILTIN:
case NL_TYPE_COUNT:
case UCL_TYPE_INT:
case UCL_TYPE_BUILTIN:
case UCL_TYPE_COUNT:
break;
}
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 "nihilispm_internal.h"
#include "uclisp.h"
#include "internal.h"
// TODO: remove these
#include <stdbool.h>
@@ -21,19 +21,19 @@ static const char *reserved_symbols[] = {
// 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';
}
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;
}
static bool nl_is_delimiter(char c) {
return nl_is_whitespace(c) || nl_is_token(c) || c == '\0';
static bool ucl_is_delimiter(char c) {
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);
const char *start = *curr_src;
@@ -54,7 +54,7 @@ struct nl_object *nl_token_next(const char **curr_src) {
str[0] = **curr_src;
str[1] = '\0';
(*curr_src)++;
return nl_cell_create(nl_symbol_create(str), NULL);
return ucl_cell_create(ucl_symbol_create(str), NULL);
case QUOTE_CHAR:
// skip beginning quote
(*curr_src)++;
@@ -66,19 +66,19 @@ struct nl_object *nl_token_next(const char **curr_src) {
(*curr_src)++;
// -2 for removing start/end quotes
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': {
char *end = NULL;
long value = strtol(*curr_src, &end, 0);
*curr_src = end;
return nl_cell_create(nl_int_create(value), NULL);
return ucl_cell_create(ucl_int_create(value), NULL);
}
default:
while (!nl_is_delimiter(**curr_src)) {
while (!ucl_is_delimiter(**curr_src)) {
(*curr_src)++;
}
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;
}
struct nl_object *nl_tokenize(const char *source) {
struct nl_object *tokens = NULL, *curr_token = NULL, **next_token = &tokens;
struct ucl_object *ucl_tokenize(const char *source) {
struct ucl_object *tokens = NULL, *curr_token = NULL, **next_token = &tokens;
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->cell.cdr;
}
@@ -99,78 +99,78 @@ struct nl_object *nl_tokenize(const char *source) {
return tokens;
}
struct nl_object *nl_parse_token_atom(struct nl_object *maybe_atom) {
struct nl_object *atom = NULL;
struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom) {
struct ucl_object *atom = NULL;
switch (maybe_atom->type) {
case NL_TYPE_CELL:
case UCL_TYPE_CELL:
// Cell types currently are not valid for tokens
assert(false);
break;
case NL_TYPE_SYMBOL: {
case UCL_TYPE_SYMBOL: {
// Check for reserved tokens first, which indicate special, non-atom behavior
for (int i = 0; i < ARRAY_SIZE(reserved_symbols); i++) {
if (!strcmp(maybe_atom->string, reserved_symbols[i])) {
return NULL;
}
}
atom = nl_symbol_create(strdup(maybe_atom->symbol));
atom = ucl_symbol_create(strdup(maybe_atom->symbol));
break;
}
case NL_TYPE_STRING:
atom = nl_string_create(strdup(maybe_atom->string));
case UCL_TYPE_STRING:
atom = ucl_string_create(strdup(maybe_atom->string));
break;
case NL_TYPE_INT:
atom = nl_int_create(maybe_atom->integer);
case UCL_TYPE_INT:
atom = ucl_int_create(maybe_atom->integer);
break;
case NL_TYPE_COUNT:
case UCL_TYPE_COUNT:
assert(false);
}
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:
// - 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 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);
if (*token_iter == 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 nl_object *next_sexp = nl_parse_token_atom(token);
struct ucl_object *token = (*token_iter)->cell.car;
struct ucl_object *next_sexp = ucl_parse_token_atom(token);
if (next_sexp != NULL) {
*token_iter = (*token_iter)->cell.cdr;
return next_sexp;
}
assert(token->type == NL_TYPE_SYMBOL);
assert(token->type == UCL_TYPE_SYMBOL);
if (token->symbol[0] == START_LIST_CHAR) {
struct nl_object *list = NULL;
struct nl_object **next_node = &list;
struct ucl_object *list = NULL;
struct ucl_object **next_node = &list;
// Consume the START_LIST_CHAR
*token_iter = (*token_iter)->cell.cdr;
while (1) {
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;
if (list == NULL) {
list = nl_cell_create(NULL, NULL);
list = ucl_cell_create(NULL, NULL);
}
return list;
}
next_sexp = nl_parse_tokens_recursive(token_iter);
next_sexp = ucl_parse_tokens_recursive(token_iter);
if (next_sexp == NULL) {
// Error somewhere in the recursive parsing
nl_object_delete(list);
ucl_object_delete(list);
return NULL;
}
*next_node = nl_cell_create(next_sexp, NULL);
*next_node = ucl_cell_create(next_sexp, NULL);
next_node = &(*next_node)->cell.cdr;
}
} 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_recursive -> error on EOF)
struct nl_object *nl_parse_tokens(struct nl_object *tokens) {
struct nl_object* resultl = NULL;
struct nl_object** token_iter = &tokens;
struct nl_object** next_cell = &resultl;
struct ucl_object *ucl_parse_tokens(struct ucl_object *tokens) {
struct ucl_object* resultl = NULL;
struct ucl_object** token_iter = &tokens;
struct ucl_object** next_cell = &resultl;
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) {
goto error;
}
*next_cell = nl_cell_create(new_sexp, NULL);
*next_cell = ucl_cell_create(new_sexp, NULL);
next_cell = &(*next_cell)->cell.cdr;
}
if (resultl == NULL) {
return nl_cell_create(NULL, NULL);
return ucl_cell_create(NULL, NULL);
}
return resultl;
error:
nl_object_delete(resultl);
ucl_object_delete(resultl);
return NULL;
}
// TODO: Should the parse a single sexp (return the last-parsed position), or
// all sexps in the source (return a list of sexps)?
struct nl_object *nl_parse(const char *source) {
struct nl_object *tokens = nl_tokenize(source);
struct nl_object *sexp = nl_parse_tokens(tokens);
nl_object_delete(tokens);
struct ucl_object *ucl_parse(const char *source) {
struct ucl_object *tokens = ucl_tokenize(source);
struct ucl_object *sexp = ucl_parse_tokens(tokens);
ucl_object_delete(tokens);
return sexp;
}
/* struct ParseResult *nl_parse(const char *source) { */
/* struct Cell *tokens = nl_tokenize(source); */
/* struct ParseResult *ucl_parse(const char *source) { */
/* struct Cell *tokens = ucl_tokenize(source); */
/* struct Cell *sexp = n */
/* } */

View File

@@ -1,7 +1,7 @@
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_state.h"
#include "nihilispm_utility.h"
#include "uclisp.h"
#include "internal.h"
#include "state.h"
#include "utility.h"
#include <assert.h>
#include <string.h>
@@ -10,18 +10,18 @@
// Implements state as a giant alist
// TODO: Consider generalizing the alist concept
struct nl_state {
struct nl_object *list;
struct ucl_state {
struct ucl_object *list;
};
#define NAME_POSITION 0
#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) {
assert(item->type == NL_TYPE_CELL);
const char *item_name = nl_list_nth(item, NAME_POSITION)->string;
assert(item->type == UCL_TYPE_CELL);
const char *item_name = ucl_list_nth(item, NAME_POSITION)->string;
if (!strcmp(name, item_name)) {
return item;
}
@@ -30,18 +30,18 @@ static struct nl_object *nl_state_get_cell(struct nl_state *state, const char *n
return NULL;
}
struct nl_object *nl_state_get(struct nl_state *state, const char *name) {
struct nl_object *cell = nl_state_get_cell(state, name);
NL_COND_OR_RET_ERROR(cell != NULL, "Unknown name");
return nl_list_nth(cell, DATA_POSITION);
struct ucl_object *ucl_state_get(struct ucl_state *state, const char *name) {
struct ucl_object *cell = ucl_state_get_cell(state, name);
UCL_COND_OR_RET_ERROR(cell != NULL, "Unknown name");
return ucl_list_nth(cell, DATA_POSITION);
}
void nl_state_put(struct nl_state *state, const char *name, struct nl_object *obj) {
struct nl_object *cell = nl_state_get_cell(state, name);
void ucl_state_put(struct ucl_state *state, const char *name, struct ucl_object *obj) {
struct ucl_object *cell = ucl_state_get_cell(state, name);
if (cell == NULL) {
nl_list_append(state->list,
nl_tuple_create(
nl_string_create(strdup(name)),
ucl_list_append(state->list,
ucl_tuple_create(
ucl_string_create(strdup(name)),
obj));
} else {
// 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 nl_state *state = malloc(sizeof(struct nl_state));
state->list = nl_nil_create();
struct ucl_state *ucl_state_create() {
struct ucl_state *state = malloc(sizeof(struct ucl_state));
state->list = ucl_nil_create();
return state;
}
void nl_state_delete(struct nl_state *state) {
nl_object_delete(state->list);
void ucl_state_delete(struct ucl_state *state) {
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 "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "uclisp.h"
#include "internal.h"
#include "utility.h"
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
struct nl_object *nl_car(struct nl_object *list) {
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_car'");
struct ucl_object *ucl_car(struct ucl_object *list) {
UCL_COND_OR_RET_ERROR(
list != NULL && list->type == UCL_TYPE_CELL,
"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) {
return nl_nil_create();
return ucl_nil_create();
}
return car;
}
struct nl_object *nl_cdr(struct nl_object *list) {
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_cdr'");
struct ucl_object *ucl_cdr(struct ucl_object *list) {
UCL_COND_OR_RET_ERROR(
list != NULL && list->type == UCL_TYPE_CELL,
"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) {
return nl_nil_create();
return ucl_nil_create();
}
return cdr;
}
struct nl_object *nl_nil_create() {
return nl_cell_create(NULL, NULL);
struct ucl_object *ucl_nil_create() {
return ucl_cell_create(NULL, NULL);
}
struct nl_object *nl_t_create() {
return nl_symbol_create(strdup("t"));
struct ucl_object *ucl_t_create() {
return ucl_symbol_create(strdup("t"));
}
struct nl_object *nl_predicate(bool value) {
struct ucl_object *ucl_predicate(bool value) {
if (value) {
return nl_t_create();
return ucl_t_create();
} else {
return nl_nil_create();
return ucl_nil_create();
}
}
struct nl_object *nl_list_length(struct nl_object *list) {
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_length'");
struct ucl_object *ucl_list_length(struct ucl_object *list) {
UCL_COND_OR_RET_ERROR(
list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'ucl_list_length'");
struct nl_object *node = list;
struct ucl_object *node = list;
if (list->cell.car == NULL) {
return nl_int_create(0);
return ucl_int_create(0);
}
int length = 1;
@@ -64,18 +64,18 @@ struct nl_object *nl_list_length(struct nl_object *list) {
length++;
}
return nl_int_create(length);
return ucl_int_create(length);
}
struct nl_object *nl_list_nth(struct nl_object *list, int n) {
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_'");
struct ucl_object *ucl_list_nth(struct ucl_object *list, int n) {
UCL_COND_OR_RET_ERROR(
list != NULL && list->type == UCL_TYPE_CELL,
"Invalid type of argument 0 to 'ucl_list_'");
int length = nl_list_length(list)->integer;
NL_COND_OR_RET_ERROR(length > n, "Position n >= list length in nl_list_nth");
int length = ucl_list_length(list)->integer;
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++) {
node = node->cell.cdr;
}
@@ -83,19 +83,19 @@ struct nl_object *nl_list_nth(struct nl_object *list, int n) {
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
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 nl_object *tuple = nl_cell_create(obj0, NULL);
nl_list_append(tuple, obj1);
struct ucl_object *ucl_tuple_create(struct ucl_object *obj0, struct ucl_object *obj1) {
struct ucl_object *tuple = ucl_cell_create(obj0, NULL);
ucl_list_append(tuple, obj1);
return tuple;
}
struct nl_object *nl_list_append(struct nl_object *list, struct nl_object *obj) {
struct nl_object *iter = list;
struct ucl_object *ucl_list_append(struct ucl_object *list, struct ucl_object *obj) {
struct ucl_object *iter = list;
if (list->cell.car == NULL) {
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) {
iter = iter->cell.cdr;
}
iter->cell.cdr = nl_cell_create(obj, NULL);
iter->cell.cdr = ucl_cell_create(obj, NULL);
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