switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
; -*- scheme -*-
|
2008-06-30 21:54:22 -04:00
|
|
|
; utilities for AST processing
|
|
|
|
|
|
|
|
(define (symconcat s1 s2)
|
2009-06-29 23:21:41 -04:00
|
|
|
(symbol (string s1 s2)))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (list-adjoin item lst)
|
|
|
|
(if (member item lst)
|
|
|
|
lst
|
|
|
|
(cons item lst)))
|
|
|
|
|
|
|
|
(define (index-of item lst start)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((null? lst) #f)
|
2008-06-30 21:54:22 -04:00
|
|
|
((eq item (car lst)) start)
|
2009-01-31 20:53:58 -05:00
|
|
|
(#t (index-of item (cdr lst) (+ start 1)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (each f l)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? l) l
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(begin (f (car l))
|
2008-06-30 21:54:22 -04:00
|
|
|
(each f (cdr l)))))
|
|
|
|
|
|
|
|
(define (maptree-pre f tr)
|
|
|
|
(let ((new-t (f tr)))
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (pair? new-t)
|
2008-06-30 21:54:22 -04:00
|
|
|
(map (lambda (e) (maptree-pre f e)) new-t)
|
|
|
|
new-t)))
|
|
|
|
|
|
|
|
(define (maptree-post f tr)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (not (pair? tr))
|
2008-06-30 21:54:22 -04:00
|
|
|
(f tr)
|
|
|
|
(let ((new-t (map (lambda (e) (maptree-post f e)) tr)))
|
|
|
|
(f new-t))))
|
|
|
|
|
2008-12-29 16:53:21 -05:00
|
|
|
(define (foldtree-pre f t zero)
|
|
|
|
(if (not (pair? t))
|
|
|
|
(f t zero)
|
|
|
|
(foldl t (lambda (e state) (foldtree-pre f e state)) (f t zero))))
|
|
|
|
|
|
|
|
(define (foldtree-post f t zero)
|
|
|
|
(if (not (pair? t))
|
|
|
|
(f t zero)
|
|
|
|
(f t (foldl t (lambda (e state) (foldtree-post f e state)) zero))))
|
|
|
|
|
|
|
|
; general tree transformer
|
|
|
|
; folds in preorder (foldtree-pre), maps in postorder (maptree-post)
|
|
|
|
; therefore state changes occur immediately, just by looking at the current node,
|
|
|
|
; while transformation follows evaluation order. this seems to be the most natural
|
|
|
|
; approach.
|
|
|
|
; (mapper tree state) - should return transformed tree given current state
|
|
|
|
; (folder tree state) - should return new state
|
|
|
|
(define (map&fold t zero mapper folder)
|
|
|
|
(let ((head (and (pair? t) (car t))))
|
|
|
|
(cond ((eq? head 'quote)
|
|
|
|
t)
|
|
|
|
((or (eq? head 'the) (eq? head 'meta))
|
|
|
|
(list head
|
|
|
|
(cadr t)
|
|
|
|
(map&fold (caddr t) zero mapper folder)))
|
|
|
|
(else
|
|
|
|
(let ((new-s (folder t zero)))
|
|
|
|
(mapper
|
|
|
|
(if (pair? t)
|
|
|
|
; head symbol is a tag; never transform it
|
|
|
|
(cons (car t)
|
|
|
|
(map (lambda (e) (map&fold e new-s mapper folder))
|
|
|
|
(cdr t)))
|
|
|
|
t)
|
|
|
|
new-s))))))
|
|
|
|
|
2009-03-17 17:53:55 -04:00
|
|
|
; convert to proper list, i.e. remove "dots", and append
|
|
|
|
(define (append.2 l tail)
|
|
|
|
(cond ((null? l) tail)
|
|
|
|
((atom? l) (cons l tail))
|
|
|
|
(#t (cons (car l) (append.2 (cdr l) tail)))))
|
|
|
|
|
|
|
|
; transform code by calling (f expr env) on each subexpr, where
|
|
|
|
; env is a list of lexical variables in effect at that point.
|
|
|
|
(define (lexical-walk f t)
|
|
|
|
(map&fold t () f
|
|
|
|
(lambda (tree state)
|
|
|
|
(if (and (eq? (car t) 'lambda)
|
|
|
|
(pair? (cdr t)))
|
|
|
|
(append.2 (cadr t) state)
|
|
|
|
state))))
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
; collapse forms like (&& (&& (&& (&& a b) c) d) e) to (&& a b c d e)
|
|
|
|
(define (flatten-left-op op e)
|
|
|
|
(maptree-post (lambda (node)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (and (pair? node)
|
2008-06-30 21:54:22 -04:00
|
|
|
(eq (car node) op)
|
2009-01-31 20:53:58 -05:00
|
|
|
(pair? (cdr node))
|
|
|
|
(pair? (cadr node))
|
2008-06-30 21:54:22 -04:00
|
|
|
(eq (caadr node) op))
|
|
|
|
(cons op
|
|
|
|
(append (cdadr node) (cddr node)))
|
|
|
|
node))
|
|
|
|
e))
|
|
|
|
|
|
|
|
; convert all local variable references to (lexref rib slot name)
|
|
|
|
; where rib is the nesting level and slot is the stack slot#
|
|
|
|
; name is just there for reference
|
|
|
|
; this assumes lambda is the only remaining naming form
|
|
|
|
(define (lookup-var v env lev)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? env) v
|
2008-06-30 21:54:22 -04:00
|
|
|
(let ((i (index-of v (car env) 0)))
|
|
|
|
(if i (list 'lexref lev i v)
|
|
|
|
(lookup-var v (cdr env) (+ lev 1))))))
|
|
|
|
(define (lvc- e env)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((symbol? e) (lookup-var e env 0))
|
|
|
|
((pair? e)
|
2008-06-30 21:54:22 -04:00
|
|
|
(if (eq (car e) 'quote)
|
|
|
|
e
|
2009-01-31 20:53:58 -05:00
|
|
|
(let* ((newvs (and (eq (car e) 'lambda) (cadr e)))
|
|
|
|
(newenv (if newvs (cons newvs env) env)))
|
|
|
|
(if newvs
|
|
|
|
(cons 'lambda
|
|
|
|
(cons (cadr e)
|
|
|
|
(map (lambda (se) (lvc- se newenv))
|
|
|
|
(cddr e))))
|
|
|
|
(map (lambda (se) (lvc- se env)) e)))))
|
|
|
|
(#t e)))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (lexical-var-conversion e)
|
|
|
|
(lvc- e ()))
|
|
|
|
|
|
|
|
; convert let to lambda
|
|
|
|
(define (let-expand e)
|
|
|
|
(maptree-post (lambda (n)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (and (pair? n) (eq (car n) 'let))
|
2008-06-30 21:54:22 -04:00
|
|
|
`((lambda ,(map car (cadr n)) ,@(cddr n))
|
|
|
|
,@(map cadr (cadr n)))
|
|
|
|
n))
|
|
|
|
e))
|
|
|
|
|
2008-12-29 16:53:21 -05:00
|
|
|
; alpha renaming
|
|
|
|
; transl is an assoc list ((old-sym-name . new-sym-name) ...)
|
|
|
|
(define (alpha-rename e transl)
|
|
|
|
(map&fold e
|
|
|
|
()
|
|
|
|
; mapper: replace symbol if unbound
|
|
|
|
(lambda (t env)
|
|
|
|
(if (symbol? t)
|
|
|
|
(let ((found (assq t transl)))
|
|
|
|
(if (and found
|
|
|
|
(not (memq t env)))
|
|
|
|
(cdr found)
|
|
|
|
t))
|
|
|
|
t))
|
|
|
|
; folder: add locals to environment if entering a new scope
|
|
|
|
(lambda (t env)
|
|
|
|
(if (and (pair? t) (or (eq? (car t) 'let)
|
|
|
|
(eq? (car t) 'lambda)))
|
|
|
|
(append (cadr t) env)
|
|
|
|
env))))
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
; flatten op with any associativity
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define-macro (flatten-all-op op e)
|
2008-06-30 21:54:22 -04:00
|
|
|
`(pattern-expand
|
|
|
|
(pattern-lambda (,op (-- l ...) (-- inner (,op ...)) (-- r ...))
|
|
|
|
(cons ',op (append l (cdr inner) r)))
|
|
|
|
,e))
|
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define-macro (pattern-lambda pat body)
|
2008-06-30 21:54:22 -04:00
|
|
|
(let* ((args (patargs pat))
|
|
|
|
(expander `(lambda ,args ,body)))
|
|
|
|
`(lambda (expr)
|
|
|
|
(let ((m (match ',pat expr)))
|
|
|
|
(if m
|
|
|
|
; matches; perform expansion
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(apply ,expander (map (lambda (var) (cdr (or (assq var m) '(0 . #f))))
|
2008-06-30 21:54:22 -04:00
|
|
|
',args))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
#f)))))
|