factor/library/alien/structs.factor

59 lines
1.7 KiB
Factor
Raw Normal View History

2005-04-09 18:30:46 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: alien
2005-04-09 22:43:41 -04:00
USING: assembler compiler errors generic hashtables kernel lists
math namespaces parser sequences strings words ;
2005-04-09 18:30:46 -04:00
! Some code for interfacing with C structures.
: define-getter ( offset type name -- )
#! Define a word with stack effect ( alien -- obj ) in the
#! current 'in' vocabulary.
create-in >r
[ "getter" get ] bind cons r> swap define-compound ;
: define-setter ( offset type name -- )
#! Define a word with stack effect ( obj alien -- ) in the
#! current 'in' vocabulary.
2005-05-18 16:26:22 -04:00
"set-" swap append create-in >r
2005-04-09 18:30:46 -04:00
[ "setter" get ] bind cons r> swap define-compound ;
: define-field ( offset type name -- offset )
>r c-type dup >r [ "align" get ] bind align r> r>
2005-05-18 16:26:22 -04:00
"struct-name" get swap "-" swap append3
2005-04-09 18:30:46 -04:00
( offset type name -- )
3dup define-getter 3dup define-setter
drop [ "width" get ] bind + ;
: define-member ( max type -- max )
c-type [ "width" get ] bind max ;
: define-struct-type ( width -- )
#! Define inline and pointer type for the struct. Pointer
#! type is exactly like void*.
2005-04-16 00:23:27 -04:00
[
"width" set
2005-04-17 18:34:09 -04:00
cell "align" set
2005-04-16 00:23:27 -04:00
[ swap <displaced-alien> ] "getter" set
]
"struct-name" get define-c-type
"struct-name" get "in" get init-c-type ;
2005-04-09 18:30:46 -04:00
: BEGIN-STRUCT: ( -- offset )
scan "struct-name" set 0 ; parsing
: FIELD: ( offset -- offset )
scan scan define-field ; parsing
: END-STRUCT ( length -- )
2005-04-09 22:43:41 -04:00
define-struct-type ; parsing
2005-04-09 18:30:46 -04:00
: BEGIN-UNION: ( -- max )
scan "struct-name" set 0 ; parsing
: MEMBER: ( max -- max )
scan define-member ; parsing
: END-UNION ( max -- )
2005-04-09 22:43:41 -04:00
define-struct-type ; parsing