Skip to content

Commit

Permalink
- Changed the behavior of the given/when method; now it calls '~~' in…
Browse files Browse the repository at this point in the history
…stead of '=='

- Added the given/exact method which calls '==' internally.
- given/> now points to given/exact
- given/~ now points to given/when

Example:
	given("hello")
	   when("e") 	  { say "when"; continue  }   # true ("hello" contains "e")
	   when(/^he/)    { say "when2"; continue }   # true ("hello" matches /^he/)
	   exact("hello") { say "exact"; continue }   # true ("hello" == "hello")

- Improved the smart-match operator (~~) to make a finall '==' call before returning false.
  • Loading branch information
trizen committed Sep 15, 2015
1 parent 7fd6816 commit ceecf82
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 17 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ scripts/quicksort.sf
scripts/quicksort_in_parallel.sf
scripts/quoted_strings.sf
scripts/rand.sf
scripts/read_a_configuration_file.sf
scripts/rec_reverse.sf
scripts/recursion.sf
scripts/regex.sf
Expand Down
3 changes: 2 additions & 1 deletion lib/Sidef/Object/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ package Sidef::Object::Object {
return $second->contains($first);
}

Sidef::Types::Bool::Bool->false;
state $method = '==';
$first->$method($second);
};

# Negation of smart match
Expand Down
8 changes: 2 additions & 6 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,6 @@ package Sidef::Parser {
error => "operator '$method' requires a right-side argument",
);
}

}

$has_arg || do {
Expand Down Expand Up @@ -2570,11 +2569,8 @@ package Sidef::Parser {
if (defined $arg) {
push @{$struct{$self->{class}}[-1]{call}}, {method => 'do', arg => [$arg]};

if (/\G\h*(\R\h*)?(?=$self->{method_name_re}|$self->{operators_re})/goc) {

if (defined $1) {
$self->{line}++;
}
if (not(($self->parse_whitespace(code => $opt{code}))[1])
and /(?=$self->{method_name_re}|$self->{operators_re})/o) {

my $code = '. ' . substr($_, pos);
my $methods = $self->parse_methods(code => \$code);
Expand Down
20 changes: 16 additions & 4 deletions lib/Sidef/Types/Block/Switch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ package Sidef::Types::Block::Switch {
sub when {
my ($self, $arg) = @_;

if (ref($self->{obj}) eq ref($arg)) {
state $method = '~~';
if ($arg->$method($self->{obj})->get_value) {
$self->{do_block} = 1;
}

$self;
}

sub exact {
my ($self, $arg) = @_;

if (ref($arg) eq ref($self->{obj})) {
state $method = '==';
if ($self->{obj}->$method($arg)->get_value) {
if ($self->{obj}->$method($arg)) {
$self->{do_block} = 1;
}
}
Expand All @@ -32,7 +43,7 @@ package Sidef::Types::Block::Switch {
}
}
else {
return $self->when($arg);
return $self->exact($arg);
}

$self;
Expand All @@ -58,8 +69,9 @@ package Sidef::Types::Block::Switch {

{
no strict 'refs';
*{__PACKAGE__ . '::' . '>'} = \&when;
*{__PACKAGE__ . '::' . '~'} = \&when;
*{__PACKAGE__ . '::' . '?'} = \&case;
*{__PACKAGE__ . '::' . '>'} = \&exact;
*{__PACKAGE__ . '::' . ':'} = \&default;
}

Expand Down
7 changes: 4 additions & 3 deletions scripts/given_when_test.sf
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#

range(1, 10).each { |i|

given (i)
> 3 { say "three"; next }
? (i%2 == 0) { say "even"; next }
> 9 { say "nine"; break };
> 3 { say "three"; next } # exact
? (i.is_even) { say "even"; next } # boolean
~ 9 { say "nine"; break }; # smartmatch

if (i % 2 == 0) {
die "Got an even number!";
Expand Down
69 changes: 69 additions & 0 deletions scripts/read_a_configuration_file.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/ruby

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

var fullname = (var favouritefruit = "");
var needspeeling = (var seedsremoved = false);
var otherfamily = [];

DATA.each { |line|
var(key, value) = line.strip.split(/\h+/, 2)...;

given(key)
~ /^([#;]|\h*$)/ { }
> "FULLNAME" { fullname = value }
> "FAVOURITEFRUIT" { favouritefruit = value }
> "NEEDSPEELING" { needspeeling = true }
> "SEEDSREMOVED" { seedsremoved = true }
> "OTHERFAMILY" { otherfamily = value.split(',')»strip»() }
~ /^./ { say "#{key}: unknown key" }
;
}

say "fullname = #{fullname}";
say "favouritefruit = #{favouritefruit}";
say "needspeeling = #{needspeeling}";
say "seedsremoved = #{seedsremoved}";

otherfamily.each_with_index {|i, name|
say "otherfamily(#{i+1}) = #{name}";
};

assert_eq(fullname, 'Foo Barber');
assert_eq(favouritefruit, 'banana');
assert_eq(needspeeling, true);
assert_eq(seedsremoved, false);
assert_eq(otherfamily[0], 'Rhu Barber');
assert_eq(otherfamily[1], 'Harry Barber');

__DATA__
# This is a configuration file in standard configuration file format
#
# Lines beginning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.

# This is the fullname parameter
FULLNAME Foo Barber

# This is a favourite fruit
FAVOURITEFRUIT banana

# This is a boolean that should be set
NEEDSPEELING

# This boolean is commented out
; SEEDSREMOVED

# Configuration option names are not case sensitive, but configuration parameter
# data is case sensitive and may be preserved by the application program.

# An optional equals sign can be used to separate configuration parameter data
# from the option name. This is dropped by the parser.

# A configuration option may take multiple parameters separated by commas.
# Leading and trailing whitespace around parameter names and parameter data fields
# are ignored by the application program.

OTHERFAMILY Rhu Barber, Harry Barber
6 changes: 3 additions & 3 deletions scripts/switch.sf
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ given([8,9,10])
when ("x") do {
"Not the right place! (1)".die;
}
when([8,10,9]) do {
exact([8,10,9]) do {
"Not the right place! (2)".die;
}
when ([8,9,10]) do {
exact([8,9,10]) do {
"Right place! (5)".say;
}
when ([42]) do {
Expand All @@ -107,7 +107,7 @@ given(0)
when (1) do {
"Not the right place! (1)".die;
}
when ('0') do {
exact ('0') do {
"Not the right place! (2)".die;
}
when (0) do {
Expand Down

0 comments on commit ceecf82

Please sign in to comment.