2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013-2014 Yuichi Nishiwaki and other picrin contributors.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-09-14 04:54:53 -04:00
|
|
|
#ifndef PICRIN_H
|
|
|
|
#define PICRIN_H
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <limits.h>
|
2015-01-17 10:17:37 -05:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-01-25 22:20:32 -05:00
|
|
|
#include "picrin/config.h"
|
2015-05-27 12:48:38 -04:00
|
|
|
|
2015-06-18 14:23:07 -04:00
|
|
|
#include "picrin/compat.h"
|
2015-06-23 09:08:18 -04:00
|
|
|
#include "picrin/kvec.h"
|
2015-06-24 16:56:15 -04:00
|
|
|
#include "picrin/khash.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-18 14:06:57 -04:00
|
|
|
typedef struct pic_state pic_state;
|
|
|
|
|
2015-07-04 02:30:02 -04:00
|
|
|
#include "picrin/value.h"
|
2015-06-18 14:10:11 -04:00
|
|
|
#include "picrin/irep.h"
|
2015-06-18 14:06:57 -04:00
|
|
|
#include "picrin/file.h"
|
2015-06-18 14:14:55 -04:00
|
|
|
#include "picrin/read.h"
|
2015-06-18 14:27:03 -04:00
|
|
|
#include "picrin/gc.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-24 18:05:41 -04:00
|
|
|
KHASH_DECLARE(s, const char *, pic_sym *);
|
|
|
|
|
2015-06-08 08:04:04 -04:00
|
|
|
typedef struct pic_checkpoint {
|
2015-06-22 04:06:13 -04:00
|
|
|
PIC_OBJECT_HEADER
|
2014-09-17 02:09:15 -04:00
|
|
|
struct pic_proc *in;
|
|
|
|
struct pic_proc *out;
|
|
|
|
int depth;
|
2015-06-08 08:04:04 -04:00
|
|
|
struct pic_checkpoint *prev;
|
|
|
|
} pic_checkpoint;
|
2014-09-17 02:09:15 -04:00
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
typedef struct {
|
|
|
|
int argc, retc;
|
|
|
|
pic_code *ip;
|
|
|
|
pic_value *fp;
|
2015-07-04 01:30:46 -04:00
|
|
|
struct pic_irep *irep;
|
2015-05-30 09:34:51 -04:00
|
|
|
struct pic_context *cxt;
|
2014-08-25 00:38:09 -04:00
|
|
|
int regc;
|
|
|
|
pic_value *regs;
|
2015-05-30 09:34:51 -04:00
|
|
|
struct pic_context *up;
|
2014-08-25 00:38:09 -04:00
|
|
|
} pic_callinfo;
|
|
|
|
|
2015-05-27 13:12:26 -04:00
|
|
|
typedef void *(*pic_allocf)(void *, size_t);
|
|
|
|
|
2015-06-18 14:06:57 -04:00
|
|
|
struct pic_state {
|
2014-08-25 00:38:09 -04:00
|
|
|
int argc;
|
|
|
|
char **argv, **envp;
|
|
|
|
|
2015-05-27 13:12:26 -04:00
|
|
|
pic_allocf allocf;
|
2015-06-25 13:09:06 -04:00
|
|
|
void *userdata;
|
2015-05-27 13:12:26 -04:00
|
|
|
|
2015-06-08 08:04:04 -04:00
|
|
|
pic_checkpoint *cp;
|
2015-06-22 14:12:17 -04:00
|
|
|
struct pic_cont *cc;
|
|
|
|
int ccnt;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_value *sp;
|
|
|
|
pic_value *stbase, *stend;
|
|
|
|
|
|
|
|
pic_callinfo *ci;
|
|
|
|
pic_callinfo *cibase, *ciend;
|
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
struct pic_proc **xp;
|
|
|
|
struct pic_proc **xpbase, **xpend;
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_code *ip;
|
|
|
|
|
2015-06-09 05:36:39 -04:00
|
|
|
pic_value ptable;
|
|
|
|
|
2015-01-22 04:06:10 -05:00
|
|
|
struct pic_lib *lib, *prev_lib;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-07-01 16:36:09 -04:00
|
|
|
pic_sym *sQUOTE, *sQUASIQUOTE, *sUNQUOTE, *sUNQUOTE_SPLICING;
|
|
|
|
pic_sym *sSYNTAX_QUOTE, *sSYNTAX_QUASIQUOTE, *sSYNTAX_UNQUOTE, *sSYNTAX_UNQUOTE_SPLICING;
|
|
|
|
pic_sym *sDEFINE_LIBRARY, *sIMPORT, *sEXPORT, *sCOND_EXPAND;
|
|
|
|
pic_sym *sGREF, *sCREF, *sLREF, *sCALL;
|
|
|
|
|
|
|
|
pic_sym *uDEFINE, *uLAMBDA, *uIF, *uBEGIN, *uQUOTE, *uSETBANG, *uDEFINE_MACRO;
|
|
|
|
pic_sym *uDEFINE_LIBRARY, *uIMPORT, *uEXPORT, *uCOND_EXPAND;
|
|
|
|
pic_sym *uCONS, *uCAR, *uCDR, *uNILP, *uSYMBOLP, *uPAIRP;
|
|
|
|
pic_sym *uADD, *uSUB, *uMUL, *uDIV, *uEQ, *uLT, *uLE, *uGT, *uGE, *uNOT;
|
2015-06-09 12:06:56 -04:00
|
|
|
pic_sym *uVALUES, *uCALL_WITH_VALUES;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-07-04 05:48:48 -04:00
|
|
|
pic_value pCONS, pCAR, pCDR, pNILP, pPAIRP, pSYMBOLP, pNOT;
|
2015-07-06 06:13:00 -04:00
|
|
|
pic_value pADD, pSUB, pMUL, pDIV, pEQ, pLT, pLE, pGT, pGE;
|
2015-07-04 05:48:48 -04:00
|
|
|
|
2014-09-01 00:01:17 -04:00
|
|
|
struct pic_lib *PICRIN_BASE;
|
|
|
|
struct pic_lib *PICRIN_USER;
|
|
|
|
|
2014-09-09 12:41:10 -04:00
|
|
|
pic_value features;
|
|
|
|
|
2015-06-24 18:05:41 -04:00
|
|
|
khash_t(s) syms; /* name to symbol */
|
2015-06-09 13:16:38 -04:00
|
|
|
int ucnt;
|
2015-01-18 07:25:34 -05:00
|
|
|
struct pic_dict *globals;
|
|
|
|
struct pic_dict *macros;
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value libs;
|
2015-06-09 05:31:46 -04:00
|
|
|
struct pic_reg *attrs;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-18 14:14:55 -04:00
|
|
|
pic_reader reader;
|
2015-06-18 14:06:57 -04:00
|
|
|
xFILE files[XOPEN_MAX];
|
2015-06-18 14:10:11 -04:00
|
|
|
pic_code iseq[2]; /* for pic_apply_trampoline */
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-18 22:14:29 -05:00
|
|
|
bool gc_enable;
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_heap *heap;
|
|
|
|
struct pic_object **arena;
|
|
|
|
size_t arena_size, arena_idx;
|
2015-06-09 05:31:46 -04:00
|
|
|
struct pic_reg *regs;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-18 01:50:01 -04:00
|
|
|
pic_value err;
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
char *native_stack_start;
|
2015-06-18 14:06:57 -04:00
|
|
|
};
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
typedef pic_value (*pic_func_t)(pic_state *);
|
|
|
|
|
2015-05-28 03:42:16 -04:00
|
|
|
void *pic_malloc(pic_state *, size_t);
|
2014-08-25 00:38:09 -04:00
|
|
|
void *pic_realloc(pic_state *, void *, size_t);
|
|
|
|
void *pic_calloc(pic_state *, size_t, size_t);
|
|
|
|
struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt);
|
|
|
|
struct pic_object *pic_obj_alloc_unsafe(pic_state *, size_t, enum pic_tt);
|
|
|
|
void pic_free(pic_state *, void *);
|
|
|
|
|
|
|
|
void pic_gc_run(pic_state *);
|
|
|
|
pic_value pic_gc_protect(pic_state *, pic_value);
|
|
|
|
size_t pic_gc_arena_preserve(pic_state *);
|
|
|
|
void pic_gc_arena_restore(pic_state *, size_t);
|
|
|
|
#define pic_void(exec) \
|
2015-01-17 04:36:23 -05:00
|
|
|
pic_void_(PIC_GENSYM(ai), exec)
|
2014-08-25 00:38:09 -04:00
|
|
|
#define pic_void_(ai,exec) do { \
|
|
|
|
size_t ai = pic_gc_arena_preserve(pic); \
|
|
|
|
exec; \
|
|
|
|
pic_gc_arena_restore(pic, ai); \
|
|
|
|
} while (0)
|
|
|
|
|
2015-05-27 13:12:26 -04:00
|
|
|
void *pic_default_allocf(void *, size_t);
|
2015-06-25 13:09:06 -04:00
|
|
|
pic_state *pic_open(pic_allocf, void *);
|
2014-08-25 00:38:09 -04:00
|
|
|
void pic_close(pic_state *);
|
2015-06-25 13:09:06 -04:00
|
|
|
void pic_set_argv(pic_state *, int argc, char *argv[], char **envp);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-09 12:41:10 -04:00
|
|
|
void pic_add_feature(pic_state *, const char *);
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_proc *pic_get_proc(pic_state *);
|
|
|
|
int pic_get_args(pic_state *, const char *, ...);
|
|
|
|
|
2014-09-21 05:19:13 -04:00
|
|
|
bool pic_eq_p(pic_value, pic_value);
|
|
|
|
bool pic_eqv_p(pic_value, pic_value);
|
2014-08-25 00:38:09 -04:00
|
|
|
bool pic_equal_p(pic_state *, pic_value, pic_value);
|
|
|
|
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *pic_intern(pic_state *, pic_str *);
|
|
|
|
pic_sym *pic_intern_cstr(pic_state *, const char *);
|
|
|
|
const char *pic_symbol_name(pic_state *, pic_sym *);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
pic_value pic_read(pic_state *, struct pic_port *);
|
|
|
|
pic_value pic_read_cstr(pic_state *, const char *);
|
|
|
|
|
2015-01-17 10:32:52 -05:00
|
|
|
void pic_load_port(pic_state *, struct pic_port *);
|
2014-09-08 05:50:15 -04:00
|
|
|
void pic_load_cstr(pic_state *, const char *);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2015-06-19 08:21:04 -04:00
|
|
|
void pic_define(pic_state *, const char *, pic_value);
|
|
|
|
void pic_defun(pic_state *, const char *, pic_func_t);
|
|
|
|
void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *);
|
2015-06-23 13:02:26 -04:00
|
|
|
/* functions suffixed with '_' will not perform automatic export */
|
|
|
|
void pic_define_(pic_state *, const char *, pic_value);
|
|
|
|
void pic_defun_(pic_state *, const char *, pic_func_t);
|
|
|
|
void pic_defvar_(pic_state *, const char *, pic_value, struct pic_proc *);
|
2015-06-19 08:21:04 -04:00
|
|
|
|
|
|
|
pic_value pic_ref(pic_state *, struct pic_lib *, const char *);
|
|
|
|
void pic_set(pic_state *, struct pic_lib *, const char *, pic_value);
|
2014-09-15 11:29:19 -04:00
|
|
|
pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, pic_list);
|
2015-06-18 10:15:09 -04:00
|
|
|
pic_value pic_funcall0(pic_state *pic, struct pic_lib *, const char *);
|
|
|
|
pic_value pic_funcall1(pic_state *pic, struct pic_lib *, const char *, pic_value);
|
|
|
|
pic_value pic_funcall2(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value);
|
|
|
|
pic_value pic_funcall3(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value, pic_value);
|
2014-09-15 11:16:30 -04:00
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value pic_apply(pic_state *, struct pic_proc *, pic_value);
|
|
|
|
pic_value pic_apply0(pic_state *, struct pic_proc *);
|
|
|
|
pic_value pic_apply1(pic_state *, struct pic_proc *, pic_value);
|
|
|
|
pic_value pic_apply2(pic_state *, struct pic_proc *, pic_value, pic_value);
|
|
|
|
pic_value pic_apply3(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value);
|
|
|
|
pic_value pic_apply4(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value);
|
|
|
|
pic_value pic_apply5(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value, pic_value);
|
2015-07-04 05:01:30 -04:00
|
|
|
pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, size_t, pic_value *);
|
|
|
|
pic_value pic_apply_trampoline_list(pic_state *, struct pic_proc *, pic_value);
|
2015-06-15 12:52:18 -04:00
|
|
|
pic_value pic_eval(pic_state *, pic_value, struct pic_env *);
|
|
|
|
struct pic_proc *pic_compile(pic_state *, pic_value, struct pic_env *);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-19 08:21:04 -04:00
|
|
|
struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *);
|
|
|
|
|
2015-05-30 11:25:40 -04:00
|
|
|
struct pic_lib *pic_make_library(pic_state *, pic_value);
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_lib *pic_find_library(pic_state *, pic_value);
|
|
|
|
|
|
|
|
#define pic_deflibrary(pic, spec) \
|
2015-01-22 04:06:10 -05:00
|
|
|
for (((assert(pic->prev_lib == NULL)), \
|
|
|
|
(pic->prev_lib = pic->lib), \
|
2015-05-30 11:25:40 -04:00
|
|
|
(pic->lib = pic_find_library(pic, pic_read_cstr(pic, (spec)))), \
|
|
|
|
(pic->lib = pic->lib \
|
|
|
|
? pic->lib \
|
|
|
|
: pic_make_library(pic, pic_read_cstr(pic, (spec))))); \
|
2015-01-22 04:06:10 -05:00
|
|
|
pic->prev_lib != NULL; \
|
|
|
|
((pic->lib = pic->prev_lib), \
|
|
|
|
(pic->prev_lib = NULL)))
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-16 12:52:20 -04:00
|
|
|
void pic_import(pic_state *, struct pic_lib *);
|
2015-01-20 02:02:28 -05:00
|
|
|
void pic_export(pic_state *, pic_sym *);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-25 22:22:38 -05:00
|
|
|
PIC_NORETURN void pic_panic(pic_state *, const char *);
|
|
|
|
PIC_NORETURN void pic_errorf(pic_state *, const char *, ...);
|
2014-08-25 00:38:09 -04:00
|
|
|
void pic_warnf(pic_state *, const char *, ...);
|
2014-09-21 05:07:09 -04:00
|
|
|
const char *pic_errmsg(pic_state *);
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_str *pic_get_backtrace(pic_state *);
|
2015-05-27 11:14:10 -04:00
|
|
|
void pic_print_backtrace(pic_state *, xFILE *);
|
2014-09-24 20:44:21 -04:00
|
|
|
struct pic_dict *pic_attr(pic_state *, pic_value);
|
|
|
|
pic_value pic_attr_ref(pic_state *, pic_value, const char *);
|
|
|
|
void pic_attr_set(pic_state *, pic_value, const char *, pic_value);
|
|
|
|
|
2014-09-14 05:16:02 -04:00
|
|
|
struct pic_port *pic_stdin(pic_state *);
|
|
|
|
struct pic_port *pic_stdout(pic_state *);
|
|
|
|
struct pic_port *pic_stderr(pic_state *);
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value pic_write(pic_state *, pic_value); /* returns given obj */
|
|
|
|
pic_value pic_fwrite(pic_state *, pic_value, xFILE *);
|
|
|
|
void pic_printf(pic_state *, const char *, ...);
|
2015-06-18 13:05:56 -04:00
|
|
|
void pic_fprintf(pic_state *, struct pic_port *, const char *, ...);
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_value pic_display(pic_state *, pic_value);
|
|
|
|
pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);
|
2015-05-28 10:30:55 -04:00
|
|
|
|
|
|
|
#if DEBUG
|
2015-06-18 09:59:22 -04:00
|
|
|
# define pic_debug(pic,obj) pic_fwrite(pic,obj,xstderr)
|
2015-05-28 10:30:55 -04:00
|
|
|
# define pic_fdebug(pic,obj,file) pic_fwrite(pic,obj,file)
|
|
|
|
#endif
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-28 10:57:10 -04:00
|
|
|
#include "picrin/blob.h"
|
|
|
|
#include "picrin/cont.h"
|
|
|
|
#include "picrin/data.h"
|
|
|
|
#include "picrin/dict.h"
|
|
|
|
#include "picrin/error.h"
|
|
|
|
#include "picrin/lib.h"
|
|
|
|
#include "picrin/macro.h"
|
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/port.h"
|
|
|
|
#include "picrin/proc.h"
|
|
|
|
#include "picrin/record.h"
|
|
|
|
#include "picrin/string.h"
|
|
|
|
#include "picrin/symbol.h"
|
|
|
|
#include "picrin/vector.h"
|
2015-06-09 05:31:46 -04:00
|
|
|
#include "picrin/reg.h"
|
2015-05-28 10:57:10 -04:00
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|