deparse actually sorta working
[scpubgit/DX.git] / lib / DX / Utils.pm
1 package DX::Utils;
2
3 use strictures 2;
4 use Exporter 'import';
5
6 my @const = (
7   my @dep_types = qw(EXISTENCE_OF INDICES_OF TYPE_OF CONTENTS_OF),
8   my @ev_types = qw(VALUE_SET VALUE_EXISTS),
9 );
10
11 our @EXPORT_OK = (
12   @const,
13   (my @builders = qw(step string number dict proposition)),
14   'deparse',
15 );
16
17 our %EXPORT_TAGS = (
18   all => \@EXPORT_OK,
19   dep_types => \@dep_types,
20   event_types => \@ev_types,
21   builders => \@builders,
22 );
23
24 require constant;
25
26 # use constant INDICES_OF => \*INDICES_OF;
27
28 constant->import(+{
29   map {; no strict 'refs'; $_ => \*$_ } @const
30 });
31
32 # $INDICES_OF = 1, ...
33
34 do { no strict 'refs'; ${$dep_types[$_-1]} = $_ } for 1..@dep_types;
35
36 # VALUE_EXISTS needs to trigger indices checks on its parent
37
38 our $VALUE_EXISTS = 1;
39
40 # VALUE_EXISTS triggers all types, VALUE_SET all but EXISTENCE_OF
41
42 our @VALUE_EXISTS = (EXISTENCE_OF(), INDICES_OF(), TYPE_OF(), CONTENTS_OF());
43 our @VALUE_SET = (INDICES_OF(), TYPE_OF(), CONTENTS_OF());
44
45 sub step {
46   DX::Step::Normal->new(@_);
47 }
48
49 sub string {
50   DX::Value::String->new(string_value => $_[0])
51 }
52
53 sub number {
54   DX::Value::Number->new(number_value => $_[0]);
55 }
56
57 sub dict {
58   DX::Value::Dict->new(
59     members => { @_ },
60   );
61 }
62
63 sub proposition {
64   my ($pred, @args) = @_;
65   DX::Proposition->new(
66     predicate => $pred,
67     args => \@args,
68   );
69 }
70
71 {
72   require DX::Deparse;
73   my $dp = DX::Deparse->new;
74
75   sub deparse {
76     my ($thing) = @_;
77     $dp->fmt($thing);
78   }
79 }
80
81 # Here so that circular require doesn't stab us in the face
82
83 require DX::Step::Normal;
84 require DX::Value::String;
85 require DX::Value::Number;
86 require DX::Value::Dict;
87 require DX::Proposition;
88
89 1;