Merge branch 'master' into equal

This commit is contained in:
Sunrim KIM (keen)
2014-06-18 00:48:41 +09:00
32 changed files with 957 additions and 230 deletions
+8 -7
View File
@@ -294,7 +294,7 @@ analyze_global_var(analyze_state *state, pic_sym sym)
xh_entry *e;
size_t i;
if ((e = xh_get(&pic->global_tbl, sym))) {
if ((e = xh_get_int(&pic->global_tbl, sym))) {
i = xh_val(e, size_t);
}
else {
@@ -302,7 +302,7 @@ analyze_global_var(analyze_state *state, pic_sym sym)
if (i >= pic->gcapa) {
pic_error(pic, "global table overflow");
}
xh_put(&pic->global_tbl, sym, &i);
xh_put_int(&pic->global_tbl, sym, &i);
}
return pic_list2(pic, pic_symbol_value(state->sGREF), pic_int_value(i));
}
@@ -561,7 +561,7 @@ analyze_add(analyze_state *state, pic_value obj, bool tailpos)
ARGC_ASSERT_GE(0);
switch (pic_length(pic, obj)) {
case 1:
return pic_int_value(0);
return pic_list2(pic, pic_symbol_value(pic->sQUOTE), pic_int_value(0));
case 2:
return analyze(state, pic_car(pic, pic_cdr(pic, obj)), tailpos);
default:
@@ -598,7 +598,7 @@ analyze_mul(analyze_state *state, pic_value obj, bool tailpos)
ARGC_ASSERT_GE(0);
switch (pic_length(pic, obj)) {
case 1:
return pic_int_value(1);
return pic_list2(pic, pic_symbol_value(pic->sQUOTE), pic_int_value(1));
case 2:
return analyze(state, pic_car(pic, pic_cdr(pic, obj)), tailpos);
default:
@@ -825,6 +825,7 @@ analyze_node(analyze_state *state, pic_value obj, bool tailpos)
case PIC_TT_IREP:
case PIC_TT_DATA:
case PIC_TT_BOX:
case PIC_TT_DICT:
pic_errorf(pic, "invalid expression given: ~s", obj);
}
UNREACHABLE();
@@ -929,18 +930,18 @@ create_activation(codegen_context *cxt)
for (i = 0; i < cxt->args.size; ++i) {
var = xv_get(&cxt->args, i);
n = i + offset;
xh_put(&regs, *var, &n);
xh_put_int(&regs, *var, &n);
}
offset += i;
for (i = 0; i < cxt->locals.size; ++i) {
var = xv_get(&cxt->locals, i);
n = i + offset;
xh_put(&regs, *var, &n);
xh_put_int(&regs, *var, &n);
}
for (i = 0; i < cxt->captures.size; ++i) {
var = xv_get(&cxt->captures, i);
if ((n = xh_val(xh_get(&regs, *var), size_t)) <= cxt->args.size || (cxt->varg && n == cxt->args.size + 1)) {
if ((n = xh_val(xh_get_int(&regs, *var), size_t)) <= cxt->args.size || (cxt->varg && n == cxt->args.size + 1)) {
/* copy arguments to capture variable area */
cxt->code[cxt->clen].insn = OP_LREF;
cxt->code[cxt->clen].u.i = n;
+100
View File
@@ -0,0 +1,100 @@
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/dict.h"
static pic_value
pic_dict_dict(pic_state *pic)
{
struct pic_dict *dict;
pic_get_args(pic, "");
dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TT_DICT);
xh_init_int(&dict->hash, sizeof(pic_value));
return pic_obj_value(dict);
}
static pic_value
pic_dict_dict_p(pic_state *pic)
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_bool_value(pic_dict_p(obj));
}
static pic_value
pic_dict_dict_ref(pic_state *pic)
{
struct pic_dict *dict;
pic_sym key;
xh_entry *e;
pic_get_args(pic, "dm", &dict, &key);
e = xh_get_int(&dict->hash, key);
if (! e) {
pic_errorf(pic, "element not found for a key: ~s", pic_sym_value(key));
}
return xh_val(e, pic_value);
}
static pic_value
pic_dict_dict_set(pic_state *pic)
{
struct pic_dict *dict;
pic_sym key;
pic_value val;
pic_get_args(pic, "dmo", &dict, &key, &val);
xh_put_int(&dict->hash, key, &val);
return pic_none_value();
}
static pic_value
pic_dict_dict_del(pic_state *pic)
{
struct pic_dict *dict;
pic_sym key;
pic_get_args(pic, "dm", &dict, &key);
if (xh_get_int(&dict->hash, key) == NULL) {
pic_errorf(pic, "no slot named ~s found in dictionary", pic_sym_value(key));
}
xh_del_int(&dict->hash, key);
return pic_none_value();
}
static pic_value
pic_dict_dict_size(pic_state *pic)
{
struct pic_dict *dict;
pic_get_args(pic, "d", &dict);
return pic_int_value(dict->hash.count);
}
void
pic_init_dict(pic_state *pic)
{
pic_deflibrary ("(picrin dictionary)") {
pic_defun(pic, "dictionary", pic_dict_dict);
pic_defun(pic, "dictionary?", pic_dict_dict_p);
pic_defun(pic, "dictionary-ref", pic_dict_dict_ref);
pic_defun(pic, "dictionary-set!", pic_dict_dict_set);
pic_defun(pic, "dictionary-delete", pic_dict_dict_del);
pic_defun(pic, "dictionary-size", pic_dict_dict_size);
}
}
+17 -2
View File
@@ -20,6 +20,7 @@
#include "picrin/var.h"
#include "picrin/data.h"
#include "picrin/box.h"
#include "picrin/dict.h"
#if GC_DEBUG
# include <string.h>
@@ -504,6 +505,16 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
gc_mark(pic, box->value);
break;
}
case PIC_TT_DICT: {
struct pic_dict *dict = (struct pic_dict *)obj;
xh_iter it;
xh_begin(&it, &dict->hash);
while (xh_next(&it)) {
gc_mark(pic, xh_val(it.e, pic_value));
}
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
@@ -533,8 +544,7 @@ gc_mark_phase(pic_state *pic)
{
pic_value *stack;
pic_callinfo *ci;
size_t i;
int j;
size_t i, j;
xh_iter it;
/* block */
@@ -658,6 +668,11 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
case PIC_TT_BOX: {
break;
}
case PIC_TT_DICT: {
struct pic_dict *dict = (struct pic_dict *)obj;
xh_destroy(&dict->hash);
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
+2
View File
@@ -29,6 +29,7 @@ void pic_init_macro(pic_state *);
void pic_init_var(pic_state *);
void pic_init_load(pic_state *);
void pic_init_write(pic_state *);
void pic_init_dict(pic_state *);
void pic_load_piclib(pic_state *);
@@ -93,6 +94,7 @@ pic_init_core(pic_state *pic)
pic_init_var(pic); DONE;
pic_init_load(pic); DONE;
pic_init_write(pic); DONE;
pic_init_dict(pic); DONE;
pic_load_piclib(pic); DONE;
+53
View File
@@ -60,3 +60,56 @@ pic_find_library(pic_state *pic, pic_value spec)
}
return pic_lib_ptr(pic_cdr(pic, v));
}
void
pic_import(pic_state *pic, pic_value spec)
{
struct pic_lib *lib;
xh_iter it;
lib = pic_find_library(pic, spec);
if (! lib) {
pic_errorf(pic, "library not found: ~a", spec);
}
xh_begin(&it, &lib->exports);
while (xh_next(&it)) {
#if DEBUG
printf("* importing %s as %s\n", pic_symbol_name(pic, xh_key(it.e, pic_sym)), pic_symbol_name(pic, xh_val(it.e, pic_sym)));
#endif
pic_put_rename(pic, pic->lib->senv, xh_key(it.e, pic_sym), xh_val(it.e, pic_sym));
}
}
void
pic_export(pic_state *pic, pic_sym sym)
{
pic_sym rename;
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
}
#if DEBUG
printf("* exporting %s as %s\n", pic_symbol_name(pic, sym), pic_symbol_name(pic, rename));
#endif
xh_put_int(&pic->lib->exports, sym, &rename);
}
void
pic_export_as(pic_state *pic, pic_sym sym, pic_sym as)
{
pic_sym rename;
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
}
#if DEBUG
printf("* exporting %s as %s\n", pic_symbol_name(pic, as), pic_symbol_name(pic, rename));
#endif
xh_put_int(&pic->lib->exports, as, &rename);
}
+27 -43
View File
@@ -52,7 +52,7 @@ pic_put_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym renam
{
UNUSED(pic);
xh_put(&senv->renames, sym, &rename);
xh_put_int(&senv->renames, sym, &rename);
}
bool
@@ -62,7 +62,7 @@ pic_find_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym *ren
UNUSED(pic);
if ((e = xh_get(&senv->renames, sym)) == NULL) {
if ((e = xh_get_int(&senv->renames, sym)) == NULL) {
return false;
}
if (rename != NULL) {
@@ -71,43 +71,6 @@ pic_find_rename(pic_state *pic, struct pic_senv *senv, pic_sym sym, pic_sym *ren
return true;
}
void
pic_import(pic_state *pic, pic_value spec)
{
struct pic_lib *lib;
xh_iter it;
lib = pic_find_library(pic, spec);
if (! lib) {
pic_errorf(pic, "library not found: ~a", spec);
}
xh_begin(&it, &lib->exports);
while (xh_next(&it)) {
#if DEBUG
printf("* importing %s as %s\n", pic_symbol_name(pic, xh_key(it.e, pic_sym)), pic_symbol_name(pic, xh_val(it.e, pic_sym)));
#endif
pic_put_rename(pic, pic->lib->senv, xh_key(it.e, pic_sym), xh_val(it.e, pic_sym));
}
}
void
pic_export(pic_state *pic, pic_sym sym)
{
pic_sym rename;
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
pic_errorf(pic, "export: symbol not defined %s", pic_symbol_name(pic, sym));
}
#if DEBUG
printf("* exporting %s as %s\n", pic_symbol_name(pic, sym), pic_symbol_name(pic, rename));
#endif
xh_put(&pic->lib->exports, sym, &rename);
}
static void
define_macro(pic_state *pic, pic_sym rename, struct pic_proc *proc, struct pic_senv *senv)
{
@@ -117,7 +80,7 @@ define_macro(pic_state *pic, pic_sym rename, struct pic_proc *proc, struct pic_s
mac->senv = senv;
mac->proc = proc;
xh_put(&pic->macros, rename, &mac);
xh_put_int(&pic->macros, rename, &mac);
}
static struct pic_macro *
@@ -125,7 +88,7 @@ find_macro(pic_state *pic, pic_sym rename)
{
xh_entry *e;
if ((e = xh_get(&pic->macros, rename)) == NULL) {
if ((e = xh_get_int(&pic->macros, rename)) == NULL) {
return NULL;
}
return xh_val(e, struct pic_macro *);
@@ -304,14 +267,34 @@ macroexpand_import(pic_state *pic, pic_value expr)
static pic_value
macroexpand_export(pic_state *pic, pic_value expr)
{
extern pic_value pic_export_as(pic_state *, pic_sym, pic_sym);
pic_value spec;
pic_sym sRENAME, sym, as;
sRENAME = pic_intern_cstr(pic, "rename");
pic_for_each (spec, pic_cdr(pic, expr)) {
if (! pic_sym_p(spec)) {
if (pic_sym_p(spec)) {
sym = as = pic_sym(spec);
}
else if (pic_list_p(spec) && pic_eq_p(pic_car(pic, spec), pic_sym_value(sRENAME))) {
if (pic_length(pic, spec) != 3) {
pic_error(pic, "syntax error");
}
if (! pic_sym_p(pic_list_ref(pic, spec, 1))) {
pic_error(pic, "syntax error");
}
sym = pic_sym(pic_list_ref(pic, spec, 1));
if (! pic_sym_p(pic_list_ref(pic, spec, 2))) {
pic_error(pic, "syntax error");
}
as = pic_sym(pic_list_ref(pic, spec, 2));
}
else {
pic_error(pic, "syntax error");
}
/* TODO: warn if symbol is shadowed by local variable */
pic_export(pic, pic_sym(spec));
pic_export_as(pic, sym, as);
}
return pic_none_value();
@@ -586,6 +569,7 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_valu
case PIC_TT_IREP:
case PIC_TT_DATA:
case PIC_TT_BOX:
case PIC_TT_DICT:
pic_errorf(pic, "unexpected value type: ~s", expr);
}
UNREACHABLE();
+1 -1
View File
@@ -224,7 +224,7 @@ pic_number_max(pic_state *pic)
f = fmax(f, pic_float(argv[i]));
}
else {
pic_error(pic, "min: number required");
pic_error(pic, "max: number required");
}
}
+136 -6
View File
@@ -434,6 +434,136 @@ pic_port_char_ready_p(pic_state *pic)
return pic_true_value(); /* FIXME: always returns #t */
}
static pic_value
pic_port_read_string(pic_state *pic){
struct pic_port *port = pic_stdin(pic), *buf;
pic_str *str;
int k, i;
char c;
pic_get_args(pic, "i|p", &k, &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-stritg");
buf = pic_open_output_string(pic);
for(i = 0; i < k; ++i) {
c = xfgetc(port->file);
if( c == EOF){
break;
}
xfputc(c, buf->file);
}
str = pic_get_output_string(pic, buf);
if (pic_strlen(str) == 0 && c == EOF) {
return pic_eof_object();
}
else {
return pic_obj_value(str);
}
}
static pic_value
pic_port_read_byte(pic_state *pic){
struct pic_port *port = pic_stdin(pic);
char c;
pic_get_args(pic, "|p", &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-u8");
if ((c = xfgetc(port->file)) == EOF) {
return pic_eof_object();
}
return pic_int_value(c);
}
static pic_value
pic_port_peek_byte(pic_state *pic)
{
char c;
struct pic_port *port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "peek-u8");
if ((c = xfgetc(port->file)) == EOF) {
return pic_eof_object();
}
else {
xungetc(c, port->file);
return pic_int_value(c);
}
}
static pic_value
pic_port_byte_ready_p(pic_state *pic)
{
struct pic_port *port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "u8-ready?");
return pic_true_value(); /* FIXME: always returns #t */
}
static pic_value
pic_port_read_blob(pic_state *pic){
struct pic_port *port = pic_stdin(pic);
int k, i;
char *buf;
pic_get_args(pic, "i|p", &k, &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector");
buf = pic_calloc(pic, k, sizeof(char));
i = xfread(buf, sizeof(char), k, port->file);
if ( i == 0 ) {
return pic_eof_object();
}
else {
pic_realloc(pic, buf, i);
return pic_obj_value(pic_blob_new(pic, buf, i));
}
}
static pic_value
pic_port_read_blob_ip(pic_state *pic){
struct pic_port *port;
struct pic_blob *bv;
int i, n, start, end, len;
char *buf;
n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end);
switch (n) {
case 1:
port = pic_stdin(pic);
case 2:
start = 0;
case 3:
end = bv->len;
}
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!");
len = end - start;
buf = pic_calloc(pic, len, sizeof(char));
i = xfread(buf, sizeof(char), len, port->file);
memcpy(bv->data + start, buf, i);
pic_free(pic, buf);
if ( i == 0) {
return pic_eof_object();
}
else {
return pic_int_value(i);
}
}
static pic_value
pic_port_newline(pic_state *pic)
{
@@ -571,12 +701,12 @@ pic_init_port(pic_state *pic)
pic_defun(pic, "eof-object?", pic_port_eof_object_p);
pic_defun(pic, "eof-object", pic_port_eof_object);
pic_defun(pic, "char-ready?", pic_port_char_ready_p);
/* pic_defun(pic, "read-string", pic_port_read_string); */
/* pic_defun(pic, "read-u8", pic_port_read_byte); */
/* pic_defun(pic, "peek-u8", pic_port_peek_byte); */
/* pic_defun(pic, "u8-ready?", pic_port_byte_ready_p); */
/* pic_defun(pic, "read-bytevector", pic_port_read_blob); */
/* pic_defun(pic, "peek-bytevector!", pic_port_read_blob_ip); */
pic_defun(pic, "read-string", pic_port_read_string);
pic_defun(pic, "read-u8", pic_port_read_byte);
pic_defun(pic, "peek-u8", pic_port_peek_byte);
pic_defun(pic, "u8-ready?", pic_port_byte_ready_p);
pic_defun(pic, "read-bytevector", pic_port_read_blob);
pic_defun(pic, "read-bytevector!", pic_port_read_blob_ip);
/* output */
pic_defun(pic, "newline", pic_port_newline);
+4 -4
View File
@@ -53,7 +53,7 @@ read_label_set(int i, yyscan_t scanner)
val = pic_cons(pic, pic_none_value(), pic_none_value());
xh_put(&yylabels, i, &val);
xh_put_int(&yylabels, i, &val);
tmp = read(tok, scanner);
pic_pair_ptr(val)->car = pic_car(pic, tmp);
@@ -67,7 +67,7 @@ read_label_set(int i, yyscan_t scanner)
val = pic_obj_value(pic_vec_new(pic, 0));
xh_put(&yylabels, i, &val);
xh_put_int(&yylabels, i, &val);
tmp = pic_vec_ptr(read(tok, scanner));
SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data);
@@ -79,7 +79,7 @@ read_label_set(int i, yyscan_t scanner)
{
val = read(tok, scanner);
xh_put(&yylabels, i, &val);
xh_put_int(&yylabels, i, &val);
return val;
}
@@ -91,7 +91,7 @@ read_label_ref(int i, yyscan_t scanner)
{
xh_entry *e;
e = xh_get(&yylabels, i);
e = xh_get_int(&yylabels, i);
if (! e) {
error("label of given index not defined", scanner);
}
+4
View File
@@ -33,6 +33,9 @@
%option extra-type="struct parser_control *"
%option never-interactive
/* shebang */
shebang #!.*$
/* comment */
comment ;.*$
@@ -71,6 +74,7 @@ label #{uinteger}
[ \t\n\r] /* skip whitespace */
{comment} /* skip comment */
{shebang} /* skip shebang */
"#|" {
BEGIN(BLOCK_COMMENT);
+3 -3
View File
@@ -27,7 +27,7 @@ pic_intern(pic_state *pic, const char *str, size_t len)
id = pic->sym_cnt++;
xh_put(&pic->syms, cstr, &id);
xh_put(&pic->sym_names, id, &cstr);
xh_put_int(&pic->sym_names, id, &cstr);
return id;
}
@@ -50,7 +50,7 @@ pic_gensym(pic_state *pic, pic_sym base)
/* don't put the symbol to pic->syms to keep it uninterned */
uniq = pic->sym_cnt++;
xh_put(&pic->sym_names, uniq, &str);
xh_put_int(&pic->sym_names, uniq, &str);
return uniq;
}
@@ -64,7 +64,7 @@ pic_interned_p(pic_state *pic, pic_sym sym)
const char *
pic_symbol_name(pic_state *pic, pic_sym sym)
{
return xh_val(xh_get(&pic->sym_names, sym), const char *);
return xh_val(xh_get_int(&pic->sym_names, sym), const char *);
}
static pic_value
+42 -2
View File
@@ -19,6 +19,7 @@
#include "picrin/lib.h"
#include "picrin/macro.h"
#include "picrin/error.h"
#include "picrin/dict.h"
#define GET_OPERAND(pic,n) ((pic)->ci->fp[(n)])
@@ -33,6 +34,28 @@ pic_get_proc(pic_state *pic)
return pic_proc_ptr(v);
}
/**
* char type
* ---- ----
* o object
* i int
* I int with exactness
* f float
* F float with exactness
* s string object
* z c string
* m symbol
* v vector object
* b bytevector object
* c char
* l lambda object
* p port object
* d dictionary object
*
* | optional operator
* * variable length operator
*/
int
pic_get_args(pic_state *pic, const char *format, ...)
{
@@ -306,6 +329,23 @@ pic_get_args(pic_state *pic, const char *format, ...)
}
break;
}
case 'd': {
struct pic_dict **d;
pic_value v;
d = va_arg(ap, struct pic_dict **);
if (i < argc) {
v = GET_OPERAND(pic,i);
if (pic_dict_p(v)) {
*d = pic_dict_ptr(v);
}
else {
pic_error(pic, "pic_get_args, expected dictionary");
}
i++;
}
break;
}
default:
pic_error(pic, "pic_get_args: invalid argument specifier given");
}
@@ -339,7 +379,7 @@ global_ref(pic_state *pic, const char *name)
if (! pic_find_rename(pic, pic->lib->senv, sym, &rename)) {
return SIZE_MAX;
}
if (! (e = xh_get(&pic->global_tbl, rename))) {
if (! (e = xh_get_int(&pic->global_tbl, rename))) {
return SIZE_MAX;
}
return xh_val(e, size_t);
@@ -365,7 +405,7 @@ global_def(pic_state *pic, const char *name)
if (pic->glen >= pic->gcapa) {
pic_error(pic, "global table overflow");
}
xh_put(&pic->global_tbl, rename, &gidx);
xh_put_int(&pic->global_tbl, rename, &gidx);
return gidx;
}
+3
View File
@@ -338,6 +338,9 @@ write_core(struct writer_control *p, pic_value obj)
case PIC_TT_BOX:
xfprintf(file, "#<box %p>", pic_ptr(obj));
break;
case PIC_TT_DICT:
xfprintf(file, "#<dict %p>", pic_ptr(obj));
break;
}
}