extlib/benz -> lib/
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
/** enable libc? */
|
||||
/* #define PIC_USE_LIBC 1 */
|
||||
|
||||
/** enable stdio */
|
||||
/* #define PIC_USE_STDIO 1 */
|
||||
|
||||
/** enable specific features */
|
||||
/* #define PIC_USE_WRITE 1 */
|
||||
|
||||
/** essential external functions */
|
||||
/* #define PIC_JMPBUF jmp_buf */
|
||||
/* #define PIC_SETJMP(pic, buf) setjmp(buf) */
|
||||
/* #define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val)) */
|
||||
/* #define PIC_ABORT(pic) abort() */
|
||||
|
||||
/** I/O configuration */
|
||||
/* #define PIC_BUFSIZ 1024 */
|
||||
@@ -0,0 +1,299 @@
|
||||
/**
|
||||
* Copyright (c) 2013-2016 Picrin developers.
|
||||
*
|
||||
* 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_H
|
||||
#define PICRIN_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "picrin/setup.h"
|
||||
|
||||
typedef struct pic_state pic_state;
|
||||
|
||||
#if PIC_NAN_BOXING
|
||||
# include <stdint.h>
|
||||
typedef uint64_t pic_value;
|
||||
#else
|
||||
typedef struct {
|
||||
unsigned char type;
|
||||
union {
|
||||
void *data;
|
||||
double f;
|
||||
int i;
|
||||
char c;
|
||||
} u;
|
||||
} pic_value;
|
||||
#endif
|
||||
|
||||
typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n);
|
||||
|
||||
pic_state *pic_open(pic_allocf f, void *userdata);
|
||||
void pic_close(pic_state *);
|
||||
|
||||
void pic_add_feature(pic_state *, const char *feature);
|
||||
|
||||
void *pic_malloc(pic_state *, size_t);
|
||||
void *pic_realloc(pic_state *, void *, size_t);
|
||||
void *pic_calloc(pic_state *, size_t, size_t);
|
||||
void pic_free(pic_state *, void *);
|
||||
|
||||
typedef pic_value (*pic_func_t)(pic_state *);
|
||||
|
||||
void *pic_alloca(pic_state *, size_t);
|
||||
size_t pic_enter(pic_state *);
|
||||
void pic_leave(pic_state *, size_t);
|
||||
pic_value pic_protect(pic_state *, pic_value);
|
||||
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_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);
|
||||
pic_value pic_closure_ref(pic_state *, int i);
|
||||
void pic_closure_set(pic_state *, int i, pic_value v);
|
||||
pic_value pic_funcall(pic_state *, const char *lib, const char *name, int n, ...);
|
||||
pic_value pic_make_var(pic_state *, pic_value init, pic_value conv);
|
||||
|
||||
pic_value pic_return(pic_state *, int n, ...);
|
||||
pic_value pic_vreturn(pic_state *, int n, va_list);
|
||||
pic_value pic_valuesk(pic_state *, int n, pic_value *retv);
|
||||
int pic_receive(pic_state *, int n, pic_value *retv);
|
||||
|
||||
void pic_make_library(pic_state *, const char *lib);
|
||||
void pic_in_library(pic_state *, const char *lib);
|
||||
bool pic_find_library(pic_state *, const char *lib);
|
||||
const char *pic_current_library(pic_state *);
|
||||
void pic_import(pic_state *, const char *lib);
|
||||
void pic_export(pic_state *, pic_value sym);
|
||||
|
||||
typedef void (*pic_panicf)(pic_state *, const char *msg);
|
||||
|
||||
pic_panicf pic_atpanic(pic_state *, pic_panicf f);
|
||||
PIC_NORETURN void pic_panic(pic_state *, const char *msg);
|
||||
PIC_NORETURN void pic_error(pic_state *, const char *msg, int n, ...);
|
||||
PIC_NORETURN void pic_raise(pic_state *, pic_value v);
|
||||
pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs);
|
||||
|
||||
pic_value pic_lambda(pic_state *, pic_func_t f, int n, ...);
|
||||
pic_value pic_vlambda(pic_state *, pic_func_t f, int n, va_list);
|
||||
pic_value pic_call(pic_state *, pic_value proc, int, ...);
|
||||
pic_value pic_vcall(pic_state *, pic_value proc, int, va_list);
|
||||
pic_value pic_apply(pic_state *, pic_value proc, int n, pic_value *argv);
|
||||
pic_value pic_applyk(pic_state *, pic_value proc, int n, pic_value *argv);
|
||||
|
||||
int pic_int(pic_state *, pic_value i);
|
||||
double pic_float(pic_state *, pic_value f);
|
||||
char pic_char(pic_state *, pic_value c);
|
||||
#define pic_bool(pic,b) (! pic_false_p(pic, b))
|
||||
const char *pic_str(pic_state *, pic_value str);
|
||||
#define pic_sym(pic,s) (pic_str(pic, pic_sym_name(pic, (s))))
|
||||
unsigned char *pic_blob(pic_state *, pic_value blob, int *len);
|
||||
void *pic_data(pic_state *, pic_value data);
|
||||
|
||||
typedef struct {
|
||||
const char *type_name;
|
||||
void (*dtor)(pic_state *, void *);
|
||||
void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value));
|
||||
} pic_data_type;
|
||||
|
||||
pic_value pic_invalid_value(pic_state *);
|
||||
pic_value pic_undef_value(pic_state *);
|
||||
pic_value pic_int_value(pic_state *, int);
|
||||
pic_value pic_float_value(pic_state *, double);
|
||||
pic_value pic_char_value(pic_state *, char);
|
||||
pic_value pic_true_value(pic_state *);
|
||||
pic_value pic_false_value(pic_state *);
|
||||
#define pic_bool_value(pic, b) ((b) ? pic_true_value(pic) : pic_false_value(pic))
|
||||
pic_value pic_eof_object(pic_state *);
|
||||
pic_value pic_str_value(pic_state *, const char *str, int len);
|
||||
#define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr))
|
||||
#define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1))
|
||||
pic_value pic_strf_value(pic_state *, const char *fmt, ...);
|
||||
pic_value pic_vstrf_value(pic_state *, const char *fmt, va_list ap);
|
||||
pic_value pic_blob_value(pic_state *, const unsigned char *buf, int len);
|
||||
pic_value pic_data_value(pic_state *, void *ptr, const pic_data_type *type);
|
||||
|
||||
enum {
|
||||
PIC_TYPE_INVALID = 1,
|
||||
PIC_TYPE_FLOAT = 2,
|
||||
PIC_TYPE_INT = 3,
|
||||
PIC_TYPE_CHAR = 4,
|
||||
PIC_TYPE_EOF = 5,
|
||||
PIC_TYPE_UNDEF = 6,
|
||||
PIC_TYPE_TRUE = 8,
|
||||
PIC_TYPE_NIL = 7,
|
||||
PIC_TYPE_FALSE = 9,
|
||||
PIC_IVAL_END = 10,
|
||||
/* -------------------- */
|
||||
PIC_TYPE_STRING = 16,
|
||||
PIC_TYPE_VECTOR = 17,
|
||||
PIC_TYPE_BLOB = 18,
|
||||
PIC_TYPE_PORT = 20,
|
||||
PIC_TYPE_ERROR = 21,
|
||||
PIC_TYPE_ID = 22,
|
||||
PIC_TYPE_ENV = 23,
|
||||
PIC_TYPE_DATA = 24,
|
||||
PIC_TYPE_DICT = 25,
|
||||
PIC_TYPE_WEAK = 26,
|
||||
PIC_TYPE_RECORD = 27,
|
||||
PIC_TYPE_SYMBOL = 28,
|
||||
PIC_TYPE_PAIR = 29,
|
||||
PIC_TYPE_CXT = 30,
|
||||
PIC_TYPE_CP = 31,
|
||||
PIC_TYPE_FUNC = 32,
|
||||
PIC_TYPE_IREP = 33
|
||||
};
|
||||
|
||||
#define pic_invalid_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INVALID)
|
||||
#define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF)
|
||||
#define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT)
|
||||
#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_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)
|
||||
#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING)
|
||||
#define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TYPE_BLOB)
|
||||
#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FUNC || pic_type(pic, v) == PIC_TYPE_IREP)
|
||||
#define pic_nil_p(pic,v) (pic_type(pic,v) == PIC_TYPE_NIL)
|
||||
#define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PAIR)
|
||||
#define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TYPE_VECTOR)
|
||||
#define pic_dict_p(pic,v) (pic_type(pic,v) == PIC_TYPE_DICT)
|
||||
#define pic_weak_p(pic,v) (pic_type(pic,v) == PIC_TYPE_WEAK)
|
||||
#define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT)
|
||||
#define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL)
|
||||
bool pic_data_p(pic_state *, pic_value, const pic_data_type *);
|
||||
|
||||
int pic_type(pic_state *, pic_value);
|
||||
const char *pic_typename(pic_state *, int);
|
||||
|
||||
bool pic_eq_p(pic_state *, pic_value, pic_value);
|
||||
bool pic_eqv_p(pic_state *, pic_value, pic_value);
|
||||
bool pic_equal_p(pic_state *, pic_value, pic_value);
|
||||
|
||||
/* pair */
|
||||
pic_value pic_cons(pic_state *, pic_value car, pic_value cdr);
|
||||
pic_value pic_car(pic_state *, pic_value pair);
|
||||
pic_value pic_cdr(pic_state *, pic_value pair);
|
||||
void pic_set_car(pic_state *, pic_value pair, pic_value car);
|
||||
void pic_set_cdr(pic_state *, pic_value pair, pic_value cdr);
|
||||
pic_value pic_caar(pic_state *, pic_value);
|
||||
pic_value pic_cadr(pic_state *, pic_value);
|
||||
pic_value pic_cdar(pic_state *, pic_value);
|
||||
pic_value pic_cddr(pic_state *, pic_value);
|
||||
|
||||
/* list */
|
||||
pic_value pic_nil_value(pic_state *);
|
||||
bool pic_list_p(pic_state *, pic_value);
|
||||
pic_value pic_make_list(pic_state *, int n, pic_value *argv);
|
||||
pic_value pic_list(pic_state *, int n, ...);
|
||||
pic_value pic_vlist(pic_state *, int n, va_list);
|
||||
pic_value pic_list_ref(pic_state *, pic_value list, int i);
|
||||
void pic_list_set(pic_state *, pic_value list, int i, pic_value v);
|
||||
pic_value pic_list_tail(pic_state *, pic_value list, int i);
|
||||
int pic_length(pic_state *, pic_value list);
|
||||
pic_value pic_reverse(pic_state *, pic_value list);
|
||||
pic_value pic_append(pic_state *, pic_value xs, pic_value ys);
|
||||
|
||||
/* vector */
|
||||
pic_value pic_make_vec(pic_state *, int n, pic_value *argv);
|
||||
pic_value pic_vec_ref(pic_state *, pic_value vec, int i);
|
||||
void pic_vec_set(pic_state *, pic_value vec, int i, pic_value v);
|
||||
int pic_vec_len(pic_state *, pic_value vec);
|
||||
|
||||
/* dictionary */
|
||||
pic_value pic_make_dict(pic_state *);
|
||||
pic_value pic_dict_ref(pic_state *, pic_value dict, pic_value key);
|
||||
void pic_dict_set(pic_state *, pic_value dict, pic_value key, pic_value);
|
||||
void pic_dict_del(pic_state *, pic_value dict, pic_value key);
|
||||
bool pic_dict_has(pic_state *, pic_value dict, pic_value key);
|
||||
int pic_dict_size(pic_state *, pic_value dict);
|
||||
bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_value *key, pic_value *val);
|
||||
|
||||
/* ephemeron */
|
||||
pic_value pic_make_weak(pic_state *);
|
||||
pic_value pic_weak_ref(pic_state *, pic_value weak, pic_value key);
|
||||
void pic_weak_set(pic_state *, pic_value weak, pic_value key, pic_value val);
|
||||
void pic_weak_del(pic_state *, pic_value weak, pic_value key);
|
||||
bool pic_weak_has(pic_state *, pic_value weak, pic_value key);
|
||||
|
||||
/* symbol */
|
||||
pic_value pic_intern(pic_state *, pic_value str);
|
||||
#define pic_intern_str(pic,s,i) pic_intern(pic, pic_str_value(pic, (s), (i)))
|
||||
#define pic_intern_cstr(pic,s) pic_intern(pic, pic_cstr_value(pic, (s)))
|
||||
#define pic_intern_lit(pic,lit) pic_intern(pic, pic_lit_value(pic, lit))
|
||||
pic_value pic_sym_name(pic_state *, pic_value sym);
|
||||
|
||||
/* string */
|
||||
int pic_str_len(pic_state *, pic_value str);
|
||||
char pic_str_ref(pic_state *, pic_value str, int i);
|
||||
pic_value pic_str_cat(pic_state *, pic_value str1, pic_value str2);
|
||||
pic_value pic_str_sub(pic_state *, pic_value str, int i, int j);
|
||||
int pic_str_cmp(pic_state *, pic_value str1, pic_value str2);
|
||||
int pic_str_hash(pic_state *, pic_value str);
|
||||
|
||||
|
||||
|
||||
/* External I/O */
|
||||
|
||||
#define PIC_SEEK_CUR 0
|
||||
#define PIC_SEEK_END 1
|
||||
#define PIC_SEEK_SET 2
|
||||
|
||||
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);
|
||||
long pic_fseek(pic_state *, pic_value port, long offset, int whence);
|
||||
int pic_fclose(pic_state *, pic_value port);
|
||||
|
||||
void pic_clearerr(pic_state *, pic_value port);
|
||||
int pic_feof(pic_state *, pic_value port);
|
||||
int pic_ferror(pic_state *, pic_value port);
|
||||
|
||||
int pic_fputc(pic_state *, int c, pic_value port);
|
||||
int pic_fgetc(pic_state *, pic_value port);
|
||||
int pic_fputs(pic_state *, const char *s, pic_value port);
|
||||
char *pic_fgets(pic_state *, char *s, int size, pic_value port);
|
||||
int pic_ungetc(pic_state *, int c, pic_value port);
|
||||
int pic_fflush(pic_state *, pic_value port);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_EXTRA_H
|
||||
#define PICRIN_EXTRA_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#if PIC_USE_LIBC
|
||||
void *pic_default_allocf(void *, void *, size_t);
|
||||
#endif
|
||||
|
||||
pic_value pic_read(pic_state *, pic_value port);
|
||||
pic_value pic_read_cstr(pic_state *, const char *);
|
||||
|
||||
pic_value pic_expand(pic_state *, pic_value program, pic_value env);
|
||||
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)) { \
|
||||
pic_make_library(pic, lib); \
|
||||
} \
|
||||
pic_in_library(pic, lib); \
|
||||
} while (0)
|
||||
|
||||
#define pic_try pic_try_(PIC_GENSYM(cont), PIC_GENSYM(jmp))
|
||||
#define pic_try_(cont, jmp) \
|
||||
do { \
|
||||
extern pic_value pic_start_try(pic_state *, PIC_JMPBUF *); \
|
||||
extern void pic_end_try(pic_state *, pic_value); \
|
||||
extern pic_value pic_err(pic_state *); \
|
||||
PIC_JMPBUF jmp; \
|
||||
if (PIC_SETJMP(pic, jmp) == 0) { \
|
||||
pic_value pic_try_cookie_ = pic_start_try(pic, &jmp);
|
||||
#define pic_catch(e) pic_catch_(e, PIC_GENSYM(label))
|
||||
#define pic_catch_(e, label) \
|
||||
pic_end_try(pic, pic_try_cookie_); \
|
||||
} else { \
|
||||
e = pic_err(pic); \
|
||||
goto label; \
|
||||
} \
|
||||
} while (0); \
|
||||
if (0) \
|
||||
label:
|
||||
|
||||
/* 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
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,234 @@
|
||||
/* 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 */
|
||||
@@ -0,0 +1,220 @@
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 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
|
||||
@@ -0,0 +1,472 @@
|
||||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#include "picconf.h"
|
||||
|
||||
#ifndef PIC_USE_LIBC
|
||||
# define PIC_USE_LIBC 1
|
||||
#endif
|
||||
|
||||
#ifndef PIC_USE_STDIO
|
||||
# define PIC_USE_STDIO 1
|
||||
#endif
|
||||
|
||||
#ifndef PIC_USE_WRITE
|
||||
# define PIC_USE_WRITE 1
|
||||
#endif
|
||||
|
||||
#ifndef PIC_JMPBUF
|
||||
# include <setjmp.h>
|
||||
# define PIC_JMPBUF jmp_buf
|
||||
#endif
|
||||
|
||||
#ifndef PIC_SETJMP
|
||||
# include <setjmp.h>
|
||||
# define PIC_SETJMP(pic, buf) setjmp(buf)
|
||||
#endif
|
||||
|
||||
#ifndef PIC_LONGJMP
|
||||
# include <setjmp.h>
|
||||
# define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val))
|
||||
#endif
|
||||
|
||||
#ifndef PIC_ABORT
|
||||
void abort(void);
|
||||
# define PIC_ABORT(pic) abort()
|
||||
#endif
|
||||
|
||||
#ifndef PIC_BUFSIZ
|
||||
# define PIC_BUFSIZ 1024
|
||||
#endif
|
||||
|
||||
#ifndef PIC_ARENA_SIZE
|
||||
# define PIC_ARENA_SIZE (8 * 1024)
|
||||
#endif
|
||||
|
||||
#ifndef PIC_HEAP_PAGE_SIZE
|
||||
# define PIC_HEAP_PAGE_SIZE (4 * 1024 * 1024)
|
||||
#endif
|
||||
|
||||
#ifndef PIC_PAGE_REQUEST_THRESHOLD
|
||||
# define PIC_PAGE_REQUEST_THRESHOLD(total) ((total) * 77 / 100)
|
||||
#endif
|
||||
|
||||
#ifndef PIC_STACK_SIZE
|
||||
# define PIC_STACK_SIZE 2048
|
||||
#endif
|
||||
|
||||
#ifndef PIC_RESCUE_SIZE
|
||||
# define PIC_RESCUE_SIZE 30
|
||||
#endif
|
||||
|
||||
#ifndef PIC_SYM_POOL_SIZE
|
||||
# define PIC_SYM_POOL_SIZE (2 * 1024)
|
||||
#endif
|
||||
|
||||
#ifndef PIC_IREP_SIZE
|
||||
# define PIC_IREP_SIZE 8
|
||||
#endif
|
||||
|
||||
#ifndef PIC_POOL_SIZE
|
||||
# define PIC_POOL_SIZE 8
|
||||
#endif
|
||||
|
||||
#ifndef PIC_SYMS_SIZE
|
||||
# define PIC_SYMS_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef PIC_ISEQ_SIZE
|
||||
# define PIC_ISEQ_SIZE 1024
|
||||
#endif
|
||||
|
||||
/* check compatibility */
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
# include <stdbool.h>
|
||||
#else
|
||||
# define bool char
|
||||
# define true 1
|
||||
# define false 0
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
# include <stddef.h>
|
||||
#elif ! defined(offsetof)
|
||||
# define offsetof(s,m) ((size_t)(&(((s *)0)->m) - 0))
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
# include <stdint.h>
|
||||
#else
|
||||
# if INT_MAX > 2147483640L /* imported from luaconf.h */
|
||||
typedef int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
# else
|
||||
typedef long int32_t;
|
||||
typedef unsigned long uint32_t;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 201112L
|
||||
# include <stdnoreturn.h>
|
||||
# define PIC_NORETURN noreturn
|
||||
#elif __GNUC__ || __clang__
|
||||
# define PIC_NORETURN __attribute__((noreturn))
|
||||
#else
|
||||
# define PIC_NORETURN
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
# define PIC_INLINE static inline
|
||||
#elif __GNUC__ || __clang__
|
||||
# define PIC_INLINE static __inline__
|
||||
#else
|
||||
# define PIC_INLINE static
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
# define PIC_UNUSED(v)
|
||||
#elif __GNUC__ || __clang__
|
||||
# define PIC_UNUSED(v) v __attribute__((unused))
|
||||
#else
|
||||
# define PIC_UNUSED(v) v
|
||||
#endif
|
||||
|
||||
#define PIC_GENSYM2_(x,y) PIC_G##x##_##y##_
|
||||
#define PIC_GENSYM1_(x,y) PIC_GENSYM2_(x,y)
|
||||
#if defined(__COUNTER__)
|
||||
# define PIC_GENSYM(x) PIC_GENSYM1_(__COUNTER__,x)
|
||||
#else
|
||||
# define PIC_GENSYM(x) PIC_GENSYM1_(__LINE__,x)
|
||||
#endif
|
||||
|
||||
#if __GNUC__
|
||||
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
||||
#endif
|
||||
#if GCC_VERSION >= 40500 || __clang__
|
||||
# define PIC_UNREACHABLE() (__builtin_unreachable())
|
||||
#else
|
||||
# define PIC_UNREACHABLE() (assert(false))
|
||||
#endif
|
||||
#if __GNUC__
|
||||
# undef GCC_VERSION
|
||||
#endif
|
||||
|
||||
#define PIC_SWAP(type,a,b) PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b)
|
||||
#define PIC_SWAP_HELPER_(type,tmp,a,b) \
|
||||
do { \
|
||||
type tmp = (a); \
|
||||
(a) = (b); \
|
||||
(b) = tmp; \
|
||||
} while (0)
|
||||
|
||||
|
||||
#if PIC_USE_LIBC
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#else
|
||||
|
||||
# define assert(v) (void)0
|
||||
|
||||
PIC_INLINE int
|
||||
isspace(int c)
|
||||
{
|
||||
return c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f' || c == '\n';
|
||||
}
|
||||
|
||||
PIC_INLINE int
|
||||
tolower(int c)
|
||||
{
|
||||
return ('A' <= c && c <= 'Z') ? c - 'A' + 'a' : c;
|
||||
}
|
||||
|
||||
PIC_INLINE int
|
||||
isdigit(int c)
|
||||
{
|
||||
return '0' <= c && c <= '9';
|
||||
}
|
||||
|
||||
PIC_INLINE char *
|
||||
strchr(const char *s, int c)
|
||||
{
|
||||
do {
|
||||
if (*s == c)
|
||||
return (char *)s;
|
||||
} while (*s++ != '\0');
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PIC_INLINE size_t
|
||||
strlen(const char *s)
|
||||
{
|
||||
size_t l = 0;
|
||||
|
||||
while (*s++) {
|
||||
l++;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
PIC_INLINE int
|
||||
strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
while (*s1 && *s1 == *s2) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return (unsigned)*s1 - (unsigned)*s2;
|
||||
}
|
||||
|
||||
PIC_INLINE long
|
||||
strtol(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
long l = 0;
|
||||
char c;
|
||||
int n;
|
||||
|
||||
while (1) {
|
||||
c = *nptr;
|
||||
if ('0' <= c && c <= '9')
|
||||
n = c - '0';
|
||||
else if ('a' <= c && c <= 'z')
|
||||
n = c - 'a' + 10;
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
n = c - 'A' + 10;
|
||||
else
|
||||
goto exit;
|
||||
|
||||
if (base <= n)
|
||||
goto exit;
|
||||
|
||||
l = l * base + n;
|
||||
nptr++;
|
||||
}
|
||||
exit:
|
||||
if (endptr)
|
||||
*endptr = (char *)nptr;
|
||||
return l;
|
||||
}
|
||||
|
||||
PIC_INLINE void *
|
||||
memset(void *s, int n, size_t c)
|
||||
{
|
||||
char *p = s;
|
||||
|
||||
while (c-- > 0) {
|
||||
*p++ = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
PIC_INLINE void *
|
||||
memcpy(void *dst, const void *src, size_t n)
|
||||
{
|
||||
const char *s = src;
|
||||
char *d = dst;
|
||||
|
||||
while (n-- > 0) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
PIC_INLINE void *
|
||||
memmove(void *dst, const void *src, size_t n)
|
||||
{
|
||||
const char *s = src;
|
||||
char *d = dst;
|
||||
|
||||
if (d <= s || d >= s + n) {
|
||||
memcpy(dst, src, n);
|
||||
} else {
|
||||
s += n;
|
||||
d += n;
|
||||
while (n-- > 0) {
|
||||
*--d = *--s;
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
PIC_INLINE int
|
||||
memcmp(const void *b1, const void *b2, size_t n)
|
||||
{
|
||||
const char *s1 = b1, *s2 = b2;
|
||||
|
||||
while (*s1 == *s2 && n-- > 0) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return (unsigned)*s1 - (unsigned)*s2;
|
||||
}
|
||||
|
||||
PIC_INLINE char *
|
||||
strcpy(char *dst, const char *src)
|
||||
{
|
||||
char *d = dst;
|
||||
|
||||
while ((*dst++ = *src++) != 0);
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
PIC_INLINE double
|
||||
atof(const char *nptr)
|
||||
{
|
||||
int c;
|
||||
double f, g, h;
|
||||
int exp, s, i, e;
|
||||
unsigned u;
|
||||
|
||||
/* note that picrin_read always assures that *nptr is a digit, never a '+' or '-' */
|
||||
/* in other words, the result of atof will always be positive */
|
||||
|
||||
/* mantissa */
|
||||
/* pre '.' */
|
||||
u = *nptr++ - '0';
|
||||
while (isdigit(c = *nptr)) {
|
||||
u = u * 10 + (*nptr++ - '0');
|
||||
}
|
||||
if (c == '.') {
|
||||
nptr++;
|
||||
/* after '.' */
|
||||
g = 0, e = 0;
|
||||
while (isdigit(c = *nptr)) {
|
||||
g = g * 10 + (*nptr++ - '0');
|
||||
e++;
|
||||
}
|
||||
h = 1.0;
|
||||
while (e-- > 0) {
|
||||
h /= 10;
|
||||
}
|
||||
f = u + g * h;
|
||||
}
|
||||
else {
|
||||
f = u;
|
||||
}
|
||||
/* suffix, i.e., exponent */
|
||||
s = 0;
|
||||
exp = 0;
|
||||
c = *nptr;
|
||||
|
||||
if (c == 'e' && c == 'E') {
|
||||
nptr++;
|
||||
switch ((c = *nptr++)) {
|
||||
case '-':
|
||||
s = 1;
|
||||
case '+':
|
||||
c = *nptr++;
|
||||
default:
|
||||
exp = c - '0';
|
||||
while (isdigit(c = *nptr)) {
|
||||
exp = exp * 10 + (*nptr++ - '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
e = 10;
|
||||
for (i = 0; exp; ++i) {
|
||||
if ((exp & 1) != 0) {
|
||||
f = s ? f / e : (f * e);
|
||||
}
|
||||
e *= e;
|
||||
exp >>= 1;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if PIC_USE_STDIO
|
||||
# include <stdio.h>
|
||||
|
||||
PIC_INLINE void
|
||||
pic_dtoa(double dval, char *buf)
|
||||
{
|
||||
sprintf(buf, "%g", dval);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
PIC_INLINE void
|
||||
pic_dtoa(double dval, char *buf)
|
||||
{
|
||||
# define fabs(x) ((x) >= 0 ? (x) : -(x))
|
||||
long lval, tlval;
|
||||
int ival;
|
||||
int scnt, ecnt, cnt = 0;
|
||||
if (dval < 0) {
|
||||
dval = -dval;
|
||||
buf[cnt++] = '-';
|
||||
}
|
||||
lval = tlval = (long)dval;
|
||||
scnt = cnt;
|
||||
do {
|
||||
buf[cnt++] = '0' + (tlval % 10);
|
||||
} while ((tlval /= 10) != 0);
|
||||
ecnt = cnt;
|
||||
while (scnt < ecnt) {
|
||||
char c = buf[scnt];
|
||||
buf[scnt++] = buf[--ecnt];
|
||||
buf[ecnt] = c;
|
||||
}
|
||||
buf[cnt++] = '.';
|
||||
dval -= lval;
|
||||
if ((ival = fabs(dval) * 1e4 + 0.5) == 0) {
|
||||
buf[cnt++] = '0';
|
||||
buf[cnt++] = '0';
|
||||
buf[cnt++] = '0';
|
||||
buf[cnt++] = '0';
|
||||
} else {
|
||||
if (ival < 1000) buf[cnt++] = '0';
|
||||
if (ival < 100) buf[cnt++] = '0';
|
||||
if (ival < 10) buf[cnt++] = '0';
|
||||
scnt = cnt;
|
||||
do {
|
||||
buf[cnt++] = '0' + (ival % 10);
|
||||
} while ((ival /= 10) != 0);
|
||||
ecnt = cnt;
|
||||
while (scnt < ecnt) {
|
||||
char c = buf[scnt];
|
||||
buf[scnt++] = buf[--ecnt];
|
||||
buf[ecnt] = c;
|
||||
}
|
||||
}
|
||||
buf[cnt] = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef PIC_DOUBLE_TO_CSTRING
|
||||
#define PIC_DOUBLE_TO_CSTRING pic_dtoa
|
||||
#endif
|
||||
void PIC_DOUBLE_TO_CSTRING(double, char *);
|
||||
|
||||
#ifndef PIC_CSTRING_TO_DOUBLE
|
||||
#define PIC_CSTRING_TO_DOUBLE atof
|
||||
#endif
|
||||
double PIC_CSTRING_TO_DOUBLE(const char *);
|
||||
|
||||
/* optional features available? */
|
||||
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
|
||||
# define PIC_DIRECT_THREADED_VM 1
|
||||
#else
|
||||
# define PIC_DIRECT_THREADED_VM 0
|
||||
#endif
|
||||
|
||||
#if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
|
||||
# define PIC_NAN_BOXING 1
|
||||
#else
|
||||
# define PIC_NAN_BOXING 0
|
||||
#endif
|
||||
|
||||
#if PIC_USE_LIBC && (defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)))
|
||||
# include <unistd.h>
|
||||
# define PIC_MEMALIGN(pic, buf, alignment, size) posix_memalign(buf, alignment, size)
|
||||
# define PIC_BITMAP_GC 1
|
||||
#endif
|
||||
Reference in New Issue
Block a user