You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by mi...@apache.org on 2010/10/14 10:48:40 UTC
[lucy-commits] svn commit: r1022422 [6/43] - in /incubator/lucy/trunk/import: ./
charmonizer/ charmonizer/src/ charmonizer/src/Charmonizer/
charmonizer/src/Charmonizer/Core/ charmonizer/src/Charmonizer/Probe/
charmonizer/src/Charmonizer/Test/ clownfish/ clownfish/lib...
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Function.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Function.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Function.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Function.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,129 @@
+use strict;
+use warnings;
+
+package Clownfish::Function;
+use base qw( Clownfish::Symbol );
+use Carp;
+use Clownfish::Util qw( verify_args a_isa_b );
+use Clownfish::Type;
+use Clownfish::ParamList;
+
+my %new_PARAMS = (
+ return_type => undef,
+ class_name => undef,
+ class_cnick => undef,
+ param_list => undef,
+ micro_sym => undef,
+ docucomment => undef,
+ parcel => undef,
+ inline => 0,
+ exposure => 'parcel',
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = $either->SUPER::new( %new_PARAMS, @_ );
+
+ # Validate.
+ for (qw( return_type class_name param_list )) {
+ confess("$_ is mandatory") unless defined $self->{$_};
+ }
+ confess("Invalid micro_sym: '$self->{micro_sym}'")
+ unless $self->{micro_sym} =~ /^[a-z0-9_]+$/;
+ confess 'param_list must be a ParamList object'
+ unless a_isa_b( $self->{param_list}, "Clownfish::ParamList" );
+ confess 'return_type must be a Type object'
+ unless a_isa_b( $self->{return_type}, "Clownfish::Type" );
+
+ return $self;
+}
+
+sub get_return_type { shift->{return_type} }
+sub get_param_list { shift->{param_list} }
+sub get_docucomment { shift->{docucomment} }
+sub inline { shift->{inline} }
+
+sub void { shift->{return_type}->is_void }
+
+sub full_func_sym { shift->SUPER::full_sym }
+sub short_func_sym { shift->SUPER::short_sym }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Function - Metadata describing a function.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Function->new(
+ class_name => 'Crustacean::Lobster::LobsterClaw', # required
+ class_cnick => 'LobClaw', # default: special
+ return_type => $int_type, # required
+ param_list => $param_list, # required
+ micro_sym => 'compare', # required
+ docucomment => $docucomment, # default: undef
+ parcel => 'Crustacean', # default: special
+ exposure => 'public', # default: parcel
+ inline => 1, # default: false
+ );
+
+=over
+
+=item * B<class_name> - The full name of the class in whose namespace the
+function resides.
+
+=item * B<class_cnick> - The C nickname for the class.
+
+=item * B<return_type> - A L<Clownfish::Type> representing the function's
+return type.
+
+=item * B<param_list> - A L<Clownfish::ParamList> representing the
+function's argument list.
+
+=item * B<micro_sym> - The lower case name of the function, without any
+namespacing prefixes.
+
+=item * B<docucomment> - A L<Clownfish::DocuComment> describing the
+function.
+
+=item * B<parcel> - A L<Clownfish::Parcel> or a parcel name.
+
+=item * B<exposure> - The function's exposure (see L<Clownfish::Symbol>).
+
+=item * B<inline> - Should be true if the function should be inlined by the
+compiler.
+
+=back
+
+=head2 get_return_type get_param_list get_docucomment inline
+
+Accessors.
+
+=head2 void
+
+Returns true if the function has a void return type, false otherwise.
+
+=head2 full_func_sym
+
+A synonym for full_sym().
+
+=head2 short_func_sym
+
+A synonym for short_sym().
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2006-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Function.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Hierarchy.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Hierarchy.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Hierarchy.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Hierarchy.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,233 @@
+use strict;
+use warnings;
+
+package Clownfish::Hierarchy;
+use Carp;
+use File::Find qw( find );
+use File::Spec::Functions qw( catfile splitpath );
+use File::Path qw( mkpath );
+use Fcntl;
+
+use Clownfish::Util qw( slurp_file current verify_args );
+use Clownfish::Class;
+use Clownfish::Parser;
+
+our %new_PARAMS = (
+ source => undef,
+ dest => undef,
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = bless {
+ parser => Clownfish::Parser->new,
+ trees => {},
+ files => {},
+ %new_PARAMS,
+ @_,
+ },
+ ref($either) || $either;
+ for (qw( source dest)) {
+ confess("Missing required param '$_'") unless $self->{$_};
+ }
+ return $self;
+}
+
+# Accessors.
+sub get_source { shift->{source} }
+sub get_dest { shift->{dest} }
+
+# Return flattened hierarchies.
+sub ordered_classes {
+ my $self = shift;
+ my @all;
+ for my $tree ( values %{ $self->{trees} } ) {
+ push @all, $tree->tree_to_ladder;
+ }
+ return @all;
+}
+
+sub files { values %{ shift->{files} } }
+
+# Slurp all Clownfish header files.
+# Arrange the class objects into inheritance trees.
+sub build {
+ my $self = shift;
+ $self->_parse_cf_files;
+ $_->grow_tree for values %{ $self->{trees} };
+}
+
+sub _parse_cf_files {
+ my $self = shift;
+ my $source = $self->{source};
+
+ # Collect filenames.
+ my @all_source_paths;
+ find(
+ { wanted => sub {
+ if ( $File::Find::name =~ /\.cfh$/ ) {
+ push @all_source_paths, $File::Find::name
+ unless /#/; # skip emacs .#filename.h lock files
+ }
+ },
+ no_chdir => 1,
+ follow => 1, # follow symlinks if possible (noop on Windows)
+ },
+ $source,
+ );
+
+ # Process any file that has at least one class declaration.
+ my %classes;
+ for my $source_path (@all_source_paths) {
+ # Derive the name of the class that owns the module file.
+ my $source_class = $source_path;
+ $source_class =~ s/\.cfh$//;
+ $source_class =~ s/^\Q$source\E\W*//
+ or die "'$source_path' doesn't start with '$source'";
+ $source_class =~ s/\W/::/g;
+
+ # Slurp, parse, add parsed file to pool.
+ my $content = slurp_file($source_path);
+ $content = $self->{parser}->strip_plain_comments($content);
+ my $file = $self->{parser}
+ ->file( $content, 0, source_class => $source_class, );
+ confess("parse error for $source_path") unless defined $file;
+ $self->{files}{$source_class} = $file;
+ for my $class ( $file->classes ) {
+ my $class_name = $class->get_class_name;
+ confess "$class_name already defined"
+ if exists $classes{$class_name};
+ $classes{$class_name} = $class;
+ }
+ }
+
+ # Wrangle the classes into hierarchies and figure out inheritance.
+ while ( my ( $class_name, $class ) = each %classes ) {
+ my $parent_name = $class->get_parent_class_name;
+ if ( defined $parent_name ) {
+ if ( not exists $classes{$parent_name} ) {
+ confess( "parent class '$parent_name' not defined "
+ . "for class '$class_name'" );
+ }
+ $classes{$parent_name}->add_child($class);
+ }
+ else {
+ $self->{trees}{$class_name} = $class;
+ }
+ }
+}
+
+sub propagate_modified {
+ my ( $self, $modified ) = @_;
+ # Seed the recursive write.
+ my $somebody_is_modified;
+ for my $tree ( values %{ $self->{trees} } ) {
+ next unless $self->_propagate_modified( $tree, $modified );
+ $somebody_is_modified = 1;
+ }
+ return $somebody_is_modified || $modified;
+}
+
+# Recursive helper function.
+sub _propagate_modified {
+ my ( $self, $class, $modified ) = @_;
+ my $file = $self->{files}{ $class->get_source_class };
+ my $source_path = $file->cfh_path( $self->{source} );
+ my $h_path = $file->h_path( $self->{dest} );
+
+ if ( !current( $source_path, $h_path ) ) {
+ $modified = 1;
+ }
+
+ if ($modified) {
+ $file->set_modified($modified);
+ }
+
+ # Proceed to the next generation.
+ my $somebody_is_modified = $modified;
+ for my $kid ( $class->children ) {
+ if ( $class->final ) {
+ confess( "Attempt to inherit from final class "
+ . $class->get_class_name . " by "
+ . $kid->get_class_name );
+ }
+ if ( $self->_propagate_modified( $kid, $modified ) ) {
+ $somebody_is_modified = 1;
+ }
+ }
+
+ return $somebody_is_modified;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Hierarchy - A class hierarchy.
+
+=head1 DESCRIPTION
+
+A Clownfish::Hierarchy consists of all the classes defined in files within
+a source directory and its subdirectories.
+
+There may be more than one tree within the Hierarchy, since all "inert"
+classes are root nodes, and since Clownfish does not officially define any
+core classes itself from which all instantiable classes must descend.
+
+=head1 METHODS
+
+=head2 new
+
+ my $hierarchy = Clownfish::Hierarchy->new(
+ source => undef, # required
+ dest => undef, # required
+ );
+
+=over
+
+=item * B<source> - The directory we begin reading files from.
+
+=item * B<dest> - The directory where the autogenerated files will be written.
+
+=back
+
+=head2 build
+
+ $hierarchy->build;
+
+Parse every Clownfish header file which can be found under C<source>, building
+up the object hierarchy.
+
+=head2 ordered_classes
+
+ my @classes = $hierarchy->ordered_classes;
+
+Return all Classes as a list with the property that every parent class will
+precede all of its children.
+
+=head2 propagate_modified
+
+ $hierarchy->propagate_modified($modified);
+
+Visit all File objects in the hierarchy. If a parent node is modified, mark
+all of its children as modified.
+
+If the supplied argument is true, mark all Files as modified.
+
+=head2 get_source get_dest
+
+Accessors.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2006-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Hierarchy.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Method.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Method.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Method.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Method.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,330 @@
+use strict;
+use warnings;
+
+package Clownfish::Method;
+use base qw( Clownfish::Function );
+use Clownfish::Util qw( verify_args );
+use Carp;
+
+my %new_PARAMS = (
+ return_type => undef,
+ class_name => undef,
+ class_cnick => undef,
+ param_list => undef,
+ macro_sym => undef,
+ docucomment => undef,
+ parcel => undef,
+ abstract => undef,
+ final => undef,
+ exposure => 'parcel',
+ # Private, used only by finalize().
+ novel => undef,
+ short_typedef => undef,
+);
+
+sub new {
+ my ( $class, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ my $abstract = delete $args{abstract};
+ my $final = delete $args{final};
+ my $macro_sym = delete $args{macro_sym};
+ my $novel = delete $args{novel};
+ my $short_typedef = delete $args{short_typedef};
+
+ # Validate macro_sym, derive micro_sym.
+ confess("macro_sym is required") unless defined $macro_sym;
+ confess("Invalid macro_sym: '$macro_sym'")
+ unless $macro_sym =~ /^[A-Z][A-Za-z0-9]*(?:_[A-Z0-9][A-Za-z0-9]*)*$/;
+ $args{micro_sym} = lc($macro_sym);
+
+ # Create self, add in novel member vars.
+ my $self = $class->SUPER::new(%args);
+ $self->{macro_sym} = $macro_sym;
+ $self->{abstract} = $abstract;
+ $self->{final} = $final;
+
+ # Assume that this method is novel until we discover when applying
+ # inheritance that it was overridden.
+ $self->{novel} = defined $novel ? $novel : 1;
+
+ # Verify that the first element in the arg list is a self.
+ my $args = $self->get_param_list->get_variables;
+ confess "Not enough args" unless @$args;
+ my $specifier = $args->[0]->get_type->get_specifier;
+ my ($struct_sym) = $self->{class_name} =~ /(\w+)$/;
+ confess
+ "First arg type doesn't match class: $self->{class_name} $specifier"
+ unless $specifier eq $self->get_prefix . $struct_sym;
+
+ # Cache typedef.
+ $self->{short_typedef}
+ = defined $short_typedef
+ ? $short_typedef
+ : "$self->{class_cnick}_$self->{micro_sym}_t";
+
+ return $self;
+}
+
+sub abstract { shift->{abstract} }
+sub novel { shift->{novel} }
+sub final { shift->{final} }
+sub get_macro_sym { shift->{macro_sym} }
+
+sub self_type { shift->get_param_list->get_variables->[0]->get_type }
+
+sub short_method_sym {
+ my ( $self, $invoker ) = @_;
+ confess("Missing invoker") unless $invoker;
+ return $invoker . "_$self->{macro_sym}";
+}
+
+sub full_method_sym {
+ my ( $self, $invoker ) = @_;
+ return $self->get_Prefix . $self->short_method_sym($invoker);
+}
+
+# The name of the variable which stores the method's vtable offset.
+sub full_offset_sym {
+ my ( $self, $invoker ) = @_;
+ confess("Missing invoker") unless $invoker;
+ return $self->get_Prefix . "$invoker\_$self->{macro_sym}_OFFSET";
+}
+
+sub full_callback_sym { shift->full_func_sym . "_CALLBACK" }
+sub full_override_sym { shift->full_func_sym . "_OVERRIDE" }
+
+sub short_typedef { shift->{short_typedef} }
+sub full_typedef {
+ my $self = shift;
+ return $self->get_prefix . $self->{short_typedef};
+}
+
+sub override {
+ my ( $self, $orig ) = @_;
+
+ # Check that the override attempt is legal.
+ confess( "Attempt to override final method '$orig->{micro_sym}' from "
+ . "$orig->{class_cnick} by $self->{class_cnick}" )
+ if $orig->final;
+ if ( !$self->compatible($orig) ) {
+ my $func_name = $self->full_func_sym;
+ my $orig_func = $orig->full_func_sym;
+ confess("Non-matching signatures for $func_name and $orig_func");
+ }
+
+ # Mark the Method as no longer novel.
+ $self->{novel} = 0;
+}
+
+sub compatible {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{macro_sym} eq $other->{macro_sym};
+ return 0 if ( $self->public xor $other->public );
+ my $param_list = $self->get_param_list;
+ my $other_param_list = $other->get_param_list;
+ my $arg_vars = $param_list->get_variables;
+ my $other_vars = $other_param_list->get_variables;
+ my $initial_vals = $param_list->get_initial_values;
+ my $other_vals = $other_param_list->get_initial_values;
+ return 0 unless @$arg_vars == @$other_vars;
+ return 0 unless @$initial_vals == @$other_vals;
+
+ # Validate initial values.
+ for ( my $i = 1; $i <= $#$arg_vars; $i++ ) {
+ return 0 unless $other_vars->[$i]->equals( $arg_vars->[$i] );
+ my $val = $initial_vals->[$i];
+ my $other_val = $other_vals->[$i];
+ if ( defined $val ) {
+ return 0 unless defined $other_val;
+ return 0 unless $val eq $other_val;
+ }
+ else {
+ return 0 if defined $other_val;
+ }
+ }
+
+ # Weak validation of return type to allow covariant object return types.
+ my $return_type = $self->{return_type};
+ my $other_return_type = $other->{return_type};
+ if ( $return_type->is_object ) {
+ return 0 unless $return_type->similar($other_return_type);
+ }
+ else {
+ return 0 unless $return_type->equals($other_return_type);
+ }
+
+ return 1;
+}
+
+sub finalize {
+ my $self = shift;
+ my %args;
+ $args{$_} = $self->{$_} for keys %new_PARAMS; # including short_typedef
+ $args{final} = 1;
+ my $finalized = $self->new(%args);
+ $finalized->{novel} = $self->{novel};
+ return $finalized;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Method - Metadata describing an instance method.
+
+=head1 DESCRIPTION
+
+Clownfish::Method is a specialized subclass of Clownfish::Function, with
+the first argument required to be an Obj.
+
+When compiling Clownfish code to C, Method objects generate all the code
+that Function objects do, but also create symbols for indirect invocation via
+VTable.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Method->new(
+ parcel => 'Crustacean', # default: special
+ class_name => 'Crustacean::Lobster::LobsterClaw', # required
+ class_cnick => 'LobClaw', # default: special
+ macro_sym => 'Pinch', # required
+ return_type => $void_type, # required
+ param_list => $param_list, # required
+ exposure => undef, # default: 'parcel'
+ docucomment => $docucomment, # default: undef
+ abstract => undef, # default: undef
+ final => 1, # default: undef
+ );
+
+=over
+
+=item * B<param_list> - A Clownfish::ParamList. The first element must be an
+object of the class identified by C<class_name>.
+
+=item * B<macro_sym> - The mixed case name which will be used when invoking the
+method.
+
+=item * B<abstract> - Indicate whether the method is abstract.
+
+=item * B<final> - Indicate whether the method is final.
+
+=item * B<parcel>, B<class_name>, B<class_cnick>, B<return_type>,
+B<docucomment>, - see L<Clownfish::Function>.
+
+=back
+
+=head2 abstract final get_macro_sym
+
+Getters.
+
+=head2 novel
+
+Returns true if the method's class is the first in the inheritance hierarchy
+in which the method was declared -- i.e. the method is neither inherited nor
+overridden.
+
+=head2 self_type
+
+Return the L<Clownfish::Type> for C<self>.
+
+=head2 short_method_sym
+
+ # e.g. "LobClaw_Pinch"
+ my $short_sym = $method->short_method_sym("LobClaw");
+
+Returns the symbol used to invoke the method (minus the parcel Prefix).
+
+=head2 full_method_sym
+
+ # e.g. "Crust_LobClaw_Pinch"
+ my $full_sym = $method->full_method_sym("LobClaw");
+
+Returns the fully-qualified symbol used to invoke the method.
+
+=head2 full_offset_sym
+
+ # e.g. "Crust_LobClaw_Pinch_OFFSET"
+ my $offset_sym = $method->full_offset_sym("LobClaw");
+
+Returns the fully qualified name of the variable which stores the method's
+vtable offset.
+
+=head2 full_callback_sym
+
+ # e.g. "crust_LobClaw_pinch_CALLBACK"
+ my $callback_sym = $method->full_calback_sym("LobClaw");
+
+Returns the fully qualified name of the variable which stores the method's
+Callback object.
+
+=head2 full_override_sym
+
+ # e.g. "crust_LobClaw_pinch_OVERRIDE"
+ my $override_func_sym = $method->full_override_sym("LobClaw");
+
+Returns the fully qualified name of the function which implements the callback
+to the host in the event that a host method has been defined which overrides
+this method.
+
+=head2 short_typedef
+
+ # e.g. "Claw_pinch_t"
+ my $short_typedef = $method->short_typedef;
+
+Returns the typedef symbol for this method, which is derived from the class
+nick of the first class in which the method was declared.
+
+=head2 full_typedef
+
+ # e.g. "crust_Claw_pinch_t"
+ my $full_typedef = $method->full_typedef;
+
+Returns the fully-qualified typedef symbol including parcel prefix.
+
+=head2 override
+
+ $method->override($method_being_overridden);
+
+Let the Method know that it is overriding a method which was defined in a
+parent class, and verify that the override is valid.
+
+All methods start out believing that they are "novel", because we don't know
+about inheritance until we build the hierarchy after all files have been
+parsed. override() is a way of going back and relabeling a method as
+overridden when new information has become available: in this case, that a
+parent class has defined a method with the same name.
+
+=head2 finalize
+
+ my $final_method = $method->finalize;
+
+As with override, above, this is for going back and changing the nature of a
+Method after new information has become available -- typically, when we
+discover that the method has been inherited by a "final" class.
+
+However, we don't modify the original Method as with override(). Inherited
+Method objects are shared between parent and child classes; if a shared Method
+object were to become final, it would interfere with its own inheritance. So,
+we make a copy, slightly modified to indicate that it is "final".
+
+=head2 compatible
+
+ confess("Can't override") unless $method->compatible($other);
+
+Returns true if the methods have signatures and attributes which allow
+one to override the other.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2006-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Method.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/ParamList.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/ParamList.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/ParamList.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/ParamList.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,127 @@
+use strict;
+use warnings;
+
+package Clownfish::ParamList;
+use Clownfish::Variable;
+use Clownfish::Util qw( verify_args );
+use Carp;
+
+our %new_PARAMS = (
+ variables => undef,
+ initial_values => undef,
+ variadic => undef,
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = bless { %new_PARAMS, @_, }, ref($either) || $either;
+
+ # Validate variables.
+ confess "variables must be an arrayref"
+ unless ref( $self->{variables} ) eq 'ARRAY';
+ for my $var ( @{ $self->{variables} } ) {
+ confess "invalid variable: '$var'"
+ unless ref($var) && $var->isa("Clownfish::Variable");
+ }
+
+ # Validate or init initial_values.
+ if ( defined $self->{initial_values} ) {
+ confess "variables must be an arrayref"
+ unless ref( $self->{variables} ) eq 'ARRAY';
+ my $num_init = scalar @{ $self->{initial_values} };
+ my $num_vars = $self->num_vars;
+ confess("mismatch of num vars and init values: $num_vars $num_init")
+ unless $num_init == $num_vars;
+ }
+ else {
+ my @initial_values;
+ $#initial_values = $#{ $self->{variables} };
+ $self->{initial_values} = \@initial_values;
+ }
+
+ return $self;
+}
+
+sub get_variables { shift->{variables} }
+sub get_initial_values { shift->{initial_values} }
+sub variadic { shift->{variadic} }
+sub num_vars { scalar @{ shift->{variables} } }
+
+sub to_c {
+ my $self = shift;
+ my $string = join( ', ', map { $_->local_c } @{ $self->{variables} } );
+ $string .= ", ..." if $self->{variadic};
+ return $string;
+}
+
+sub name_list {
+ my $self = shift;
+ return join( ', ', map { $_->micro_sym } @{ $self->{variables} } );
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::ParamList - parameter list.
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::ParamList->new(
+ variables => \@vars, # required
+ initial_values => \@vals, # default: undef
+ variadic => 1, # default: false
+ );
+
+=over
+
+=item * B<variables> - An array where each element is a
+L<Clownfish::Variable>.
+
+=item * B<initial_values> - If supplied, an array of default values, one for
+each variable.
+
+=item * B<variadic> - Should be true if the function is variadic.
+
+=back
+
+=head2 get_variables get_initial_values variadic
+
+Accessors.
+
+=head2 num_vars
+
+Return the number of variables in the ParamList, including "self" for methods.
+
+=head2 to_c
+
+ # Prints "Obj* self, Foo* foo, Bar* bar".
+ print $param_list->to_c;
+
+Return a list of the variable's types and names, joined by commas.
+
+=head2 name_list
+
+ # Prints "self, foo, bar".
+ print $param_list->name_list;
+
+Return the variable's names, joined by commas.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/ParamList.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parcel.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parcel.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parcel.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parcel.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,143 @@
+use strict;
+use warnings;
+
+package Clownfish::Parcel;
+use base qw( Exporter );
+use Clownfish::Util qw( verify_args );
+use Carp;
+
+our %parcels;
+
+our %singleton_PARAMS = (
+ name => undef,
+ cnick => undef,
+);
+
+# Create the default parcel.
+our $default_parcel = __PACKAGE__->singleton(
+ name => 'DEFAULT',
+ cnick => '',
+);
+
+sub default_parcel {$default_parcel}
+
+sub singleton {
+ my ( $either, %args ) = @_;
+ verify_args( \%singleton_PARAMS, %args ) or confess $@;
+ my ( $name, $cnick ) = @args{qw( name cnick )};
+
+ # Return the default parcel for either a blank name or an undefined name.
+ return $default_parcel unless $name;
+
+ # Return an existing singleton if the parcel has already been registered.
+ my $existing = $parcels{$name};
+ if ($existing) {
+ if ( $cnick and $cnick ne $existing->{cnick} ) {
+ confess( "cnick '$cnick' for parcel '$name' conflicts with "
+ . "'$existing->{cnick}'" );
+ }
+ return $existing;
+ }
+
+ # Register new parcel. Default cnick to name.
+ my $self = bless { %singleton_PARAMS, %args, }, ref($either) || $either;
+ defined $self->{cnick} or $self->{cnick} = $self->{name};
+ $parcels{$name} = $self;
+
+ # Pre-generate prefixes.
+ $self->{Prefix} = length $self->{cnick} ? "$self->{cnick}_" : "";
+ $self->{prefix} = lc( $self->{Prefix} );
+ $self->{PREFIX} = uc( $self->{Prefix} );
+
+ return $self;
+}
+
+# Accessors.
+sub get_prefix { shift->{prefix} }
+sub get_Prefix { shift->{Prefix} }
+sub get_PREFIX { shift->{PREFIX} }
+sub get_name { shift->{name} }
+sub get_cnick { shift->{cnick} }
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{name} eq $other->{name};
+ return 0 unless $self->{cnick} eq $other->{cnick};
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Parcel - Collection of code.
+
+=head1 DESCRIPTION
+
+A Parcel is a cohesive collection of code, which could, in theory, be
+published as as a single entity.
+
+Clownfish supports two-tier manual namespacing, using a prefix, an optional
+class nickname, and the local symbol:
+
+ prefix_ClassNick_local_symbol
+
+Clownfish::Parcel supports the first tier, specifying initial prefixes.
+These prefixes come in three capitalization variants: prefix_, Prefix_, and
+PREFIX_.
+
+=head1 CLASS METHODS
+
+=head2 singleton
+
+ Clownfish::Parcel->singleton(
+ name => 'Crustacean',
+ cnick => 'Crust',
+ );
+
+Add a Parcel singleton to a global registry. May be called multiple times,
+but only with compatible arguments.
+
+=over
+
+=item *
+
+B<name> - The name of the parcel.
+
+=item *
+
+B<cnick> - The C nickname for the parcel, which will be used as a prefix for
+generated global symbols. Must be mixed case and start with a capital letter.
+Defaults to C<name>.
+
+=back
+
+=head2 default_parcel
+
+ $parcel ||= Clownfish::Parcel->default_parcel;
+
+Return the singleton for default parcel, which has no prefix.
+
+=head1 OBJECT METHODS
+
+=head2 get_prefix get_Prefix get_PREFIX
+
+Return one of the three capitalization variants for the parcel's prefix.
+
+=head2 get_name get_cnick
+
+Accessors.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2006-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parcel.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parser.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parser.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parser.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parser.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,566 @@
+use strict;
+use warnings;
+
+package Clownfish::Parser;
+use base qw( Parse::RecDescent );
+
+use Clownfish::Parcel;
+use Clownfish::Type;
+use Clownfish::Type::Primitive;
+use Clownfish::Type::Integer;
+use Clownfish::Type::Float;
+use Clownfish::Type::Void;
+use Clownfish::Type::VAList;
+use Clownfish::Type::Arbitrary;
+use Clownfish::Type::Object;
+use Clownfish::Type::Composite;
+use Clownfish::Variable;
+use Clownfish::DocuComment;
+use Clownfish::Function;
+use Clownfish::Method;
+use Clownfish::Class;
+use Clownfish::CBlock;
+use Clownfish::File;
+use Carp;
+
+our $grammar = <<'END_GRAMMAR';
+
+file:
+ { Clownfish::Parser->set_parcel(undef); 0; }
+ major_block[%arg](s) eofile
+ { Clownfish::Parser->new_file( \%item, \%arg ) }
+
+major_block:
+ class_declaration[%arg]
+ | embed_c
+ | parcel_definition
+
+parcel_definition:
+ 'parcel' class_name cnick(?) ';'
+ {
+ my $parcel = Clownfish::Parser->new_parcel( \%item );
+ Clownfish::Parser->set_parcel($parcel);
+ $parcel;
+ }
+
+embed_c:
+ '__C__'
+ /.*?(?=__END_C__)/s
+ '__END_C__'
+ { Clownfish::CBlock->new( contents => $item[2] ) }
+
+class_declaration:
+ docucomment(?)
+ exposure_specifier(?) class_modifier(s?) 'class' class_name
+ cnick(?)
+ class_inheritance(?)
+ class_attribute(s?)
+ '{'
+ declaration_statement[
+ class => $item{class_name},
+ cnick => $item{'cnick(?)'}[0],
+ parent => $item{'class_inheritance(?)'}[0],
+ ](s?)
+ '}'
+ { Clownfish::Parser->new_class( \%item, \%arg ) }
+
+class_modifier:
+ 'inert'
+ | 'abstract'
+ | 'final'
+ { $item[1] }
+
+class_inheritance:
+ 'inherits' class_name
+ { $item[2] }
+
+class_attribute:
+ ':' /[a-z]+(?!\w)/
+ { $item[2] }
+
+class_name:
+ object_type_specifier ( "::" object_type_specifier )(s?)
+ { join('::', $item[1], @{ $item[2] } ) }
+
+cnick:
+ 'cnick'
+ /([A-Z][A-Za-z0-9]+)(?!\w)/
+ { $1 }
+
+declaration_statement:
+ var_declaration_statement[%arg]
+ | subroutine_declaration_statement[%arg]
+ | <error>
+
+var_declaration_statement:
+ exposure_specifier(?) variable_modifier(s?) type declarator ';'
+ {
+ $return = {
+ exposure => $item[1][0] || 'parcel',
+ modifiers => $item[2],
+ declared => Clownfish::Parser->new_var( \%item, \%arg ),
+ };
+ }
+
+subroutine_declaration_statement:
+ docucomment(?)
+ exposure_specifier(?)
+ subroutine_modifier(s?)
+ type
+ declarator
+ param_list
+ ';'
+ {
+ $return = {
+ exposure => $item[2],
+ modifiers => $item[3],
+ declared => Clownfish::Parser->new_sub( \%item, \%arg ),
+ };
+ }
+
+param_list:
+ '('
+ param_list_elem(s? /,/)
+ (/,\s*.../)(?)
+ ')'
+ {
+ Clownfish::Parser->new_param_list( $item[2], $item[3][0] ? 1 : 0 );
+ }
+
+param_list_elem:
+ param_variable assignment(?)
+ { [ $item[1], $item[2][0] ] }
+
+param_variable:
+ type declarator
+ { Clownfish::Parser->new_var(\%item); }
+
+assignment:
+ '=' scalar_constant
+ { $item[2] }
+
+type:
+ nullable(?) simple_type type_postfix(s?)
+ { Clownfish::Parser->simple_or_composite_type(\%item) }
+
+nullable:
+ 'nullable'
+
+simple_type:
+ object_type
+ | primitive_type
+ | void_type
+ | va_list_type
+ | arbitrary_type
+ { $item[1] }
+
+object_type:
+ type_qualifier(s?) object_type_specifier '*'
+ { Clownfish::Parser->new_object_type(\%item); }
+
+primitive_type:
+ c_integer_type
+ | chy_integer_type
+ | float_type
+ { $item[1] }
+
+c_integer_type:
+ type_qualifier(s?) c_integer_specifier
+ { Clownfish::Parser->new_integer_type(\%item) }
+
+chy_integer_type:
+ type_qualifier(s?) chy_integer_specifier
+ { Clownfish::Parser->new_integer_type(\%item) }
+
+float_type:
+ type_qualifier(s?) c_float_specifier
+ { Clownfish::Parser->new_float_type(\%item) }
+
+void_type:
+ type_qualifier(s?) void_type_specifier
+ { Clownfish::Parser->new_void_type(\%item) }
+
+va_list_type:
+ va_list_type_specifier
+ { Clownfish::Type::VAList->new }
+
+arbitrary_type:
+ arbitrary_type_specifier
+ { Clownfish::Parser->new_arbitrary_type(\%item); }
+
+type_qualifier:
+ 'const'
+ | 'incremented'
+ | 'decremented'
+ | 'nullable'
+
+subroutine_modifier:
+ 'inert'
+ | 'inline'
+ | 'abstract'
+ | 'final'
+ { $item[1] }
+
+exposure_specifier:
+ 'public'
+ | 'private'
+ | 'parcel'
+ | 'local'
+
+variable_modifier:
+ 'inert'
+ { $item[1] }
+
+type_specifier:
+ ( object_type_specifier
+ | primitive_type_specifier
+ | void_type_specifier
+ | va_list_type_specifier
+ | arbitrary_type_specifier
+ )
+ { $item[1] }
+
+primitive_type_specifier:
+ chy_integer_specifier
+ | c_integer_specifier
+ | c_float_specifier
+ { $item[1] }
+
+chy_integer_specifier:
+ /(?:chy_)?(bool)_t(?!\w)/
+
+c_integer_specifier:
+ /(?:(?:u?int(?:8|16|32|64)_t)|(?:char|int|short|long|size_t))(?!\w)/
+
+c_float_specifier:
+ /(?:float|double)(?!\w)/
+
+void_type_specifier:
+ /void(?!\w)/
+
+va_list_type_specifier:
+ /va_list(?!\w)/
+
+arbitrary_type_specifier:
+ /\w+_t(?!\w)/
+
+object_type_specifier:
+ /[A-Z]+[A-Z0-9]*[a-z]+[A-Za-z0-9]*(?!\w)/
+
+declarator:
+ identifier
+ { $item[1] }
+
+type_postfix:
+ '*'
+ { '*' }
+ | '[' ']'
+ { '[]' }
+ | '[' constant_expression ']'
+ { "[$item[2]]" }
+
+identifier:
+ ...!reserved_word /[a-zA-Z_]\w*/x
+ { $item[2] }
+
+docucomment:
+ /\/\*\*.*?\*\//s
+ { Clownfish::DocuComment->parse($item[1]) }
+
+constant_expression:
+ /\d+/
+ | /[A-Z_]+/
+
+scalar_constant:
+ hex_constant
+ | float_constant
+ | integer_constant
+ | string_literal
+ | 'NULL'
+ | 'true'
+ | 'false'
+
+integer_constant:
+ /(?:-\s*)?\d+/
+ { $item[1] }
+
+hex_constant:
+ /0x[a-fA-F0-9]+/
+ { $item[1] }
+
+float_constant:
+ /(?:-\s*)?\d+\.\d+/
+ { $item[1] }
+
+string_literal:
+ /"(?:[^"\\]|\\.)*"/
+ { $item[1] }
+
+reserved_word:
+ /(const|double|enum|extern|float|register|signed|sizeof
+ |inert|struct|typedef|union|unsigned|void)(?!\w)/x
+ | chy_integer_specifier
+ | c_integer_specifier
+
+eofile:
+ /^\Z/
+
+END_GRAMMAR
+
+sub new { return shift->SUPER::new($grammar) }
+
+sub strip_plain_comments {
+ my ( $self, $text ) = @_;
+ while ( $text =~ m#(/\*[^*].*?\*/)#ms ) {
+ my $blanked = $1;
+ $blanked =~ s/\S/ /g;
+ $text =~ s#/\*[^*].*?\*/#$blanked#ms;
+ }
+ return $text;
+}
+
+our $parcel = undef;
+sub set_parcel { $parcel = $_[1] }
+
+sub new_integer_type {
+ my ( undef, $item ) = @_;
+ my $specifier = $item->{c_integer_specifier}
+ || $item->{chy_integer_specifier};
+ my %args = ( specifier => $specifier );
+ $args{$_} = 1 for @{ $item->{'type_qualifier(s?)'} };
+ return Clownfish::Type::Integer->new(%args);
+}
+
+sub new_float_type {
+ my ( undef, $item ) = @_;
+ my %args = ( specifier => $item->{c_float_specifier} );
+ $args{$_} = 1 for @{ $item->{'type_qualifier(s?)'} };
+ return Clownfish::Type::Float->new(%args);
+}
+
+sub new_void_type {
+ my ( undef, $item ) = @_;
+ my %args = ( specifier => $item->{void_type_specifier} );
+ $args{$_} = 1 for @{ $item->{'type_qualifier(s?)'} };
+ return Clownfish::Type::Void->new(%args);
+}
+
+sub new_arbitrary_type {
+ my ( undef, $item ) = @_;
+ return Clownfish::Type::Arbitrary->new(
+ specifier => $item->{arbitrary_type_specifier},
+ parcel => $parcel,
+ );
+}
+
+sub new_object_type {
+ my ( undef, $item ) = @_;
+ my %args = (
+ specifier => $item->{object_type_specifier},
+ parcel => $parcel,
+ );
+ $args{$_} = 1 for @{ $item->{'type_qualifier(s?)'} };
+ return Clownfish::Type::Object->new(%args);
+}
+
+sub simple_or_composite_type {
+ my ( undef, $item ) = @_;
+ my $simple_type = $item->{simple_type};
+ my $postfixes = $item->{'type_postfix(s?)'};
+ my $nullable = scalar @{ $item->{'nullable(?)'} } ? 1 : undef;
+ my $type;
+
+ if ( !@$postfixes ) {
+ if ($nullable) {
+ my $type_class = ref($simple_type);
+ confess "$type_class can't be 'nullable'" unless
+ $simple_type->isa("Clownfish::Type::Object");
+ $simple_type->set_nullable($nullable);
+ }
+ return $simple_type;
+ }
+ else {
+ my %args = (
+ child => $simple_type,
+ indirection => 0,
+ nullable => $nullable,
+ );
+ for my $postfix (@$postfixes) {
+ if ( $postfix =~ /\[/ ) {
+ $args{array} ||= '';
+ $args{array} .= $postfix;
+ }
+ elsif ( $postfix eq '*' ) {
+ $args{indirection}++;
+ }
+ }
+ return Clownfish::Type::Composite->new(%args);
+ }
+}
+
+sub new_var {
+ my ( undef, $item, $arg ) = @_;
+ my $exposure = $item->{'exposure_specifier(?)'}[0];
+ my %args = $exposure ? ( exposure => $exposure ) : ();
+ if ($arg) {
+ $args{class_name} = $arg->{class} if $arg->{class};
+ $args{class_cnick} = $arg->{cnick} if $arg->{cnick};
+ }
+ return Clownfish::Variable->new(
+ parcel => $parcel,
+ type => $item->{type},
+ micro_sym => $item->{declarator},
+ %args,
+ );
+}
+
+sub new_param_list {
+ my ( undef, $param_list_elems, $variadic ) = @_;
+ my @vars = map { $_->[0] } @$param_list_elems;
+ my @vals = map { $_->[1] } @$param_list_elems;
+ return Clownfish::ParamList->new(
+ variables => \@vars,
+ initial_values => \@vals,
+ variadic => $variadic,
+ );
+}
+
+sub new_sub {
+ my ( undef, $item, $arg ) = @_;
+ my $class;
+ my $modifiers = $item->{'subroutine_modifier(s?)'};
+ my $docucom = $item->{'docucomment(?)'}[0];
+ my $exposure = $item->{'exposure_specifier(?)'}[0];
+ my $inert = scalar grep { $_ eq 'inert' } @$modifiers;
+ my %extra_args = $exposure ? ( exposure => $exposure ) : ();
+
+ if ($inert) {
+ $class = 'Clownfish::Function';
+ $extra_args{micro_sym} = $item->{declarator};
+ $extra_args{inline} = scalar grep { $_ eq 'inline' } @$modifiers;
+ }
+ else {
+ $class = 'Clownfish::Method';
+ $extra_args{macro_sym} = $item->{declarator};
+ $extra_args{abstract} = scalar grep { $_ eq 'abstract' } @$modifiers;
+ $extra_args{final} = scalar grep { $_ eq 'final' } @$modifiers;
+ }
+
+ return $class->new(
+ parcel => $parcel,
+ docucomment => $docucom,
+ class_name => $arg->{class},
+ class_cnick => $arg->{cnick},
+ return_type => $item->{type},
+ param_list => $item->{param_list},
+ %extra_args,
+ );
+}
+
+sub new_class {
+ my ( undef, $item, $arg ) = @_;
+ my ( @member_vars, @inert_vars, @functions, @methods );
+ my $source_class = $arg->{source_class} || $item->{class_name};
+ my %class_modifiers
+ = map { ( $_ => 1 ) } @{ $item->{'class_modifier(s?)'} };
+ my %class_attributes
+ = map { ( $_ => 1 ) } @{ $item->{'class_attribute(s?)'} };
+
+ for my $declaration ( @{ $item->{'declaration_statement(s?)'} } ) {
+ my $declared = $declaration->{declared};
+ my $exposure = $declaration->{exposure};
+ my $modifiers = $declaration->{modifiers};
+ my $inert = ( scalar grep {/inert/} @$modifiers ) ? 1 : 0;
+ my $subs = $inert ? \@functions : \@methods;
+ my $vars = $inert ? \@inert_vars : \@member_vars;
+
+ if ( $declared->isa('Clownfish::Variable') ) {
+ push @$vars, $declared;
+ }
+ else {
+ push @$subs, $declared;
+ }
+ }
+
+ return Clownfish::Class->create(
+ parcel => $parcel,
+ class_name => $item->{class_name},
+ cnick => $item->{'cnick(?)'}[0],
+ parent_class_name => $item->{'class_inheritance(?)'}[0],
+ member_vars => \@member_vars,
+ functions => \@functions,
+ methods => \@methods,
+ inert_vars => \@inert_vars,
+ docucomment => $item->{'docucomment(?)'}[0],
+ source_class => $source_class,
+ inert => $class_modifiers{inert},
+ final => $class_modifiers{final},
+ attributes => \%class_attributes,
+ );
+}
+
+sub new_file {
+ my ( undef, $item, $arg ) = @_;
+
+ return Clownfish::File->new(
+ parcel => $parcel,
+ blocks => $item->{'major_block(s)'},
+ source_class => $arg->{source_class},
+ );
+}
+
+sub new_parcel {
+ my ( undef, $item ) = @_;
+ Clownfish::Parcel->singleton(
+ name => $item->{class_name},
+ cnick => $item->{'cnick(?)'}[0],
+ );
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Parser - Parse Clownfish header files.
+
+=head1 SYNOPSIS
+
+ my $class_def = $parser->class($class_text);
+
+=head1 DESCRIPTION
+
+Clownfish::Parser is a combined lexer/parser which parses Clownfish header
+files. It is not at all strict, as it relies heavily on the C parser to pick
+up errors such as misspelled type names.
+
+=head1 METHODS
+
+=head2 new
+
+Constructor, takes no arguments.
+
+=head2 strip_plain_comments
+
+ my $stripped = $parser->strip_plain_comments($code_with_comments);
+
+Remove plain C comments from supplied code. All non-whitespace characters are
+turned to spaces; all whitespace characters are preserved, so that the number
+of lines is consistent between before and after.
+
+JavaDoc-syntax "DocuComments", which begin with "/**" are left alone.
+
+This is a sloppy implementation which will mangle quoted comments and such.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Parser.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Symbol.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Symbol.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Symbol.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Symbol.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,208 @@
+use strict;
+use warnings;
+
+package Clownfish::Symbol;
+use Clownfish::Parcel;
+use Clownfish::Util qw( a_isa_b );
+use Scalar::Util qw( blessed );
+use Carp;
+
+my %new_PARAMS = (
+ parcel => undef,
+ exposure => undef,
+ class_name => undef,
+ class_cnick => undef,
+ micro_sym => undef,
+);
+
+my $struct_regex = qr/[A-Z]+[A-Z0-9]*[a-z]+[A-Za-z0-9]*/;
+my $class_name_regex = qr/^$struct_regex(::$struct_regex)*$/;
+
+sub new {
+ my ( $either, %args ) = @_;
+
+ # Acquire a Parcel.
+ my $parcel = $args{parcel};
+ if ( !defined $parcel ) {
+ $parcel = Clownfish::Parcel->default_parcel;
+ }
+ elsif ( blessed($parcel) ) {
+ confess("Not a Clownfish::Parcel")
+ unless $parcel->isa('Clownfish::Parcel');
+ }
+ else {
+ $parcel = Clownfish::Parcel->singleton( name => $args{parcel} );
+ }
+
+ # Create the object.
+ my $self = bless { %new_PARAMS, %args, parcel => $parcel },
+ ref($either) || $either;
+
+ # Validate micro_sym.
+ confess "micro_sym is required" unless $self->{micro_sym};
+ confess("Invalid micro_sym: '$self->{micro_sym}'")
+ unless $self->{micro_sym} =~ /^[A-Za-z_][A-Za-z0-9_]*$/;
+
+ # Validate exposure.
+ confess("Invalid value for 'exposure': $self->{exposure}")
+ unless $self->{exposure} =~ /^(?:public|parcel|private|local)$/;
+
+ # Validate class name, validate or derive class_cnick.
+ if ( defined $self->{class_name} ) {
+ confess("Invalid class name: $self->{class_name}")
+ unless $self->{class_name} =~ $class_name_regex;
+ if ( !defined $self->{class_cnick} ) {
+ $self->{class_name} =~ /(\w+)$/;
+ $self->{class_cnick} = $1;
+ }
+ confess("Invalid class_cnick: $self->{class_cnick}")
+ unless $self->{class_cnick} =~ /^[A-Z]+[A-Za-z0-9]*$/;
+ }
+ elsif ( defined $self->{class_cnick} ) {
+ # Sanity check class_cnick without class_name.
+ confess("Can't supply class_cnick without class_name");
+ }
+
+ return $self;
+}
+
+sub get_parcel { shift->{parcel} }
+sub get_class_name { shift->{class_name} }
+sub get_class_cnick { shift->{class_cnick} }
+sub micro_sym { shift->{micro_sym} }
+
+sub get_prefix { shift->{parcel}->get_prefix; }
+sub get_Prefix { shift->{parcel}->get_Prefix; }
+sub get_PREFIX { shift->{parcel}->get_PREFIX; }
+
+sub public { shift->{exposure} eq 'public' }
+sub private { shift->{exposure} eq 'private' }
+sub parcel { shift->{exposure} eq 'parcel' }
+sub local { shift->{exposure} eq 'local' }
+
+sub full_sym {
+ my $self = shift;
+ my $prefix = $self->get_prefix;
+ return "$prefix$self->{class_cnick}_$self->{micro_sym}";
+}
+
+sub short_sym {
+ my $self = shift;
+ return "$self->{class_cnick}_$self->{micro_sym}";
+}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless a_isa_b( $other, __PACKAGE__ );
+ return 0 unless $self->{micro_sym} eq $other->{micro_sym};
+ return 0 unless $self->{parcel}->equals( $other->{parcel} );
+ if ( defined $self->{exposure} ) {
+ return 0 unless defined $other->{exposure};
+ return 0 unless $self->{exposure} eq $other->{exposure};
+ }
+ else {
+ return 0 if defined $other->{exposure};
+ }
+ if ( defined $self->{class_name} ) {
+ return 0 unless defined $other->{class_name};
+ return 0 unless $self->{class_name} eq $other->{class_name};
+ }
+ else {
+ return 0 if defined $other->{class_name};
+ }
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Symbol - Abstract base class for Clownfish symbols.
+
+=head1 DESCRIPTION
+
+Clownfish::Symbol serves as an abstract parent class for entities which may
+live in the global namespace, such as classes, functions, methods, and
+variables.
+
+=head1 CONSTRUCTOR
+
+ my $symbol = MySymbol->new(
+ parcel => 'Crustacean', # default: special
+ exposure => 'parcel', # required
+ class_name => 'Crustacean::Lobster', # default: undef
+ class_cnick => undef, # default: special
+ micro_sym => 'average_lifespan', # required
+ );
+
+=over
+
+=item * B<parcel> - A Clownfish::Parcel, or a string that can be used to
+create/retrieve one. If not supplied, will be assigned to the default Parcel.
+
+=item * B<exposure> - The scope in which the symbol is exposed. Must be
+'public', 'parcel', 'private', or 'local'.
+
+=item * B<class_name> - A optional class name, consisting of one or more
+components separated by "::". Each component must start with a capital
+letter, contain at least one lower-case letter, and consist entirely of the
+characters [A-Za-z0-9].
+
+=item * B<class_cnick> - The C nickname associated with the supplied class
+name. If not supplied, will be derived if possible from C<class_name> by
+extracting the last class name component.
+
+=item * B<micro_sym> - The local identifier for the symbol.
+
+=back
+
+=head1 OBJECT METHODS
+
+=head2 get_parcel get_class_name get_class_cnick micro_sym
+
+Getters.
+
+=head2 get_prefix get_Prefix get_PREFIX
+
+Get a string prefix, delegating to C<parcel> member var.
+
+=head2 public parcel private local
+
+ if ( $sym->public ) { do_x() }
+ elsif ( $sym->parcel ) { do_y() }
+
+Indicate whether the symbol matches a given access level.
+
+=head2 equals
+
+ do_stuff() if $sym->equals($other_sym);
+
+Returns true if the symbols are "equal", false otherwise.
+
+=head2 short_sym
+
+ # e.g. "Lobster_average_lifespan"
+ print $symbol->short_sym;
+
+Returns the C representation for the symbol minus the parcel's prefix.
+
+=head2 full_sym
+
+ # e.g. "crust_Lobster_average_lifespan"
+ print $symbol->full_sym;
+
+Returns the fully qualified C representation for the symbol.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Symbol.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,167 @@
+use strict;
+use warnings;
+
+package Clownfish::Type;
+use Clownfish::Parcel;
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => undef,
+ indirection => undef,
+ parcel => undef,
+ c_string => undef,
+);
+
+sub new {
+ my $either = shift;
+ my $package = ref($either) || $either;
+ confess( __PACKAGE__ . "is an abstract class" )
+ if $package eq __PACKAGE__;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = bless { %new_PARAMS, @_, }, $package;
+ if ( defined $self->{parcel} ) {
+ if ( !blessed( $self->{parcel} ) ) {
+ $self->{parcel}
+ = Clownfish::Parcel->singleton( name => $self->{parcel} );
+ }
+ confess("Not a Clownfish::Parcel")
+ unless $self->{parcel}->isa('Clownfish::Parcel');
+ }
+ return $self;
+}
+
+sub get_specifier { shift->{specifier} }
+sub get_parcel { shift->{parcel} }
+sub const { shift->{const} }
+sub nullable { shift->{nullable} }
+
+sub is_object {0}
+sub is_primitive {0}
+sub is_integer {0}
+sub is_floating {0}
+sub is_void {0}
+sub is_composite {0}
+sub is_string_type {0}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless blessed($other);
+ return 0 unless $other->isa(__PACKAGE__);
+ return 1;
+}
+
+sub to_c { shift->{c_string} }
+sub set_c_string { $_[0]->{c_string} = $_[1] }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type - A variable's type.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = MyType->new(
+ specifier => 'char', # default undef
+ indirection => undef, # default 0
+ const => 1, # default undef
+ parcel => undef, # default undef
+ c_string => undef, # default undef
+ );
+
+Abstract constructor.
+
+=over
+
+=item *
+
+B<specifier> - The C name for the type, not including any indirection or array
+subscripts.
+
+=item *
+
+B<indirection> - integer indicating level of indirection. Example: the C type
+"float**" has a specifier of "float" and indirection 2.
+
+=item *
+
+B<const> - should be true if the type is const.
+
+=item *
+
+B<parcel> - A Clownfish::Parcel or a parcel name.
+
+=item *
+
+B<c_string> - The C representation of the type.
+
+=back
+
+=head2 equals
+
+ do_stuff() if $type->equals($other);
+
+Returns true if two Clownfish::Type objects are equivalent. The default
+implementation merely checks that C<$other> is a Clownfish::Type object, so
+it should be overridden in all subclasses.
+
+=head2 to_c
+
+ # Declare variable "foo".
+ print $type->to_c . " foo;\n";
+
+Return the C representation of the type.
+
+=head2 set_c_string
+
+Set the C representation of the type.
+
+=head2 get_specifier get_parcel const nullable
+
+Accessors.
+
+=head2 is_object is_primitive is_integer is_floating is_composite is_void
+
+ do_stuff() if $type->is_object;
+
+Shorthand for various $type->isa($package) calls.
+
+=over
+
+=item * is_object: Clownfish::Type::Object
+
+=item * is_primitive: Clownfish::Type::Primitive
+
+=item * is_integer: Clownfish::Type::Integer
+
+=item * is_floating: Clownfish::Type::Float
+
+=item * is_void: Clownfish::Type::Void
+
+=item * is_composite: Clownfish::Type::Composite
+
+=back
+
+=head2 is_string_type
+
+Returns true if $type represents a Clownfish type which holds unicode
+strings.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Arbitrary.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Arbitrary.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Arbitrary.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Arbitrary.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,103 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Arbitrary;
+use base qw( Clownfish::Type );
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ parcel => undef,
+ specifier => undef,
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = $either->SUPER::new(@_);
+
+ # Validate specifier.
+ confess("illegal specifier: '$self->{specifier}")
+ unless $self->{specifier} =~ /^\w+$/;
+
+ if ( $self->{specifier} =~ /^[A-Z]/ and $self->{parcel} ) {
+ my $prefix = $self->{parcel}->get_prefix;
+ # Add $prefix to what appear to be namespaced types.
+ $self->{specifier} = $prefix . $self->{specifier}
+ unless $self->{specifier} =~ /^$prefix/;
+ }
+
+ # Cache C representation.
+ my $string = $self->const ? 'const ' : '';
+ $string .= $self->{specifier};
+ $self->set_c_string($string);
+
+ return $self;
+}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless blessed($other);
+ return 0 unless $other->isa(__PACKAGE__);
+ return 0 unless $self->{specifier} eq $other->{specifier};
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::Arbitrary - An arbitrary type.
+
+=head1 DESCRIPTION
+
+The "arbitrary" type class is a hack that spares us from having to support C
+types with complex declaration syntaxes -- such as unions, structs, enums, or
+function pointers -- from within Clownfish itself.
+
+The only constraint is that the C<specifier> must end in "_t". This allows us
+to create complex types in a C header file...
+
+ typedef union { float f; int i; } floatint_t;
+
+... pound-include the C header, then use the resulting typedef in a Clownfish
+header file and have it parse as an "arbitrary" type.
+
+ floatint_t floatint;
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type->new(
+ specifier => 'floatint_t', # required
+ parcel => 'Crustacean', # default: undef
+ );
+
+=over
+
+=item * B<specifier> - The name of the type, which must end in "_t".
+
+=item * B<parcel> - A L<Clownfish::Parcel> or a parcel name.
+
+=back
+
+If C<parcel> is supplied and C<specifier> begins with a capital letter, the
+Parcel's prefix will be prepended to the specifier:
+
+ foo_t -> foo_t # no prefix prepending
+ Lobster_foo_t -> crust_Lobster_foo_t # prefix prepended
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Arbitrary.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Composite.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Composite.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Composite.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Composite.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,117 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Composite;
+use base qw( Clownfish::Type );
+use Clownfish::Util qw( verify_args a_isa_b );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ child => undef,
+ indirection => undef,
+ array => undef,
+ nullable => undef,
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ my $array = delete $args{array};
+ my $child = delete $args{child};
+ my $nullable = delete $args{nullable};
+ confess("Missing required param 'child'")
+ unless a_isa_b( $child, "Clownfish::Type" );
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ my $self = $either->SUPER::new(%args);
+ $self->{child} = $child;
+ $self->{array} = $array;
+ $self->{nullable} = $nullable;
+
+ # Default indirection to 0.
+ $self->{indirection} ||= 0;
+
+ # Cache C representation.
+ # NOTE: Array postfixes are NOT included.
+ my $string = $self->{child}->to_c;
+ for ( my $i = 0; $i < $self->{indirection}; $i++ ) {
+ $string .= '*';
+ }
+ $self->set_c_string($string);
+
+ return $self;
+}
+
+sub get_specifier { shift->{child}->get_specifier }
+sub get_array { shift->{array} }
+sub is_composite {1}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{indirection} == $other->{indirection};
+ return 0 unless $self->{child}->equals( $other->{child} );
+ return 0 if ( $self->{array} xor $other->{array} );
+ return 0 if ( $self->{array} and $self->{array} ne $other->{array} );
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::Composite - A composite type, e.g. Obj**.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::Composite->new(
+ specifier => 'char', # required
+ indirection => undef, # default 0
+ array => '[]', # default undef,
+ const => 1, # default undef
+ );
+
+=over
+
+=item *
+
+B<specifier> - The name of the type, not including any indirection or array
+subscripts. If the type begins with a capital letter, it will be assumed to
+be an object type.
+
+=item *
+
+B<indirection> - integer indicating level of indirection. Example: the C type
+"float**" has a specifier of "float" and indirection 2.
+
+=item *
+
+B<array> - A string describing an array postfix.
+
+=item *
+
+B<const> - should be 1 if the type is const.
+
+=back
+
+=head2 get_array
+
+Accessor for the array string.
+
+=head2 get_specifier
+
+Overridden to return the child Type's specifier.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Composite.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Float.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Float.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Float.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Float.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,77 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Float;
+use base qw( Clownfish::Type::Primitive );
+use Clownfish::Util qw( verify_args );
+use Carp;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => undef,
+);
+
+our %specifiers = (
+ float => undef,
+ double => undef,
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ confess("Unknown specifier: '$args{specifier}'")
+ unless exists $specifiers{ $args{specifier} };
+
+ # Cache the C representation of this type.
+ my $c_string = $args{const} ? "const $args{specifier}" : $args{specifier};
+
+ return $either->SUPER::new( %args, c_string => $c_string );
+}
+
+sub is_floating {1}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::Float - A primitive Type representing a floating point
+number.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::Float represents floating point types of various widths.
+Currently only two are supported:
+
+ float
+ double
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::Float->new(
+ const => 1, # default: undef
+ specifier => 'double', # required
+ );
+
+=over
+
+=item * B<const> - Should be true if the type is const.
+
+=item * B<specifier> - Must match one of the supported types.
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Float.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Integer.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Integer.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Integer.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Integer.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,114 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Integer;
+use base qw( Clownfish::Type::Primitive );
+use Clownfish::Util qw( verify_args );
+use Carp;
+use Config;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => undef,
+);
+
+our %specifiers = (
+ bool_t => $Config{intsize},
+ int8_t => 1,
+ int16_t => 2,
+ int32_t => 4,
+ int64_t => 8,
+ uint8_t => 1,
+ uint16_t => 2,
+ uint32_t => 4,
+ uint64_t => 8,
+ char => 1,
+ int => $Config{intsize},
+ short => $Config{shortsize},
+ long => $Config{longsize},
+ size_t => $Config{sizesize},
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ my $sizeof = $specifiers{ $args{specifier} }
+ or confess("Unknown specifier: '$args{specifier}'");
+
+ # Cache the C representation of this type.
+ my $c_string = $args{const} ? 'const ' : '';
+ if ( $args{specifier} eq 'bool_t' ) {
+ $c_string .= "chy_";
+ }
+ $c_string .= $args{specifier};
+
+ my $self = $either->SUPER::new( %args, c_string => $c_string );
+ $self->{sizeof} = $sizeof;
+ return $self;
+}
+
+sub is_integer {1}
+sub sizeof { shift->{sizeof} }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::Integer - A primitive Type representing an integer.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::Integer holds integer types of various widths and various
+styles. Support is limited to a subset of the standard C integer types:
+
+ int8_t
+ int16_t
+ int32_t
+ int64_t
+ uint8_t
+ uint16_t
+ uint32_t
+ uint64_t
+ char
+ short
+ int
+ long
+ size_t
+
+Many others are not supported: "signed" or "unsigned" anything, "long long",
+"ptrdiff_t", "off_t", etc.
+
+The following Charmonizer typedefs are supported:
+
+ bool_t
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::Integer->new(
+ const => 1, # default: undef
+ specifier => 'char', # required
+ );
+
+=over
+
+=item * B<const> - Should be true if the type is const.
+
+=item * B<specifier> - Must match one of the supported types.
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Integer.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Object.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Object.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Object.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Object.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,159 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Object;
+use base qw( Clownfish::Type );
+use Clownfish::Parcel;
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => undef,
+ indirection => 1,
+ parcel => undef,
+ incremented => 0,
+ decremented => 0,
+ nullable => 0,
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ my $incremented = delete $args{incremented} || 0;
+ my $decremented = delete $args{decremented} || 0;
+ my $nullable = delete $args{nullable} || 0;
+ my $indirection = delete $args{indirection};
+ $indirection = 1 unless defined $indirection;
+ my $self = $either->SUPER::new(%args);
+ $self->{incremented} = $incremented;
+ $self->{decremented} = $decremented;
+ $self->{indirection} = $indirection;
+ $self->{nullable} = $nullable;
+ $self->{parcel} ||= Clownfish::Parcel->default_parcel;
+ my $prefix = $self->{parcel}->get_prefix;
+
+ # Validate params.
+ confess("Indirection must be 1") unless $self->{indirection} == 1;
+ confess("Can't be both incremented and decremented")
+ if ( $incremented && $decremented );
+ confess("Missing required param 'specifier'")
+ unless defined $self->{specifier};
+ confess("Illegal specifier: '$self->{specifier}")
+ unless $self->{specifier}
+ =~ /^(?:$prefix)?[A-Z][A-Za-z0-9]*[a-z]+[A-Za-z0-9]*(?!\w)/;
+
+ # Add $prefix if necessary.
+ $self->{specifier} = $prefix . $self->{specifier}
+ unless $self->{specifier} =~ /^$prefix/;
+
+ # Cache C representation.
+ my $string = $self->const ? 'const ' : '';
+ $string .= "$self->{specifier}*";
+ $self->set_c_string($string);
+
+ # Cache boolean indicating whether this type is a string type.
+ $self->{is_string_type} = $self->{specifier} =~ /CharBuf/ ? 1 : 0;
+
+ return $self;
+}
+
+sub is_object {1}
+sub incremented { shift->{incremented} }
+sub decremented { shift->{decremented} }
+sub is_string_type { shift->{is_string_type} }
+
+sub set_nullable { $_[0]->{nullable} = $_[1] }
+
+sub similar {
+ my ( $self, $other ) = @_;
+ for (qw( const incremented decremented nullable )) {
+ return 0 if ( $self->{$_} xor $other->{$_} );
+ }
+ return 1;
+}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->similar($other);
+ return 0 unless $self->{specifier} eq $other->{specifier};
+ return 1;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Clownfish::Type::Clownfish - An object Type.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::Object supports object types for all classes. The type's
+C<specifier> must match the last component of the class name -- i.e. for the
+class "Crustacean::Lobster" it must be "Lobster".
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::Object->new(
+ specifier => "Lobster", # required
+ parcel => "Crustacean", # default: the default Parcel.
+ const => undef, # default undef
+ indirection => 1, # default 1
+ incremented => 1, # default 0
+ decremented => 0, # default 0
+ nullable => 1, # default 0
+ );
+
+=over
+
+=item * B<specifier> - Required. Must follow the rules for
+L<Clownfish::Class> class name components.
+
+=item * B<parcel> - A L<Clownfish::Parcel> or a parcel name.
+
+=item * B<const> - Should be true if the Type is const. Note that this refers
+to the object itself and not the pointer.
+
+=item * B<indirection> - Level of indirection. Must be 1 if supplied.
+
+=item * B<incremented> - Indicate whether the caller must take responsibility
+for an added refcount.
+
+=item * B<decremented> - Indicate whether the caller must account for
+for a refcount decrement.
+
+=item * B<nullable> - Indicate whether the object specified by this type may
+be NULL.
+
+=back
+
+The Parcel's prefix will be prepended to the specifier by new().
+
+=head2 incremented
+
+Returns true if the Type is incremented.
+
+=head2 decremented
+
+Returns true if the Type is decremented.
+
+=head2 similar
+
+ do_stuff() if $type->similar($other_type);
+
+Weak checking of type which allows for covariant return types.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Object.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Primitive.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Primitive.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Primitive.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Primitive.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Primitive;
+use base qw( Clownfish::Type );
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => undef,
+ c_string => undef,
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ my $package = ref($either) || $either;
+ confess( __PACKAGE__ . " is abstract" ) if $package eq __PACKAGE__;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ return bless { %new_PARAMS, %args }, $package;
+}
+
+sub is_primitive {1}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless blessed($other);
+ return 0 unless $other->isa(__PACKAGE__);
+ return 0 unless $self->{specifier} eq $other->{specifier};
+ return 0 if ( $self->{const} xor $other->{const} );
+ return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::Primitive - Abstract base class for primitive types.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::Primitive serves as a common parent class for primitive
+types including L<Clownfish::Type::Integer> and
+L<Clownfish::Type::Float>.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = MyPrimitiveType->new(
+ const => 1, # default: undef
+ specifier => 'char', # default: undef
+ c_string => 'char', # default: undef
+ );
+
+Abstract constructor. See L<Clownfish::Type> for parameter definitions.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Primitive.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/VAList.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/VAList.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/VAList.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/VAList.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,65 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::VAList;
+use base qw( Clownfish::Type );
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = ( specifier => 'va_list' );
+
+sub new {
+ my ( $either, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ return $either->SUPER::new(
+ specifier => 'va_list',
+ c_string => 'va_list'
+ );
+}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless blessed($other);
+ return 0 unless $other->isa(__PACKAGE__);
+ return 1;
+}
+
+1;
+
+__END__
+
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Type::VAList - A Type to support C's va_list.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::VAList represents the C va_list type, from stdarg.h.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::VAList->new(
+ specifier => 'va_list', # default: va_list
+ );
+
+=over
+
+=item * B<specifier>. Must be "va_list" if supplied.
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/VAList.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Void.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Void.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Void.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Void.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,76 @@
+use strict;
+use warnings;
+
+package Clownfish::Type::Void;
+use base qw( Clownfish::Type );
+use Clownfish::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+ const => undef,
+ specifier => 'void',
+);
+
+sub new {
+ my ( $either, %args ) = @_;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ my $c_string = $args{const} ? "const void" : "void";
+ return $either->SUPER::new(
+ %new_PARAMS,
+ %args,
+ specifier => 'void',
+ c_string => $c_string
+ );
+}
+
+sub is_void {1}
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless blessed($other);
+ return 0 unless $other->isa(__PACKAGE__);
+ return 1;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Clownfish::Type::Void - The void Type.
+
+=head1 DESCRIPTION
+
+Clownfish::Type::Void is used to represent a void return type. It is also
+used in conjuction with with L<Clownfish::Type::Composite> to support the
+C<void*> opaque pointer type.
+
+=head1 METHODS
+
+=head2 new
+
+ my $type = Clownfish::Type::Void->new(
+ specifier => 'void', # default: void
+ const => 1, # default: undef
+ );
+
+=over
+
+=item * B<specifier> - Must be "void" if supplied.
+
+=item * B<const> - Should be true if the type is const. (Useful in the
+context of C<const void*>).
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Type/Void.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Util.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Util.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Util.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Util.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,164 @@
+use strict;
+use warnings;
+
+package Clownfish::Util;
+use base qw( Exporter );
+use Scalar::Util qw( blessed );
+use Carp;
+use Fcntl;
+
+our @EXPORT_OK = qw(
+ slurp_file
+ current
+ strip_c_comments
+ verify_args
+ a_isa_b
+ write_if_changed
+);
+
+sub slurp_file {
+ my $path = shift;
+ open( my $fh, '<', $path ) or confess("Can't open '$path': $!");
+ local $/;
+ return <$fh>;
+}
+
+sub current {
+ my ( $orig, $dest ) = @_;
+ my $bubble_time = time;
+ $orig = [$orig] unless ref($orig) eq 'ARRAY';
+ $dest = [$dest] unless ref($dest) eq 'ARRAY';
+
+ # If a destination file doesn't exist, we're not current.
+ for (@$dest) {
+ return 0 unless -e $_;
+ }
+
+ # Find the oldest file from the destination group.
+ for (@$dest) {
+ my $candidate = ( stat($_) )[9];
+ $bubble_time = $candidate if $candidate < $bubble_time;
+ }
+
+ # If any source file is newer than the oldest dest, we're not current.
+ for (@$orig) {
+ confess "Missing source file '$_'" unless -e $_;
+ my $candidate = ( stat($_) )[9];
+ return 0 if $candidate > $bubble_time;
+ }
+
+ # Current!
+ return 1;
+}
+
+sub strip_c_comments {
+ my $c_code = shift;
+ $c_code =~ s#/\*.*?\*/##gsm;
+ return $c_code;
+}
+
+sub verify_args {
+ my $defaults = shift; # leave the rest of @_ intact
+
+ # Verify that args came in pairs.
+ if ( @_ % 2 ) {
+ my ( $package, $filename, $line ) = caller(1);
+ $@ = "Parameter error: odd number of args at $filename line $line\n";
+ return 0;
+ }
+
+ # Verify keys, ignore values.
+ while (@_) {
+ my ( $var, undef ) = ( shift, shift );
+ next if exists $defaults->{$var};
+ my ( $package, $filename, $line ) = caller(1);
+ $@ = "Invalid parameter: '$var' at $filename line $line\n";
+ return 0;
+ }
+
+ return 1;
+}
+
+sub a_isa_b {
+ my ( $thing, $class ) = @_;
+ return 0 unless blessed($thing);
+ return $thing->isa($class);
+}
+
+sub write_if_changed {
+ my ( $path, $content ) = @_;
+ my $write = 1;
+ if ( -e $path ) {
+ open( my $fh, '<', $path ) or confess "Can't open '$path': $!";
+ my $orig = do { local $/; <$fh> };
+ return if $orig eq $content;
+ }
+ unlink $path;
+ sysopen( my $fh, $path, O_CREAT | O_EXCL | O_WRONLY )
+ or confess "Can't open '$path': $!";
+ print $fh $content;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Util - Miscellaneous helper functions.
+
+=head1 DESCRIPTION
+
+Clownfish::Util provides a few convenience functions used internally by
+other Clownfish modules.
+
+=head1 FUNCTIONS
+
+=head2 slurp_file
+
+ my $foo_contents = slurp_file('foo.txt');
+
+Open a file, read it in, return its contents. Assumes either binary data or
+text with an encoding of Latin-1.
+
+=head2 current
+
+ compile('foo.c') unless current( 'foo.c', 'foo.o' );
+
+Given two elements, which may be either scalars or arrays, verify that
+everything in the second group exists and was created later than anything in
+the first group.
+
+=head2 verify_args
+
+ verify_args( \%defaults, @_ ) or confess $@;
+
+Verify that named parameters exist in a defaults hash. Returns false and sets
+$@ if a problem is detected.
+
+=head2 strip_c_comments
+
+ my $c_minus_comments = strip_c_comments($c_source_code);
+
+Quick 'n' dirty stripping of C comments. Will massacre stuff like comments
+embedded in string literals, so watch out.
+
+=head2 write_if_changed
+
+ write_if_changed( $path, $content );
+
+Test whether there's a file at C<$path> which already matches C<$content>
+exactly. If something has changed, write the file. Otherwise do nothing (and
+avoid bumping the file's modification time).
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Util.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Variable.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/lib/Clownfish/Variable.pm?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/lib/Clownfish/Variable.pm (added)
+++ incubator/lucy/trunk/import/clownfish/lib/Clownfish/Variable.pm Thu Oct 14 08:48:04 2010
@@ -0,0 +1,139 @@
+use strict;
+use warnings;
+
+package Clownfish::Variable;
+use base qw( Clownfish::Symbol );
+use Clownfish::Type;
+use Clownfish::Util qw( verify_args a_isa_b );
+use Carp;
+
+our %new_PARAMS = (
+ type => undef,
+ micro_sym => undef,
+ parcel => undef,
+ exposure => 'local',
+ class_name => undef,
+ class_cnick => undef,
+);
+
+sub new {
+ my $either = shift;
+ verify_args( \%new_PARAMS, @_ ) or confess $@;
+ my $self = $either->SUPER::new( %new_PARAMS, @_ );
+ confess "invalid type"
+ unless a_isa_b( $self->{type}, "Clownfish::Type" );
+ return $self;
+}
+
+sub get_type { shift->{type} }
+
+sub equals {
+ my ( $self, $other ) = @_;
+ return 0 unless $self->{type}->equals( $other->{type} );
+ return $self->SUPER::equals($other);
+}
+
+sub local_c {
+ my $self = shift;
+ my $type = $self->{type};
+ my $array_str = '';
+ if ( $type->is_composite ) {
+ $array_str = $type->get_array || '';
+ }
+ my $type_str = $array_str ? $type->to_c : $type->to_c;
+ return "$type_str $self->{micro_sym}$array_str";
+}
+
+sub global_c {
+ my $self = shift;
+ my $type = $self->{type};
+ my $name = $self->full_sym;
+ my $postfix = '';
+ if ( $type->is_composite ) {
+ $postfix = $type->get_array || '';
+ }
+ return $type->to_c . " $name$postfix";
+}
+
+sub local_declaration { return shift->local_c . ';' }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Clownfish::Variable - A Clownfish variable.
+
+=head1 DESCRIPTION
+
+A variable, having a L<Type|Clownfish::Type>, a micro_sym (i.e. name), an
+exposure, and optionally, a location in the global namespace hierarchy.
+
+Variable objects which exist only within a local scope, e.g. those within
+parameter lists, do not need to know about class. In contrast, inert class
+vars, for example, need to know class information so that they can declare
+themselves properly.
+
+=head1 METHODS
+
+=head2 new
+
+ my $var = Clownfish::Variable->new(
+ parcel => 'Crustacean',
+ type => $int32_t_type, # required
+ micro_sym => 'average_lifespan', # required
+ exposure => 'parcel', # default: 'local'
+ class_name => "Crustacean::Lobster", # default: undef
+ class_cnick => "Lobster", # default: undef
+ );
+
+=over
+
+=item * B<type> - A L<Clownfish::Type>.
+
+=item * B<micro_sym> - The variable's name, without any namespacing prefixes.
+
+=item * B<exposure> - See L<Clownfish::Symbol>.
+
+=item * B<class_name> - See L<Clownfish::Symbol>.
+
+=item * B<class_cnick> - See L<Clownfish::Symbol>.
+
+=back
+
+=head2 local_c
+
+ # e.g. "int32_t average_lifespan"
+ print $variable->local_c;
+
+Returns a string with the Variable's C type and its C<micro_sym>.
+
+=head2 global_c
+
+ # e.g. "int32_t crust_Lobster_average_lifespan"
+ print $variable->global_c;
+
+Returns a string with the Variable's C type and its fully qualified name
+within the global namespace.
+
+=head2 local_declaration
+
+ # e.g. "int32_t average_lifespan;"
+ print $variable->local_declaration;
+
+Returns C code appropriate for declaring the variable in a local scope, such
+as within a struct definition, or as an automatic variable within a C
+function.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008-2010 Marvin Humphrey
+
+This program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
Propchange: incubator/lucy/trunk/import/clownfish/lib/Clownfish/Variable.pm
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/import/clownfish/t/000-load.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/import/clownfish/t/000-load.t?rev=1022422&view=auto
==============================================================================
--- incubator/lucy/trunk/import/clownfish/t/000-load.t (added)
+++ incubator/lucy/trunk/import/clownfish/t/000-load.t Thu Oct 14 08:48:04 2010
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+use File::Find 'find';
+
+my @modules;
+
+find(
+ { no_chdir => 1,
+ wanted => sub {
+ return unless $File::Find::name =~ /\.pm$/;
+ push @modules, $File::Find::name;
+ }
+ },
+ 'lib'
+);
+for (@modules) {
+ s/^.*?Clownfish/Clownfish/;
+ s/\.pm$//;
+ s/\W+/::/g;
+ use_ok($_);
+}
+