Skip to content

Commit

Permalink
- Added support for constant folding.
Browse files Browse the repository at this point in the history
Example:
	"abc" + "def";    # is optimized to "abcdef" before execution
	3/4 + 5; 	  # is optimized to 5.75

The constant folding support is available at `-O1` optimization level.
  • Loading branch information
trizen committed Aug 22, 2015
1 parent 80899b1 commit cc439e4
Show file tree
Hide file tree
Showing 4 changed files with 502 additions and 65 deletions.
21 changes: 19 additions & 2 deletions lib/Sidef/Deparse/Sidef.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ package Sidef::Deparse::Sidef {

sub _dump_array {
my ($self, $array) = @_;
'[' . join(', ', map { $self->deparse_expr(ref($_) eq 'HASH' ? $_ : {self => $_->get_value}) } @{$array}) . ']';
'[' . join(
', ',
map {
$self->deparse_expr(
ref($_) eq 'HASH' ? $_
: {
self => (
ref($_) eq 'Sidef::Variable::Variable' ? $_->get_value
: $_
)
}
)
} @{$array}
)
. ']';
}

sub _dump_class_name {
Expand All @@ -71,7 +85,7 @@ package Sidef::Deparse::Sidef {
# Self obj
my $ref = ref($obj);
if ($ref eq 'HASH') {
$code = join(', ', $self->deparse_script($obj));
$code = join(', ', exists($obj->{self}) ? $self->deparse_expr($obj) : $self->deparse_script($obj));
if ($self->{extra_parens}) {
$code = "($code)";
}
Expand Down Expand Up @@ -244,6 +258,9 @@ package Sidef::Deparse::Sidef {
elsif ($ref eq 'Sidef::Module::Func') {
$code = "%S($obj->{module})";
}
elsif ($ref eq 'Sidef::Types::Array::List') {
$code = join(', ', map { $self->deparse_expr({self => $_}) } @{$obj});
}
elsif ($ref eq 'Sidef::Types::Block::Gather') {
if (exists $addr{refaddr($obj->{block})}) {
$code = '';
Expand Down
Loading

0 comments on commit cc439e4

Please sign in to comment.