2008-06-30 21:54:22 -04:00
|
|
|
/*
|
|
|
|
Extra femtoLisp builtin functions
|
|
|
|
*/
|
|
|
|
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2017-08-19 14:54:32 -04:00
|
|
|
#include <math.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2019-08-09 16:25:20 -04:00
|
|
|
#include <stdint.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-26 15:12:15 -04:00
|
|
|
#include "scheme.h"
|
2019-08-09 15:08:44 -04:00
|
|
|
|
2019-08-28 05:49:10 -04:00
|
|
|
extern char **environ;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
size_t llength(value_t v)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
while (iscons(v)) {
|
|
|
|
n++;
|
|
|
|
v = cdr_(v);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_nconc(value_t *args, uint32_t nargs)
|
2009-03-16 23:29:17 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t lst, first;
|
|
|
|
value_t *pcdr;
|
|
|
|
struct cons *c;
|
|
|
|
uint32_t i;
|
|
|
|
|
2009-03-16 23:29:17 -04:00
|
|
|
if (nargs == 0)
|
2009-08-09 14:04:03 -04:00
|
|
|
return FL_NIL;
|
2019-08-18 18:14:09 -04:00
|
|
|
first = FL_NIL;
|
|
|
|
pcdr = &first;
|
|
|
|
i = 0;
|
2009-04-22 11:02:49 -04:00
|
|
|
while (1) {
|
2009-07-20 00:57:17 -04:00
|
|
|
lst = args[i++];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (i >= nargs)
|
|
|
|
break;
|
2009-04-21 21:40:10 -04:00
|
|
|
if (iscons(lst)) {
|
|
|
|
*pcdr = lst;
|
2019-08-09 12:28:14 -04:00
|
|
|
c = (struct cons *)ptr(lst);
|
2009-03-16 23:29:17 -04:00
|
|
|
while (iscons(c->cdr))
|
2019-08-09 12:28:14 -04:00
|
|
|
c = (struct cons *)ptr(c->cdr);
|
2009-03-16 23:29:17 -04:00
|
|
|
pcdr = &c->cdr;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (lst != FL_NIL) {
|
2009-04-21 21:40:10 -04:00
|
|
|
type_error("nconc", "cons", lst);
|
2009-03-16 23:29:17 -04:00
|
|
|
}
|
|
|
|
}
|
2009-04-21 21:40:10 -04:00
|
|
|
*pcdr = lst;
|
2009-03-16 23:29:17 -04:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_assq(value_t *args, uint32_t nargs)
|
2009-03-16 23:29:17 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t item;
|
|
|
|
value_t v;
|
2009-03-16 23:29:17 -04:00
|
|
|
value_t bind;
|
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
argcount("assq", nargs, 2);
|
|
|
|
item = args[0];
|
|
|
|
v = args[1];
|
2009-03-16 23:29:17 -04:00
|
|
|
while (iscons(v)) {
|
|
|
|
bind = car_(v);
|
|
|
|
if (iscons(bind) && car_(bind) == item)
|
|
|
|
return bind;
|
|
|
|
v = cdr_(v);
|
|
|
|
}
|
|
|
|
return FL_F;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_memq(value_t *args, uint32_t nargs)
|
2009-03-16 23:29:17 -04:00
|
|
|
{
|
|
|
|
argcount("memq", nargs, 2);
|
|
|
|
while (iscons(args[1])) {
|
2019-08-09 12:28:14 -04:00
|
|
|
struct cons *c = (struct cons *)ptr(args[1]);
|
2009-03-16 23:29:17 -04:00
|
|
|
if (c->car == args[0])
|
|
|
|
return args[1];
|
|
|
|
args[1] = c->cdr;
|
|
|
|
}
|
|
|
|
return FL_F;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_length(value_t *args, uint32_t nargs)
|
2009-04-09 00:04:27 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t a;
|
2019-08-09 12:36:20 -04:00
|
|
|
struct cvalue *cv;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
|
|
|
argcount("length", nargs, 1);
|
|
|
|
a = args[0];
|
2009-04-09 00:04:27 -04:00
|
|
|
if (isvector(a)) {
|
|
|
|
return fixnum(vector_size(a));
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscprim(a)) {
|
2019-08-09 12:36:20 -04:00
|
|
|
cv = (struct cvalue *)ptr(a);
|
2009-04-09 00:04:27 -04:00
|
|
|
if (cp_class(cv) == bytetype)
|
|
|
|
return fixnum(1);
|
|
|
|
else if (cp_class(cv) == wchartype)
|
2019-08-09 12:25:43 -04:00
|
|
|
return fixnum(
|
|
|
|
u8_charlen(*(uint32_t *)cp_data((struct cprim *)cv)));
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscvalue(a)) {
|
2019-08-09 12:36:20 -04:00
|
|
|
cv = (struct cvalue *)ptr(a);
|
2009-04-09 00:04:27 -04:00
|
|
|
if (cv_class(cv)->eltype != NULL)
|
|
|
|
return size_wrap(cvalue_arraylen(a));
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (a == FL_NIL) {
|
2009-04-09 00:04:27 -04:00
|
|
|
return fixnum(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscons(a)) {
|
2009-04-09 00:04:27 -04:00
|
|
|
return fixnum(llength(a));
|
|
|
|
}
|
|
|
|
type_error("length", "sequence", a);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO
|
2009-04-09 00:04:27 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_f_raise(value_t *args, uint32_t nargs)
|
2009-03-24 22:28:21 -04:00
|
|
|
{
|
|
|
|
argcount("raise", nargs, 1);
|
2009-08-28 20:54:51 -04:00
|
|
|
fl_raise(args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO
|
2009-03-24 22:28:21 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_exit(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
if (nargs > 0)
|
|
|
|
exit(tofixnum(args[0], "exit"));
|
|
|
|
exit(0);
|
2009-08-09 14:04:03 -04:00
|
|
|
return FL_NIL;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_symbol(value_t *args, uint32_t nargs)
|
2008-12-28 03:01:18 -05:00
|
|
|
{
|
2009-06-29 23:21:41 -04:00
|
|
|
argcount("symbol", nargs, 1);
|
2009-08-23 03:06:57 -04:00
|
|
|
if (!fl_isstring(args[0]))
|
2009-06-29 23:21:41 -04:00
|
|
|
type_error("symbol", "string", args[0]);
|
2008-12-28 03:01:18 -05:00
|
|
|
return symbol(cvalue_data(args[0]));
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_keywordp(value_t *args, uint32_t nargs)
|
2009-07-16 21:30:26 -04:00
|
|
|
{
|
|
|
|
argcount("keyword?", nargs, 1);
|
2019-08-09 12:30:15 -04:00
|
|
|
return (issymbol(args[0]) && iskeyword((struct symbol *)ptr(args[0])))
|
|
|
|
? FL_T
|
|
|
|
: FL_F;
|
2009-07-16 21:30:26 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_top_level_value(value_t *args, uint32_t nargs)
|
2009-04-17 10:41:15 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct symbol *sym;
|
|
|
|
|
2009-04-17 10:41:15 -04:00
|
|
|
argcount("top-level-value", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
sym = tosymbol(args[0], "top-level-value");
|
2009-04-17 10:41:15 -04:00
|
|
|
if (sym->binding == UNBOUND)
|
2010-04-29 14:01:26 -04:00
|
|
|
fl_raise(fl_list2(UnboundError, args[0]));
|
2009-04-17 10:41:15 -04:00
|
|
|
return sym->binding;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_set_top_level_value(value_t *args, uint32_t nargs)
|
2009-04-17 10:41:15 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct symbol *sym;
|
|
|
|
|
2009-04-17 10:41:15 -04:00
|
|
|
argcount("set-top-level-value!", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
sym = tosymbol(args[0], "set-top-level-value!");
|
2009-07-28 00:16:20 -04:00
|
|
|
if (!isconstant(sym))
|
2009-04-17 10:41:15 -04:00
|
|
|
sym->binding = args[1];
|
|
|
|
return args[1];
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:30:15 -04:00
|
|
|
static void global_env_list(struct symbol *root, value_t *pv)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
while (root != NULL) {
|
2009-04-20 20:56:05 -04:00
|
|
|
if (root->name[0] != ':' && (root->binding != UNBOUND)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
*pv = fl_cons(tagptr(root, TAG_SYM), *pv);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2009-03-23 16:44:19 -04:00
|
|
|
global_env_list(root->left, pv);
|
2008-06-30 21:54:22 -04:00
|
|
|
root = root->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:30:15 -04:00
|
|
|
extern struct symbol *symtab;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_global_env(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t lst;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
(void)args;
|
|
|
|
argcount("environment", nargs, 0);
|
2019-08-18 18:14:09 -04:00
|
|
|
lst = FL_NIL;
|
2009-05-18 22:54:56 -04:00
|
|
|
fl_gc_handle(&lst);
|
|
|
|
global_env_list(symtab, &lst);
|
|
|
|
fl_free_gc_handles(1);
|
|
|
|
return lst;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2008-07-18 00:16:07 -04:00
|
|
|
extern value_t QUOTE;
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_constantp(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
argcount("constant?", nargs, 1);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (issymbol(args[0]))
|
2019-08-09 12:30:15 -04:00
|
|
|
return (isconstant((struct symbol *)ptr(args[0])) ? FL_T : FL_F);
|
2008-07-18 00:16:07 -04:00
|
|
|
if (iscons(args[0])) {
|
|
|
|
if (car_(args[0]) == QUOTE)
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_T;
|
|
|
|
return FL_F;
|
2008-07-18 00:16:07 -04:00
|
|
|
}
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_T;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_integer_valuedp(value_t *args, uint32_t nargs)
|
2009-02-01 00:41:43 -05:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
double d;
|
|
|
|
void *data;
|
|
|
|
|
2009-04-17 10:41:15 -04:00
|
|
|
argcount("integer-valued?", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2009-02-01 00:41:43 -05:00
|
|
|
if (isfixnum(v)) {
|
|
|
|
return FL_T;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscprim(v)) {
|
2019-08-09 12:25:43 -04:00
|
|
|
numerictype_t nt = cp_numtype((struct cprim *)ptr(v));
|
2009-02-01 00:41:43 -05:00
|
|
|
if (nt < T_FLOAT)
|
|
|
|
return FL_T;
|
2019-08-18 18:14:09 -04:00
|
|
|
data = cp_data((struct cprim *)ptr(v));
|
2009-02-01 00:41:43 -05:00
|
|
|
if (nt == T_FLOAT) {
|
2019-08-09 07:02:02 -04:00
|
|
|
float f = *(float *)data;
|
|
|
|
if (f < 0)
|
|
|
|
f = -f;
|
2009-02-01 00:41:43 -05:00
|
|
|
if (f <= FLT_MAXINT && (float)(int32_t)f == f)
|
|
|
|
return FL_T;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-02-01 00:41:43 -05:00
|
|
|
assert(nt == T_DOUBLE);
|
2019-08-18 18:14:09 -04:00
|
|
|
d = *(double *)data;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (d < 0)
|
|
|
|
d = -d;
|
2009-02-01 00:41:43 -05:00
|
|
|
if (d <= DBL_MAXINT && (double)(int64_t)d == d)
|
|
|
|
return FL_T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FL_F;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_integerp(value_t *args, uint32_t nargs)
|
2009-04-17 10:41:15 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t v;
|
|
|
|
|
2009-04-17 10:41:15 -04:00
|
|
|
argcount("integer?", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[0];
|
2009-04-17 10:41:15 -04:00
|
|
|
return (isfixnum(v) ||
|
2019-08-09 12:25:43 -04:00
|
|
|
(iscprim(v) && cp_numtype((struct cprim *)ptr(v)) < T_FLOAT))
|
2019-08-09 07:02:02 -04:00
|
|
|
? FL_T
|
|
|
|
: FL_F;
|
2009-04-17 10:41:15 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_fixnum(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
argcount("fixnum", nargs, 1);
|
2009-01-02 18:00:21 -05:00
|
|
|
if (isfixnum(args[0])) {
|
2008-06-30 21:54:22 -04:00
|
|
|
return args[0];
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscprim(args[0])) {
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *cp = (struct cprim *)ptr(args[0]);
|
2009-01-02 18:00:21 -05:00
|
|
|
return fixnum(conv_to_long(cp_data(cp), cp_numtype(cp)));
|
|
|
|
}
|
2009-03-23 16:44:19 -04:00
|
|
|
type_error("fixnum", "number", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_truncate(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
argcount("truncate", nargs, 1);
|
|
|
|
if (isfixnum(args[0]))
|
|
|
|
return args[0];
|
2009-01-02 18:00:21 -05:00
|
|
|
if (iscprim(args[0])) {
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *cp = (struct cprim *)ptr(args[0]);
|
2009-01-02 18:00:21 -05:00
|
|
|
void *data = cp_data(cp);
|
|
|
|
numerictype_t nt = cp_numtype(cp);
|
|
|
|
double d;
|
|
|
|
if (nt == T_FLOAT)
|
2019-08-09 07:02:02 -04:00
|
|
|
d = (double)*(float *)data;
|
2009-01-02 18:00:21 -05:00
|
|
|
else if (nt == T_DOUBLE)
|
2019-08-09 07:02:02 -04:00
|
|
|
d = *(double *)data;
|
2009-01-02 18:00:21 -05:00
|
|
|
else
|
|
|
|
return args[0];
|
2009-04-22 11:02:49 -04:00
|
|
|
if (d > 0) {
|
|
|
|
if (d > (double)U64_MAX)
|
|
|
|
return args[0];
|
2009-01-02 18:00:21 -05:00
|
|
|
return return_from_uint64((uint64_t)d);
|
2009-04-22 11:02:49 -04:00
|
|
|
}
|
|
|
|
if (d > (double)S64_MAX || d < (double)S64_MIN)
|
|
|
|
return args[0];
|
2009-01-02 18:00:21 -05:00
|
|
|
return return_from_int64((int64_t)d);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
type_error("truncate", "number", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_vector_alloc(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
fixnum_t i;
|
|
|
|
value_t f, v;
|
2019-08-18 18:14:09 -04:00
|
|
|
int k;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
if (nargs == 0)
|
|
|
|
lerror(ArgError, "vector.alloc: too few arguments");
|
2009-11-18 12:38:16 -05:00
|
|
|
i = (fixnum_t)toulong(args[0], "vector.alloc");
|
2008-06-30 21:54:22 -04:00
|
|
|
if (i < 0)
|
|
|
|
lerror(ArgError, "vector.alloc: invalid size");
|
2017-08-09 16:59:04 -04:00
|
|
|
v = alloc_vector((unsigned)i, 0);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (nargs == 2)
|
|
|
|
f = args[1];
|
|
|
|
else
|
2010-01-06 13:27:28 -05:00
|
|
|
f = FL_UNSPECIFIED;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (k = 0; k < i; k++)
|
|
|
|
vector_elt(v, k) = f;
|
2008-06-30 21:54:22 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2008-12-28 03:01:18 -05:00
|
|
|
static double todouble(value_t a, char *fname)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
if (isfixnum(a))
|
|
|
|
return (double)numval(a);
|
2009-01-02 18:00:21 -05:00
|
|
|
if (iscprim(a)) {
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *cp = (struct cprim *)ptr(a);
|
2009-01-02 18:00:21 -05:00
|
|
|
numerictype_t nt = cp_numtype(cp);
|
|
|
|
return conv_to_double(cp_data(cp), nt);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
type_error(fname, "number", a);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2009-03-16 23:29:17 -04:00
|
|
|
static value_t fl_path_cwd(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *ptr;
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
if (nargs > 1)
|
|
|
|
argcount("path.cwd", nargs, 1);
|
|
|
|
if (nargs == 0) {
|
|
|
|
char buf[1024];
|
|
|
|
get_cwd(buf, sizeof(buf));
|
2008-09-10 22:37:38 -04:00
|
|
|
return string_from_cstr(buf);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
ptr = tostring(args[0], "path.cwd");
|
2008-06-30 21:54:22 -04:00
|
|
|
if (set_cwd(ptr))
|
2009-03-24 22:28:21 -04:00
|
|
|
lerrorf(IOError, "path.cwd: could not cd to %s", ptr);
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_T;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-02 12:46:27 -04:00
|
|
|
value_t builtin_file_exists(value_t *args, uint32_t nargs)
|
2009-08-13 23:17:21 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *str;
|
|
|
|
|
2019-09-02 12:46:27 -04:00
|
|
|
argcount("file-exists?", nargs, 1);
|
|
|
|
str = tostring(args[0], "file-exists?");
|
2019-08-18 06:39:08 -04:00
|
|
|
return os_path_exists(str) ? FL_T : FL_F;
|
2009-08-13 23:17:21 -04:00
|
|
|
}
|
|
|
|
|
2019-08-28 05:49:10 -04:00
|
|
|
value_t builtin_get_environment_variables(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
struct accum acc = ACCUM_EMPTY;
|
|
|
|
char **pairs;
|
|
|
|
const char *pair;
|
|
|
|
const char *pivot;
|
|
|
|
value_t name, value;
|
|
|
|
|
2019-10-13 15:57:27 -04:00
|
|
|
// TODO: memory leak: acc not freed on type error
|
2019-08-28 05:49:10 -04:00
|
|
|
(void)args;
|
|
|
|
argcount("get-environment-variables", nargs, 0);
|
|
|
|
for (pairs = environ; (pair = *pairs); pairs++) {
|
|
|
|
pivot = strchr(pair, '=');
|
|
|
|
if (!pivot) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name = string_from_cstrn(pair, pivot - pair);
|
|
|
|
value = string_from_cstr(pivot + 1);
|
|
|
|
accum_pair(&acc, name, value);
|
|
|
|
}
|
|
|
|
return acc.list;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_get_environment_variable(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *name;
|
|
|
|
char *val;
|
|
|
|
|
2019-08-28 05:49:10 -04:00
|
|
|
argcount("get-environment-variable", nargs, 1);
|
|
|
|
name = tostring(args[0], "get-environment-variable");
|
2019-08-18 18:14:09 -04:00
|
|
|
val = getenv(name);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (val == NULL)
|
|
|
|
return FL_F;
|
2008-06-30 21:54:22 -04:00
|
|
|
if (*val == 0)
|
|
|
|
return symbol_value(emptystringsym);
|
2008-09-10 22:37:38 -04:00
|
|
|
return cvalue_static_cstring(val);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-28 05:49:10 -04:00
|
|
|
value_t builtin_set_environment_variable(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-21 15:16:07 -04:00
|
|
|
const char *name;
|
|
|
|
const char *value;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-28 05:49:10 -04:00
|
|
|
argcount("set-environment-variable", nargs, 2);
|
|
|
|
name = tostring(args[0], "set-environment-variable");
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
if (args[1] == FL_F) {
|
2019-08-21 15:16:07 -04:00
|
|
|
value = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2019-08-28 05:49:10 -04:00
|
|
|
value = tostring(args[1], "set-environment-variable");
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-21 15:16:07 -04:00
|
|
|
os_setenv(name, value);
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_T;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_rand(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
fixnum_t r;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
2008-08-16 17:15:36 -04:00
|
|
|
#ifdef BITS64
|
2019-08-09 07:02:02 -04:00
|
|
|
r = ((((uint64_t)random()) << 32) | random()) & 0x1fffffffffffffffLL;
|
2008-08-16 17:15:36 -04:00
|
|
|
#else
|
|
|
|
r = random() & 0x1fffffff;
|
|
|
|
#endif
|
|
|
|
return fixnum(r);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_rand32(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
uint32_t r;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
2019-08-18 18:14:09 -04:00
|
|
|
r = random();
|
2008-08-16 17:15:36 -04:00
|
|
|
#ifdef BITS64
|
|
|
|
return fixnum(r);
|
|
|
|
#else
|
|
|
|
return mk_uint32(r);
|
|
|
|
#endif
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_rand64(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
uint64_t r;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
2019-08-18 18:14:09 -04:00
|
|
|
r = (((uint64_t)random()) << 32) | random();
|
2008-08-16 17:15:36 -04:00
|
|
|
return mk_uint64(r);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_randd(value_t *args, uint32_t nargs)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
2008-06-30 21:54:22 -04:00
|
|
|
return mk_double(rand_double());
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
static value_t fl_randf(value_t *args, uint32_t nargs)
|
2008-08-16 17:15:36 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
2008-08-16 17:15:36 -04:00
|
|
|
return mk_float(rand_float());
|
|
|
|
}
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
#define MATH_FUNC_1ARG(name) \
|
|
|
|
static value_t fl_##name(value_t *args, uint32_t nargs) \
|
|
|
|
{ \
|
|
|
|
argcount(#name, nargs, 1); \
|
|
|
|
if (iscprim(args[0])) { \
|
|
|
|
struct cprim *cp = (struct cprim *)ptr(args[0]); \
|
|
|
|
numerictype_t nt = cp_numtype(cp); \
|
|
|
|
if (nt == T_FLOAT) { \
|
|
|
|
float f = *(float *)cp_data(cp); \
|
|
|
|
return mk_float(name((double)f)); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return mk_double(name(todouble(args[0], #name))); \
|
2019-08-09 07:02:02 -04:00
|
|
|
}
|
2017-08-19 14:54:32 -04:00
|
|
|
|
|
|
|
MATH_FUNC_1ARG(sqrt)
|
|
|
|
MATH_FUNC_1ARG(exp)
|
|
|
|
MATH_FUNC_1ARG(log)
|
|
|
|
MATH_FUNC_1ARG(sin)
|
|
|
|
MATH_FUNC_1ARG(cos)
|
|
|
|
MATH_FUNC_1ARG(tan)
|
|
|
|
MATH_FUNC_1ARG(asin)
|
|
|
|
MATH_FUNC_1ARG(acos)
|
|
|
|
MATH_FUNC_1ARG(atan)
|
|
|
|
|
2019-08-28 07:07:50 -04:00
|
|
|
static const char help_text[] =
|
|
|
|
""
|
|
|
|
"----------------------------------------------------------------------\n"
|
|
|
|
"You are in Up Scheme.\n"
|
|
|
|
"Type (exit) to exit.\n"
|
|
|
|
"----------------------------------------------------------------------\n";
|
|
|
|
|
|
|
|
static value_t builtin_help_star(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
(void)nargs;
|
|
|
|
ios_puts(help_text, ios_stdout);
|
|
|
|
return FL_T;
|
|
|
|
}
|
|
|
|
|
2013-06-08 19:29:15 -04:00
|
|
|
extern void stringfuncs_init(void);
|
|
|
|
extern void table_init(void);
|
|
|
|
extern void iostream_init(void);
|
2019-08-25 15:39:35 -04:00
|
|
|
extern void print_init(void);
|
2008-08-05 00:34:14 -04:00
|
|
|
|
2019-08-09 12:20:18 -04:00
|
|
|
static struct builtinspec builtin_info[] = {
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "environment", fl_global_env },
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
{ "constant?", fl_constantp },
|
2009-04-17 10:41:15 -04:00
|
|
|
{ "top-level-value", fl_top_level_value },
|
|
|
|
{ "set-top-level-value!", fl_set_top_level_value },
|
2009-08-28 20:54:51 -04:00
|
|
|
{ "raise", fl_f_raise },
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "exit", fl_exit },
|
2009-06-29 23:21:41 -04:00
|
|
|
{ "symbol", fl_symbol },
|
2009-07-16 21:30:26 -04:00
|
|
|
{ "keyword?", fl_keywordp },
|
2009-04-17 10:41:15 -04:00
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "fixnum", fl_fixnum },
|
|
|
|
{ "truncate", fl_truncate },
|
2009-02-01 00:41:43 -05:00
|
|
|
{ "integer?", fl_integerp },
|
2009-04-17 10:41:15 -04:00
|
|
|
{ "integer-valued?", fl_integer_valuedp },
|
2009-03-16 23:29:17 -04:00
|
|
|
{ "nconc", fl_nconc },
|
2009-05-31 17:06:04 -04:00
|
|
|
{ "append!", fl_nconc },
|
2009-03-16 23:29:17 -04:00
|
|
|
{ "assq", fl_assq },
|
|
|
|
{ "memq", fl_memq },
|
2009-04-09 00:04:27 -04:00
|
|
|
{ "length", fl_length },
|
2008-11-23 02:12:37 -05:00
|
|
|
|
|
|
|
{ "vector.alloc", fl_vector_alloc },
|
|
|
|
|
|
|
|
{ "rand", fl_rand },
|
|
|
|
{ "rand.uint32", fl_rand32 },
|
|
|
|
{ "rand.uint64", fl_rand64 },
|
|
|
|
{ "rand.double", fl_randd },
|
|
|
|
{ "rand.float", fl_randf },
|
|
|
|
|
2017-08-19 14:54:32 -04:00
|
|
|
{ "sqrt", fl_sqrt },
|
2019-08-09 07:02:02 -04:00
|
|
|
{ "exp", fl_exp },
|
|
|
|
{ "log", fl_log },
|
|
|
|
{ "sin", fl_sin },
|
|
|
|
{ "cos", fl_cos },
|
|
|
|
{ "tan", fl_tan },
|
2017-08-19 14:54:32 -04:00
|
|
|
{ "asin", fl_asin },
|
|
|
|
{ "acos", fl_acos },
|
|
|
|
{ "atan", fl_atan },
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "path.cwd", fl_path_cwd },
|
|
|
|
|
2019-08-28 07:07:50 -04:00
|
|
|
{ "help*", builtin_help_star },
|
|
|
|
|
2019-08-12 11:18:46 -04:00
|
|
|
{ "import-procedure", builtin_import },
|
2019-08-11 15:32:55 -04:00
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2013-06-08 19:29:15 -04:00
|
|
|
void builtins_init(void)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2008-11-23 02:12:37 -05:00
|
|
|
assign_global_builtins(builtin_info);
|
2008-08-05 00:34:14 -04:00
|
|
|
stringfuncs_init();
|
2008-12-20 01:16:00 -05:00
|
|
|
table_init();
|
2009-02-08 22:22:31 -05:00
|
|
|
iostream_init();
|
2019-08-25 15:39:35 -04:00
|
|
|
print_init();
|
2019-08-28 04:11:55 -04:00
|
|
|
os_init();
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|