Merge branch 'remove-undefinedp'

This commit is contained in:
Yuichi Nishiwaki 2015-07-18 15:33:42 +09:00
commit b7bb3fa1cf
9 changed files with 114 additions and 136 deletions

View File

@ -44,11 +44,11 @@
((wrap (let ((register (make-register)))
(lambda (var)
(let ((id (register var)))
(if (undefined? id)
(if id
(cdr id)
(let ((id (make-identifier var env)))
(register var id)
id)
id)))))
id))))))
(walk (lambda (f form)
(cond
((variable? form)
@ -106,11 +106,11 @@
((rename (let ((register (make-register)))
(lambda (var)
(let ((id (register var)))
(if (undefined? id)
(if id
(cdr id)
(let ((id (make-identifier var mac-env)))
(register var id)
id)
id)))))
id))))))
(compare (lambda (x y)
(variable=?
(make-identifier x use-env)
@ -124,25 +124,25 @@
(letrec
((inject (lambda (var1)
(let ((var2 (register1 var1)))
(if (undefined? var2)
(if var2
(cdr var2)
(let ((var2 (make-identifier var1 use-env)))
(register1 var1 var2)
(register2 var2 var1)
var2)
var2))))
var2)))))
(rename (let ((register (make-register)))
(lambda (var)
(let ((id (register var)))
(if (undefined? id)
(if id
(cdr id)
(let ((id (make-identifier var mac-env)))
(register var id)
id)
id)))))
id))))))
(flip (lambda (var2) ; unwrap if injected, wrap if not injected
(let ((var1 (register2 var2)))
(if (undefined? var1)
(rename var2)
var1))))
(if var1
(cdr var1)
(rename var2)))))
(walk (lambda (f form)
(cond
((variable? form)

View File

@ -350,11 +350,12 @@
(letrec
((#,'rename (let ((reg (make-register)))
(lambda (x)
(if (undefined? (reg x))
(let ((y (reg x)))
(if y
(cdr y)
(let ((id (make-identifier x env)))
(reg x id)
id)
(reg x))))))
id)))))))
(lambda #,'it
#,(compile-rules rules))))))

View File

@ -17,9 +17,9 @@
(letrec ((setter
(lambda (proc)
(let ((setter (dictionary-ref (attribute proc) '@@setter)))
(if (undefined? setter)
(error "no setter found")
setter))))
(if setter
(cdr setter)
(error "no setter found")))))
(set-setter!
(lambda (proc setter)
(dictionary-set! (attribute proc) '@@setter setter))))

View File

@ -117,7 +117,7 @@ Symbol-to-object hash table.
- **(dictionary-ref dict key)**
Look up dictionary dict for a value associated with key. If dict has a slot for key `key`, the value stored in the slot is returned. Otherwise `#undefined` is returned.
Look up dictionary dict for a value associated with key. If dict has a slot for key `key`, a pair containing the key object and the associated value is returned. Otherwise `#f` is returned.
- **(dictionary-set! dict key obj)**

View File

@ -489,17 +489,17 @@ my $src = <<'EOL';
(letrec
((wrap (lambda (var1)
(let ((var2 (register1 var1)))
(if (undefined? var2)
(if var2
(cdr var2)
(let ((var2 (make-identifier var1 env)))
(register1 var1 var2)
(register2 var2 var1)
var2)
var2))))
var2)))))
(unwrap (lambda (var2)
(let ((var1 (register2 var2)))
(if (undefined? var1)
var2
var1))))
(if var1
(cdr var1)
var2))))
(walk (lambda (f form)
(cond
((variable? form)
@ -923,85 +923,85 @@ const char pic_boot[][80] = {
"ap cdr renames)\n ,body))))))\n\n(define (transformer f)\n (lambda (form",
" env)\n (let ((register1 (make-register))\n (register2 (make-register)",
"))\n (letrec\n ((wrap (lambda (var1)\n (let ((var2 ",
"(register1 var1)))\n (if (undefined? var2)\n ",
" (let ((var2 (make-identifier var1 env)))\n (regi",
"ster1 var1 var2)\n (register2 var2 var1)\n ",
" var2)\n var2))))\n (unwrap (lambda ",
"(var2)\n (let ((var1 (register2 var2)))\n ",
" (if (undefined? var1)\n var2\n ",
" var1))))\n (walk (lambda (f form)\n (cond\n ",
" ((variable? form)\n (f form))\n ",
"((pair? form)\n (cons (walk f (car form)) (walk f (cdr form))",
"))\n ((vector? form)\n (list->vector (walk ",
"f (vector->list form))))\n (else\n form))))",
")\n (let ((form (cdr form)))\n (walk unwrap (apply f (walk wrap fo",
"rm))))))))\n\n(define-macro define-syntax\n (lambda (form env)\n (let ((formal (",
"car (cdr form)))\n (body (cdr (cdr form))))\n (if (pair? formal)\n ",
" `(,(the 'define-syntax) ,(car formal) (,the-lambda ,(cdr formal) ,@body",
"))\n `(,the-define-macro ,formal (,(the 'transformer) (,the-begin ,@body",
")))))))\n\n(define-macro letrec-syntax\n (lambda (form env)\n (let ((formal (car",
" (cdr form)))\n (body (cdr (cdr form))))\n `(let ()\n ,@(ma",
"p (lambda (x)\n `(,(the 'define-syntax) ,(car x) ,(cadr x)))\n ",
" formal)\n ,@body))))\n\n(define-macro let-syntax\n (lambda (fo",
"rm env)\n `(,(the 'letrec-syntax) ,@(cdr form))))\n\n\n;;; library primitives\n\n(d",
"efine-macro define-library\n (lambda (form _)\n (let ((name (cadr form))\n ",
" (body (cddr form)))\n (let ((old-library (current-library))\n ",
" (new-library (or (find-library name) (make-library name))))\n (let ((env ",
"(library-environment new-library)))\n (current-library new-library)\n ",
" (for-each (lambda (expr) (eval expr env)) body)\n (current-library",
" old-library))))))\n\n(define-macro cond-expand\n (lambda (form _)\n (letrec\n ",
" ((test (lambda (form)\n (or\n (eq? form 'els",
"e)\n (and (symbol? form)\n (memq form (feat",
"ures)))\n (and (pair? form)\n (case (car fo",
"rm)\n ((library) (find-library (cadr form)))\n ",
" ((not) (not (test (cadr form))))\n ((and) (l",
"et loop ((form (cdr form)))\n (or (null? form)\n ",
" (and (test (car form)) (loop (cdr form)))))",
")\n ((or) (let loop ((form (cdr form)))\n ",
" (and (pair? form)\n (or (tes",
"t (car form)) (loop (cdr form))))))\n (else #f)))))))\n ",
" (let loop ((clauses (cdr form)))\n (if (null? clauses)\n #und",
"efined\n (if (test (caar clauses))\n `(,the-begin ,@(cda",
"r clauses))\n (loop (cdr clauses))))))))\n\n(define-macro import\n (",
"lambda (form _)\n (let ((caddr\n (lambda (x) (car (cdr (cdr x)))))\n ",
" (prefix\n (lambda (prefix symbol)\n (string->symbol\n",
" (string-append\n (symbol->string prefix)\n ",
" (symbol->string symbol))))))\n (letrec\n ((extract\n (l",
"ambda (spec)\n (case (car spec)\n ((only rename prefix",
" except)\n (extract (cadr spec)))\n (else\n ",
" (or (find-library spec) (error \"library not found\" spec))))))\n ",
" (collect\n (lambda (spec)\n (case (car spec)\n ",
" ((only)\n (let ((alist (collect (cadr spec))))\n ",
" (map (lambda (var) (assq var alist)) (cddr spec))))\n ((renam",
"e)\n (let ((alist (collect (cadr spec)))\n (",
"renames (map (lambda (x) `((car x) . (cadr x))) (cddr spec))))\n ",
" (map (lambda (s) (or (assq (car s) renames) s)) alist)))\n ((pre",
"fix)\n (let ((alist (collect (cadr spec))))\n (m",
"ap (lambda (s) (cons (prefix (caddr spec) (car s)) (cdr s))) alist)))\n ",
" ((except)\n (let ((alist (collect (cadr spec))))\n ",
" (let loop ((alist alist))\n (if (null? alist)\n ",
" '()\n (if (memq (caar alist) (cddr spe",
"c))\n (loop (cdr alist))\n ",
" (cons (car alist) (loop (cdr alist))))))))\n (else\n ",
" (let ((lib (or (find-library spec) (error \"library not found\" spec))))\n ",
" (map (lambda (x) (cons x x)) (library-exports lib))))))))\n (",
"letrec\n ((import\n (lambda (spec)\n (let ",
"((lib (extract spec))\n (alist (collect spec)))\n ",
" (for-each\n (lambda (slot)\n (libr",
"ary-import lib (cdr slot) (car slot)))\n alist)))))\n ",
"(for-each import (cdr form)))))))\n\n(define-macro export\n (lambda (form _)\n (",
"letrec\n ((collect\n (lambda (spec)\n (cond\n ",
" ((symbol? spec)\n `(,spec . ,spec))\n ((and (list? spec)",
" (= (length spec) 3) (eq? (car spec) 'rename))\n `(,(list-ref spec 1",
") . ,(list-ref spec 2)))\n (else\n (error \"malformed expo",
"rt\")))))\n (export\n (lambda (spec)\n (let ((slot (co",
"llect spec)))\n (library-export (car slot) (cdr slot))))))\n (f",
"or-each export (cdr form)))))\n\n(export define lambda quote set! if begin define-",
"macro\n let let* letrec letrec*\n let-values let*-values define-valu",
"es\n quasiquote unquote unquote-splicing\n and or\n cond case ",
"else =>\n do when unless\n parameterize\n define-syntax\n ",
" syntax-quote syntax-unquote\n syntax-quasiquote syntax-unquote-splicing\n",
" let-syntax letrec-syntax\n syntax-error)\n\n\n",
"(register1 var1)))\n (if var2\n (cdr v",
"ar2)\n (let ((var2 (make-identifier var1 env)))\n ",
" (register1 var1 var2)\n (register2 va",
"r2 var1)\n var2)))))\n (unwrap (lambda (var2)\n",
" (let ((var1 (register2 var2)))\n (if v",
"ar1\n (cdr var1)\n var2))))\n ",
" (walk (lambda (f form)\n (cond\n ((v",
"ariable? form)\n (f form))\n ((pair? form)\n",
" (cons (walk f (car form)) (walk f (cdr form))))\n ",
" ((vector? form)\n (list->vector (walk f (vector->lis",
"t form))))\n (else\n form)))))\n (let",
" ((form (cdr form)))\n (walk unwrap (apply f (walk wrap form))))))))\n\n(d",
"efine-macro define-syntax\n (lambda (form env)\n (let ((formal (car (cdr form)",
"))\n (body (cdr (cdr form))))\n (if (pair? formal)\n `(,(t",
"he 'define-syntax) ,(car formal) (,the-lambda ,(cdr formal) ,@body))\n `",
"(,the-define-macro ,formal (,(the 'transformer) (,the-begin ,@body)))))))\n\n(defi",
"ne-macro letrec-syntax\n (lambda (form env)\n (let ((formal (car (cdr form)))\n",
" (body (cdr (cdr form))))\n `(let ()\n ,@(map (lambda (x)\n",
" `(,(the 'define-syntax) ,(car x) ,(cadr x)))\n f",
"ormal)\n ,@body))))\n\n(define-macro let-syntax\n (lambda (form env)\n `(",
",(the 'letrec-syntax) ,@(cdr form))))\n\n\n;;; library primitives\n\n(define-macro de",
"fine-library\n (lambda (form _)\n (let ((name (cadr form))\n (body (cd",
"dr form)))\n (let ((old-library (current-library))\n (new-library ",
"(or (find-library name) (make-library name))))\n (let ((env (library-envir",
"onment new-library)))\n (current-library new-library)\n (for-eac",
"h (lambda (expr) (eval expr env)) body)\n (current-library old-library))",
"))))\n\n(define-macro cond-expand\n (lambda (form _)\n (letrec\n ((test (l",
"ambda (form)\n (or\n (eq? form 'else)\n ",
" (and (symbol? form)\n (memq form (features)))\n ",
" (and (pair? form)\n (case (car form)\n ",
" ((library) (find-library (cadr form)))\n (",
"(not) (not (test (cadr form))))\n ((and) (let loop ((form",
" (cdr form)))\n (or (null? form)\n ",
" (and (test (car form)) (loop (cdr form))))))\n ",
" ((or) (let loop ((form (cdr form)))\n ",
" (and (pair? form)\n (or (test (car form)) ",
"(loop (cdr form))))))\n (else #f)))))))\n (let loop (",
"(clauses (cdr form)))\n (if (null? clauses)\n #undefined\n ",
" (if (test (caar clauses))\n `(,the-begin ,@(cdar clauses))\n ",
" (loop (cdr clauses))))))))\n\n(define-macro import\n (lambda (form _",
")\n (let ((caddr\n (lambda (x) (car (cdr (cdr x)))))\n (prefi",
"x\n (lambda (prefix symbol)\n (string->symbol\n ",
"(string-append\n (symbol->string prefix)\n (symbol->st",
"ring symbol))))))\n (letrec\n ((extract\n (lambda (spec)\n ",
" (case (car spec)\n ((only rename prefix except)\n ",
" (extract (cadr spec)))\n (else\n (or (f",
"ind-library spec) (error \"library not found\" spec))))))\n (collect\n ",
" (lambda (spec)\n (case (car spec)\n ((only)\n ",
" (let ((alist (collect (cadr spec))))\n (map (lam",
"bda (var) (assq var alist)) (cddr spec))))\n ((rename)\n ",
" (let ((alist (collect (cadr spec)))\n (renames (map (",
"lambda (x) `((car x) . (cadr x))) (cddr spec))))\n (map (lambda",
" (s) (or (assq (car s) renames) s)) alist)))\n ((prefix)\n ",
" (let ((alist (collect (cadr spec))))\n (map (lambda (s)",
" (cons (prefix (caddr spec) (car s)) (cdr s))) alist)))\n ((except",
")\n (let ((alist (collect (cadr spec))))\n (let ",
"loop ((alist alist))\n (if (null? alist)\n ",
" '()\n (if (memq (caar alist) (cddr spec))\n ",
" (loop (cdr alist))\n (cons (car al",
"ist) (loop (cdr alist))))))))\n (else\n (let ((lib ",
"(or (find-library spec) (error \"library not found\" spec))))\n (",
"map (lambda (x) (cons x x)) (library-exports lib))))))))\n (letrec\n ",
" ((import\n (lambda (spec)\n (let ((lib (extract",
" spec))\n (alist (collect spec)))\n (for-e",
"ach\n (lambda (slot)\n (library-import lib",
" (cdr slot) (car slot)))\n alist)))))\n (for-each impo",
"rt (cdr form)))))))\n\n(define-macro export\n (lambda (form _)\n (letrec\n ",
" ((collect\n (lambda (spec)\n (cond\n ((symbol? spe",
"c)\n `(,spec . ,spec))\n ((and (list? spec) (= (length sp",
"ec) 3) (eq? (car spec) 'rename))\n `(,(list-ref spec 1) . ,(list-ref",
" spec 2)))\n (else\n (error \"malformed export\")))))\n ",
" (export\n (lambda (spec)\n (let ((slot (collect spec)))\n",
" (library-export (car slot) (cdr slot))))))\n (for-each export",
" (cdr form)))))\n\n(export define lambda quote set! if begin define-macro\n ",
"let let* letrec letrec*\n let-values let*-values define-values\n qua",
"siquote unquote unquote-splicing\n and or\n cond case else =>\n ",
" do when unless\n parameterize\n define-syntax\n syntax-quote",
" syntax-unquote\n syntax-quasiquote syntax-unquote-splicing\n let-sy",
"ntax letrec-syntax\n syntax-error)\n\n\n",
"",
""
};

View File

@ -116,9 +116,9 @@ pic_dict_dictionary_ref(pic_state *pic)
pic_get_args(pic, "dm", &dict, &key);
if (! pic_dict_has(pic, dict, key)) {
return pic_undef_value();
return pic_false_value();
}
return pic_dict_ref(pic, dict, key);
return pic_cons(pic, pic_obj_value(key), pic_dict_ref(pic, dict, key));
}
static pic_value

View File

@ -66,9 +66,9 @@ static pic_value
reg_get(pic_state *pic, struct pic_reg *reg, void *key)
{
if (! pic_reg_has(pic, reg, key)) {
return pic_undef_value();
return pic_false_value();
}
return pic_reg_ref(pic, reg, key);
return pic_cons(pic, pic_obj_value(key), pic_reg_ref(pic, reg, key));
}
static pic_value

View File

@ -18,7 +18,6 @@ pic_add_feature(pic_state *pic, const char *feature)
pic_push(pic, pic_obj_value(pic_intern(pic, feature)), pic->features);
}
void pic_init_undef(pic_state *);
void pic_init_bool(pic_state *);
void pic_init_pair(pic_state *);
void pic_init_port(pic_state *);
@ -162,7 +161,6 @@ pic_init_core(pic_state *pic)
VM(pic->uGT, ">");
VM(pic->uGE, ">=");
pic_init_undef(pic); DONE;
pic_init_bool(pic); DONE;
pic_init_pair(pic); DONE;
pic_init_port(pic); DONE;

View File

@ -1,21 +0,0 @@
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
static pic_value
pic_undef_undefined_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_undef_p(v) ? pic_true_value() : pic_false_value();
}
void
pic_init_undef(pic_state *pic)
{
pic_defun(pic, "undefined?", pic_undef_undefined_p);
}