Add lots of utilties, initial builtins
This commit is contained in:
142
src/builtins.c
Normal file
142
src/builtins.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "nihilispm.h"
|
||||
#include "nihilispm_internal.h"
|
||||
#include "nihilispm_utility.h"
|
||||
#include "builtins.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
LISP_FUNC_0(nl_builtin_hello_world) {
|
||||
return nl_string_create(strdup("Hello, world!"));
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_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_COUNT:
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_symbol_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_SYMBOL);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_string_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_STRING);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_int_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_INT);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_list_p, arg) {
|
||||
return nl_predicate(arg->type == NL_TYPE_CELL);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_car, arg) {
|
||||
return nl_car(arg);
|
||||
}
|
||||
|
||||
LISP_FUNC_1(nl_builtin_cdr, arg) {
|
||||
return nl_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 NULL;
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
int len = strlen(arg0->string) + strlen(arg1->string);
|
||||
char *outstr = malloc(strlen(arg0->string) + strlen(arg1->string));
|
||||
outstr[0] = '\0';
|
||||
strcat(outstr, arg0->string);
|
||||
strcat(outstr, arg1->string);
|
||||
|
||||
return nl_string_create(outstr);
|
||||
}
|
||||
|
||||
LISP_FUNC_0(nl_builtin_now_millis_mono) {
|
||||
// TODO: Implement and move to a 'platform' file
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user