Skip to content

Commit

Permalink
removed the tracing operators again
Browse files Browse the repository at this point in the history
  • Loading branch information
memowe committed Dec 9, 2014
1 parent 19f5500 commit 90aca3c
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 142 deletions.
58 changes: 2 additions & 56 deletions lib/PerLisp.pm
Expand Up @@ -20,12 +20,6 @@ has parser => PerLisp::Parser->new;
has input => IO::Handle->new->fdopen(fileno(STDIN), 'r');
has output => IO::Handle->new->fdopen(fileno(STDOUT),'w');

# trace log callback
has tracer => (default => sub { my $self = shift; sub {
my $str = shift;
$self->output->print($str . "\n");
}});

# set operators
sub init {
my $self = shift;
Expand Down Expand Up @@ -110,52 +104,6 @@ sub init {
},
));

# trace operator: add a trace to functions and operators
$self->context->set(trace => PerLisp::Expr::Operator->new(
name => 'trace',
code => sub {
my ($context, @symbols) = @_;

# lookup
my @fn_exprs = map { $context->get($_->name) } @symbols;

# check traceability (duck typing)
for my $fn_expr (@fn_exprs) {
die $fn_expr->to_string . " isn't traceable.\n"
unless $fn_expr->can('tracer');
}

# activate trace
$_->tracer($self->tracer) for @fn_exprs;

# return summary
return 'traced: ' . join ', ' => map { $_->name } @symbols;
},
));

# untrace operator: remove a trace from functions and operators
$self->context->set(untrace => PerLisp::Expr::Operator->new(
name => 'untrace',
code => sub {
my ($context, @symbols) = @_;

# lookup
my @fn_exprs = map { $context->get($_->name) } @symbols;

# check traceability (duck typing)
for my $fn_expr (@fn_exprs) {
die $fn_expr->to_string . " isn't traceable.\n"
unless $fn_expr->can('tracer');
}

# deactivate trace
$_->tracer(undef) for @fn_exprs;

# return summary
return 'untraced: ' . join ', ' => map { $_->name } @symbols;
},
));

# init definitions
$self->eval($PerLisp::Init::definitions);

Expand Down Expand Up @@ -190,10 +138,8 @@ sub read_eval_print_loop {

# try to eval and print
eval {
for my $val ($self->eval($line, $self->context)) {
my $val_str = ref($val) ? $val->to_string : $val // '';
$self->output->print($val_str . "\n");
}
my @values = $self->eval($line, $self->context);
$self->output->print($_->to_string . "\n") for @values;
};

# catch errors
Expand Down
2 changes: 0 additions & 2 deletions lib/PerLisp/Expr/Function.pm
Expand Up @@ -6,7 +6,6 @@ extends 'PerLisp::Expr';
has params => [];
has body => (required => 1);
has context => (required => 1);
has tracer => (); # a tracer code ref

sub eval {
my ($self, $context) = @_;
Expand Down Expand Up @@ -78,7 +77,6 @@ sub apply {
params => \@params,
body => $self->body,
context => $local_context,
tracer => $self->tracer,
);
}

Expand Down
16 changes: 1 addition & 15 deletions lib/PerLisp/Expr/List.pm
Expand Up @@ -34,22 +34,8 @@ sub eval {
die $fn_expr->to_string . " can't be applied.\n"
unless $function->can('apply');

# call trace
my $ctrace = '(' . join(' ' => map { $_->to_string } $fn_expr, @args) . ')';
$function->tracer->("\tCall\t$ctrace")
if $function->tracer;

# apply
my $ret_val = $function->apply($context, \@args);

# return trace
my $ret_val_str = ref($ret_val) ? $ret_val->to_string : $ret_val // '';
my $rtrace = $ret_val_str . " from $ctrace";
$function->tracer->("\tReturn\t$rtrace")
if $function->tracer;

# done
return $ret_val;
return $function->apply($context, \@args);
}

sub to_string {
Expand Down
1 change: 0 additions & 1 deletion lib/PerLisp/Expr/Operator.pm
Expand Up @@ -5,7 +5,6 @@ extends 'PerLisp::Expr';

has name => (required => 1);
has code => (required => 1);
has tracer => (); # a tracer code ref

sub eval {
my ($self, $context) = @_;
Expand Down
File renamed without changes.
68 changes: 0 additions & 68 deletions t/70-trace.t

This file was deleted.

0 comments on commit 90aca3c

Please sign in to comment.