Skip to content

Commit

Permalink
- Renamed the "InitMy" and "My" to "InitLocal" and "Local" respectively.
Browse files Browse the repository at this point in the history
- Improved the definition of local variables.

Example:
	local x = 42;		# works now
	say x;			# prints: 42

In addition, local variables can now be declared in more places, such as:

	(local x = 42; say x);	# 42
	say x;			# nil

However, using local variables is not actually recommended and they may dispar in the future as well.
  • Loading branch information
trizen committed Aug 25, 2015
1 parent 09915d2 commit 7b47eee
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 45 deletions.
5 changes: 3 additions & 2 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ lib/Sidef/Types/Block/For.pod
lib/Sidef/Types/Block/Fork.pm
lib/Sidef/Types/Block/Fork.pod
lib/Sidef/Types/Block/Gather.pm
lib/Sidef/Types/Block/Gather.pod
lib/Sidef/Types/Block/Given.pm
lib/Sidef/Types/Block/Given.pod
lib/Sidef/Types/Block/Next.pm
Expand Down Expand Up @@ -126,11 +127,11 @@ lib/Sidef/Variable/ClassInit.pod
lib/Sidef/Variable/ClassVar.pm
lib/Sidef/Variable/Init.pm
lib/Sidef/Variable/Init.pod
lib/Sidef/Variable/InitMy.pm
lib/Sidef/Variable/InitLocal.pm
lib/Sidef/Variable/LazyMethod.pm
lib/Sidef/Variable/Local.pm
lib/Sidef/Variable/Magic.pm
lib/Sidef/Variable/Magic.pod
lib/Sidef/Variable/My.pm
lib/Sidef/Variable/Ref.pm
lib/Sidef/Variable/Ref.pod
lib/Sidef/Variable/Struct.pm
Expand Down
10 changes: 5 additions & 5 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,18 @@
"Sidef::Variable::Init" : {
"file" : "lib/Sidef/Variable/Init.pm"
},
"Sidef::Variable::InitMy" : {
"file" : "lib/Sidef/Variable/InitMy.pm"
"Sidef::Variable::InitLocal" : {
"file" : "lib/Sidef/Variable/InitLocal.pm"
},
"Sidef::Variable::LazyMethod" : {
"file" : "lib/Sidef/Variable/LazyMethod.pm"
},
"Sidef::Variable::Local" : {
"file" : "lib/Sidef/Variable/Local.pm"
},
"Sidef::Variable::Magic" : {
"file" : "lib/Sidef/Variable/Magic.pm"
},
"Sidef::Variable::My" : {
"file" : "lib/Sidef/Variable/My.pm"
},
"Sidef::Variable::PerlVar" : {
"file" : "lib/Sidef/Deparse/Perl.pm"
},
Expand Down
8 changes: 4 additions & 4 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ provides:
file: lib/Sidef/Variable/ClassVar.pm
Sidef::Variable::Init:
file: lib/Sidef/Variable/Init.pm
Sidef::Variable::InitMy:
file: lib/Sidef/Variable/InitMy.pm
Sidef::Variable::InitLocal:
file: lib/Sidef/Variable/InitLocal.pm
Sidef::Variable::LazyMethod:
file: lib/Sidef/Variable/LazyMethod.pm
Sidef::Variable::Local:
file: lib/Sidef/Variable/Local.pm
Sidef::Variable::Magic:
file: lib/Sidef/Variable/Magic.pm
Sidef::Variable::My:
file: lib/Sidef/Variable/My.pm
Sidef::Variable::PerlVar:
file: lib/Sidef/Deparse/Perl.pm
Sidef::Variable::PerlVarRef:
Expand Down
6 changes: 3 additions & 3 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package Sidef::Deparse::Perl {
use Scalar::Util qw(refaddr reftype);
use File::Basename qw(dirname);

# This module is under development...
# This module is highly experimental!

my %addr;
my %type;
Expand Down Expand Up @@ -244,10 +244,10 @@ HEADER
$code = "struct $obj->{__NAME__} {" . $self->_dump_vars(@vars) . '}';
}
}
elsif ($ref eq 'Sidef::Variable::InitMy') {
elsif ($ref eq 'Sidef::Variable::InitLocal') {
$code = "my $obj->{name}";
}
elsif ($ref eq 'Sidef::Variable::My') {
elsif ($ref eq 'Sidef::Variable::Local') {
$code = "$obj->{name}";
}
elsif ($ref eq 'Sidef::Object::Unary') {
Expand Down
8 changes: 3 additions & 5 deletions lib/Sidef/Deparse/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package Sidef::Deparse::Sidef {
our @ISA = qw(Sidef);
use Scalar::Util qw(refaddr reftype);

# This module is under development...

my %addr;

sub new {
Expand Down Expand Up @@ -137,10 +135,10 @@ package Sidef::Deparse::Sidef {
$code = "struct $obj->{__NAME__} {" . $self->_dump_vars(@vars) . '}';
}
}
elsif ($ref eq 'Sidef::Variable::InitMy') {
$code = "my $obj->{name}";
elsif ($ref eq 'Sidef::Variable::InitLocal') {
$code = "local $obj->{name}";
}
elsif ($ref eq 'Sidef::Variable::My') {
elsif ($ref eq 'Sidef::Variable::Local') {
$code = "$obj->{name}";
}
elsif ($ref eq 'Sidef::Variable::Init') {
Expand Down
20 changes: 11 additions & 9 deletions lib/Sidef/Exec.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ package Sidef::Exec {
}
}

if (ref $self_obj eq 'Sidef::Types::Array::HCArray') {
if (ref($self_obj) eq 'Sidef::Types::Array::HCArray') {
local $self->{var_ref} = 0;
$self_obj = Sidef::Types::Array::Array->new(
map {
Expand All @@ -63,7 +63,7 @@ package Sidef::Exec {
} @{$self_obj}
);
}
elsif (ref $self_obj eq 'Sidef::Variable::My') {
elsif (ref($self_obj) eq 'Sidef::Variable::Local') {
$self_obj = $self->{vars}{$self_obj->_get_name};
}

Expand Down Expand Up @@ -369,9 +369,11 @@ package Sidef::Exec {
: $struct->{$class}[$i - 1]->_get_name
}
= Sidef::Variable::Variable->new(
name => ref($struct->{$class}[$i - 1]) eq 'HASH' ? $struct->{$class}[$i - 1]{self}->_get_name
: $struct->{$class}[$i - 1]->_get_name,
type => 'var'
name => (
ref($struct->{$class}[$i - 1]) eq 'HASH' ? $struct->{$class}[$i - 1]{self}->_get_name
: $struct->{$class}[$i - 1]->_get_name
),
type => 'var',
)
);

Expand All @@ -380,8 +382,8 @@ package Sidef::Exec {
my $expr = $struct->{$class}[$self->{expr_i}];
my $ref_expr = ref($expr);

if (($ref_expr eq 'HASH' and ref($expr->{self}) eq 'Sidef::Variable::InitMy')
or $ref_expr eq 'Sidef::Variable::InitMy') {
if (($ref_expr eq 'HASH' and ref($expr->{self}) eq 'Sidef::Variable::InitLocal')
or $ref_expr eq 'Sidef::Variable::InitLocal') {
goto INIT_VAR;
}

Expand All @@ -390,8 +392,8 @@ package Sidef::Exec {
$ref_expr eq 'HASH' ? $self->execute_expr($expr)
: $ref_expr eq 'Sidef::Types::Array::HCArray' ? $self->execute_expr({self => $expr})
: $ref_expr eq 'Sidef::Variable::Init' ? do { $expr->set_value; $expr }
: $ref_expr eq 'Sidef::Variable::My' ? $self->{vars}{$expr->_get_name}
: $expr;
: $ref_expr eq 'Sidef::Variable::Local' ? $self->{vars}{$expr->_get_name}
: $expr;

my $ref_obj = ref($obj);
if ( $ref_obj eq 'Sidef::Types::Block::Return'
Expand Down
31 changes: 18 additions & 13 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ package Sidef::Parser {
assert_eq
assert_ne
my
local
var
const
func
Expand Down Expand Up @@ -1034,13 +1034,15 @@ package Sidef::Parser {
return Sidef::Types::Number::Number->new($#{$vars});
}

# Declaration of the 'my' special variable + class, method and function declarations
# Declaration of local variables, classes, methods and functions
if (
/\G(my|func|class)\b\h*/gc
/\G(local|func|class)\b\h*/gc
|| /\G(->)\h*/gc
|| (exists($self->{current_class})
&& /\G(method)\b\h*/gc)
) {

my $beg_pos = $-[0];
my $type =
$1 eq '->'
? exists($self->{current_class}) && !(exists($self->{current_method}))
Expand Down Expand Up @@ -1085,7 +1087,7 @@ package Sidef::Parser {
$name =
/\G($self->{var_name_re})\h*/goc ? $1
: $type eq 'method' && /\G($self->{operators_re})\h*/goc ? $+
: $type ne 'my' ? ''
: $type ne 'local' ? ''
: $self->fatal_error(
error => "invalid '$type' declaration",
expected => "expected a name",
Expand All @@ -1108,7 +1110,7 @@ package Sidef::Parser {
}

my $obj =
$type eq 'my' ? Sidef::Variable::My->new($name)
$type eq 'local' ? Sidef::Variable::Local->new($name)
: $type eq 'func' ? Sidef::Variable::Variable->new(name => $name, type => $type, class => $class_name)
: $type eq 'method' ? Sidef::Variable::Variable->new(name => $name, type => $type, class => $class_name)
: $type eq 'class'
Expand Down Expand Up @@ -1142,8 +1144,11 @@ package Sidef::Parser {
};
}

if ($type eq 'my') {
return Sidef::Variable::InitMy->new($name);
if ($type eq 'local') {
if (/\G(?![,;)\]\}])/) {
pos($_) = $beg_pos + 5;
}
return Sidef::Variable::InitLocal->new($name);
}

if ($type eq 'class') {
Expand Down Expand Up @@ -1694,15 +1699,15 @@ package Sidef::Parser {
#warn qq{[!] Implicit declaration of variable "$name", at line $self->{line}\n};
unshift @{$self->{vars}{$class}},
{
obj => Sidef::Variable::My->new($name),
obj => Sidef::Variable::Local->new($name),
name => $name,
count => 0,
type => 'my',
type => 'local',
line => $self->{line},
};

pos($_) -= length($name);
return Sidef::Variable::InitMy->new($name);
return Sidef::Variable::InitLocal->new($name);
}

# Type constant
Expand Down Expand Up @@ -2017,7 +2022,7 @@ package Sidef::Parser {
my ($obj, $obj_key) = $self->parse_expr(code => $opt{code});

# This object can't take any method!
if (ref($obj) eq 'Sidef::Variable::InitMy') {
if (ref($obj) eq 'Sidef::Variable::InitLocal') {
return $obj;
}

Expand Down Expand Up @@ -2436,8 +2441,8 @@ package Sidef::Parser {
if (defined $obj) {
push @{$struct{$self->{class}}}, {self => $obj};

if (ref $obj eq 'Sidef::Variable::InitMy') {
/\G\h*;+/gc;
if (ref $obj eq 'Sidef::Variable::InitLocal') {
/\G\h*[,;]+/gc;
redo;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Sidef::Variable::InitMy {
package Sidef::Variable::InitLocal {

sub new {
my (undef, $name) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Variable/My.pm → lib/Sidef/Variable/Local.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Sidef::Variable::My {
package Sidef::Variable::Local {

sub new {
my (undef, $name) = @_;
Expand Down
4 changes: 2 additions & 2 deletions utils/pod_generator.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
'Sidef::Types::Block::Break' => [qw(new)],
'Sidef::Variable::Variable' => [qw(set_value is_defined get_type get_value new)],
'Sidef::Variable::Ref' => [qw(get_var new)],
'Sidef::Variable::My' => [qw(new)],
'Sidef::Variable::Local' => [qw(new)],
'Sidef::Variable::Init' => [qw(new)],
'Sidef::Variable::InitMy' => [qw(new)],
'Sidef::Variable::InitLocal' => [qw(new)],
'Sidef::Sys::Sys' => [qw(new)],
'Sidef::Math::Math' => [qw(new)],
'Sidef::Time::Localtime' => [qw(new)],
Expand Down

0 comments on commit 7b47eee

Please sign in to comment.