deparse needs to show ?X for introduced names
[scpubgit/DX.git] / lib / DX / ShellSession.pm
CommitLineData
9eedd677 1package DX::ShellSession;
2
3use Tcl;
4use Scalar::Util qw(weaken);
fa8f5696 5use DX::Expander;
d1b6cb33 6use DX::Proposition;
7use DX::Utils qw(deparse);
9eedd677 8use DX::Class;
9
10has shell_state => (is => 'rwp', required => 1, isa => ShellState);
11
fa8f5696 12has expander => (
13 is => 'lazy', builder => sub { DX::Expander->new(tcl => $_[0]->tcl) },
14 handles => [ qw(expand_args) ],
15);
16
9eedd677 17has tcl => (is => 'lazy', builder => sub {
18 my ($self) = @_;
19 weaken $self;
20 my $tcl = Tcl->new;
21 $tcl->CreateCommand('?' => sub {
384a5e93 22 $self->apply_to_state([ 'new_query_state' ], [ mode => 'query' ]);
9eedd677 23 });
24 $tcl->CreateCommand('?+' => sub {
384a5e93 25 $self->apply_to_state([ mode => 'query' ]);
9eedd677 26 });
384a5e93 27 my $qvars;
9eedd677 28 $tcl->CreateCommand('.' => sub {
384a5e93 29 $self->apply_to_state([ mode => 'shell' ]);
30 $qvars->();
9eedd677 31 });
d1b6cb33 32 $tcl->CreateCommand(qlist => sub {
33 push our @Result, map [ output => $_ ], @{
34 $self->shell_state->current_query_state->proposition_sequence->members
35 };
36 return;
37 });
384a5e93 38 $tcl->CreateCommand(qvars => $qvars = sub {
39 my $locals = $self->shell_state->current_query_state->search_state
40 ->current_hypothesis->scope->locals->[0];
41 push our @Result, [ output => $locals ];
42 return;
43 });
aae0d764 44 $tcl->CreateCommand(qdeps => sub {
45 my $rps = $self->shell_state->current_query_state->search_state
46 ->current_hypothesis->resolved_propositions;
47 push our @Result, [ output => $rps ];
48 return;
49 });
fa8f5696 50 foreach my $pred (
51 keys %{$self->shell_state->template_query_state->predicates}
52 ) {
53 $tcl->CreateCommand($pred => sub {
54 my (undef, undef, undef, @args) = @_; # ($data, $interp, $pred)
55 $self->apply_predicate($pred => @args);
56 });
57 }
9eedd677 58 return $tcl;
59});
60
61sub apply_to_state {
62 my ($self, @to_apply) = @_;
63 my $state = $self->shell_state;
64 our @Result;
65 foreach my $to_apply (@to_apply) {
66 my ($change, @args) = @$to_apply;
67 ($state, my @this_result) = $state->${\"with_${change}"}(@args);
68 push @Result, @this_result;
69 }
70 $self->_set_shell_state($state);
71 return;
72}
73
74sub is_complete_command_string {
75 my ($self, $string) = @_;
76 return !!$self->tcl->icall(info => complete => $string);
77}
78
79sub eval_command_string {
80 my ($self, $string) = @_;
81 local our @Result;
fa8f5696 82 try {
83 $self->tcl->Eval($string);
84 } catch {
85 push @Result, [ output => $_ ];
86 };
d1b6cb33 87 return map {
88 ($_->[0] eq 'output' and ref($_->[1]))
89 ? [ output => deparse($_->[1]) ]
90 : $_
91 } @Result;
9eedd677 92}
93
fa8f5696 94sub apply_predicate {
d1b6cb33 95 my ($self, $pred, @arg_strings) = @_;
fa8f5696 96 die "Can't call predicate ${pred} outside a query\n"
97 unless $self->shell_state->mode eq 'query';
d1b6cb33 98 my @args = $self->expand_args(@arg_strings);
384a5e93 99 my ($intro, $need) = ({}, {});
d1b6cb33 100 foreach my $arg (@args) {
101 next if ref($arg);
102 # ?Foo is intro, Foo is need
103 ($arg =~ s/^\?// ? $intro : $need)->{$arg} = 1;
104 }
105 my $prop = DX::Proposition->new(
106 predicate => $pred,
107 args => \@args,
5f12a9d8 108 introduced_names => $intro,
109 required_names => $need,
d1b6cb33 110 );
31d445d3 111 my $old_qstate = $self->shell_state->current_query_state;
112 my $qstate = $old_qstate->with_additional_proposition($prop);
113 my $old_action_count = @{
114 $old_qstate->search_state->current_hypothesis->actions
115 };
116 my @actions = @{$qstate->search_state->current_hypothesis->actions};
117 push our @Result,
118 map [ output => $_ ], @actions[$old_action_count..$#actions];
d1b6cb33 119 $self->_set_shell_state(
120 $self->shell_state->but(
121 current_query_state => $qstate
122 )
123 );
124 return;
fa8f5696 125}
126
9eedd677 1271;