picrin/include/picrin.h

233 lines
7.5 KiB
C
Raw Normal View History

2014-01-17 06:58:31 -05:00
/**
2014-03-26 17:53:21 -04:00
* Copyright (c) 2013-2014 Yuichi Nishiwaki and other picrin contributors.
2014-01-17 06:58:31 -05:00
*
* 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.
*/
2013-10-10 03:15:41 -04:00
#ifndef PICRIN_H__
#define PICRIN_H__
2014-01-17 22:41:39 -05:00
#if defined(__cplusplus)
extern "C" {
#endif
2013-10-10 04:54:35 -04:00
#include <stddef.h>
2013-10-11 04:36:51 -04:00
#include <stdbool.h>
#include <setjmp.h>
2013-10-27 05:38:41 -04:00
#include <stdio.h>
#include <stdint.h>
2014-03-24 23:24:53 -04:00
#include <limits.h>
2014-02-11 09:25:25 -05:00
#include <assert.h>
2013-10-10 04:54:35 -04:00
2014-03-20 10:09:45 -04:00
#include "xvect/xvect.h"
2014-02-06 11:24:51 -05:00
#include "xhash/xhash.h"
2014-02-09 00:52:53 -05:00
#include "xfile/xfile.h"
#include "xrope/xrope.h"
2014-02-06 11:24:51 -05:00
2014-04-05 23:44:11 -04:00
#include "picrin/config.h"
2014-03-19 04:43:53 -04:00
#include "picrin/util.h"
2013-10-10 03:34:24 -04:00
#include "picrin/value.h"
2014-03-07 08:06:43 -05:00
typedef struct pic_code pic_code;
2013-10-16 02:30:52 -04:00
2013-11-04 16:07:36 -05:00
typedef struct {
2014-02-20 02:33:18 -05:00
int argc, retc;
2014-03-07 08:06:43 -05:00
pic_code *ip;
pic_value *fp;
2013-10-28 21:16:56 -04:00
struct pic_env *env;
2014-03-23 00:09:35 -04:00
int regc;
pic_value *regs;
struct pic_env *up;
2013-10-15 10:29:34 -04:00
} pic_callinfo;
2014-03-07 08:09:12 -05:00
typedef struct pic_block {
2013-11-11 04:04:21 -05:00
struct pic_block *prev;
int depth;
struct pic_proc *in, *out;
unsigned refcnt;
2014-03-07 08:09:12 -05:00
} pic_block;
2013-11-11 04:04:21 -05:00
2013-10-10 03:15:41 -04:00
typedef struct {
2013-10-20 22:51:02 -04:00
int argc;
char **argv, **envp;
2014-03-07 08:09:12 -05:00
pic_block *blk;
2013-11-11 04:04:21 -05:00
2013-10-11 11:16:19 -04:00
pic_value *sp;
2013-10-11 11:20:53 -04:00
pic_value *stbase, *stend;
2013-10-15 10:29:34 -04:00
pic_callinfo *ci;
pic_callinfo *cibase, *ciend;
2014-03-07 08:06:43 -05:00
pic_code *ip;
2014-02-03 20:40:03 -05:00
2013-10-28 13:11:31 -04:00
pic_sym sDEFINE, sLAMBDA, sIF, sBEGIN, sQUOTE, sSETBANG;
pic_sym sQUASIQUOTE, sUNQUOTE, sUNQUOTE_SPLICING;
2014-07-17 22:50:08 -04:00
pic_sym sDEFINE_SYNTAX, sDEFINE_MACRO, sLET_SYNTAX;
2013-12-07 05:31:04 -05:00
pic_sym sDEFINE_LIBRARY, sIMPORT, sEXPORT;
2014-01-18 14:47:32 -05:00
pic_sym sCONS, sCAR, sCDR, sNILP;
2014-01-18 23:20:28 -05:00
pic_sym sADD, sSUB, sMUL, sDIV, sMINUS;
2014-02-02 00:55:46 -05:00
pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT;
2014-07-13 21:08:11 -04:00
pic_sym rDEFINE, rLAMBDA, rIF, rBEGIN, rQUOTE, rSETBANG;
2014-07-17 22:51:13 -04:00
pic_sym rDEFINE_SYNTAX, rDEFINE_MACRO, rLET_SYNTAX;
2014-07-13 21:08:11 -04:00
pic_sym rDEFINE_LIBRARY, rIMPORT, rEXPORT;
2014-03-25 02:29:26 -04:00
xhash syms; /* name to symbol */
xhash sym_names; /* symbol to name */
2014-02-12 10:14:03 -05:00
int sym_cnt;
int uniq_sym_cnt;
2013-10-20 01:05:35 -04:00
2014-03-25 02:29:26 -04:00
xhash global_tbl;
pic_value *globals;
size_t glen, gcapa;
2014-03-25 02:29:26 -04:00
xhash macros;
2014-02-11 20:39:20 -05:00
2013-12-07 08:36:14 -05:00
pic_value lib_tbl;
struct pic_lib *lib;
2013-11-26 11:30:30 -05:00
2014-06-25 02:37:24 -04:00
xhash rlabels;
jmp_buf *jmp;
struct pic_error *err;
struct pic_jmpbuf *try_jmps;
2013-11-22 10:19:31 -05:00
struct pic_heap *heap;
2014-05-26 02:51:18 -04:00
struct pic_object **arena;
2014-05-26 03:06:41 -04:00
size_t arena_size, arena_idx;
2013-11-09 00:14:25 -05:00
char *native_stack_start;
2013-10-10 03:15:41 -04:00
} pic_state;
typedef pic_value (*pic_func_t)(pic_state *);
void *pic_alloc(pic_state *, size_t);
#define pic_malloc(pic,size) pic_alloc(pic,size) /* obsoleted */
2013-10-15 22:21:41 -04:00
void *pic_realloc(pic_state *, void *, size_t);
2013-11-30 22:44:43 -05:00
void *pic_calloc(pic_state *, size_t, size_t);
2013-10-13 03:55:07 -04:00
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 *);
2014-01-16 08:02:09 -05:00
void pic_gc_run(pic_state *);
2014-03-24 02:14:54 -04:00
pic_value pic_gc_protect(pic_state *, pic_value);
2014-05-26 03:06:41 -04:00
size_t pic_gc_arena_preserve(pic_state *);
void pic_gc_arena_restore(pic_state *, size_t);
2014-07-13 07:20:04 -04:00
#define pic_void(exec) \
pic_void_(GENSYM(ai), exec)
#define pic_void_(ai,exec) do { \
size_t ai = pic_gc_arena_preserve(pic); \
exec; \
pic_gc_arena_restore(pic, ai); \
} while (0)
2013-10-20 22:51:02 -04:00
pic_state *pic_open(int argc, char *argv[], char **envp);
2013-10-10 03:19:10 -04:00
void pic_close(pic_state *);
2013-10-10 03:15:41 -04:00
2014-03-03 10:29:00 -05:00
void pic_define(pic_state *, const char *, pic_value); /* automatic export */
pic_value pic_ref(pic_state *, const char *);
void pic_set(pic_state *, const char *, pic_value);
2014-01-12 02:06:57 -05:00
2014-07-12 09:07:06 -04:00
pic_value pic_funcall(pic_state *pic, const char *name, pic_list args);
2013-11-09 00:12:59 -05:00
struct pic_proc *pic_get_proc(pic_state *);
int pic_get_args(pic_state *, const char *, ...);
void pic_defun(pic_state *, const char *, pic_func_t);
2013-11-26 12:09:15 -05:00
void pic_defmacro(pic_state *, const char *, struct pic_proc *);
2013-12-07 09:29:29 -05:00
bool pic_equal_p(pic_state *, pic_value, pic_value);
2014-02-28 10:13:11 -05:00
pic_sym pic_intern(pic_state *, const char *, size_t);
2013-10-28 13:11:31 -04:00
pic_sym pic_intern_cstr(pic_state *, const char *);
const char *pic_symbol_name(pic_state *, pic_sym);
2014-01-12 02:03:36 -05:00
pic_sym pic_gensym(pic_state *, pic_sym);
bool pic_interned_p(pic_state *, pic_sym);
2013-10-10 04:06:26 -04:00
char *pic_strdup(pic_state *, const char *);
char *pic_strndup(pic_state *, const char *, size_t);
2013-10-29 02:51:37 -04:00
2014-06-25 02:39:04 -04:00
pic_value pic_read(pic_state *, struct pic_port *);
pic_value pic_read_cstr(pic_state *, const char *);
pic_list pic_parse_file(pic_state *, FILE *); /* #f for incomplete input */
pic_list pic_parse_cstr(pic_state *, const char *);
2013-10-11 02:18:37 -04:00
2014-01-13 00:51:52 -05:00
pic_value pic_load(pic_state *, const char *);
2014-04-05 13:43:49 -04:00
pic_value pic_load_cstr(pic_state *, const char *);
2014-01-13 00:51:52 -05:00
pic_value pic_apply(pic_state *, struct pic_proc *, pic_value);
2014-03-25 07:54:57 -04:00
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);
pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, pic_value);
2014-03-03 08:23:10 -05:00
pic_value pic_eval(pic_state *, pic_value);
2014-01-20 02:57:39 -05:00
struct pic_proc *pic_compile(pic_state *, pic_value);
2013-11-18 02:36:44 -05:00
pic_value pic_macroexpand(pic_state *, pic_value);
2013-10-11 04:36:51 -04:00
void pic_in_library(pic_state *, pic_value);
2013-12-07 21:44:14 -05:00
struct pic_lib *pic_make_library(pic_state *, pic_value);
2013-12-07 12:42:34 -05:00
struct pic_lib *pic_find_library(pic_state *, pic_value);
2013-12-07 10:05:06 -05:00
2014-03-02 08:36:32 -05:00
#define pic_deflibrary(spec) \
2014-03-19 04:55:52 -04:00
pic_deflibrary_helper__(GENSYM(i), GENSYM(prev_lib), spec)
2014-03-02 08:23:57 -05:00
#define pic_deflibrary_helper__(i, prev_lib, spec) \
for (int i = 0; ! i; ) \
for (struct pic_lib *prev_lib; ! i; ) \
2014-06-25 02:39:04 -04:00
for ((prev_lib = pic->lib), pic_make_library(pic, pic_read_cstr(pic, spec)), pic_in_library(pic, pic_read_cstr(pic, spec)); ! i++; pic->lib = prev_lib)
2013-12-08 02:17:28 -05:00
2014-01-08 01:38:31 -05:00
void pic_import(pic_state *, pic_value);
2013-12-07 23:44:39 -05:00
void pic_export(pic_state *, pic_sym);
2014-03-19 04:37:06 -04:00
noreturn void pic_abort(pic_state *, const char *);
noreturn void pic_errorf(pic_state *, const char *, ...);
2014-03-25 01:31:24 -04:00
void pic_warnf(pic_state *, const char *, ...);
2014-03-29 07:02:11 -04:00
pic_str *pic_get_backtrace(pic_state *);
2014-04-06 01:00:45 -04:00
void pic_print_backtrace(pic_state *, struct pic_error *);
2013-10-12 01:40:27 -04:00
2014-03-24 01:57:10 -04:00
/* obsoleted */
noreturn static inline void pic_error(pic_state *pic, const char *msg)
{
pic_errorf(pic, msg);
}
2014-03-25 01:31:24 -04:00
static inline void pic_warn(pic_state *pic, const char *msg)
{
pic_warnf(pic, msg);
}
2014-03-24 01:57:10 -04:00
const char *pic_errmsg(pic_state *);
2014-03-03 10:28:39 -05:00
pic_value pic_write(pic_state *, pic_value); /* returns given obj */
pic_value pic_fwrite(pic_state *, pic_value, xFILE *);
2014-03-03 08:43:59 -05:00
void pic_printf(pic_state *, const char *, ...);
2014-03-03 10:28:39 -05:00
pic_value pic_display(pic_state *, pic_value);
pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);
/* obsoleted macros */
#define pic_debug(pic,obj) pic_write(pic,obj)
#define pic_fdebug(pic,obj,file) pic_fwrite(pic,obj,file)
2013-10-10 04:48:01 -04:00
2014-01-17 22:41:39 -05:00
#if defined(__cplusplus)
}
#endif
2013-10-10 03:15:41 -04:00
#endif