factor/core/quotations/quotations.factor

83 lines
1.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2013-03-05 13:34:47 -05:00
USING: accessors arrays kernel kernel.private math sequences
sequences.private slots.private ;
2007-09-20 18:09:08 -04:00
IN: quotations
BUILTIN: quotation
{ array array read-only initial: { } }
cached-effect
cache-counter ;
PRIMITIVE: jit-compile ( quot -- )
PRIMITIVE: quotation-code ( quot -- start end )
PRIMITIVE: quotation-compiled? ( quot -- ? )
<PRIVATE
PRIMITIVE: array>quotation ( array -- quot )
: uncurry ( curry -- obj quot )
{ curry } declare dup 2 slot swap 3 slot ; inline
: uncompose ( compose -- quot quot2 )
{ compose } declare dup 2 slot swap 3 slot ; inline
PRIVATE>
M: quotation call (call) ;
M: curry call uncurry call ;
2009-05-10 17:39:17 -04:00
M: compose call uncompose [ call ] dip call ;
2007-09-20 18:09:08 -04:00
M: wrapper equal?
2012-07-21 13:22:44 -04:00
over wrapper? [ [ wrapped>> ] same? ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
UNION: callable quotation curry compose ;
2007-09-20 18:09:08 -04:00
M: callable equal?
over callable? [ sequence= ] [ 2drop f ] if ;
M: quotation length array>> length ;
2007-09-20 18:09:08 -04:00
M: quotation nth-unsafe array>> nth-unsafe ;
2007-09-20 18:09:08 -04:00
: >quotation ( seq -- quot )
>array array>quotation ; inline
M: callable like drop dup quotation? [ >quotation ] unless ;
2007-09-20 18:09:08 -04:00
INSTANCE: quotation immutable-sequence
: 1quotation ( obj -- quot ) 1array array>quotation ;
2007-09-20 18:09:08 -04:00
GENERIC: literalize ( obj -- wrapped )
M: object literalize ;
M: wrapper literalize <wrapper> ;
M: curry length quot>> length 1 + ;
2007-09-20 18:09:08 -04:00
M: curry nth
over 0 =
[ nip obj>> literalize ]
[ [ 1 - ] dip quot>> nth ]
if ;
2007-09-20 18:09:08 -04:00
INSTANCE: curry immutable-sequence
M: compose length
[ first>> length ] [ second>> length ] bi + ;
M: compose virtual-exemplar first>> ;
M: compose virtual@
2dup first>> length < [
first>>
] [
[ first>> length - ] [ second>> ] bi
] if ;
INSTANCE: compose virtual-sequence