App-PythonToPerl

 view release on metacpan or  search on metacpan

lib/Python/Function.pm  view on Meta::CPAN

        my string::arrayref $arguments_split_perl = [];

        if ($self->isa('Python::Method')) {
            if ((scalar @{$arguments_split}) == 0) {
                carp 'WARNING WPYFU016: method \'', $self->{symbol_scoped}, '\', missing \'self\' argument, no arguments provided, carping';
            }
            elsif ($arguments_split->[0] ne 'self') {
                if ( grep( /^self$/, @{$arguments_split} ) ) {
                    carp 'WARNING WPYFU017a: method \'', $self->{symbol_scoped},
                        '\', \'self\' argument present but not first argument provided, not setting data type as class name, carping';
                }
                else {
                    carp 'WARNING WPYFU017b: method \'', $self->{symbol_scoped},
                        '\', missing \'self\' argument, not present in arguments provided, carping';
                }

                $self->{perl_source_code_full} .= $self->python_preparsed_to_perl_source_arguments($arguments_split, $arguments_split_perl);
            }
            else {
                if ((not exists $self->{scope}) or (not defined $self->{scope}) or ($self->{scope} eq '')) {
                    croak 'ERROR EPYFU018: method \'', $self->{symbol_scoped},
                        '\', missing \'scope\' object property, croaking';
                }
                # we know the first argument is correctly set to 'self', because we did not trigger the `ne 'self'` check above;
                # current scope is the name of this method's enclosing class, so that is the same as the name of the $self data type;
                # set $self data type and use `shift` to discard 'self' from the split arguments array
                push @{$arguments_split_perl}, ('my ' . $self->{scope} . ' $self');
                shift @{$arguments_split};

                $self->{perl_source_code_full} .= $self->python_preparsed_to_perl_source_arguments($arguments_split, $arguments_split_perl);
            }
        }
        else {  # not a method, either a normal function or an inner function
            if ((scalar @{$arguments_split}) > 0) {
                $self->{perl_source_code_full} .= $self->python_preparsed_to_perl_source_arguments($arguments_split, $arguments_split_perl);
            }
        }
    }




    # keep Perl source code split by component, for active component checking below
    my string::arrayref $perl_source_code_split = [];

    # if there are any active components (anything other than comments and whitespace and blank lines) in the function body,
    # then record the $perl_source_code_split index of the last active component;
    # used to place the final closing curly brace and thus maintain the original line count
    my boolean $final_active_component_index = -1;

    # de-parse & translate function body
    foreach my Python::Component $python_preparsed_component (@{$self->{python_preparsed}}) {
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', de-parsing & translating function body, about to call python_preparsed_to_perl_source() on $python_preparsed_component = ', Dumper($python_prepa...
        push @{$perl_source_code_split}, $python_preparsed_component->python_preparsed_to_perl_source($openai);
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', de-parsing & translating function body, ret from call to python_preparsed_to_perl_source(), received $perl_source_code_split->[-1] = \'', $perl_...

        if ((not $python_preparsed_component->isa('Python::Comment')) and
            (not $python_preparsed_component->isa('Python::Whitespace')) and
            (not $python_preparsed_component->isa('Python::Blank'))) {
            $final_active_component_index = (scalar @{$perl_source_code_split}) - 1;
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', de-parsing & translating function body, updated $final_active_component_index = ', $final_active_component_index, "\n";
        }

print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', de-parsing & translating function body, ret from call python_preparsed_to_perl_source()', "\n";
    }

    # close function body with curly brace, placed after the final active component in the function body, 
    # or directly after the function header if no active components in body
    my string $function_end = ' }  # END sub ' . $self->{symbol} . '()'; 
    if ($final_active_component_index > -1) {
        # end function after final active component in function body,
        # with trailing comment to label closing curly brace as end of function
        $perl_source_code_split->[$final_active_component_index] .= $function_end;
#        $perl_source_code_split->[$final_active_component_index] .= '  ## ACTIVE BODY COMPONENT';
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', appending closing curly brace after final active component in function body, have $perl_source_code_split->[', $final_active_component_index, ']...
    }
    else {
        $self->{perl_source_code_full} .= $function_end;
#        $self->{perl_source_code_full} .= '  ## HEADER COMPONENT';
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', appending closing curly brace after function header due to lack of any active components in function body, have $self->{perl_source_code_full} =...
    }

    # assemble Perl source code in function body, if present
    if ((scalar @{$perl_source_code_split}) > 0) {
        $self->{perl_source_code_full} .= "\n";  # newline after function header
        $self->{perl_source_code_full} .= join "\n", @{$perl_source_code_split};
print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\' assembling generated Perl source code from non-empty function body $perl_source_code_split = ', Dumper($perl_source_code_split), "\n";
    }

print 'in Python::Function->python_preparsed_to_perl_source(), function \'', $self->{symbol_scoped}, '\', about to return $self->{perl_source_code_full} = \'', $self->{perl_source_code_full}, '\'', "\n";
#    if ($final_active_component_index > -1) {
#die 'TMP DEBUG';
#    }

    # return de-parsed & translated Perl source code
    return $self->{perl_source_code_full};
}


# PYFU02x
sub python_preparsed_to_perl_source_arguments {
# return Perl source code for function arguments
    { my string $RETURN_TYPE };
    ( my Python::Function $self, my string::arrayref $arguments_split, my string::arrayref $arguments_split_perl ) = @ARG;
print 'in Python::Function->python_preparsed_to_perl_source_arguments(), function \'', $self->{symbol_scoped}, '\', top of subroutine', "\n";

# NEED FIX: implement Python-to-Perl translation for arguments with default values provided
# NEED FIX: implement Python-to-Perl translation for arguments with default values provided
# NEED FIX: implement Python-to-Perl translation for arguments with default values provided

# NEED FIX: handle non-positional function arguments appearing after '*' parameter delimiter
# NEED FIX: handle non-positional function arguments appearing after '*' parameter delimiter
# NEED FIX: handle non-positional function arguments appearing after '*' parameter delimiter

# NEED FIX: handle **kwargs & other special function arguments
# NEED FIX: handle **kwargs & other special function arguments
# NEED FIX: handle **kwargs & other special function arguments

# def __FOO__(self, *, BAR=None, var_BAR=1e-9, force_alpha="warn"):
# def FOO_BAR(code, extra_preargs=[], extra_postargs=[]):



( run in 0.862 second using v1.01-cache-2.11-cpan-140bd7fdf52 )