1 package DX::ShellSession;
4 use Scalar::Util qw(weaken);
7 use DX::ActionBuilder::Normal;
8 use DX::RuleDefinitionContext;
9 use DX::Utils qw(deparse);
10 use Types::Standard qw(InstanceOf);
13 has shell_state => (is => 'rwp', required => 1, isa => ShellState);
16 is => 'lazy', builder => sub { DX::Expander->new(tcl => $_[0]->tcl) },
17 isa => Expander, handles => [ qw(expand_args) ],
20 has tcl => (is => 'lazy', isa => InstanceOf['Tcl'], builder => sub {
24 $tcl->CreateCommand('?' => sub {
25 $self->apply_to_state([ 'new_query_state' ], [ mode => 'query' ]);
27 $tcl->CreateCommand('?+' => sub {
28 $self->apply_to_state([ mode => 'query' ]);
31 $tcl->CreateCommand('.' => sub {
32 $self->apply_to_state([ mode => 'shell' ]);
35 $tcl->CreateCommand('...' => sub {
36 $self->apply_to_state([ mode => 'shell' ]);
37 my $orig = my $cur = $self->shell_state->current_query_state;
39 $self->_set_shell_state(
40 $self->shell_state->but(current_query_state => $cur)
43 $cur = eval { $cur->with_forced_backtrack };
44 push our @Result, [ output => $@ ] if $@;
46 $self->_set_shell_state(
47 $self->shell_state->but(
48 current_query_state => $orig
53 $tcl->CreateCommand(qlist => sub {
54 push our @Result, map [ output => $_ ], @{
55 $self->shell_state->current_query_state->proposition_sequence->members
59 $tcl->CreateCommand(qvars => $qvars = sub {
60 my $locals = $self->shell_state->current_query_state->search_state
61 ->current_hypothesis->scope->locals->[0];
62 push our @Result, [ output => $locals ];
65 $tcl->CreateCommand(qdeps => sub {
66 my $rps = $self->shell_state->current_query_state->search_state
67 ->current_hypothesis->resolved_propositions;
68 push our @Result, [ output => $rps ];
71 $tcl->CreateCommand(qact => sub {
72 my $act = $self->shell_state->current_query_state->search_state
73 ->current_hypothesis->actions;
74 push our @Result, map [ output => $_ ], @$act;
78 keys %{$self->shell_state->template_query_state->predicates}
80 $tcl->CreateCommand($pred => sub {
81 my (undef, undef, undef, @args) = @_; # ($data, $interp, $pred)
82 (our $Shell_Context)->apply_predicate($pred => @args);
85 $tcl->CreateCommand(const => sub {
86 my (undef, undef, undef, $name, $value) = @_;
87 my $tqs = $self->shell_state->template_query_state;
88 my $new_tqs = $tqs->but(
89 globals => $tqs->globals->with_member_at(
90 $name => DX::ActionBuilder::Null->new(
91 target_path => [ $name ],
92 )->apply_to_value($self->expand_args($value)),
95 $self->_set_shell_state(
96 $self->shell_state->but(
97 template_query_state => $new_tqs
101 $tcl->CreateCommand(state => sub {
102 my (undef, undef, undef, $name, $value) = @_;
103 my $tqs = $self->shell_state->template_query_state;
104 my $new_tqs = $tqs->but(
105 globals => $tqs->globals->with_member_at(
106 $name => DX::ActionBuilder::Normal->new(
107 target_path => [ $name ],
108 )->apply_to_value($self->expand_args($value)),
111 $self->_set_shell_state(
112 $self->shell_state->but(
113 template_query_state => $new_tqs
117 $tcl->CreateCommand(trace => sub {
118 my (undef, undef, undef, @trace) = @_;
119 $self->_set_shell_state(
120 $self->shell_state->with_trace_changes(@trace)
123 $tcl->CreateCommand(rule => sub {
124 my (undef, undef, undef, $pred, $args, $body) = @_;
125 local our $Shell_Context = DX::RuleDefinitionContext->new(
126 arg_names => [ $self->tcl->SplitList($args) ],
127 expander => $self->expander,
129 $self->tcl->Eval($body);
130 my $rule = $Shell_Context->bake_rule;
131 my $tqs = $self->shell_state->template_query_state;
132 my $new_tqs = $tqs->but(
138 $self->_set_shell_state(
139 $self->shell_state->but(
140 template_query_state => $new_tqs
143 $self->tcl->CreateCommand($pred => sub {
144 my (undef, undef, undef, @args) = @_; # ($data, $interp, $pred)
145 (our $Shell_Context)->apply_predicate($pred => @args);
152 my ($self, @to_apply) = @_;
153 my $state = $self->shell_state;
155 foreach my $to_apply (@to_apply) {
156 my ($change, @args) = @$to_apply;
157 ($state, my @this_result) = $state->${\"with_${change}"}(@args);
158 push @Result, @this_result;
160 $self->_set_shell_state($state);
164 sub is_complete_command_string {
165 my ($self, $string) = @_;
166 return !!$self->tcl->icall(info => complete => $string);
169 sub eval_command_string {
170 my ($self, $string) = @_;
173 no warnings 'redefine';
174 local *DX::Utils::trace = $self->shell_state->trace_sub;
175 local our $Shell_Context = $self;
176 $self->tcl->Eval($string);
178 push @Result, [ output => $_ ];
181 ($_->[0] eq 'output' and ref($_->[1]))
182 ? [ output => deparse($_->[1]) ]
187 sub apply_predicate {
188 my ($self, $pred, @arg_strings) = @_;
189 die "Can't call predicate ${pred} outside a query\n"
190 unless $self->shell_state->mode eq 'query';
191 my @args = $self->expand_args(@arg_strings);
192 my ($intro, $need) = ({}, {});
193 foreach my $arg (@args) {
195 next if $arg =~ /^\??[a-z]/; # skip globals
196 # ?Foo is intro, Foo is need
197 ($arg =~ s/^\?// ? $intro : $need)->{$arg} = 1;
199 my $prop = DX::Proposition->new(
202 introduced_names => $intro,
203 required_names => $need,
205 my $old_qstate = $self->shell_state->current_query_state;
206 my $qstate = $old_qstate->with_additional_proposition($prop);
207 my $old_application_count = @{
208 $old_qstate->search_state->current_hypothesis->action_applications
210 my @applications = @{
211 $qstate->search_state->current_hypothesis->action_applications
214 map [ output => $_ ],
215 @applications[$old_application_count..$#applications];
216 $self->_set_shell_state(
217 $self->shell_state->but(
218 current_query_state => $qstate