Skip to content

Commit

Permalink
- Flat lists in array indices will now return back a list of lvalues.
Browse files Browse the repository at this point in the history
Example:
	var arr = %w(a b c d e f g);

	var ind = [0,1,2];
	say arr[ind...]			# prints: "abc"

The previous syntax `arr.@[ind]` is considered deprecated.
  • Loading branch information
trizen committed Dec 17, 2015
1 parent 0603970 commit 5fa3c64
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 10 deletions.
4 changes: 4 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ scripts/RosettaCode/rpn_to_infix_conversion.sf
scripts/RosettaCode/runge-kutta_method.sf
scripts/RosettaCode/runtime_evaluation.sf
scripts/RosettaCode/runtime_evaluation_in_an_environment.sf
scripts/RosettaCode/s_expressions.sf
scripts/RosettaCode/search_a_list.sf
scripts/RosettaCode/search_a_list_1.sf
scripts/RosettaCode/secure_temporary_file.sf
Expand All @@ -478,6 +479,8 @@ scripts/RosettaCode/shunting-yard_algorithm.sf
scripts/RosettaCode/sidef_3d_ascii.sf
scripts/RosettaCode/sierpinski_carpet.sf
scripts/RosettaCode/sierpinski_triangle.sf
scripts/RosettaCode/sieve_of_eratosthenes.sf
scripts/RosettaCode/singleton.sf
scripts/RosettaCode/singly-linked_list_element_definition.sf
scripts/RosettaCode/singly-linked_list_element_insertion.sf
scripts/RosettaCode/sort_an_integer_array.sf
Expand Down Expand Up @@ -505,6 +508,7 @@ scripts/RosettaCode/stack.sf
scripts/RosettaCode/stack_1.sf
scripts/RosettaCode/standard_deviation.sf
scripts/RosettaCode/standard_deviation_2.sf
scripts/RosettaCode/stem_and_leaf_plot.sf
scripts/RosettaCode/string_comparison.sf
scripts/RosettaCode/the_isaac_cipher.sf
scripts/RosettaCode/universal_turing_machine.sf
Expand Down
17 changes: 7 additions & 10 deletions lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,8 @@ HEADER
if (substr($code, -1) eq '@') {
$code .= $self->_dump_unpacked_indices($pos);
}
elsif ($#{$pos} > 0) {
$code = '@{' . $code . '}' . $self->_dump_indices($pos);
}
else {
$code .= '->' . $self->_dump_indices($pos);
$code = '@{' . $code . '}' . $self->_dump_indices($pos);
}
}
else {
Expand All @@ -1189,11 +1186,8 @@ HEADER
if (substr($code, -1) eq '@') {
$code .= $self->_dump_unpacked_lookups($key);
}
elsif ($#{$key} > 0) {
$code = '@{' . $code . '}' . $self->_dump_lookups($key);
}
else {
$code .= '->' . $self->_dump_lookups($key);
$code = '@{' . $code . '}' . $self->_dump_lookups($key);
}
}

Expand Down Expand Up @@ -1326,14 +1320,17 @@ HEADER
# <=> method
if ($method eq '<=>') {
$code =
'Sidef::Types::Number::Number->new(' . $code . 'cmp' . $self->deparse_args(@{$call->{arg}}) . ')';
'Sidef::Types::Number::Number->new(do{'
. $code . '}cmp'
. $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(' . $code . '~~' . $self->deparse_args(@{$call->{arg}}) . ')';
$code =
'Sidef::Types::Bool::Bool->new(do{' . $code . '}~~' . $self->deparse_args(@{$call->{arg}}) . ')';
$code .= '->not' if ($method eq '!~');
next;
}
Expand Down
54 changes: 54 additions & 0 deletions scripts/RosettaCode/s_expressions.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/S-Expressions#Sidef
#

var t = frequire('Text::Balanced');

func sexpr(txt) {
txt.trim!;

var m = txt.match(/^\((.*)\)$/s)
|| die "Not an S-expression: <<#{txt}>>";
txt = m[0];

var w;
var ret = [];
while (!txt.is_empty) {
given (txt.first) {
when('(') {
(w, txt) = t.extract_bracketed(txt, '()');
w = sexpr(w);
}
when ('"') {
(w, txt) = t.extract_delimited(txt, '"')
w.sub!(/^"(.*)"/, {|s1| s1 });
}
default {
txt.sub!(/^(\S+)/, {|s1| w = s1; '' });
}
}
ret << w;
txt.trim_beg!;
}
return ret;
}

func sexpr2txt(String e) {
e ~~ /[\s"\(\)]/ ? do { e.gsub!('"', '\\"'); %Q("#{e}") } : e;
}

func sexpr2txt(expr) {
'(' + expr.map {|e| sexpr2txt(e) }.join(' ') + ')';
}

var s = sexpr(%q{
((data "quoted data" 123 4.5)
(data (!@# (4.5) "(more" "data)")))
});

say s; # dump structure
say sexpr2txt(s); # convert back
30 changes: 30 additions & 0 deletions scripts/RosettaCode/singleton.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Singleton
#

class Singleton(name) {
static instance;

method new(name) {
instance ||= Singleton.bless(Hash(name => name));
}
method new {
Singleton.new(nil);
}
}

var s1 = Singleton('foo');
say s1.name; #=> 'foo'
say s1.object_id; #=> '30424504'

var s2 = Singleton();
say s2.name; #=> 'foo'
say s2.object_id; #=> '30424504'

s2.name = 'bar'; # change name in s2
say s1.name; #=> 'bar'

assert_eq(s1.object_id, s2.object_id);
assert_eq(s1.name, s2.name);
30 changes: 30 additions & 0 deletions scripts/RosettaCode/stem_and_leaf_plot.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Stem-and-leaf_plot
#

var data = %i(
12 127 28 42 39 113 42 18 44 118 44
37 113 124 37 48 127 36 29 31 125 139
131 115 105 132 104 123 35 113 122 42 117
119 58 109 23 105 63 27 44 105 99 41
128 121 116 125 32 61 37 127 29 113 121
58 114 126 53 114 96 25 109 7 31 141
46 13 27 43 117 116 27 7 68 40 31
115 124 42 128 52 71 118 117 38 27 106
33 117 116 111 40 119 47 105 57 122 109
124 115 43 120 43 27 27 18 28 48 125
107 114 34 133 45 120 30 127 31 116 146
).sort;

var stem_unit = 10;
var h = data.group_by { |i| i / stem_unit -> int }

var rng = RangeNum(h.keys.map{.to_i}.minmax);
var stem_format = "%#{rng.min.len.max(rng.max.len)}d";

rng.each { |stem|
var leafs = (h{stem} \\ [])
say(stem_format % stem, ' | ', leafs.map { _ % stem_unit }.join(' '))
}

0 comments on commit 5fa3c64

Please sign in to comment.