]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/prettyprint/prettyprint.factor
ded8f692cdf874da97dabefe3f57d2aab4c6eb19
[factor.git] / basis / alien / prettyprint / prettyprint.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel combinators alien alien.strings alien.c-types
4 alien.parser alien.syntax arrays assocs effects math.parser
5 prettyprint.backend prettyprint.custom prettyprint.sections
6 definitions see see.private sequences strings words ;
7 IN: alien.prettyprint
8
9 M: alien pprint*
10     {
11         { [ dup expired? ] [ drop \ BAD-ALIEN pprint-word ] }
12         { [ dup pinned-c-ptr? not ] [ drop "( displaced alien )" text ] }
13         [ \ ALIEN: [ alien-address >hex text ] pprint-prefix ]
14     } cond ;
15
16 M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ;
17
18 M: c-type-word definer drop \ C-TYPE: f ;
19 M: c-type-word definition drop f ;
20 M: c-type-word declarations. drop ;
21
22 GENERIC: pprint-c-type ( c-type -- )
23 M: word pprint-c-type pprint-word ;
24 M: wrapper pprint-c-type wrapped>> pprint-word ;
25 M: string pprint-c-type text ;
26 M: array pprint-c-type pprint* ;
27
28 M: typedef-word definer drop \ TYPEDEF: f ;
29
30 M: typedef-word synopsis*
31     {
32         [ seeing-word ]
33         [ definer. ]
34         [ "c-type" word-prop pprint-c-type ]
35         [ pprint-word ]
36     } cleave ;
37
38 : pprint-function-arg ( type name -- )
39     [ pprint-c-type ] [ text ] bi* ;
40
41 : pprint-function-args ( types names -- )
42     zip [ ] [
43         unclip-last
44         [ [ first2 "," append pprint-function-arg ] each ] dip
45         first2 pprint-function-arg
46     ] if-empty ;
47
48 : pprint-library ( library -- )
49     [ \ LIBRARY: [ text ] pprint-prefix ] when* ;
50
51 M: alien-function-word definer
52     drop \ FUNCTION: \ ; ;
53 M: alien-function-word definition drop f ;
54 M: alien-function-word synopsis*
55     {
56         [ seeing-word ]
57         [ def>> second pprint-library ]
58         [ definer. ]
59         [ def>> first pprint-c-type ]
60         [ pprint-word ]
61         [
62             <block "(" text
63             [ def>> fourth ] [ stack-effect in>> ] bi
64             pprint-function-args
65             ")" text block>
66         ]
67     } cleave ;
68
69 M: alien-callback-type-word definer
70     drop \ CALLBACK: \ ; ;
71 M: alien-callback-type-word definition drop f ;
72 M: alien-callback-type-word synopsis*
73     {
74         [ seeing-word ]
75         [ "callback-library" word-prop pprint-library ]
76         [ definer. ]
77         [ def>> first pprint-c-type ]
78         [ pprint-word ]
79         [
80             <block "(" text 
81             [ def>> second ] [ "callback-effect" word-prop in>> ] bi
82             pprint-function-args
83             ")" text block>
84         ]
85     } cleave ;