Skip to content

Commit

Permalink
Merge pull request #89 from catb0t/force-namedparam
Browse files Browse the repository at this point in the history
Deprecate `Object.:`; add :, ¦ and ⫶
  • Loading branch information
trizen authored Mar 20, 2019
2 parents 9f663c0 + 4ed066c commit d8af138
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 23 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ scripts/Tests/objects_init.sf
scripts/Tests/one-dimensional_cellular_automata.sf
scripts/Tests/one-dimensional_cellular_automata_2.sf
scripts/Tests/open.sf
scripts/Tests/pair_namedparam_force_ops.sf
scripts/Tests/pairs_test.sf
scripts/Tests/palindrome_recursive.sf
scripts/Tests/pascal_matrix_generation.sf
Expand Down
17 changes: 16 additions & 1 deletion lib/Sidef/Object/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,26 @@ package Sidef::Object::Object {
$func->call($arg, @args);
};

# Pair operator
# Deprecated pair method: ASCII colon; U+3A
*{__PACKAGE__ . '::' . ':'} = sub {
Sidef::Types::Array::Pair->new($_[0], $_[1]);
};

# TODO: v4.0: the following 2 methods should create an Array with 2 elements
# Pair (2-Array) method: Fullwidth Colon; U+FF1A
*{__PACKAGE__ . '::' . ''} = sub {
Sidef::Types::Array::Pair->new($_[0], $_[1]);
};

# Pair (2-Array) method: Broken bar; U+A6
# Easier to type than U+FF1A, but uglier
*{__PACKAGE__ . '::' . '¦'} = \&{__PACKAGE__ . '::' . ''};

# NamedParam method: Triple Colon Operator; U+2AF6
*{__PACKAGE__ . '::' . ''} = sub {
Sidef::Variable::NamedParam->new($_[0], $_[1]);
};

# Logical AND
*{__PACKAGE__ . '::' . '&&'} = sub {
$_[0] ? $_[1] : $_[0];
Expand Down
6 changes: 5 additions & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ package Sidef::Parser {
...
!= ..
\\\\= \\\\
!! ! : « » ~
!! !
: : ⫶ ¦
« » ~
);

qr{
Expand Down Expand Up @@ -1150,6 +1152,8 @@ package Sidef::Parser {
return Sidef::Types::String::String->new($1);
}

# Bareword followed by a colon becomes a NamedParam with the bareword
# on the LHS
if (/\G([^\W\d]\w*+):(?![=:])/gc) {
my $name = $1;
my $obj = $self->parse_obj(code => $opt{code});
Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/hash_concat.sf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ assert_eq(hash{:a}, :b)
hash += %w(c d) # 2-item array
assert_eq(hash{:c}, :d)

hash += "2":3 # an actual Pair
hash += "2"3 # an actual Pair
assert_eq(hash{"2"}, 3)

say "** Test passed!"
18 changes: 18 additions & 0 deletions scripts/Tests/pair_namedparam_force_ops.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/ruby

var f = :name
assert_eq(f⫶ 1 -> dump, name: 1 -> dump)
assert_eq(f⫶ 1 -> dump, (f) 1 -> dump)
assert_eq((f) 1 -> dump, name: 1 -> dump)
assert_eq(:a⫶ 1 -> dump, a:1 -> dump)

var n = 1

assert_ne(n: 2 -> dump, (n): 2 -> dump)
assert_eq((n): 2 -> dump, 1: 2 -> dump)
assert_eq(n:2, Pair(1, 2))
assert_eq(1:2, 1+2i)
assert_eq(1 2, Pair(1, 2))
assert_eq(1¦ 2, Pair(1, 2))

say "** Test passed!"
2 changes: 1 addition & 1 deletion scripts/Tests/pairs_test.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## This script is used for testing only
#

var tree = 'root':'child':'grandchild':'end';
var tree = 'root''child''grandchild''end';

while (true) {
say tree.first;
Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/roman_numerals_decoding.sf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func roman2arabic (roman) {
## MAIN
#

['MCMXC':1990, 'MMVIII':2008, 'MDCLXVI':1666].each { |pair|
[:MCMXC:1990, :MMVIII:2008, :MDCLXVI:1666].each { |pair|

var arabic = roman2arabic(pair.first) == pair.second ||
die "Error occurred on #{pair.first}\n";
Expand Down
26 changes: 13 additions & 13 deletions scripts/Tests/roman_numerals_decoding_3.sf
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

func roman2arabic(digit) {
digit.uc.trans([
'M': '1000+',
'CM': '900+',
'D': '500+',
'CD': '400+',
'C': '100+',
'XC': '90+',
'L': '50+',
'XL': '40+',
'X': '10+',
'IX': '9+',
'V': '5+',
'IV': '4+',
'I': '1+',
'M' '1000+',
'CM' '900+',
'D' '500+',
'CD' '400+',
'C' '100+',
'XC' '90+',
'L' '50+',
'XL' '40+',
'X' '10+',
'IX' '9+',
'V' '5+',
'IV' '4+',
'I' '1+',
]).split('+').map{.to_i}.sum;
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/roman_numerals_encoding.sf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func arabic2roman (num) {
## MAIN
#

[[1990,'MCMXC'], [2008,'MMVIII'], [1666,'MDCLXVI']].each { |pair|
[1990:MCMXC, 2008:MMVIII, 1666:MDCLXVI].each { |pair|
var roman = arabic2roman(pair[0]);
roman == pair[1] || "Error occurred on number: #{pair[0]}\n".die;
"%s in roman is %s".printlnf(pair[0], roman);
Expand Down
8 changes: 4 additions & 4 deletions scripts/Tests/roman_numerals_encoding_2.sf
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
func arabic2roman(num, roman='') {

static lookup = [
:M:1000, :CM:900, :D:500,
:CD:400, :C:100, :XC:90,
:L:50, :XL:40, :X:10,
:IX:9, :V:5, :IV:4, :I:1
:M1000, :CM900, :D500,
:CD400, :C100, :XC90,
:L50, :XL40, :X10,
:IX9, :V5, :IV4, :I1
];

lookup.each { |pair|
Expand Down

0 comments on commit d8af138

Please sign in to comment.