Acme-Syntax-Python

 view release on metacpan or  search on metacpan

lib/Acme/Syntax/Python.pm  view on Meta::CPAN

            s{def (.+)\((.+)\):}{sub $1 \{ my(\$class, $2) = \@_; my \$self = \{\};}gmx;
            $self->{_class_block}->{($self->{_block_depth} + 1)} = 1;
        } else {
            s{def (.+)\((.+)\):}{sub $1 \{ my($2) = \@_;}gmx;
        }
        _start_block($self);
    }

    #Handle def with no Params
    if(/def (.+):/) {
        if($1 eq "__init__") {
            s{def (.+):}{sub $1 \{ my (\$class) = shift; my \$self = \{\};}gmx;
            $self->{_class_block}->{($self->{_block_depth} + 1)} = 1;
        } else {
            s{def (.+):}{sub $1 \{}gmx;
        }
        _start_block($self);
    }

    s{__init__}{new}gmx;

    if(/elif (.+)/) {
        s{elif (.+)}{elsif $1}gmx;
    }
    elsif(/if (.*)/) {
        s{if (.*)}{if $1}gmx;
    }
    if(/\):$/) {
        s{:$}{ \{}gmx;
        _start_block($self);
    }
    if(/else:/) {
        s{:$}{\{}gmx;
        _start_block($self);
    }

    if($self->{_debug}) {
        print "$self->{line_no} $_";
    }
    return $status;
}

sub _handle_spacing {
    my $depth = shift;
    my $modifier = shift // 1;
    return (' ') x (4 * ($depth - $modifier));
}

sub _start_block {
    my ($self, $type) = @_;
    $self->{_in_block} = 1;
    ++ $self->{_block_depth};
    if(defined($type)) {
        $self->{$type}->{$self->{_block_depth}} = 1;
    }
}

sub _handle_block {
        my ($self) = @_;
        /^(\s*)/;
        my $depth = length ( $1 );
        if($depth < (4 * $self->{_block_depth})) {
            my $spaces = _handle_spacing($self->{_block_depth});
            if($self->{_lambda_block}->{$self->{_block_depth}}) {
                $self->{_lambda_block}->{$self->{_block_depth}} = 0;
                s/^/$spaces\};\n/;
            } elsif ($self->{_class_block}->{$self->{_block_depth}}){
                my $spaces_front = _handle_spacing($self->{_block_depth}, 0);
                $self->{_class_block}->{$self->{_block_depth}} = 0;
                s/^/$spaces_front return bless \$self, \$class;\n$spaces\}\n/;
            } else {
                s/^/$spaces\}\n/;
            }
            -- $self->{_block_depth};
        }
        if($self->{_block_depth} == 0) {
            $self->{_in_block} = 0;
        }
}

1;

__END__

=head1 NAME

Acme::Syntax::Python - Python like Syntax for Perl.

=head1 SYNOPSIS

  use Acme::Syntax::Python;
  from Data::Dump import 'dump';

  def print_dump:
      print dump "Hello";

  print_dump;

=head1 DESCRIPTION

Translates a Python like syntax into executable Perl code. Right now blocks are defined by 4 spaces. I plan on extending this to tabs as well soon.

=head1 MODULES

Include modules into your file is much like the Python way using import.

  import Data::Dump;

this would include the Data::Dump module and whatever it exported by default.

If you need to exlicity name exports you can using "from"

  from Data::Dump import 'dump';

With perl you can also define params for use, with those you would just use import as a syntatic change of use.

  import Test::More tests => 4;

=head1 FUNCTIONS

You can declare Functions just like you would in Python.



( run in 0.533 second using v1.01-cache-2.11-cpan-62a16548d74 )