2006-08-16 21:55:53 -04:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2005-08-15 03:25:39 -04:00
|
|
|
IN: generic
|
2006-05-02 14:16:59 -04:00
|
|
|
USING: arrays errors generic hashtables kernel kernel-internals
|
2006-05-15 01:01:47 -04:00
|
|
|
math namespaces sequences words ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
PREDICATE: class math-class ( object -- ? )
|
|
|
|
dup null bootstrap-word eq? [
|
2006-08-07 01:17:04 -04:00
|
|
|
drop f
|
2006-08-16 21:55:53 -04:00
|
|
|
] [
|
|
|
|
number bootstrap-word class<
|
2006-08-07 01:17:04 -04:00
|
|
|
] if ;
|
2006-05-02 20:26:48 -04:00
|
|
|
|
2006-05-02 14:16:59 -04:00
|
|
|
: math-class-compare ( class class -- n )
|
|
|
|
[
|
2006-05-02 20:26:48 -04:00
|
|
|
dup math-class?
|
2006-05-05 21:41:57 -04:00
|
|
|
[ types last/first ] [ drop { 100 100 } ] if
|
2006-05-02 14:16:59 -04:00
|
|
|
] 2apply <=> ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: math-class-max ( class class -- class )
|
2006-05-02 14:16:59 -04:00
|
|
|
[ math-class-compare 0 > ] 2keep ? ;
|
|
|
|
|
|
|
|
: (math-upgrade) ( max class -- quot )
|
2006-05-17 19:44:30 -04:00
|
|
|
dupd = [
|
|
|
|
drop [ ]
|
|
|
|
] [
|
|
|
|
"coercer" word-prop [ [ ] ] unless*
|
|
|
|
] if ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
: math-upgrade ( class1 class2 -- quot )
|
2006-05-02 14:16:59 -04:00
|
|
|
[ math-class-max ] 2keep
|
|
|
|
>r over r> (math-upgrade)
|
2006-05-17 19:44:30 -04:00
|
|
|
>r (math-upgrade) dup empty? [ 1 make-dip ] unless
|
|
|
|
r> append ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
TUPLE: no-math-method left right generic ;
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: no-math-method ( left right generic -- * )
|
|
|
|
<no-math-method> throw ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: applicable-method ( generic class -- quot )
|
2006-08-17 23:08:04 -04:00
|
|
|
over method method-def
|
|
|
|
[ ] [ [ no-math-method ] curry ] ?if ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-09-16 02:39:33 -04:00
|
|
|
: object-method ( generic -- quot )
|
2005-11-24 19:02:20 -05:00
|
|
|
object bootstrap-word applicable-method ;
|
2005-09-16 02:39:33 -04:00
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
: math-method ( word class1 class2 -- quot )
|
2006-01-09 01:34:23 -05:00
|
|
|
2dup and [
|
2005-08-15 03:25:39 -04:00
|
|
|
2dup math-upgrade >r
|
|
|
|
math-class-max over order min-class applicable-method
|
|
|
|
r> swap append
|
|
|
|
] [
|
2005-09-16 02:39:33 -04:00
|
|
|
2drop object-method
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2006-05-02 14:16:59 -04:00
|
|
|
: math-vtable* ( picker max quot -- quot )
|
2005-08-15 03:25:39 -04:00
|
|
|
[
|
2006-05-02 14:16:59 -04:00
|
|
|
rot , \ tag ,
|
|
|
|
[ >r [ type>class ] map r> map % ] { } make ,
|
2005-08-15 03:25:39 -04:00
|
|
|
\ dispatch ,
|
2005-08-25 15:27:38 -04:00
|
|
|
] [ ] make ; inline
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2006-05-02 14:16:59 -04:00
|
|
|
: math-vtable ( picker quot -- quot )
|
|
|
|
num-tags swap math-vtable* ; inline
|
|
|
|
|
2006-05-02 01:49:52 -04:00
|
|
|
: math-combination ( word -- quot )
|
2005-08-15 03:25:39 -04:00
|
|
|
\ over [
|
2006-01-09 01:34:23 -05:00
|
|
|
dup math-class? [
|
2005-08-16 15:53:30 -04:00
|
|
|
\ dup [ >r 2dup r> math-method ] math-vtable
|
2005-08-15 03:25:39 -04:00
|
|
|
] [
|
2005-09-16 02:39:33 -04:00
|
|
|
over object-method
|
2005-09-24 15:21:17 -04:00
|
|
|
] if nip
|
2005-08-16 15:53:30 -04:00
|
|
|
] math-vtable nip ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
PREDICATE: generic 2generic ( word -- ? )
|
|
|
|
"combination" word-prop [ math-combination ] = ;
|