2005-08-19 21:46:12 -04:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-08-15 03:25:39 -04:00
|
|
|
IN: generic
|
|
|
|
USING: errors generic hashtables kernel kernel-internals lists
|
|
|
|
math namespaces sequences words ;
|
|
|
|
|
2005-08-16 15:53:30 -04:00
|
|
|
! Math combination for generic dyadic upgrading arithmetic.
|
|
|
|
|
2005-08-15 03:25:39 -04:00
|
|
|
: math-priority ( class -- n )
|
|
|
|
#! Non-number classes have the highest priority.
|
|
|
|
"math-priority" word-prop [ 100 ] unless* ;
|
|
|
|
|
|
|
|
: math-class< ( class class -- ? )
|
2005-09-16 20:49:24 -04:00
|
|
|
[ math-priority ] 2apply < ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: math-class-max ( class class -- class )
|
|
|
|
[ swap math-class< ] 2keep ? ;
|
|
|
|
|
|
|
|
: math-upgrade ( left right -- quot )
|
|
|
|
2dup math-class< [
|
2005-08-22 02:06:32 -04:00
|
|
|
nip "coercer" word-prop
|
|
|
|
dup [ [ >r ] swap [ r> ] append3 ] when
|
2005-08-15 03:25:39 -04:00
|
|
|
] [
|
|
|
|
2dup swap math-class< [
|
|
|
|
drop "coercer" word-prop
|
|
|
|
] [
|
|
|
|
2drop [ ]
|
2005-09-24 15:21:17 -04:00
|
|
|
] if
|
|
|
|
] if ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
TUPLE: no-math-method left right generic ;
|
|
|
|
|
|
|
|
: no-math-method ( left right generic -- )
|
2005-09-17 22:25:18 -04:00
|
|
|
3dup <no-math-method> throw ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: applicable-method ( generic class -- quot )
|
2005-09-25 20:41:49 -04:00
|
|
|
over "methods" word-prop hash
|
|
|
|
[ ] [ [ 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
|
|
|
|
2005-08-15 03:25:39 -04:00
|
|
|
: math-method ( word left right -- quot )
|
2005-09-16 20:49:24 -04:00
|
|
|
[ type>class ] 2apply 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
|
|
|
|
2005-08-16 15:53:30 -04:00
|
|
|
: math-vtable ( picker quot -- )
|
2005-08-15 03:25:39 -04:00
|
|
|
[
|
|
|
|
swap , \ tag ,
|
2005-10-29 23:25:38 -04:00
|
|
|
[ num-tags swap 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
|
|
|
|
|
|
|
: math-class? ( object -- ? )
|
2005-09-24 15:21:17 -04:00
|
|
|
dup word? [ "math-priority" word-prop ] [ drop f ] if ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: math-combination ( word -- vtable )
|
|
|
|
\ over [
|
|
|
|
dup type>class 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 ] = ;
|