[Bps-public-commit] r13868 - in Lorzy/trunk: . lib/Lorzy/Expression
sartak at bestpractical.com
sartak at bestpractical.com
Tue Jul 8 12:45:51 EDT 2008
Author: sartak
Date: Tue Jul 8 12:45:51 2008
New Revision: 13868
Added:
Lorzy/trunk/lib/Lorzy/Expression/
Lorzy/trunk/lib/Lorzy/Expression/False.pm
Lorzy/trunk/lib/Lorzy/Expression/ForEach.pm
Lorzy/trunk/lib/Lorzy/Expression/IfThen.pm
Lorzy/trunk/lib/Lorzy/Expression/Let.pm
Lorzy/trunk/lib/Lorzy/Expression/List.pm
Lorzy/trunk/lib/Lorzy/Expression/ProgN.pm
Lorzy/trunk/lib/Lorzy/Expression/String.pm
Lorzy/trunk/lib/Lorzy/Expression/Symbol.pm
Lorzy/trunk/lib/Lorzy/Expression/True.pm
Modified:
Lorzy/trunk/ (props changed)
Lorzy/trunk/lib/Lorzy/Evaluator.pm
Lorzy/trunk/lib/Lorzy/EvaluatorResult.pm
Lorzy/trunk/lib/Lorzy/Expression.pm
Log:
r63836 at onn: sartak | 2008-07-08 12:45:47 -0400
Break up the Lorzy::Expression beast
Modified: Lorzy/trunk/lib/Lorzy/Evaluator.pm
==============================================================================
--- Lorzy/trunk/lib/Lorzy/Evaluator.pm (original)
+++ Lorzy/trunk/lib/Lorzy/Evaluator.pm Tue Jul 8 12:45:51 2008
@@ -139,9 +139,7 @@
sub _enumerate_core_expressions {
my $self = shift;
- return grep { $_ && $_->isa('Lorzy::Expression') }
- map { /^(.*)::$/ ? 'Lorzy::Expression::' . $1 : undef }
- keys %Lorzy::Expression::;
+ return Lorzy::Expression->expression_types;
}
sub _flatten_core_expression_signature {
Modified: Lorzy/trunk/lib/Lorzy/EvaluatorResult.pm
==============================================================================
--- Lorzy/trunk/lib/Lorzy/EvaluatorResult.pm (original)
+++ Lorzy/trunk/lib/Lorzy/EvaluatorResult.pm Tue Jul 8 12:45:51 2008
@@ -2,21 +2,18 @@
use Moose;
has success => (
- is => 'rw',
- isa => 'Bool'
+ is => 'rw',
+ isa => 'Bool',
);
-has error => (
+has error => (
is => 'rw',
-
-
);
-has value => (
- is => 'rw',
-# isa => 'Str | Undef | Lorzy::EvaluatorResult::RunTime',
- required => 0
- );
-
+has value => (
+ is => 'rw',
+# isa => 'Str | Undef | Lorzy::EvaluatorResult::RunTime',
+);
1;
+
Modified: Lorzy/trunk/lib/Lorzy/Expression.pm
==============================================================================
--- Lorzy/trunk/lib/Lorzy/Expression.pm (original)
+++ Lorzy/trunk/lib/Lorzy/Expression.pm Tue Jul 8 12:45:51 2008
@@ -1,261 +1,45 @@
-
package Lorzy::Expression;
-use Lorzy::FunctionArgument;
use Moose;
+use Lorzy::FunctionArgument;
+with 'Lorzy::Evaluatable';
-with 'Lorzy::Evaluatable';
+use Module::Pluggable
+ require => 1,
+ sub_name => 'expression_types',
+ search_path => __PACKAGE__;
has name => (
- is => 'ro',
- isa => 'Str');
+ is => 'ro',
+ isa => 'Str',
+);
has elements => (
- is => 'ro',
- isa => 'ArrayRef');
+ is => 'ro',
+ isa => 'ArrayRef',
+ default => sub { [] },
+);
-has signature => (
- is => 'rw',
- default => sub { {}},
- isa => 'HashRef[Lorzy::FunctionArgument]');
+has signature => (
+ is => 'rw',
+ isa => 'HashRef[Lorzy::FunctionArgument]',
+ default => sub { {} },
+);
has args => (
- is => 'rw',
+ is => 'rw',
+ isa => 'HashRef[Lorzy::Expression]',
default => sub { {} },
- isa => 'HashRef[Lorzy::Expression]');
+);
sub evaluate {
my ($self, $ev) = @_;
my $lambda = $ev->resolve_symbol_name($self->name);
- $ev->apply_script( $lambda, $self->args );
+ $ev->apply_script($lambda, $self->args);
return $ev->result->value;
}
-package Lorzy::Expression::True;
-use Moose;
-use MooseX::ClassAttribute;
-
-extends 'Lorzy::Expression';
-
-class_has signature => ( is => 'ro', default => sub { { }});
-sub evaluate {1}
-
-package Lorzy::Expression::False;
-use Moose;
-extends 'Lorzy::Expression::True';
-
-sub evaluate {
- my $self = shift;
- return ! $self->SUPER::evaluate();
-
-}
-
-package Lorzy::Expression::IfThen;
-use Moose;
-extends 'Lorzy::Expression';
-use Params::Validate qw/validate_pos/;
-use MooseX::ClassAttribute;
-
-class_has signature => (
- is => 'ro',
- default => sub {
- {
- condition => Lorzy::FunctionArgument->new(
- name => 'condition',
- type => 'Lorzy::Evaluatable'),
-
- if_true => Lorzy::FunctionArgument->new(
- name => 'if_true',
- type => 'Lorzy::Evaluatable'),
- if_false => Lorzy::FunctionArgument->new(
- name => 'if_false',
- type => 'Lorzy::Evaluatable'
- )
- }
- }
-);
-
-sub evaluate {
- my ($self, $evaluator) = validate_pos(@_, { isa => 'Lorzy::Expression'}, { isa => 'Lorzy::Evaluator'}, );
-
- my $truth= $self->args->{condition}->evaluate($evaluator);
- if ($truth) {
- return $self->args->{if_true}->evaluate($evaluator);
- } else {
- return $self->args->{if_false}->evaluate($evaluator);
- }
-}
-
-package Lorzy::Expression::String;
-use Moose;
-extends 'Lorzy::Expression';
-use Params::Validate qw/validate_pos/;
-use MooseX::ClassAttribute;
-
-class_has signature => (
- is => 'ro',
- default => sub {
- { value => Lorzy::FunctionArgument->new( name => 'value', type => 'Str' )
- };
- }
-);
-
-has '+args' => (
- isa => 'HashRef[Str]');
-
-
-sub evaluate {
- my ( $self, $eval ) = validate_pos(
- @_,
- { isa => 'Lorzy::Expression' },
- { isa => 'Lorzy::Evaluator' }
- );
-
-
- return $self->args->{'value'};
-
-}
-
-package Lorzy::Expression::ProgN;
-use MooseX::ClassAttribute;
-use Moose;
-extends 'Lorzy::Expression';
-class_has signature => ( is => 'ro', default => sub { { }});
-
-has nodes => (
- is => 'rw',
- isa => 'ArrayRef',
-);
-
-sub BUILD {
- my ($self, $params) = @_;
-
- return unless $params->{builder};
- my $nodes = $params->{builder_args}{nodes};
-
- $self->nodes( [ map { $params->{builder}->build_expression($_) } @$nodes ] );
-}
-
-sub evaluate {
- my ($self, $evaluator) = @_;
- my $res;
- Carp::cluck unless $self->nodes;
- foreach my $node (@{$self->nodes}) {
- $res = $node->evaluate($evaluator);
- }
- return $res;
-}
-
-package Lorzy::Expression::Symbol;
-use Moose;
-extends 'Lorzy::Expression';
-use Params::Validate qw/validate_pos/;
-use MooseX::ClassAttribute;
-
-class_has signature => (
- is => 'ro',
- default => sub { { symbol => Lorzy::FunctionArgument->new( name => 'symbol', type => 'Str')}});
-
-sub evaluate {
- my ($self, $eval) = validate_pos(@_, { isa => 'Lorzy::Expression'}, { isa => 'Lorzy::Evaluator'});
- my $symbol = $self->{'args'}->{'symbol'}->evaluate($eval);
- my $result = $eval->resolve_symbol_name($symbol);
- return ref($result) && $result->meta->does_role('Lorzy::Evaluatable') ? $result->evaluate($eval): $result; # XXX: figure out evaluation order here
-}
-
-package Lorzy::Expression::List;
-use Moose;
-extends 'Lorzy::Expression::ProgN';
-
-sub evaluate {
- my ($self, $evaluator) = @_;
- return bless \$self->nodes, 'Lorzy::EvaluatorResult::RunTime';
-}
-
-package Lorzy::Expression::ForEach;
-use Moose;
-extends 'Lorzy::Expression';
-use MooseX::ClassAttribute;
-
-class_has signature => (
- is => 'ro',
- default => sub { { list => Lorzy::FunctionArgument->new( name => 'list'),
- binding => Lorzy::FunctionArgument->new( name => 'Str'),
- do => Lorzy::FunctionArgument->new( name => 'Str', type => 'Lorzy::Lambda'), # XXX: type for runtime?
- }});
-
-sub evaluate {
- my ($self, $evaluator) = @_;
- my $lambda = $self->args->{do}->evaluate($evaluator);
- die unless $lambda->isa("Lorzy::Lambda");
-
- my $binding = $self->args->{binding}->evaluate($evaluator);
- my $list = $self->args->{list}->evaluate($evaluator);
-
- die unless ref($list) eq 'Lorzy::EvaluatorResult::RunTime';
- my $nodes = $$list;
-
- foreach (@$nodes) {
- $lambda->apply($evaluator, { $binding => $_ });
- }
-
-}
-
-package Lorzy::Expression::Symbol;
-use Moose;
-extends 'Lorzy::Expression';
-use Params::Validate qw/validate_pos/;
-use MooseX::ClassAttribute;
-
-class_has signature => (
- is => 'ro',
- default => sub { { symbol => Lorzy::FunctionArgument->new( name => 'symbol', type => 'Str')}});
-
-
-package Lorzy::Expression::Let;
-use Moose;
-extends 'Lorzy::Expression::ProgN';
-with 'Lorzy::Block';
-
-has bindings => (
- is => 'rw',
- isa => 'HashRef',
- default => sub { { } },
-);
-
-has lambda => (
- is => 'ro',
- isa => 'Lorzy::Lambda',
- lazy => 1,
- default => sub {
- my $self = shift;
- Lorzy::Lambda->new(
- progn => Lorzy::Expression::ProgN->new( nodes => $self->nodes ),
- signature => $self->mk_signature,
- block_id => $self->block_id,
- outer_block => $self->outer_block,
- );
- },
-);
-
-sub BUILD {
- my ($self, $params) = @_;
-
- return unless $params->{builder};
- my $bindings = $params->{builder_args}{bindings};
-
- $self->bindings->{$_} = $params->{builder}->build_expression($bindings->{$_})
- for keys %$bindings;
-
-}
-
-sub mk_signature {
- my $self = shift;
- return { map { $_ => Lorzy::FunctionArgument->new( name => $_, type => 'Str') } keys %{ $self->bindings } };
-}
-
-sub evaluate {
- my ($self, $evaluator) = @_;
- $self->lambda->apply( $evaluator, $self->bindings );
-}
+# force loading of each expression subclass
+__PACKAGE__->expression_types;
1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/False.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/False.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,8 @@
+package Lorzy::Expression::False;
+use Moose;
+extends 'Lorzy::Expression::True';
+
+sub evaluate { 0 }
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/ForEach.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/ForEach.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,42 @@
+package Lorzy::Expression::ForEach;
+use Moose;
+use MooseX::ClassAttribute;
+extends 'Lorzy::Expression';
+
+class_has signature => (
+ is => 'ro',
+ default => sub {
+ return {
+ list => Lorzy::FunctionArgument->new(
+ name => 'list',
+ ),
+ binding => Lorzy::FunctionArgument->new(
+ name => 'Str',
+ ),
+ do => Lorzy::FunctionArgument->new(
+ name => 'Str',
+ type => 'Lorzy::Lambda', # XXX: type for runtime?
+ ),
+ };
+ },
+);
+
+sub evaluate {
+ my ($self, $evaluator) = @_;
+
+ my $lambda = $self->args->{do}->evaluate($evaluator);
+ my $binding = $self->args->{binding}->evaluate($evaluator);
+ my $list = $self->args->{list}->evaluate($evaluator);
+
+ die "Invalid do-block $lambda" unless $lambda->isa("Lorzy::Lambda");
+ die "Invalid 'list' $list" unless ref($list) eq 'Lorzy::EvaluatorResult::RunTime';
+
+ my $nodes = $$list;
+
+ foreach (@$nodes) {
+ $lambda->apply($evaluator, { $binding => $_ });
+ }
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/IfThen.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/IfThen.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+package Lorzy::Expression::IfThen;
+use Moose;
+use MooseX::ClassAttribute;
+extends 'Lorzy::Expression';
+
+use Params::Validate qw/validate_pos/;
+
+class_has signature => (
+ is => 'ro',
+ default => sub {
+ return {
+ condition => Lorzy::FunctionArgument->new(
+ name => 'condition',
+ type => 'Lorzy::Evaluatable',
+ ),
+ if_true => Lorzy::FunctionArgument->new(
+ name => 'if_true',
+ type => 'Lorzy::Evaluatable',
+ ),
+ if_false => Lorzy::FunctionArgument->new(
+ name => 'if_false',
+ type => 'Lorzy::Evaluatable',
+ ),
+ };
+ }
+);
+
+sub evaluate {
+ my ($self, $evaluator) = validate_pos(@_,
+ { isa => 'Lorzy::Expression' },
+ { isa => 'Lorzy::Evaluator' },
+ );
+
+ my $truth = $self->args->{condition}->evaluate($evaluator);
+ if ($truth) {
+ return $self->args->{if_true}->evaluate($evaluator);
+ } else {
+ return $self->args->{if_false}->evaluate($evaluator);
+ }
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/Let.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/Let.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,51 @@
+package Lorzy::Expression::Let;
+use Moose;
+extends 'Lorzy::Expression::ProgN';
+with 'Lorzy::Block';
+
+has bindings => (
+ is => 'rw',
+ isa => 'HashRef',
+ default => sub { { } },
+);
+
+has lambda => (
+ is => 'ro',
+ isa => 'Lorzy::Lambda',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ Lorzy::Lambda->new(
+ progn => Lorzy::Expression::ProgN->new(nodes => $self->nodes),
+ signature => $self->mk_signature,
+ block_id => $self->block_id,
+ outer_block => $self->outer_block,
+ );
+ },
+);
+
+sub BUILD {
+ my ($self, $params) = @_;
+
+ return unless $params->{builder};
+ my $bindings = $params->{builder_args}{bindings};
+
+ $self->bindings->{$_} = $params->{builder}->build_expression($bindings->{$_})
+ for keys %$bindings;
+}
+
+sub mk_signature {
+ my $self = shift;
+ return {
+ map { $_ => Lorzy::FunctionArgument->new( name => $_, type => 'Str') }
+ keys %{ $self->bindings }
+ };
+}
+
+sub evaluate {
+ my ($self, $evaluator) = @_;
+ $self->lambda->apply($evaluator, $self->bindings);
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/List.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/List.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,11 @@
+package Lorzy::Expression::List;
+use Moose;
+extends 'Lorzy::Expression::ProgN';
+
+sub evaluate {
+ my ($self, $evaluator) = @_;
+ return bless \$self->nodes, 'Lorzy::EvaluatorResult::RunTime';
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/ProgN.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/ProgN.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,37 @@
+package Lorzy::Expression::ProgN;
+use Moose;
+use MooseX::ClassAttribute;
+extends 'Lorzy::Expression';
+
+class_has signature => (
+ is => 'ro',
+ default => sub { {} },
+);
+
+has nodes => (
+ is => 'rw',
+ isa => 'ArrayRef',
+);
+
+sub BUILD {
+ my ($self, $params) = @_;
+
+ return unless $params->{builder};
+ my $nodes = $params->{builder_args}{nodes};
+
+ $self->nodes([ map { $params->{builder}->build_expression($_) } @$nodes ]);
+}
+
+sub evaluate {
+ my ($self, $evaluator) = @_;
+ Carp::cluck("No nodes to evaluate.") unless $self->nodes;
+
+ my $res;
+ foreach my $node (@{$self->nodes}) {
+ $res = $node->evaluate($evaluator);
+ }
+ return $res;
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/String.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/String.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,35 @@
+package Lorzy::Expression::String;
+use Moose;
+use MooseX::ClassAttribute;
+extends 'Lorzy::Expression';
+
+use Params::Validate qw/validate_pos/;
+
+class_has signature => (
+ is => 'ro',
+ default => sub {
+ return {
+ value => Lorzy::FunctionArgument->new(
+ name => 'value',
+ type => 'Str',
+ ),
+ };
+ }
+);
+
+has '+args' => (
+ isa => 'HashRef[Str]',
+);
+
+sub evaluate {
+ my ($self, $eval) = validate_pos(
+ @_,
+ { isa => 'Lorzy::Expression' },
+ { isa => 'Lorzy::Evaluator' },
+ );
+
+ return $self->args->{'value'};
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/Symbol.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/Symbol.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,35 @@
+package Lorzy::Expression::Symbol;
+use Moose;
+use MooseX::ClassAttribute;
+extends 'Lorzy::Expression';
+
+use Params::Validate qw/validate_pos/;
+
+class_has signature => (
+ is => 'ro',
+ default => sub {
+ return {
+ symbol => Lorzy::FunctionArgument->new(
+ name => 'symbol',
+ type => 'Str',
+ ),
+ };
+ },
+);
+
+sub evaluate {
+ my ($self, $eval) = validate_pos(@_,
+ { isa => 'Lorzy::Expression'},
+ { isa => 'Lorzy::Evaluator'},
+ );
+
+ my $symbol = $self->{'args'}->{'symbol'}->evaluate($eval);
+ my $result = $eval->resolve_symbol_name($symbol);
+
+ return ref($result) && $result->meta->does_role('Lorzy::Evaluatable')
+ ? $result->evaluate($eval)
+ : $result; # XXX: figure out evaluation order here
+}
+
+1;
+
Added: Lorzy/trunk/lib/Lorzy/Expression/True.pm
==============================================================================
--- (empty file)
+++ Lorzy/trunk/lib/Lorzy/Expression/True.pm Tue Jul 8 12:45:51 2008
@@ -0,0 +1,15 @@
+package Lorzy::Expression::True;
+use Moose;
+use MooseX::ClassAttribute;
+
+extends 'Lorzy::Expression';
+
+class_has signature => (
+ is => 'ro',
+ default => sub { { } } ,
+);
+
+sub evaluate { 1 }
+
+1;
+
More information about the Bps-public-commit
mailing list