remove type.h and cont.h
This commit is contained in:
parent
fcb3182e08
commit
4affb1c1ce
|
@ -150,7 +150,7 @@ define_var(pic_state *pic, analyze_scope *scope, pic_value sym)
|
||||||
pic_warnf(pic, "redefining variable: ~s", sym);
|
pic_warnf(pic, "redefining variable: ~s", sym);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pic_weak_set(pic, pic->globals, sym, pic_invalid_value());
|
pic_weak_set(pic, pic->globals, sym, pic_invalid_value(pic));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +181,7 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym)
|
||||||
static pic_value
|
static pic_value
|
||||||
analyze_defer(pic_state *pic, analyze_scope *scope, pic_value form)
|
analyze_defer(pic_state *pic, analyze_scope *scope, pic_value form)
|
||||||
{
|
{
|
||||||
pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value());
|
pic_value skel = pic_cons(pic, pic_invalid_value(pic), pic_invalid_value(pic));
|
||||||
|
|
||||||
pic_set_car(pic, scope->defer, pic_cons(pic, pic_cons(pic, form, skel), pic_car(pic, scope->defer)));
|
pic_set_car(pic, scope->defer, pic_cons(pic, pic_cons(pic, form, skel), pic_car(pic, scope->defer)));
|
||||||
|
|
||||||
|
|
|
@ -113,9 +113,9 @@ pic_value pic_open_port(pic_state *, xFILE *file);
|
||||||
xFILE *pic_fileno(pic_state *, pic_value port);
|
xFILE *pic_fileno(pic_state *, pic_value port);
|
||||||
void pic_close_port(pic_state *, pic_value port);
|
void pic_close_port(pic_state *, pic_value port);
|
||||||
|
|
||||||
PIC_INLINE int pic_int(pic_state *, pic_value i);
|
int pic_int(pic_state *, pic_value i);
|
||||||
PIC_INLINE double pic_float(pic_state *, pic_value f);
|
double pic_float(pic_state *, pic_value f);
|
||||||
PIC_INLINE char pic_char(pic_state *, pic_value c);
|
char pic_char(pic_state *, pic_value c);
|
||||||
#define pic_bool(pic,b) (! pic_false_p(pic, b))
|
#define pic_bool(pic,b) (! pic_false_p(pic, b))
|
||||||
const char *pic_str(pic_state *, pic_value str);
|
const char *pic_str(pic_state *, pic_value str);
|
||||||
unsigned char *pic_blob(pic_state *, pic_value blob, int *len);
|
unsigned char *pic_blob(pic_state *, pic_value blob, int *len);
|
||||||
|
@ -127,14 +127,15 @@ typedef struct {
|
||||||
void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value));
|
void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value));
|
||||||
} pic_data_type;
|
} pic_data_type;
|
||||||
|
|
||||||
PIC_INLINE pic_value pic_undef_value(pic_state *);
|
pic_value pic_invalid_value(pic_state *);
|
||||||
PIC_INLINE pic_value pic_int_value(pic_state *, int);
|
pic_value pic_undef_value(pic_state *);
|
||||||
PIC_INLINE pic_value pic_float_value(pic_state *, double);
|
pic_value pic_int_value(pic_state *, int);
|
||||||
PIC_INLINE pic_value pic_char_value(pic_state *, char);
|
pic_value pic_float_value(pic_state *, double);
|
||||||
PIC_INLINE pic_value pic_true_value(pic_state *);
|
pic_value pic_char_value(pic_state *, char);
|
||||||
PIC_INLINE pic_value pic_false_value(pic_state *);
|
pic_value pic_true_value(pic_state *);
|
||||||
PIC_INLINE pic_value pic_bool_value(pic_state *, bool);
|
pic_value pic_false_value(pic_state *);
|
||||||
PIC_INLINE pic_value pic_eof_object(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);
|
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_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))
|
#define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1))
|
||||||
|
@ -173,11 +174,12 @@ enum {
|
||||||
PIC_TYPE_CP = 31
|
PIC_TYPE_CP = 31
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#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_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_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_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_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR)
|
||||||
#define pic_eof_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_EOF)
|
#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_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_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE)
|
||||||
#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING)
|
#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING)
|
||||||
|
@ -211,7 +213,7 @@ pic_value pic_cdar(pic_state *, pic_value);
|
||||||
pic_value pic_cddr(pic_state *, pic_value);
|
pic_value pic_cddr(pic_state *, pic_value);
|
||||||
|
|
||||||
/* list */
|
/* list */
|
||||||
PIC_INLINE pic_value pic_nil_value(pic_state *);
|
pic_value pic_nil_value(pic_state *);
|
||||||
bool pic_list_p(pic_state *, pic_value);
|
bool pic_list_p(pic_state *, pic_value);
|
||||||
pic_value pic_make_list(pic_state *, int n, pic_value *argv);
|
pic_value pic_make_list(pic_state *, int n, pic_value *argv);
|
||||||
pic_value pic_list(pic_state *, int n, ...);
|
pic_value pic_list(pic_state *, int n, ...);
|
||||||
|
@ -296,9 +298,7 @@ int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list);
|
||||||
typedef struct pic_identifier pic_id;
|
typedef struct pic_identifier pic_id;
|
||||||
typedef pic_id pic_sym;
|
typedef pic_id pic_sym;
|
||||||
|
|
||||||
#include "picrin/type.h"
|
|
||||||
#include "picrin/state.h"
|
#include "picrin/state.h"
|
||||||
#include "picrin/cont.h"
|
|
||||||
|
|
||||||
void *pic_default_allocf(void *, void *, size_t);
|
void *pic_default_allocf(void *, void *, size_t);
|
||||||
|
|
||||||
|
@ -347,6 +347,13 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *);
|
||||||
pic_in_library(pic, lib); \
|
pic_in_library(pic, lib); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
void pic_save_point(pic_state *, struct pic_cont *);
|
||||||
|
void pic_load_point(pic_state *, struct pic_cont *);
|
||||||
|
|
||||||
|
pic_value pic_make_cont(pic_state *, struct pic_cont *);
|
||||||
|
|
||||||
|
void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *);
|
||||||
|
|
||||||
/* do not return from try block! */
|
/* do not return from try block! */
|
||||||
|
|
||||||
#define pic_try \
|
#define pic_try \
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
/**
|
|
||||||
* See Copyright Notice in picrin.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PICRIN_CONT_H
|
|
||||||
#define PICRIN_CONT_H
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct pic_cont {
|
|
||||||
PIC_JMPBUF jmp;
|
|
||||||
|
|
||||||
int id;
|
|
||||||
|
|
||||||
struct pic_checkpoint *cp;
|
|
||||||
ptrdiff_t sp_offset;
|
|
||||||
ptrdiff_t ci_offset;
|
|
||||||
ptrdiff_t xp_offset;
|
|
||||||
size_t arena_idx;
|
|
||||||
pic_value ptable;
|
|
||||||
pic_code *ip;
|
|
||||||
|
|
||||||
int retc;
|
|
||||||
pic_value *retv;
|
|
||||||
|
|
||||||
struct pic_cont *prev;
|
|
||||||
};
|
|
||||||
|
|
||||||
void pic_save_point(pic_state *, struct pic_cont *);
|
|
||||||
void pic_load_point(pic_state *, struct pic_cont *);
|
|
||||||
|
|
||||||
pic_value pic_make_cont(pic_state *, struct pic_cont *);
|
|
||||||
|
|
||||||
void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *);
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -133,6 +133,8 @@ struct pic_checkpoint {
|
||||||
struct pic_checkpoint *prev;
|
struct pic_checkpoint *prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct pic_object *pic_obj_ptr(pic_value);
|
||||||
|
|
||||||
#define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o))
|
#define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o))
|
||||||
#define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o))
|
#define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o))
|
||||||
#define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o))
|
#define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o))
|
||||||
|
@ -148,12 +150,13 @@ struct pic_checkpoint {
|
||||||
#define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o))
|
#define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o))
|
||||||
#define pic_rec_ptr(pic, o) ((struct pic_record *)pic_obj_ptr(o))
|
#define pic_rec_ptr(pic, o) ((struct pic_record *)pic_obj_ptr(o))
|
||||||
|
|
||||||
#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END)
|
#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_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_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR)
|
||||||
#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD)
|
#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD)
|
||||||
#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL)
|
#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL)
|
||||||
|
|
||||||
|
pic_value pic_obj_value(void *ptr);
|
||||||
struct pic_object *pic_obj_alloc(pic_state *, size_t, int type);
|
struct pic_object *pic_obj_alloc(pic_state *, size_t, int type);
|
||||||
|
|
||||||
#define VALID_INDEX(pic, len, i) do { \
|
#define VALID_INDEX(pic, len, i) do { \
|
||||||
|
|
|
@ -22,6 +22,25 @@ struct pic_lib {
|
||||||
struct pic_dict *exports;
|
struct pic_dict *exports;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct pic_cont {
|
||||||
|
PIC_JMPBUF jmp;
|
||||||
|
|
||||||
|
int id;
|
||||||
|
|
||||||
|
struct pic_checkpoint *cp;
|
||||||
|
ptrdiff_t sp_offset;
|
||||||
|
ptrdiff_t ci_offset;
|
||||||
|
ptrdiff_t xp_offset;
|
||||||
|
size_t arena_idx;
|
||||||
|
pic_value ptable;
|
||||||
|
pic_code *ip;
|
||||||
|
|
||||||
|
int retc;
|
||||||
|
pic_value *retv;
|
||||||
|
|
||||||
|
struct pic_cont *prev;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int argc, retc;
|
int argc, retc;
|
||||||
pic_code *ip;
|
pic_code *ip;
|
||||||
|
|
|
@ -1,263 +0,0 @@
|
||||||
/**
|
|
||||||
* See Copyright Notice in picrin.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PICRIN_TYPE_H
|
|
||||||
#define PICRIN_TYPE_H
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* `invalid` value will never be seen from user-end:
|
|
||||||
* it is only used for repsenting internal special state
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_INVALID)
|
|
||||||
|
|
||||||
#if PIC_NAN_BOXING
|
|
||||||
|
|
||||||
/**
|
|
||||||
* value representation by nan-boxing:
|
|
||||||
* float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF
|
|
||||||
* ptr : 111111111111TTTT PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP
|
|
||||||
* int : 111111111111TTTT 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII
|
|
||||||
* char : 111111111111TTTT 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48)))
|
|
||||||
|
|
||||||
PIC_INLINE int
|
|
||||||
pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf);
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE double
|
|
||||||
pic_float(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
union { double f; uint64_t i; } u;
|
|
||||||
u.i = v;
|
|
||||||
return u.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE int
|
|
||||||
pic_int(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
union { int i; unsigned u; } u;
|
|
||||||
u.u = v & 0xfffffffful;
|
|
||||||
return u.i;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE char
|
|
||||||
pic_char(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return v & 0xfffffffful;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE struct pic_object *
|
|
||||||
pic_obj_ptr(pic_value v)
|
|
||||||
{
|
|
||||||
return (struct pic_object *)(0xfffffffffffful & v);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL)
|
|
||||||
|
|
||||||
PIC_INLINE int
|
|
||||||
pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return (int)(v.type);
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE double
|
|
||||||
pic_float(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return v.u.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE int
|
|
||||||
pic_int(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return v.u.i;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE char
|
|
||||||
pic_char(pic_state PIC_UNUSED(*pic), pic_value v)
|
|
||||||
{
|
|
||||||
return v.u.c;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE struct pic_object *
|
|
||||||
pic_obj_ptr(pic_value v)
|
|
||||||
{
|
|
||||||
return (struct pic_object *)(v.u.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PIC_INLINE bool
|
|
||||||
pic_valid_int(double v)
|
|
||||||
{
|
|
||||||
return INT_MIN <= v && v <= INT_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_nil_value(pic_state PIC_UNUSED(*pic))
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_NIL);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_eof_object(pic_state PIC_UNUSED(*pic))
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_EOF);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_true_value(pic_state PIC_UNUSED(*pic))
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_TRUE);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_false_value(pic_state PIC_UNUSED(*pic))
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_FALSE);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_bool_value(pic_state PIC_UNUSED(*pic), bool b)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, b ? PIC_TYPE_TRUE : PIC_TYPE_FALSE);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_undef_value(pic_state PIC_UNUSED(*pic))
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_UNDEF);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_invalid_value()
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_INVALID);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if PIC_NAN_BOXING
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_obj_value(void *ptr)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_IVAL_END);
|
|
||||||
v |= 0xfffffffffffful & (uint64_t)ptr;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_float_value(pic_state PIC_UNUSED(*pic), double f)
|
|
||||||
{
|
|
||||||
union { double f; uint64_t i; } u;
|
|
||||||
|
|
||||||
if (f != f) {
|
|
||||||
return 0x7ff8000000000000ul;
|
|
||||||
} else {
|
|
||||||
u.f = f;
|
|
||||||
return u.i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_int_value(pic_state PIC_UNUSED(*pic), int i)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_INT);
|
|
||||||
v |= (unsigned)i;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_char_value(pic_state PIC_UNUSED(*pic), char c)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_CHAR);
|
|
||||||
v |= (unsigned char)c;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_obj_value(void *ptr)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_IVAL_END);
|
|
||||||
v.u.data = ptr;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_float_value(pic_state PIC_UNUSED(*pic), double f)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_FLOAT);
|
|
||||||
v.u.f = f;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_int_value(pic_state PIC_UNUSED(*pic), int i)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_INT);
|
|
||||||
v.u.i = i;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIC_INLINE pic_value
|
|
||||||
pic_char_value(pic_state PIC_UNUSED(*pic), char c)
|
|
||||||
{
|
|
||||||
pic_value v;
|
|
||||||
|
|
||||||
pic_init_value(v, PIC_TYPE_CHAR);
|
|
||||||
v.u.c = c;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -178,7 +178,7 @@ expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred)
|
||||||
static pic_value
|
static pic_value
|
||||||
expand_defer(pic_state *pic, pic_value expr, pic_value deferred)
|
expand_defer(pic_state *pic, pic_value expr, pic_value deferred)
|
||||||
{
|
{
|
||||||
pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value());
|
pic_value skel = pic_cons(pic, pic_invalid_value(pic), pic_invalid_value(pic));
|
||||||
|
|
||||||
pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred)));
|
pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred)));
|
||||||
|
|
||||||
|
|
|
@ -262,8 +262,8 @@ pic_number_string_to_number(pic_state *pic)
|
||||||
|
|
||||||
num = strtol(str, &eptr, radix);
|
num = strtol(str, &eptr, radix);
|
||||||
if (*eptr == '\0') {
|
if (*eptr == '\0') {
|
||||||
return pic_valid_int(num)
|
return INT_MIN <= num && num <= INT_MAX
|
||||||
? pic_int_value(pic, (int)num)
|
? pic_int_value(pic, num)
|
||||||
: pic_float_value(pic, num);
|
: pic_float_value(pic, num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c)
|
||||||
c = next(pic, file);
|
c = next(pic, file);
|
||||||
} while (! (c == EOF || c == '\n'));
|
} while (! (c == EOF || c == '\n'));
|
||||||
|
|
||||||
return pic_invalid_value();
|
return pic_invalid_value(pic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
|
@ -114,7 +114,7 @@ read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return pic_invalid_value();
|
return pic_invalid_value(pic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
|
@ -122,7 +122,7 @@ read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c))
|
||||||
{
|
{
|
||||||
read(pic, file, next(pic, file));
|
read(pic, file, next(pic, file));
|
||||||
|
|
||||||
return pic_invalid_value();
|
return pic_invalid_value(pic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
|
@ -132,13 +132,13 @@ read_directive(pic_state *pic, xFILE *file, int c)
|
||||||
case 'n':
|
case 'n':
|
||||||
if (expect(pic, file, "no-fold-case")) {
|
if (expect(pic, file, "no-fold-case")) {
|
||||||
pic->reader.typecase = PIC_CASE_DEFAULT;
|
pic->reader.typecase = PIC_CASE_DEFAULT;
|
||||||
return pic_invalid_value();
|
return pic_invalid_value(pic);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
if (expect(pic, file, "fold-case")) {
|
if (expect(pic, file, "fold-case")) {
|
||||||
pic->reader.typecase = PIC_CASE_FOLD;
|
pic->reader.typecase = PIC_CASE_FOLD;
|
||||||
return pic_invalid_value();
|
return pic_invalid_value(pic);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -289,8 +289,8 @@ read_unsigned(pic_state *pic, xFILE *file, int c)
|
||||||
buf[idx] = 0;
|
buf[idx] = 0;
|
||||||
flt = PIC_CSTRING_TO_DOUBLE(buf);
|
flt = PIC_CSTRING_TO_DOUBLE(buf);
|
||||||
|
|
||||||
if (dpe == 0 && pic_valid_int(flt))
|
if (dpe == 0 && INT_MIN <= flt && flt <= INT_MAX)
|
||||||
return pic_int_value(pic, (int )flt);
|
return pic_int_value(pic, flt);
|
||||||
return pic_float_value(pic, flt);
|
return pic_float_value(pic, flt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -275,7 +275,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
||||||
pic->ireps.prev = &pic->ireps;
|
pic->ireps.prev = &pic->ireps;
|
||||||
|
|
||||||
/* raised error object */
|
/* raised error object */
|
||||||
pic->err = pic_invalid_value();
|
pic->err = pic_invalid_value(pic);
|
||||||
|
|
||||||
/* file pool */
|
/* file pool */
|
||||||
memset(pic->files, 0, sizeof pic->files);
|
memset(pic->files, 0, sizeof pic->files);
|
||||||
|
@ -392,9 +392,9 @@ pic_close(pic_state *pic)
|
||||||
pic->ci = pic->cibase;
|
pic->ci = pic->cibase;
|
||||||
pic->xp = pic->xpbase;
|
pic->xp = pic->xpbase;
|
||||||
pic->arena_idx = 0;
|
pic->arena_idx = 0;
|
||||||
pic->err = pic_invalid_value();
|
pic->err = pic_invalid_value(pic);
|
||||||
pic->globals = pic_invalid_value();
|
pic->globals = pic_invalid_value(pic);
|
||||||
pic->macros = pic_invalid_value();
|
pic->macros = pic_invalid_value(pic);
|
||||||
pic->features = pic_nil_value(pic);
|
pic->features = pic_nil_value(pic);
|
||||||
|
|
||||||
/* free all libraries */
|
/* free all libraries */
|
||||||
|
|
|
@ -5,6 +5,191 @@
|
||||||
#include "picrin.h"
|
#include "picrin.h"
|
||||||
#include "picrin/object.h"
|
#include "picrin/object.h"
|
||||||
|
|
||||||
|
#if PIC_NAN_BOXING
|
||||||
|
|
||||||
|
/**
|
||||||
|
* value representation by nan-boxing:
|
||||||
|
* float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF
|
||||||
|
* ptr : 111111111111TTTT PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP
|
||||||
|
* int : 111111111111TTTT 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII
|
||||||
|
* char : 111111111111TTTT 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48)))
|
||||||
|
|
||||||
|
int
|
||||||
|
pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf);
|
||||||
|
}
|
||||||
|
|
||||||
|
double
|
||||||
|
pic_float(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
union { double f; uint64_t i; } u;
|
||||||
|
u.i = v;
|
||||||
|
return u.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pic_int(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
union { int i; unsigned u; } u;
|
||||||
|
u.u = v & 0xfffffffful;
|
||||||
|
return u.i;
|
||||||
|
}
|
||||||
|
|
||||||
|
char
|
||||||
|
pic_char(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return v & 0xfffffffful;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pic_object *
|
||||||
|
pic_obj_ptr(pic_value v)
|
||||||
|
{
|
||||||
|
return (struct pic_object *)(0xfffffffffffful & v);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL)
|
||||||
|
|
||||||
|
int
|
||||||
|
pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return (int)(v.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
double
|
||||||
|
pic_float(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return v.u.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pic_int(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return v.u.i;
|
||||||
|
}
|
||||||
|
|
||||||
|
char
|
||||||
|
pic_char(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
|
{
|
||||||
|
return v.u.c;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pic_object *
|
||||||
|
pic_obj_ptr(pic_value v)
|
||||||
|
{
|
||||||
|
return (struct pic_object *)(v.u.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PIC_NAN_BOXING
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_obj_value(void *ptr)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_IVAL_END);
|
||||||
|
v |= 0xfffffffffffful & (uint64_t)ptr;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_float_value(pic_state PIC_UNUSED(*pic), double f)
|
||||||
|
{
|
||||||
|
union { double f; uint64_t i; } u;
|
||||||
|
|
||||||
|
if (f != f) {
|
||||||
|
return 0x7ff8000000000000ul;
|
||||||
|
} else {
|
||||||
|
u.f = f;
|
||||||
|
return u.i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_int_value(pic_state PIC_UNUSED(*pic), int i)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_TYPE_INT);
|
||||||
|
v |= (unsigned)i;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_char_value(pic_state PIC_UNUSED(*pic), char c)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_TYPE_CHAR);
|
||||||
|
v |= (unsigned char)c;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_obj_value(void *ptr)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_IVAL_END);
|
||||||
|
v.u.data = ptr;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_float_value(pic_state PIC_UNUSED(*pic), double f)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_TYPE_FLOAT);
|
||||||
|
v.u.f = f;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_int_value(pic_state PIC_UNUSED(*pic), int i)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_TYPE_INT);
|
||||||
|
v.u.i = i;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_char_value(pic_state PIC_UNUSED(*pic), char c)
|
||||||
|
{
|
||||||
|
pic_value v;
|
||||||
|
|
||||||
|
pic_init_value(v, PIC_TYPE_CHAR);
|
||||||
|
v.u.c = c;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DEFVAL(name, type) \
|
||||||
|
pic_value name(pic_state PIC_UNUSED(*pic)) { \
|
||||||
|
pic_value v; \
|
||||||
|
pic_init_value(v, type); \
|
||||||
|
return v; \
|
||||||
|
}
|
||||||
|
|
||||||
|
DEFVAL(pic_nil_value, PIC_TYPE_NIL)
|
||||||
|
DEFVAL(pic_eof_object, PIC_TYPE_EOF)
|
||||||
|
DEFVAL(pic_true_value, PIC_TYPE_TRUE)
|
||||||
|
DEFVAL(pic_false_value, PIC_TYPE_FALSE)
|
||||||
|
DEFVAL(pic_undef_value, PIC_TYPE_UNDEF)
|
||||||
|
DEFVAL(pic_invalid_value, PIC_TYPE_INVALID)
|
||||||
|
|
||||||
int
|
int
|
||||||
pic_type(pic_state PIC_UNUSED(*pic), pic_value v)
|
pic_type(pic_state PIC_UNUSED(*pic), pic_value v)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue