3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 12:29:27 +01:00

Progress on embedding Plang

This commit is contained in:
Pragmatic Software 2020-07-12 17:36:32 -07:00
parent 87765d0a9c
commit c8a1b2b886
2 changed files with 26 additions and 58 deletions

2
Plang

@ -1 +1 @@
Subproject commit 80f9afa670d2e96cd4b70df69c052a490d499273 Subproject commit 67469a66f11f00434f6d3eb946764ccfbf4a80cd

View File

@ -30,9 +30,18 @@ sub initialize {
$self->{plang} = Plang::Interpreter->new(embedded => 1, debug => $debug); $self->{plang} = Plang::Interpreter->new(embedded => 1, debug => $debug);
# register some built-in functions # register some built-in functions
$self->{plang}->{interpreter}->add_function_builtin('set', [qw/channel keyword text/], \&set_factoid); $self->{plang}->{interpreter}->add_function_builtin('factset',
$self->{plang}->{interpreter}->add_function_builtin('get', [qw/channel keyword/], \&get_factoid); # parameters are [['param1 name', default arg], ['param2 name', default arg], ...]
$self->{plang}->{interpreter}->add_function_builtin('append', [qw/channel keyword text/], \&append_factoid); [['channel', undef], ['keyword', undef], ['text', undef]],
\&set_factoid);
$self->{plang}->{interpreter}->add_function_builtin('factget',
[['channel', undef], ['keyword', undef]],
\&get_factoid);
$self->{plang}->{interpreter}->add_function_builtin('factappend',
[['channel', undef], ['keyword', undef], ['text', undef]],
\&append_factoid);
# register the `plang` command # register the `plang` command
$self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, "plang", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, "plang", 0);
@ -54,16 +63,9 @@ sub cmd_plang {
my $result = $self->run($context->{arguments}); my $result = $self->run($context->{arguments});
# check to see if we need to append final result to output # check to see if we need to append final result to output
if (defined $result) { $self->{output} .= $result->[1] if defined $result->[1];
if (ref $result->[0] eq 'ARRAY') {
foreach my $r (@$result) {
$self->{output} .= $r->[1] if defined $r->[1];
}
} else {
$self->{output} .= $result->[1] if defined $result->[1];
}
}
# return the output
return length $self->{output} ? $self->{output} : "No output."; return length $self->{output} ? $self->{output} : "No output.";
} }
@ -85,58 +87,24 @@ sub run {
$self->{output} = ""; # collect output of the embedded Plang program $self->{output} = ""; # collect output of the embedded Plang program
my $result; # result of the final statement my $result; # result of the final statement
# interpret the statements eval {
foreach my $node (@$statements) { # interpret the statements
my $ins = $node->[0]; foreach my $node (@$statements) {
my $ins = $node->[0];
if ($ins eq 'STMT') { if ($ins eq 'STMT') {
$result = $self->{plang}->{interpreter}->statement($context, $node->[1]); $result = $self->{plang}->{interpreter}->statement($context, $node->[1]);
$result = $self->handle_statement_results($result); }
} }
};
last if $self->{error}; if ($@) {
$self->{output} .= $@;
return;
} }
return $result; # return result of the final statement return $result; # return result of the final statement
} }
use Data::Dumper;
# handle a Plang statement result
sub handle_statement_results {
my ($self, $results) = @_;
if (ref $results->[0] eq 'ARRAY') {
my $ret;
foreach my $result (@$results) {
$ret = $self->handle_statement_result($result);
}
return $ret;
} else {
return $self->handle_statement_result($results);
}
}
sub handle_statement_result {
my ($self, $result) = @_;
if ($result->[0] eq 'ERROR') {
$self->{output} .= $result->[1];
$self->{error} = 1;
return;
}
if ($result->[0] eq 'WARNING') {
$self->{output} .= $result->[1];
return;
}
if ($result->[0] eq 'STDOUT') {
$self->{output} .= $result->[1];
return;
}
return $result;
}
# our custom PBot built-in functions for Plang # our custom PBot built-in functions for Plang