mirror of
https://github.com/pragma-/pbot.git
synced 2024-12-24 03:33:06 +01:00
Plugins/Plang: override print
built-in to collect output
This commit is contained in:
parent
184d6faf8f
commit
38daf04bfa
2
Plang
2
Plang
@ -1 +1 @@
|
||||
Subproject commit 78afb131f474ff191739bbc5101911b3135f0058
|
||||
Subproject commit 91432db555e7fab5ff2437b954b75731b2602f03
|
@ -19,17 +19,25 @@ use Getopt::Long qw(GetOptionsFromArray);
|
||||
sub initialize {
|
||||
my ($self, %conf) = @_;
|
||||
|
||||
# load Plang module
|
||||
my $path = $self->{pbot}->{registry}->get_value('general', 'plang_dir') // 'Plang';
|
||||
unshift @INC, $path if not grep { $_ eq $path } @INC;
|
||||
require "$path/Interpreter.pm";
|
||||
|
||||
# allow !refresh to reload these modules
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/Interpreter.pm");
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/AstInterpreter.pm");
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/Grammar.pm");
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/Lexer.pm");
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache("$path/Parser.pm");
|
||||
|
||||
# regset plang.debug 0-10 -- Plugin must be reloaded for this value to take effect.
|
||||
my $debug = $self->{pbot}->{registry}->get_value('plang', 'debug') // 0;
|
||||
|
||||
# create our Plang interpreter object
|
||||
$self->{plang} = Plang::Interpreter->new(embedded => 1, debug => $debug);
|
||||
|
||||
# register some built-in functions
|
||||
# register some PBot-specific built-in functions
|
||||
$self->{plang}->{interpreter}->add_function_builtin('factset',
|
||||
# parameters are [['param1 name', default arg], ['param2 name', default arg], ...]
|
||||
[['namespace', undef], ['keyword', undef], ['text', undef]],
|
||||
@ -43,6 +51,11 @@ sub initialize {
|
||||
[['namespace', undef], ['keyword', undef], ['text', undef]],
|
||||
sub { $self->append_factoid(@_) });
|
||||
|
||||
# override the built-in `print` function to send to our output buffer instead
|
||||
$self->{plang}->{interpreter}->add_function_builtin('print',
|
||||
[['stmt', undef], ['end', ['STRING', "\n"]]],
|
||||
sub { $self->print_override(@_) });
|
||||
|
||||
# register the `plang` command
|
||||
$self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, "plang", 0);
|
||||
}
|
||||
@ -88,7 +101,7 @@ sub run {
|
||||
return if not defined $ast;
|
||||
|
||||
# create a new environment for a Plang program
|
||||
my $context = $self->{plang}->{interpreter}->init_program;
|
||||
my $context = $self->{plang}->{interpreter}->new_context;
|
||||
|
||||
# grab our program's statements
|
||||
my $program = $ast->[0];
|
||||
@ -127,6 +140,14 @@ sub run {
|
||||
return $result; # return result of the final statement
|
||||
}
|
||||
|
||||
# overridden `print` built-in
|
||||
sub print_override {
|
||||
my ($self, $plang, $name, $arguments) = @_;
|
||||
my ($stmt, $end) = ($plang->output_value($arguments->[0]), $arguments->[1]->[1]);
|
||||
$self->{output} .= "$stmt$end";
|
||||
return ['NIL', undef];
|
||||
}
|
||||
|
||||
# our custom PBot built-in functions for Plang
|
||||
|
||||
sub is_locked {
|
||||
|
Loading…
Reference in New Issue
Block a user