Skip to content

Commit

Permalink
- Better deparsing of certain expressions.
Browse files Browse the repository at this point in the history
Example:
	expr1 ~~ expr2

is now deparsed as:
	do {expr} ~~ do {expr}

This is true for unary `!`, `<=>`, '==`, `!=` and `!~`
  • Loading branch information
trizen committed Dec 17, 2015
1 parent 5fa3c64 commit 1e2133f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
21 changes: 13 additions & 8 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1311,34 +1311,39 @@ HEADER
if ($method eq '==' or $method eq '!=') {
$code =
'Sidef::Types::Bool::Bool->new('
. ($method eq '!=' ? 'CORE::not' : '') . '('
. $code . 'eq'
. $self->deparse_args(@{$call->{arg}}) . '))';
. ($method eq '!=' ? 'CORE::not ' : '') . 'do{'
. $code
. '} eq do{'
. $self->deparse_args(@{$call->{arg}}) . '})';
next;
}

# <=> method
if ($method eq '<=>') {
$code =
'Sidef::Types::Number::Number->new(do{'
. $code . '}cmp'
. $self->deparse_args(@{$call->{arg}}) . ')';
. $code
. '} cmp do {'
. $self->deparse_args(@{$call->{arg}}) . '})';
next;
}

# !~ and ~~ methods
if ($method eq '~~' or $method eq '!~') {
$self->top_add(qq{use experimental 'smartmatch';\n});
$code =
'Sidef::Types::Bool::Bool->new(do{' . $code . '}~~' . $self->deparse_args(@{$call->{arg}}) . ')';
$code .= '->not' if ($method eq '!~');
'Sidef::Types::Bool::Bool->new('
. ($method eq '!~' ? 'CORE::not ' : '') . 'do{'
. $code
. '} ~~ do{'
. $self->deparse_args(@{$call->{arg}}) . '})';
next;
}

# ! prefix-unary
if ($ref eq 'Sidef::Object::Unary') {
if ($method eq '!') {
$code = 'Sidef::Types::Bool::Bool->new(!' . $self->deparse_args(@{$call->{arg}}) . ')';
$code = 'Sidef::Types::Bool::Bool->new(!do{' . $self->deparse_args(@{$call->{arg}}) . '})';
next;
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/Games/forest_fire.sf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Forest(p=0.01, f=0.001, height, width) {

method show {
range(0, height-1).each { |i|
say pix.@[spot[i]];
say pix[@spot[i]];
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/elementary_cellular_automaton.sf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class Automaton(rule, cells) {
method next {
var previous = cells.map{_};
var len = previous.len;
cells = [rule.@[
cells = rule.items(
previous.range.map { |i|
4*previous[i-1 % len] +
2*previous[i] +
previous[i+1 % len]
}
]];
}...
)
}

method to_s {
Expand Down
4 changes: 2 additions & 2 deletions scripts/fast_fourier_transform.sf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
func fft(arr) {
arr.len == 1 && return arr;

var evn = fft([arr.@[arr.range.grep { .is_even }]]);
var odd = fft([arr.@[arr.range.grep { .is_odd }]]);
var evn = fft([arr[arr.range.grep { .is_even }...]]);
var odd = fft([arr[arr.range.grep { .is_odd }...]]);
var twd = (Complex(0, 2) * Math::PI / arr.len);

odd.range.map {|n| odd[n] *= exp(twd * n)};
Expand Down

0 comments on commit 1e2133f

Please sign in to comment.