reorganize files
This commit is contained in:
+32
-1
@@ -74,7 +74,7 @@ void pic_gc(pic_state *);
|
||||
int pic_get_args(pic_state *, const char *fmt, ...);
|
||||
|
||||
void pic_defun(pic_state *, const char *name, pic_func_t f);
|
||||
void pic_defvar(pic_state *, const char *name, pic_value v, pic_value conv);
|
||||
void pic_defvar(pic_state *, const char *name, pic_value v);
|
||||
void pic_define(pic_state *, const char *lib, const char *name, pic_value v);
|
||||
pic_value pic_ref(pic_state *, const char *lib, const char *name);
|
||||
void pic_set(pic_state *, const char *lib, const char *name, pic_value v);
|
||||
@@ -179,6 +179,7 @@ enum {
|
||||
#define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT)
|
||||
#define pic_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR)
|
||||
#define pic_eof_p(pic, v) (pic_type(pic, v) == PIC_TYPE_EOF)
|
||||
#define pic_bool_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE || pic_type(pic,v) == PIC_TYPE_FALSE)
|
||||
#define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE)
|
||||
#define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE)
|
||||
#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL)
|
||||
@@ -270,6 +271,10 @@ int pic_str_hash(pic_state *, pic_value str);
|
||||
#define PIC_SEEK_END 1
|
||||
#define PIC_SEEK_SET 2
|
||||
|
||||
#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0)
|
||||
#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0)
|
||||
#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0)
|
||||
|
||||
pic_value pic_funopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *));
|
||||
size_t pic_fread(pic_state *, void *ptr, size_t size, size_t count, pic_value port);
|
||||
size_t pic_fwrite(pic_state *, const void *ptr, size_t size, size_t count, pic_value port);
|
||||
@@ -291,6 +296,32 @@ int pic_printf(pic_state *, const char *fmt, ...);
|
||||
int pic_fprintf(pic_state *, pic_value port, const char *fmt, ...);
|
||||
int pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap);
|
||||
|
||||
pic_value pic_fmemopen(pic_state *, const char *buf, int len, const char *mode); /* deprecated */
|
||||
int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len); /* deprecated */
|
||||
|
||||
|
||||
/* debug */
|
||||
void pic_warnf(pic_state *, const char *, ...);
|
||||
pic_value pic_get_backtrace(pic_state *);
|
||||
|
||||
|
||||
|
||||
/* utility macros */
|
||||
|
||||
#define pic_for_each(var, list, it) \
|
||||
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
|
||||
if ((var = pic_car(pic, it)), true)
|
||||
|
||||
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
|
||||
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
|
||||
|
||||
#define pic_void(pic, exec) pic_void_(pic, PIC_GENSYM(ai), exec)
|
||||
#define pic_void_(pic,ai,exec) do { \
|
||||
size_t ai = pic_enter(pic); \
|
||||
exec; \
|
||||
pic_leave(pic, ai); \
|
||||
} while (0)
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
||||
@@ -23,31 +23,9 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib);
|
||||
void pic_load(pic_state *, pic_value port);
|
||||
void pic_load_cstr(pic_state *, const char *);
|
||||
|
||||
#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0)
|
||||
#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0)
|
||||
#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0)
|
||||
|
||||
#if PIC_USE_STDIO
|
||||
pic_value pic_fopen(pic_state *, FILE *, const char *mode);
|
||||
#endif
|
||||
pic_value pic_fmemopen(pic_state *, const char *buf, int len, const char *mode);
|
||||
int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len);
|
||||
|
||||
/* utility macros */
|
||||
|
||||
#define pic_for_each(var, list, it) \
|
||||
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
|
||||
if ((var = pic_car(pic, it)), true)
|
||||
|
||||
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
|
||||
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
|
||||
|
||||
#define pic_void(pic, exec) pic_void_(pic, PIC_GENSYM(ai), exec)
|
||||
#define pic_void_(pic,ai,exec) do { \
|
||||
size_t ai = pic_enter(pic); \
|
||||
exec; \
|
||||
pic_leave(pic, ai); \
|
||||
} while (0)
|
||||
|
||||
#define pic_deflibrary(pic, lib) do { \
|
||||
if (! pic_find_library(pic, lib)) { \
|
||||
@@ -78,8 +56,6 @@ int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len);
|
||||
|
||||
/* for debug */
|
||||
|
||||
void pic_warnf(pic_state *, const char *, ...);
|
||||
pic_value pic_get_backtrace(pic_state *);
|
||||
#if PIC_USE_WRITE
|
||||
void pic_print_error(pic_state *, pic_value port, pic_value err);
|
||||
#endif
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef PICRIN_FILE_H
|
||||
#define PICRIN_FILE_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct file {
|
||||
/* buffer */
|
||||
char buf[1]; /* fallback buffer */
|
||||
long cnt; /* characters left */
|
||||
char *ptr; /* next character position */
|
||||
char *base; /* location of the buffer */
|
||||
/* operators */
|
||||
struct {
|
||||
void *cookie;
|
||||
int (*read)(pic_state *, void *, char *, int);
|
||||
int (*write)(pic_state *, void *, const char *, int);
|
||||
long (*seek)(pic_state *, void *, long, int);
|
||||
int (*close)(pic_state *, void *);
|
||||
} vtable;
|
||||
int flag; /* mode of the file access */
|
||||
};
|
||||
|
||||
enum {
|
||||
FILE_READ = 01,
|
||||
FILE_WRITE = 02,
|
||||
FILE_UNBUF = 04,
|
||||
FILE_EOF = 010,
|
||||
FILE_ERR = 020,
|
||||
FILE_LNBUF = 040
|
||||
};
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,28 +0,0 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_GC_H
|
||||
#define PICRIN_GC_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if PIC_BITMAP_GC
|
||||
# define OBJECT_HEADER \
|
||||
unsigned char tt;
|
||||
#else
|
||||
# define OBJECT_HEADER \
|
||||
unsigned char tt; \
|
||||
char gc_mark;
|
||||
#endif
|
||||
|
||||
struct heap *pic_heap_open(pic_state *);
|
||||
void pic_heap_close(pic_state *, struct heap *);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,234 +0,0 @@
|
||||
/* The MIT License
|
||||
|
||||
Copyright (c) 2015 by Yuichi Nishiwaki <yuichi.nishiwaki@gmail.com>
|
||||
Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_KHASH_H
|
||||
#define PICRIN_KHASH_H
|
||||
|
||||
#define ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
|
||||
#define ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
|
||||
#define ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
|
||||
#define ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
|
||||
#define ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
|
||||
#define ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
|
||||
#define ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
|
||||
|
||||
#define ac_roundup32(x) \
|
||||
(--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
|
||||
|
||||
#define ac_fsize(m) ((m) < 16? 1 : (m)>>4)
|
||||
#define ac_hash_upper(x) ((((x) * 2) * 77 / 100 + 1) / 2)
|
||||
|
||||
#define KHASH_DECLARE(name, khkey_t, khval_t) \
|
||||
typedef struct { \
|
||||
int n_buckets, size, n_occupied, upper_bound; \
|
||||
int *flags; \
|
||||
khkey_t *keys; \
|
||||
khval_t *vals; \
|
||||
} kh_##name##_t; \
|
||||
void kh_init_##name(kh_##name##_t *h); \
|
||||
void kh_destroy_##name(pic_state *, kh_##name##_t *h); \
|
||||
void kh_clear_##name(kh_##name##_t *h); \
|
||||
int kh_get_##name(pic_state *, const kh_##name##_t *h, khkey_t key); \
|
||||
void kh_resize_##name(pic_state *, kh_##name##_t *h, int new_n_buckets); \
|
||||
int kh_put_##name(pic_state *, kh_##name##_t *h, khkey_t key, int *ret); \
|
||||
void kh_del_##name(kh_##name##_t *h, int x);
|
||||
|
||||
#define KHASH_DEFINE(name, khkey_t, khval_t, hash_func, hash_equal) \
|
||||
KHASH_DEFINE2(name, khkey_t, khval_t, 1, hash_func, hash_equal)
|
||||
#define KHASH_DEFINE2(name, khkey_t, khval_t, kh_is_map, hash_func, hash_equal) \
|
||||
void kh_init_##name(kh_##name##_t *h) { \
|
||||
memset(h, 0, sizeof(kh_##name##_t)); \
|
||||
} \
|
||||
void kh_destroy_##name(pic_state *pic, kh_##name##_t *h) \
|
||||
{ \
|
||||
pic_free(pic, h->flags); \
|
||||
pic_free(pic, (void *)h->keys); \
|
||||
pic_free(pic, (void *)h->vals); \
|
||||
} \
|
||||
void kh_clear_##name(kh_##name##_t *h) \
|
||||
{ \
|
||||
if (h->flags) { \
|
||||
memset(h->flags, 0xaa, ac_fsize(h->n_buckets) * sizeof(int)); \
|
||||
h->size = h->n_occupied = 0; \
|
||||
} \
|
||||
} \
|
||||
int kh_get_##name(pic_state *pic, const kh_##name##_t *h, khkey_t key) \
|
||||
{ \
|
||||
(void)pic; \
|
||||
if (h->n_buckets) { \
|
||||
int k, i, last, mask, step = 0; \
|
||||
mask = h->n_buckets - 1; \
|
||||
k = hash_func(key); i = k & mask; \
|
||||
last = i; \
|
||||
while (!ac_isempty(h->flags, i) && (ac_isdel(h->flags, i) || !hash_equal(h->keys[i], key))) { \
|
||||
i = (i + (++step)) & mask; \
|
||||
if (i == last) return h->n_buckets; \
|
||||
} \
|
||||
return ac_iseither(h->flags, i)? h->n_buckets : i; \
|
||||
} else return 0; \
|
||||
} \
|
||||
void kh_resize_##name(pic_state *pic, kh_##name##_t *h, int new_n_buckets) \
|
||||
{ /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
|
||||
int *new_flags = 0; \
|
||||
int j = 1; \
|
||||
{ \
|
||||
ac_roundup32(new_n_buckets); \
|
||||
if (new_n_buckets < 4) new_n_buckets = 4; \
|
||||
if (h->size >= ac_hash_upper(new_n_buckets)) j = 0; /* requested size is too small */ \
|
||||
else { /* hash table size to be changed (shrink or expand); rehash */ \
|
||||
new_flags = pic_malloc(pic, ac_fsize(new_n_buckets) * sizeof(int)); \
|
||||
memset(new_flags, 0xaa, ac_fsize(new_n_buckets) * sizeof(int)); \
|
||||
if (h->n_buckets < new_n_buckets) { /* expand */ \
|
||||
h->keys = pic_realloc(pic, (void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
|
||||
if (kh_is_map) { \
|
||||
h->vals = pic_realloc(pic, (void *)h->vals, new_n_buckets * sizeof(khval_t)); \
|
||||
} \
|
||||
} /* otherwise shrink */ \
|
||||
} \
|
||||
} \
|
||||
if (j) { /* rehashing is needed */ \
|
||||
for (j = 0; j != h->n_buckets; ++j) { \
|
||||
if (ac_iseither(h->flags, j) == 0) { \
|
||||
khkey_t key = h->keys[j]; \
|
||||
khval_t val; \
|
||||
int new_mask; \
|
||||
new_mask = new_n_buckets - 1; \
|
||||
if (kh_is_map) val = h->vals[j]; \
|
||||
ac_set_isdel_true(h->flags, j); \
|
||||
while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
|
||||
int k, i, step = 0; \
|
||||
k = hash_func(key); \
|
||||
i = k & new_mask; \
|
||||
while (!ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
|
||||
ac_set_isempty_false(new_flags, i); \
|
||||
if (i < h->n_buckets && ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
|
||||
{ khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
|
||||
if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
|
||||
ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
|
||||
} else { /* write the element and jump out of the loop */ \
|
||||
h->keys[i] = key; \
|
||||
if (kh_is_map) h->vals[i] = val; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
|
||||
h->keys = pic_realloc(pic, (void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
|
||||
if (kh_is_map) h->vals = pic_realloc(pic, (void *)h->vals, new_n_buckets * sizeof(khval_t)); \
|
||||
} \
|
||||
pic_free(pic, h->flags); /* free the working space */ \
|
||||
h->flags = new_flags; \
|
||||
h->n_buckets = new_n_buckets; \
|
||||
h->n_occupied = h->size; \
|
||||
h->upper_bound = ac_hash_upper(h->n_buckets); \
|
||||
} \
|
||||
} \
|
||||
int kh_put_##name(pic_state *pic, kh_##name##_t *h, khkey_t key, int *ret) \
|
||||
{ \
|
||||
int x; \
|
||||
if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
|
||||
if (h->n_buckets > (h->size<<1)) { \
|
||||
kh_resize_##name(pic, h, h->n_buckets - 1); /* clear "deleted" elements */ \
|
||||
} else { \
|
||||
kh_resize_##name(pic, h, h->n_buckets + 1); /* expand the hash table */ \
|
||||
} \
|
||||
} /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
|
||||
{ \
|
||||
int k, i, site, last, mask = h->n_buckets - 1, step = 0; \
|
||||
x = site = h->n_buckets; k = hash_func(key); i = k & mask; \
|
||||
if (ac_isempty(h->flags, i)) x = i; /* for speed up */ \
|
||||
else { \
|
||||
last = i; \
|
||||
while (!ac_isempty(h->flags, i) && (ac_isdel(h->flags, i) || !hash_equal(h->keys[i], key))) { \
|
||||
if (ac_isdel(h->flags, i)) site = i; \
|
||||
i = (i + (++step)) & mask; \
|
||||
if (i == last) { x = site; break; } \
|
||||
} \
|
||||
if (x == h->n_buckets) { \
|
||||
if (ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
|
||||
else x = i; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
if (ac_isempty(h->flags, x)) { /* not present at all */ \
|
||||
h->keys[x] = key; \
|
||||
ac_set_isboth_false(h->flags, x); \
|
||||
++h->size; ++h->n_occupied; \
|
||||
*ret = 1; \
|
||||
} else if (ac_isdel(h->flags, x)) { /* deleted */ \
|
||||
h->keys[x] = key; \
|
||||
ac_set_isboth_false(h->flags, x); \
|
||||
++h->size; \
|
||||
*ret = 2; \
|
||||
} else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
|
||||
return x; \
|
||||
} \
|
||||
void kh_del_##name(kh_##name##_t *h, int x) \
|
||||
{ \
|
||||
if (x != h->n_buckets && !ac_iseither(h->flags, x)) { \
|
||||
ac_set_isdel_true(h->flags, x); \
|
||||
--h->size; \
|
||||
} \
|
||||
}
|
||||
|
||||
/* --- BEGIN OF HASH FUNCTIONS --- */
|
||||
|
||||
#define kh_ptr_hash_func(key) (int)(long)(key)
|
||||
#define kh_ptr_hash_equal(a, b) ((a) == (b))
|
||||
#define kh_int_hash_func(key) (int)(key)
|
||||
#define kh_int_hash_equal(a, b) ((a) == (b))
|
||||
PIC_INLINE int kh_str_hash_func(const char *s) {
|
||||
int h = 0;
|
||||
while (*s) {
|
||||
h = (h << 5) - h + *s++;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
#define kh_str_cmp_func(a, b) (strcmp((a), (b)) == 0)
|
||||
|
||||
|
||||
/* --- END OF HASH FUNCTIONS --- */
|
||||
|
||||
#define khash_t(name) kh_##name##_t
|
||||
#define kh_init(name, h) kh_init_##name(h)
|
||||
#define kh_destroy(name, h) kh_destroy_##name(pic, h)
|
||||
#define kh_clear(name, h) kh_clear_##name(h)
|
||||
#define kh_resize(name, h, s) kh_resize_##name(pic, h, s)
|
||||
#define kh_put(name, h, k, r) kh_put_##name(pic, h, k, r)
|
||||
#define kh_get(name, h, k) kh_get_##name(pic, h, k)
|
||||
#define kh_del(name, h, k) kh_del_##name(h, k)
|
||||
|
||||
#define kh_exist(h, x) (!ac_iseither((h)->flags, (x)))
|
||||
#define kh_key(h, x) ((h)->keys[x])
|
||||
#define kh_val(h, x) ((h)->vals[x])
|
||||
#define kh_value(h, x) ((h)->vals[x])
|
||||
#define kh_begin(h) (0)
|
||||
#define kh_end(h) ((h)->n_buckets)
|
||||
#define kh_size(h) ((h)->size)
|
||||
#define kh_n_buckets(h) ((h)->n_buckets)
|
||||
|
||||
#endif /* AC_KHASH_H */
|
||||
@@ -1,220 +0,0 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_OBJECT_H
|
||||
#define PICRIN_OBJECT_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "picrin/private/file.h"
|
||||
#include "picrin/private/khash.h"
|
||||
#include "picrin/private/gc.h"
|
||||
|
||||
typedef struct identifier symbol;
|
||||
|
||||
KHASH_DECLARE(env, struct identifier *, symbol *)
|
||||
KHASH_DECLARE(dict, symbol *, pic_value)
|
||||
KHASH_DECLARE(weak, struct object *, pic_value)
|
||||
|
||||
struct object; /* defined in gc.c */
|
||||
|
||||
struct basic {
|
||||
OBJECT_HEADER
|
||||
};
|
||||
|
||||
struct identifier {
|
||||
OBJECT_HEADER
|
||||
union {
|
||||
struct string *str;
|
||||
struct identifier *id;
|
||||
} u;
|
||||
struct env *env;
|
||||
};
|
||||
|
||||
struct env {
|
||||
OBJECT_HEADER
|
||||
khash_t(env) map;
|
||||
struct env *up;
|
||||
struct string *lib;
|
||||
};
|
||||
|
||||
struct pair {
|
||||
OBJECT_HEADER
|
||||
pic_value car;
|
||||
pic_value cdr;
|
||||
};
|
||||
|
||||
struct blob {
|
||||
OBJECT_HEADER
|
||||
unsigned char *data;
|
||||
int len;
|
||||
};
|
||||
|
||||
struct string {
|
||||
OBJECT_HEADER
|
||||
struct rope *rope;
|
||||
};
|
||||
|
||||
struct dict {
|
||||
OBJECT_HEADER
|
||||
khash_t(dict) hash;
|
||||
};
|
||||
|
||||
struct weak {
|
||||
OBJECT_HEADER
|
||||
khash_t(weak) hash;
|
||||
struct weak *prev; /* for GC */
|
||||
};
|
||||
|
||||
struct vector {
|
||||
OBJECT_HEADER
|
||||
pic_value *data;
|
||||
int len;
|
||||
};
|
||||
|
||||
struct data {
|
||||
OBJECT_HEADER
|
||||
const pic_data_type *type;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct context {
|
||||
OBJECT_HEADER
|
||||
pic_value *regs;
|
||||
int regc;
|
||||
struct context *up;
|
||||
pic_value storage[1];
|
||||
};
|
||||
|
||||
struct proc {
|
||||
OBJECT_HEADER
|
||||
union {
|
||||
struct {
|
||||
pic_func_t func;
|
||||
int localc;
|
||||
} f;
|
||||
struct {
|
||||
struct irep *irep;
|
||||
struct context *cxt;
|
||||
} i;
|
||||
} u;
|
||||
pic_value locals[1];
|
||||
};
|
||||
|
||||
struct record {
|
||||
OBJECT_HEADER
|
||||
pic_value type;
|
||||
pic_value datum;
|
||||
};
|
||||
|
||||
struct error {
|
||||
OBJECT_HEADER
|
||||
symbol *type;
|
||||
struct string *msg;
|
||||
pic_value irrs;
|
||||
struct string *stack;
|
||||
};
|
||||
|
||||
struct port {
|
||||
OBJECT_HEADER
|
||||
struct file file;
|
||||
};
|
||||
|
||||
struct checkpoint {
|
||||
OBJECT_HEADER
|
||||
struct proc *in;
|
||||
struct proc *out;
|
||||
int depth;
|
||||
struct checkpoint *prev;
|
||||
};
|
||||
|
||||
struct object *pic_obj_ptr(pic_value);
|
||||
|
||||
#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (struct identifier *)pic_obj_ptr(o))
|
||||
#define pic_sym_ptr(pic, o) (assert(pic_sym_p(pic, o)), (symbol *)pic_obj_ptr(o))
|
||||
#define pic_str_ptr(pic, o) (assert(pic_str_p(pic, o)), (struct string *)pic_obj_ptr(o))
|
||||
#define pic_blob_ptr(pic, o) (assert(pic_blob_p(pic, o)), (struct blob *)pic_obj_ptr(o))
|
||||
#define pic_pair_ptr(pic, o) (assert(pic_pair_p(pic, o)), (struct pair *)pic_obj_ptr(o))
|
||||
#define pic_vec_ptr(pic, o) (assert(pic_vec_p(pic, o)), (struct vector *)pic_obj_ptr(o))
|
||||
#define pic_dict_ptr(pic, o) (assert(pic_dict_p(pic, o)), (struct dict *)pic_obj_ptr(o))
|
||||
#define pic_weak_ptr(pic, o) (assert(pic_weak_p(pic, o)), (struct weak *)pic_obj_ptr(o))
|
||||
#define pic_data_ptr(pic, o) (assert(pic_data_p(pic, o, NULL)), (struct data *)pic_obj_ptr(o))
|
||||
#define pic_proc_ptr(pic, o) (assert(pic_proc_p(pic, o)), (struct proc *)pic_obj_ptr(o))
|
||||
#define pic_env_ptr(pic, o) (assert(pic_env_p(pic, o)), (struct env *)pic_obj_ptr(o))
|
||||
#define pic_port_ptr(pic, o) (assert(pic_port_p(pic, o)), (struct port *)pic_obj_ptr(o))
|
||||
#define pic_error_ptr(pic, o) (assert(pic_error_p(pic, o)), (struct error *)pic_obj_ptr(o))
|
||||
#define pic_rec_ptr(pic, o) (assert(pic_rec_p(pic, o)), (struct record *)pic_obj_ptr(o))
|
||||
|
||||
#define pic_obj_p(pic,v) (pic_type(pic,v) > PIC_IVAL_END)
|
||||
#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV)
|
||||
#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR)
|
||||
#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD)
|
||||
|
||||
pic_value pic_obj_value(void *ptr);
|
||||
struct object *pic_obj_alloc(pic_state *, size_t, int type);
|
||||
|
||||
#define TYPENAME_int "integer"
|
||||
#define TYPENAME_blob "bytevector"
|
||||
#define TYPENAME_char "character"
|
||||
#define TYPENAME_sym "symbol"
|
||||
#define TYPENAME_error "error"
|
||||
#define TYPENAME_proc "procedure"
|
||||
#define TYPENAME_str "string"
|
||||
#define TYPENAME_id "identifier"
|
||||
#define TYPENAME_env "environment"
|
||||
#define TYPENAME_vec "vector"
|
||||
|
||||
#define TYPE_CHECK(pic, v, type) do { \
|
||||
if (! pic_##type##_p(pic, v)) \
|
||||
pic_error(pic, TYPENAME_##type " required", 1, v); \
|
||||
} while (0)
|
||||
|
||||
#define VALID_INDEX(pic, len, i) do { \
|
||||
if (i < 0 || len <= i) pic_error(pic, "index out of range", 1, pic_int_value(pic, i)); \
|
||||
} while (0)
|
||||
#define VALID_RANGE(pic, len, s, e) do { \
|
||||
if (s < 0 || len < s) pic_error(pic, "invalid start index", 1, pic_int_value(pic, s)); \
|
||||
if (e < s || len < e) pic_error(pic, "invalid end index", 1, pic_int_value(pic, e)); \
|
||||
} while (0)
|
||||
#define VALID_ATRANGE(pic, tolen, at, fromlen, s, e) do { \
|
||||
VALID_INDEX(pic, tolen, at); \
|
||||
VALID_RANGE(pic, fromlen, s, e); \
|
||||
if (tolen - at < e - s) pic_error(pic, "invalid range", 0); \
|
||||
} while (0)
|
||||
|
||||
pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env);
|
||||
pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *);
|
||||
pic_value pic_make_proc_irep(pic_state *, struct irep *, struct context *);
|
||||
pic_value pic_make_env(pic_state *, pic_value env);
|
||||
pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum);
|
||||
|
||||
pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env);
|
||||
void pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env);
|
||||
pic_value pic_find_identifier(pic_state *, pic_value id, pic_value env);
|
||||
pic_value pic_id_name(pic_state *, pic_value id);
|
||||
|
||||
struct rope *pic_rope_incref(struct rope *);
|
||||
void pic_rope_decref(pic_state *, struct rope *);
|
||||
|
||||
#define pic_func_p(pic, proc) (pic_type(pic, proc) == PIC_TYPE_FUNC)
|
||||
#define pic_irep_p(pic, proc) (pic_type(pic, proc) == PIC_TYPE_IREP)
|
||||
|
||||
struct cont *pic_alloca_cont(pic_state *);
|
||||
pic_value pic_make_cont(pic_state *, struct cont *);
|
||||
void pic_save_point(pic_state *, struct cont *, PIC_JMPBUF *);
|
||||
void pic_exit_point(pic_state *);
|
||||
void pic_wind(pic_state *, struct checkpoint *, struct checkpoint *);
|
||||
pic_value pic_dynamic_wind(pic_state *, pic_value in, pic_value thunk, pic_value out);
|
||||
|
||||
pic_value pic_dynamic_bind(pic_state *, pic_value var, pic_value val, pic_value thunk);
|
||||
|
||||
pic_value pic_library_environment(pic_state *, const char *);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,76 +0,0 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_STATE_H
|
||||
#define PICRIN_STATE_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "picrin/private/khash.h"
|
||||
#include "picrin/private/vm.h"
|
||||
#include "picrin/private/gc.h"
|
||||
|
||||
struct lib {
|
||||
struct string *name;
|
||||
struct env *env;
|
||||
struct dict *exports;
|
||||
};
|
||||
|
||||
struct callinfo {
|
||||
int argc, retc;
|
||||
const struct code *ip;
|
||||
pic_value *fp;
|
||||
struct irep *irep;
|
||||
struct context *cxt;
|
||||
int regc;
|
||||
pic_value *regs;
|
||||
struct context *up;
|
||||
};
|
||||
|
||||
KHASH_DECLARE(oblist, struct string *, struct identifier *)
|
||||
KHASH_DECLARE(ltable, const char *, struct lib)
|
||||
|
||||
struct pic_state {
|
||||
pic_allocf allocf;
|
||||
void *userdata;
|
||||
|
||||
struct checkpoint *cp;
|
||||
struct cont *cc;
|
||||
|
||||
pic_value *sp;
|
||||
pic_value *stbase, *stend;
|
||||
|
||||
struct callinfo *ci;
|
||||
struct callinfo *cibase, *ciend;
|
||||
|
||||
const struct code *ip;
|
||||
|
||||
const char *lib;
|
||||
|
||||
pic_value features;
|
||||
|
||||
khash_t(oblist) oblist; /* string to symbol */
|
||||
int ucnt;
|
||||
pic_value globals; /* weak */
|
||||
pic_value macros; /* weak */
|
||||
khash_t(ltable) ltable;
|
||||
struct list_head ireps;
|
||||
|
||||
bool gc_enable;
|
||||
struct heap *heap;
|
||||
struct object **arena;
|
||||
size_t arena_size, arena_idx;
|
||||
|
||||
pic_value err;
|
||||
|
||||
pic_panicf panicf;
|
||||
};
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,85 +0,0 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_VM_H
|
||||
#define PICRIN_VM_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum {
|
||||
OP_NOP,
|
||||
OP_POP,
|
||||
OP_PUSHUNDEF,
|
||||
OP_PUSHNIL,
|
||||
OP_PUSHTRUE,
|
||||
OP_PUSHFALSE,
|
||||
OP_PUSHINT,
|
||||
OP_PUSHFLOAT,
|
||||
OP_PUSHCHAR,
|
||||
OP_PUSHEOF,
|
||||
OP_PUSHCONST,
|
||||
OP_GREF,
|
||||
OP_GSET,
|
||||
OP_LREF,
|
||||
OP_LSET,
|
||||
OP_CREF,
|
||||
OP_CSET,
|
||||
OP_JMP,
|
||||
OP_JMPIF,
|
||||
OP_NOT,
|
||||
OP_CALL,
|
||||
OP_TAILCALL,
|
||||
OP_RET,
|
||||
OP_LAMBDA,
|
||||
OP_CONS,
|
||||
OP_CAR,
|
||||
OP_CDR,
|
||||
OP_NILP,
|
||||
OP_SYMBOLP,
|
||||
OP_PAIRP,
|
||||
OP_ADD,
|
||||
OP_SUB,
|
||||
OP_MUL,
|
||||
OP_DIV,
|
||||
OP_EQ,
|
||||
OP_LT,
|
||||
OP_LE,
|
||||
OP_GT,
|
||||
OP_GE,
|
||||
OP_STOP
|
||||
};
|
||||
|
||||
struct code {
|
||||
int insn;
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
struct list_head {
|
||||
struct list_head *prev, *next;
|
||||
};
|
||||
|
||||
struct irep {
|
||||
struct list_head list;
|
||||
unsigned refc;
|
||||
int argc, localc, capturec;
|
||||
bool varg;
|
||||
struct code *code;
|
||||
struct irep **irep;
|
||||
int *ints;
|
||||
double *nums;
|
||||
struct object **pool;
|
||||
size_t ncode, nirep, nints, nnums, npool;
|
||||
};
|
||||
|
||||
void pic_irep_incref(pic_state *, struct irep *);
|
||||
void pic_irep_decref(pic_state *, struct irep *);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user