Skip to content

Commit

Permalink
- New implementation of the numerical system.
Browse files Browse the repository at this point in the history
- Added input history support inside `bin/sidef`. (requires Term::ReadLine::Gnu)
- Removed the support for calling Number methods via Math. (e.g.: `Math.sqrt()` no longer works)
- `n.times { ... }` and `n.of { ... }` counts from 0 up to n-1. (before, they counted from 1 up to n)
- `irand(n)` and `irand(n,m)` became always inclusive on both sides.
- Extended the `for-in` loop to support multiple ranges as arguments.

Example:

	for i in (1..3, 5..10) {
		say i
	}

Additionally, the `in` token in `for-in` is now optional:

	for i (1..10) {
		say i
	}

- Other minor changes and improvements.
  • Loading branch information
trizen committed Apr 19, 2017
1 parent 4768bee commit 6facc73
Show file tree
Hide file tree
Showing 144 changed files with 5,671 additions and 6,408 deletions.
4 changes: 2 additions & 2 deletions Build.PL
Expand Up @@ -49,7 +49,7 @@ my $builder = Module::Build->new(
'List::Util' => 1.33,
'Math::MPFR' => 3.29,
'Math::MPC' => 0,
'Math::GMPq' => 0.41,
'Math::GMPq' => 0.44,
'Math::GMPz' => 0.39,
'Socket' => 0,
'Fcntl' => 0,
Expand All @@ -59,7 +59,7 @@ my $builder = Module::Build->new(
'Time::HiRes' => 0,
'Getopt::Std' => 0,
'Term::ReadLine' => 0,
'Math::Prime::Util::GMP' => 0.42,
'Math::Prime::Util::GMP' => 0.44,
},

recommends => {
Expand Down
4 changes: 1 addition & 3 deletions MANIFEST
Expand Up @@ -71,9 +71,6 @@ lib/Sidef/Types/Null/Null.pm
lib/Sidef/Types/Null/Null.pod
lib/Sidef/Types/Number/Complex.pm
lib/Sidef/Types/Number/Complex.pod
lib/Sidef/Types/Number/Inf.pm
lib/Sidef/Types/Number/Nan.pm
lib/Sidef/Types/Number/Ninf.pm
lib/Sidef/Types/Number/Number.pm
lib/Sidef/Types/Number/Number.pod
lib/Sidef/Types/Range/Range.pm
Expand Down Expand Up @@ -665,6 +662,7 @@ scripts/Tests/fibonacci_mystery.sf
scripts/Tests/find_the_missing_permutation.sf
scripts/Tests/first_class_functions_analogously.sf
scripts/Tests/floyds_triangle.sf
scripts/Tests/for_in_extended.sf
scripts/Tests/for_in_range.sf
scripts/Tests/for_two_vars.sf
scripts/Tests/for_var_in_array.sf
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.SKIP
@@ -1,4 +1,3 @@

#!start included /usr/share/perl5/core_perl/ExtUtils/MANIFEST.SKIP
# Avoid version control files.
\bRCS\b
Expand Down
17 changes: 4 additions & 13 deletions META.json
Expand Up @@ -43,11 +43,11 @@
"File::Spec" : "0",
"Getopt::Std" : "0",
"List::Util" : "1.33",
"Math::GMPq" : "0.41",
"Math::GMPq" : "0.44",
"Math::GMPz" : "0.39",
"Math::MPC" : "0",
"Math::MPFR" : "3.29",
"Math::Prime::Util::GMP" : "0.42",
"Math::Prime::Util::GMP" : "0.44",
"Memoize" : "0",
"POSIX" : "0",
"Scalar::Util" : "0",
Expand All @@ -63,7 +63,7 @@
"provides" : {
"Sidef" : {
"file" : "lib/Sidef.pm",
"version" : "2.37"
"version" : "3.00"
},
"Sidef::Convert::Convert" : {
"file" : "lib/Sidef/Convert/Convert.pm"
Expand Down Expand Up @@ -176,15 +176,6 @@
"Sidef::Types::Number::Complex" : {
"file" : "lib/Sidef/Types/Number/Complex.pm"
},
"Sidef::Types::Number::Inf" : {
"file" : "lib/Sidef/Types/Number/Inf.pm"
},
"Sidef::Types::Number::Nan" : {
"file" : "lib/Sidef/Types/Number/Nan.pm"
},
"Sidef::Types::Number::Ninf" : {
"file" : "lib/Sidef/Types/Number/Ninf.pm"
},
"Sidef::Types::Number::Number" : {
"file" : "lib/Sidef/Types/Number/Number.pm"
},
Expand Down Expand Up @@ -226,6 +217,6 @@
"url" : "https://github.com/trizen/sidef"
}
},
"version" : "2.37",
"version" : "3.00",
"x_serialization_backend" : "JSON::PP version 2.27400"
}
14 changes: 4 additions & 10 deletions META.yml
Expand Up @@ -17,7 +17,7 @@ name: Sidef
provides:
Sidef:
file: lib/Sidef.pm
version: '2.37'
version: '3.00'
Sidef::Convert::Convert:
file: lib/Sidef/Convert/Convert.pm
Sidef::Deparse::Perl:
Expand Down Expand Up @@ -92,12 +92,6 @@ provides:
file: lib/Sidef/Types/Null/Null.pm
Sidef::Types::Number::Complex:
file: lib/Sidef/Types/Number/Complex.pm
Sidef::Types::Number::Inf:
file: lib/Sidef/Types/Number/Inf.pm
Sidef::Types::Number::Nan:
file: lib/Sidef/Types/Number/Nan.pm
Sidef::Types::Number::Ninf:
file: lib/Sidef/Types/Number/Ninf.pm
Sidef::Types::Number::Number:
file: lib/Sidef/Types/Number/Number.pm
Sidef::Types::Range::Range:
Expand Down Expand Up @@ -132,11 +126,11 @@ requires:
File::Spec: '0'
Getopt::Std: '0'
List::Util: '1.33'
Math::GMPq: '0.41'
Math::GMPq: '0.44'
Math::GMPz: '0.39'
Math::MPC: '0'
Math::MPFR: '3.29'
Math::Prime::Util::GMP: '0.42'
Math::Prime::Util::GMP: '0.44'
Memoize: '0'
POSIX: '0'
Scalar::Util: '0'
Expand All @@ -151,5 +145,5 @@ resources:
homepage: https://github.com/trizen/sidef
license: http://www.perlfoundation.org/artistic_license_2_0
repository: https://github.com/trizen/sidef
version: '2.37'
version: '3.00'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
4 changes: 2 additions & 2 deletions Makefile.PL
Expand Up @@ -15,11 +15,11 @@ WriteMakefile
'File::Spec' => 0,
'Getopt::Std' => 0,
'List::Util' => '1.33',
'Math::GMPq' => '0.41',
'Math::GMPq' => '0.44',
'Math::GMPz' => '0.39',
'Math::MPC' => 0,
'Math::MPFR' => '3.29',
'Math::Prime::Util::GMP' => '0.42',
'Math::Prime::Util::GMP' => '0.44',
'Memoize' => 0,
'POSIX' => 0,
'Scalar::Util' => 0,
Expand Down
2 changes: 0 additions & 2 deletions TODO
Expand Up @@ -15,10 +15,8 @@ New built-in classes:

Internal:
* Switch to subroutine `signatures` (as soon as this feature becomes stable).
* Unify `Sidef::Types::Number::Ninf` into `Sidef::Types::Number::Inf`.

Speculations:
* Make all core methods (including user-defined methods) real multimethods, using Class::Multimethods.
* Replace the current numerical system with Math::AnyNum.
* Bootstrap Sidef by writing a code generator in Sidef that generates Perl code.
* Rewrite all core libraries in Sidef.
35 changes: 28 additions & 7 deletions bin/sidef
Expand Up @@ -81,6 +81,15 @@ if (defined $args{i}) {
exit 0;
}

# Precision
if (defined $args{P}) {
require Sidef::Types::Number::Number;
if ($args{P} <= 0) {
die "Invalid precision: <<$args{P}>> (expected a positive integer)\n";
}
$Sidef::Types::Number::Number::PREC = $args{P} << 2;
}

# Test mode
if (defined $args{t}) {
my @argv = splice(@ARGV);
Expand Down Expand Up @@ -136,7 +145,7 @@ else {
$script_name = shift @ARGV;
read_script($script_name);
}
: (-t STDIN) ? do { code_interactive(); exit }
: (-t STDIN) ? do { code_interactive(); exit 0; }
: do { local $/; <STDIN> };

$code // exit 2;
Expand Down Expand Up @@ -401,6 +410,17 @@ sub code_interactive {
my $valid_lines = '';
my ($vars, $ref_vars_refs);

my $history_support = $term->can('ReadHistory') && $term->can('WriteHistory');
my $history_file = File::Spec->catfile($sidef->get_sidef_config_dir(), 'sidef_history.txt');

if ($history_support) {
if (not -e $history_file) {
open my $fh, '>', $history_file;
}

$term->ReadHistory($history_file);
}

MAINLOOP: {
my $line = '';

Expand Down Expand Up @@ -432,11 +452,6 @@ sub code_interactive {
close $fh;
say "** Created file: $file";
}

#~ elsif ($line =~ /\h\z/) {
#~ $line .= "\n";
#~ redo;
#~ }
elsif ($line eq 'copyright') {
print <<'EOT';
Copyright © 2013-2017 Daniel Șuteu, Ioana Fălcușan
Expand Down Expand Up @@ -520,11 +535,17 @@ EOT
$valid_lines .= "$line\n"; # store valid lines
}

if ($history_support) {
if ($line =~ /\R/) {
$term->addhistory($line);
}
$term->append_history(1, $history_file);
}

if (defined($args{r}) or defined($args{R})) {
output($ccode);
}
elsif ($line =~ /\S/ and not $line =~ /^\s*#.*$/) {
local $Sidef::Types::Number::Number::PREC = 4 * $args{P} if exists($args{P});
my @results = $sidef->execute_perl($ccode);
print $@ if $@;

Expand Down
51 changes: 22 additions & 29 deletions lib/Sidef.pm
@@ -1,7 +1,7 @@
package Sidef {

use 5.014;
our $VERSION = '2.37';
our $VERSION = '3.00';

our $SPACES = 0; # the current number of spaces
our $SPACES_INCR = 4; # the number of spaces incrementor
Expand Down Expand Up @@ -86,36 +86,29 @@ package Sidef {
eval($code);
}

sub get_sidef_vdir {
sub get_sidef_config_dir {
my ($self) = @_;
$self->{sidef_config_dir} //= $ENV{SIDEF_CONFIG_DIR}
|| File::Spec->catdir(
$ENV{XDG_CONFIG_DIR}
|| (
$ENV{HOME}
|| $ENV{LOGDIR}
|| (
$^O eq 'MSWin32'
? '\Local Settings\Application Data'
: eval { ((getpwuid($<))[7] || `echo -n ~`) }
)
|| File::Spec->curdir()
),
'.config',
'sidef'
);
}

return $self->{_sidef_vdir}
if exists($self->{_sidef_vdir});

$self->{_sidef_vdir} = File::Spec->catdir(
$self->{sidef_config_dir}
|| $ENV{SIDEF_CONFIG_DIR}
|| File::Spec->catdir(
$ENV{XDG_CONFIG_DIR}
|| File::Spec->catdir(
(
$ENV{HOME}
|| $ENV{LOGDIR}
|| (
$^O eq 'MSWin32'
? '\Local Settings\Application Data'
: eval { ((getpwuid($<))[7] || `echo -n ~`) }
)
|| File::Spec->curdir()
),
'.config'
),
'sidef'
),
"v$VERSION"
);

$self->{_sidef_vdir};
sub get_sidef_vdir {
my ($self) = @_;
$self->{_sidef_vdir} //= File::Spec->catdir($self->get_sidef_config_dir, "v$VERSION");
}

sub has_dbm_driver {
Expand Down
28 changes: 11 additions & 17 deletions lib/Sidef/Convert/Convert.pm
Expand Up @@ -28,35 +28,29 @@ package Sidef::Convert::Convert {

*to_i = \&to_int;

sub to_array {
Sidef::Types::Array::Array->new($_[0]);
}

*to_a = \&to_array;

sub to_rat {
sub to_num {
Sidef::Types::Number::Number->new($_[0]);
}

*to_r = \&to_rat;
*to_n = \&to_num;

sub to_complex {
Sidef::Types::Number::Complex->new($_[0]);
sub to_float {
Sidef::Types::Number::Number->new($_[0])->float;
}

*to_c = \&to_complex;
*to_f = \&to_float;

sub to_num {
Sidef::Types::Number::Number->new($_[0]);
sub to_rat {
Sidef::Types::Number::Number->new($_[0])->rat;
}

*to_n = \&to_num;
*to_r = \&to_rat;

sub to_float {
Sidef::Types::Number::Number->new($_[0])->float;
sub to_array {
Sidef::Types::Array::Array->new($_[0]);
}

*to_f = \&to_float;
*to_a = \&to_array;

sub to_file {
Sidef::Types::Glob::File->new("$_[0]");
Expand Down
10 changes: 0 additions & 10 deletions lib/Sidef/Convert/Convert.pod
Expand Up @@ -36,16 +36,6 @@ Aliases: I<to_bool>

=cut

=head2 to_c

Convert.to_c() -> I<Obj>

Return the

Aliases: I<to_complex>

=cut

=head2 to_caller

Convert.to_caller() -> I<Obj>
Expand Down

0 comments on commit 6facc73

Please sign in to comment.