Add error types and simple state
This commit is contained in:
@@ -22,12 +22,22 @@ LISP_FUNC_1(nl_builtin_type, arg) {
|
||||
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:
|
||||
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'");
|
||||
}
|
||||
|
||||
return nl_error_create(strdup(arg->error));
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_symbol_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_SYMBOL);
|
||||
}
|
||||
@@ -44,6 +54,10 @@ LISP_FUNC_1(nl_builtin_list_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_CELL);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_error_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_ERROR);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_car, arg) {
|
||||
return nl_car(arg);
|
||||
}
|
||||
@@ -53,78 +67,72 @@ LISP_FUNC_1(nl_builtin_cdr, arg) {
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_add, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'add'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'add'");
|
||||
}
|
||||
|
||||
return nl_int_create(arg0->integer + arg1->integer);
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_sub, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'sub'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'sub'");
|
||||
}
|
||||
|
||||
return nl_int_create(arg0->integer - arg1->integer);
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_mul, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'mul'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'mul'");
|
||||
}
|
||||
|
||||
return nl_int_create(arg0->integer * arg1->integer);
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_div, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'div'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'div'");
|
||||
}
|
||||
|
||||
return nl_int_create(arg0->integer / arg1->integer);
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_mod, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'mod'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_INT) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_INT) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'mod'");
|
||||
}
|
||||
|
||||
return nl_int_create(arg0->integer % arg1->integer);
|
||||
}
|
||||
|
||||
LISP_FUNC_2(nl_builtin_concat, arg0, arg1) {
|
||||
// TODO: Return an error type
|
||||
if (arg0->type != arg1->type) {
|
||||
return NULL;
|
||||
if (arg0->type != NL_TYPE_STRING) {
|
||||
return nl_error_create("Invalid type of argument 0 to 'concat'");
|
||||
}
|
||||
|
||||
if (arg0->type != NL_TYPE_STRING) {
|
||||
return NULL;
|
||||
if (arg1->type != NL_TYPE_STRING) {
|
||||
return nl_error_create("Invalid type of argument 1 to 'concat'");
|
||||
}
|
||||
|
||||
int len = strlen(arg0->string) + strlen(arg1->string);
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
|
||||
|
||||
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);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include "nihilispm.h"
|
||||
#include "nihilispm_state.h"
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
(void) argc, (void) argv;
|
||||
struct nl_state *state = nl_state_create();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
13
src/memory.c
13
src/memory.c
@@ -40,6 +40,14 @@ struct nl_object *nl_string_create(const char *string)
|
||||
return obj;
|
||||
}
|
||||
|
||||
struct nl_object *nl_error_create(const char *error)
|
||||
{
|
||||
struct nl_object* obj = nl_object_alloc();
|
||||
obj->type = NL_TYPE_ERROR;
|
||||
obj->error = error;
|
||||
return obj;
|
||||
}
|
||||
|
||||
static struct nl_object* nl_object_alloc() {
|
||||
return malloc(sizeof(struct nl_object));
|
||||
}
|
||||
@@ -63,6 +71,11 @@ void nl_object_delete(struct nl_object *obj) {
|
||||
case NL_TYPE_STRING:
|
||||
free((void *) obj->string);
|
||||
obj->string = NULL;
|
||||
break;
|
||||
case NL_TYPE_ERROR:
|
||||
free((void *) obj->error);
|
||||
obj->error = NULL;
|
||||
break;
|
||||
case NL_TYPE_INT:
|
||||
case NL_TYPE_COUNT:
|
||||
break;
|
||||
|
||||
@@ -6,7 +6,8 @@ enum nl_type {
|
||||
NL_TYPE_SYMBOL = 1,
|
||||
NL_TYPE_INT = 2,
|
||||
NL_TYPE_STRING = 3,
|
||||
NL_TYPE_COUNT = 4,
|
||||
NL_TYPE_ERROR = 4,
|
||||
NL_TYPE_COUNT = 5,
|
||||
};
|
||||
|
||||
struct nl_cell {
|
||||
@@ -21,6 +22,7 @@ struct nl_object {
|
||||
const char *symbol;
|
||||
int integer;
|
||||
const char *string;
|
||||
const char *error;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "nihilispm.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0])
|
||||
#define FOREACH_LIST(list, iter_name, item_name) \
|
||||
@@ -6,15 +7,15 @@
|
||||
|
||||
#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \
|
||||
for (struct nl_object *iter_name = (list), *item_name = iter_name->cell.car; \
|
||||
iter_name != NULL && (cond); \
|
||||
iter_name = token->cell.cdr, 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);
|
||||
|
||||
void nl_object_delete(struct nl_object *obj);
|
||||
|
||||
|
||||
11
src/nihilispm_state.h
Normal file
11
src/nihilispm_state.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#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
|
||||
@@ -16,11 +16,11 @@ 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_RET_IF_ERROR(obj) \
|
||||
do { \
|
||||
if ((obj)->type == NL_TYPE_ERROR) { \
|
||||
return obj; \
|
||||
} while(0)
|
||||
|
||||
#define NL_COND_OR_RET_ERROR(cond, msg) \
|
||||
do { \
|
||||
|
||||
60
src/state.c
Normal file
60
src/state.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "nihilispm.h"
|
||||
#include "nihilispm_internal.h"
|
||||
#include "nihilispm_state.h"
|
||||
#include "nihilispm_utility.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Implements state as a giant alist
|
||||
// TODO: Consider generalizing the alist concept
|
||||
|
||||
struct nl_state {
|
||||
struct nl_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) {
|
||||
FOREACH_LIST(state->list, iter, item) {
|
||||
assert(item->type == NL_TYPE_CELL);
|
||||
const char *item_name = nl_list_nth(item, NAME_POSITION)->string;
|
||||
if (!strcmp(name, item_name)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
if (cell == NULL) {
|
||||
nl_list_append(state->list,
|
||||
nl_tuple_create(
|
||||
nl_string_create(strdup(name)),
|
||||
obj));
|
||||
} else {
|
||||
// TODO: Refcounting / cleanup
|
||||
cell->cell.cdr->cell.car = obj;
|
||||
}
|
||||
}
|
||||
|
||||
struct nl_state *nl_state_create() {
|
||||
struct nl_state *state = malloc(sizeof(struct nl_state));
|
||||
state->list = nl_nil_create();
|
||||
return state;
|
||||
}
|
||||
|
||||
void nl_state_delete(struct nl_state *state) {
|
||||
nl_object_delete(state->list);
|
||||
}
|
||||
@@ -7,9 +7,9 @@
|
||||
#include <string.h>
|
||||
|
||||
struct nl_object *nl_car(struct nl_object *list) {
|
||||
if (list == NULL || list->type != NL_TYPE_CELL) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
NL_COND_OR_RET_ERROR(
|
||||
list != NULL && list->type == NL_TYPE_CELL,
|
||||
"Invalid type of argument 0 to 'nl_car'");
|
||||
|
||||
struct nl_object *car = list->cell.car;
|
||||
if (car == NULL) {
|
||||
@@ -20,9 +20,9 @@ struct nl_object *nl_car(struct nl_object *list) {
|
||||
}
|
||||
|
||||
struct nl_object *nl_cdr(struct nl_object *list) {
|
||||
if (list == NULL || list->type != NL_TYPE_CELL) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
NL_COND_OR_RET_ERROR(
|
||||
list != NULL && list->type == NL_TYPE_CELL,
|
||||
"Invalid type of argument 0 to 'nl_cdr'");
|
||||
|
||||
struct nl_object *cdr = list->cell.cdr;
|
||||
if (cdr == NULL) {
|
||||
@@ -49,9 +49,9 @@ struct nl_object *nl_predicate(bool value) {
|
||||
}
|
||||
|
||||
struct nl_object *nl_list_length(struct nl_object *list) {
|
||||
if (list == NULL || list->type != NL_TYPE_CELL) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
NL_COND_OR_RET_ERROR(
|
||||
list != NULL && list->type == NL_TYPE_CELL,
|
||||
"Invalid type of argument 0 to 'nl_list_length'");
|
||||
|
||||
struct nl_object *node = list;
|
||||
if (list->cell.car == NULL) {
|
||||
@@ -68,14 +68,12 @@ struct nl_object *nl_list_length(struct nl_object *list) {
|
||||
}
|
||||
|
||||
struct nl_object *nl_list_nth(struct nl_object *list, int n) {
|
||||
if (list == NULL || list->type != NL_TYPE_CELL) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
NL_COND_OR_RET_ERROR(
|
||||
list != NULL && list->type == NL_TYPE_CELL,
|
||||
"Invalid type of argument 0 to 'nl_list_'");
|
||||
|
||||
int length = nl_list_length(list)->integer;
|
||||
if (length <= n) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
NL_COND_OR_RET_ERROR(length > n, "Position n >= list length in nl_list_nth");
|
||||
|
||||
struct nl_object *node = list;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -87,7 +85,7 @@ struct nl_object *nl_list_nth(struct nl_object *list, int n) {
|
||||
|
||||
struct nl_object *nl_truthy(struct nl_object *obj) {
|
||||
// TODO: Implement me
|
||||
return NULL;
|
||||
return nl_error_create("Unimplemented function 'nl_truthy'");
|
||||
}
|
||||
|
||||
struct nl_object *nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1) {
|
||||
|
||||
Reference in New Issue
Block a user