factor/library/syntax/generic.factor

51 lines
1.5 KiB
Factor
Raw Normal View History

2005-02-18 20:37:01 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
! Bootstrapping trick; see doc/bootstrap.txt.
IN: !syntax
USING: arrays generic kernel lists namespaces parser sequences
syntax words ;
2005-02-18 20:37:01 -05:00
: GENERIC:
2005-08-22 14:29:43 -04:00
#! GENERIC: bar == G: bar simple-combination ;
2005-08-23 15:50:32 -04:00
CREATE dup reset-word define-generic ; parsing
2005-02-18 20:37:01 -05:00
: G:
2005-08-22 14:29:43 -04:00
#! G: word combination ;
2005-08-23 15:50:32 -04:00
CREATE dup reset-word [ define-generic* ] [ ] ; parsing
2005-02-18 20:37:01 -05:00
: UNION: ( -- class predicate definition )
#! Followed by a class name, then a list of union members.
CREATE
dup intern-symbol
dup predicate-word
[ dupd unit "predicate" set-word-prop ] keep
2005-02-18 20:37:01 -05:00
[ define-union ] [ ] ; parsing
: PREDICATE: ( -- class predicate definition )
#! Followed by a superclass name, then a class name.
scan-word
CREATE dup intern-symbol
dup rot "superclass" set-word-prop
2005-02-18 20:37:01 -05:00
dup predicate-word
[ define-predicate-class ] [ ] ; parsing
2005-02-18 20:37:01 -05:00
: TUPLE:
#! Followed by a tuple name, then slot names, then ;
scan
string-mode on
[ string-mode off define-tuple ]
f ; parsing
: M: ( -- class generic [ ] )
#! M: foo bar begins a definition of the bar generic word
#! specialized to the foo type.
scan-word scan-word [ -rot define-method ] [ ] ; parsing
2005-02-18 20:37:01 -05:00
: C:
#! Followed by a tuple name, then constructor code, then ;
#! Constructor code executes with the empty tuple on the
#! stack.
scan-word [ tuple-constructor ] keep
[ define-constructor ] [ ] ; parsing