Skip to content

Commit

Permalink
- Added the -k command-line options which makes the parser to displ…
Browse files Browse the repository at this point in the history
…ay some info messages about potential unsafe interpretations, such as interpreting "sqrt(x)" as "x.sqrt"

- Updated some Sidef scripts.
- Updated the .pod files.
  • Loading branch information
trizen committed Dec 10, 2015
1 parent f2de411 commit e31de40
Show file tree
Hide file tree
Showing 32 changed files with 80 additions and 78 deletions.
13 changes: 9 additions & 4 deletions bin/sidef
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sub NATIVE () { 0 }
my %args;
if ($#ARGV != -1 and chr ord $ARGV[0] eq '-') {
require Getopt::Std;
Getopt::Std::getopts('e:E:d:Dho:ivHn:WwbcrR:ts:CO:', \%args);
Getopt::Std::getopts('e:E:d:Dho:ivHn:WwbcrR:ts:CO:k', \%args);
}

# Help
Expand Down Expand Up @@ -223,8 +223,11 @@ sub read_script {

sub new_parser {
my (%opt) = @_;
Sidef::Parser->new(file_name => $opt{name},
script_name => $opt{name},);
Sidef::Parser->new(
file_name => $opt{name},
script_name => $opt{name},
opt => \%args,
);
}

sub optimize_struct {
Expand Down Expand Up @@ -260,6 +263,7 @@ sub parse_code {
$struct = Sidef::Parser->new(
file_name => $parser->{file_name},
script_name => $parser->{script_name},
opt => \%args,
)->parse_script(code => \$code);

$struct = optimize_struct($struct);
Expand Down Expand Up @@ -298,6 +302,7 @@ sub output_usage {
'-D' => 'dump the syntax tree of a program',
'-o file' => 'file where to dump the output',
'-O level' => ['perform code optimizations before execution', 'valid levels: 0, 1, 2'],
'-k' => 'keep track of potential unsafe parser interpretations',
'-E program' => 'one line of program',
'-H' => 'interactive help',
'-n type' => ['try to use a specific backend for Math::BigInt', 'valid types: GMP, Pari, FastCalc'],
Expand Down Expand Up @@ -636,7 +641,7 @@ HEAD

close $fh;
}
}
}
} => $path
);

Expand Down
25 changes: 1 addition & 24 deletions lib/Sidef.pod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

=encoding utf8

=head1 NAME
Expand Down Expand Up @@ -100,27 +101,3 @@ CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut

=head2 normalize_type

Sidef.normalize_type() -> I<Obj>

Return the

=cut

=head2 normalize_method

Sidef.normalize_method() -> I<Obj>

Return the

=cut

=head2 normalize_type

Sidef.normalize_type() -> I<Obj>

Return the

=cut
11 changes: 11 additions & 0 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,12 @@ package Sidef::Parser {
) {
if (exists $self->{current_method}) {
if (defined(my $var = $self->find_var('self', $class))) {

if ($self->{opt}{k}) {
print STDERR
"[INFO] `$name` is interpreted as `self.$name` at $self->{file_name} line $self->{line}\n";
}

$var->{count}++;
$var->{obj}{in_use} = 1;
return
Expand Down Expand Up @@ -2078,6 +2084,11 @@ package Sidef::Parser {
# Method call in functional style
if (not $self->{_want_name} and ($class eq $self->{class} or $class eq 'CORE')) {

if ($self->{opt}{k}) {
print STDERR
"[INFO] `$name` is interpreted as a prefix method-call at $self->{file_name} line $self->{line}\n";
}

my $pos = pos($_);
/\G\h*/gc; # remove any horizontal whitespace
my $arg = (
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Array/Array.pod
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Array.contain() -> I<Obj>

Return the

Aliases: I<contains>
Aliases: I<include>, I<contains>, I<includes>

=cut

Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Block/Block.pod
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Block.for() -> I<Obj>

Return the

Aliases: I<foreach>

=cut

=head2 fork
Expand Down
6 changes: 4 additions & 2 deletions lib/Sidef/Types/Glob/FileHandle.pod
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ Aliases: I<read_char>

=cut

=head2 is_on_tty
=head2 isatty

FileHandle.is_on_tty() -> I<Obj>
FileHandle.isatty() -> I<Obj>

Return the

Aliases: I<is_on_tty>

=cut

=head2 lines
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Hash/Hash.pod
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Hash.exists() -> I<Obj>

Return the

Aliases: I<has_key>, I<contains>
Aliases: I<has_key>, I<contain>, I<include>, I<contains>, I<includes>

=cut

Expand Down
12 changes: 8 additions & 4 deletions lib/Sidef/Types/Range/RangeNumber.pod
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ Return the

=cut

=head2 contains
=head2 contain

RangeNumber.contains() -> I<Obj>
RangeNumber.contain() -> I<Obj>

Return the

Aliases: I<includes>
Aliases: I<include>, I<contains>, I<includes>

=cut

Expand All @@ -63,7 +63,7 @@ RangeNumber.for() -> I<Obj>

Return the

Aliases: I<each>
Aliases: I<each>, I<foreach>

=cut

Expand All @@ -73,6 +73,8 @@ RangeNumber.grep() -> I<Obj>

Return the

Aliases: I<filter>, I<select>

=cut

=head2 map
Expand All @@ -81,6 +83,8 @@ RangeNumber.map() -> I<Obj>

Return the

Aliases: I<collect>

=cut

=head2 max
Expand Down
8 changes: 4 additions & 4 deletions lib/Sidef/Types/Range/RangeString.pod
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Return the

=cut

=head2 contains
=head2 contain

RangeString.contains() -> I<Obj>
RangeString.contain() -> I<Obj>

Return the

Aliases: I<includes>
Aliases: I<include>, I<contains>, I<includes>

=cut

Expand All @@ -55,7 +55,7 @@ RangeString.for() -> I<Obj>

Return the

Aliases: I<each>
Aliases: I<each>, I<foreach>

=cut

Expand Down
20 changes: 10 additions & 10 deletions lib/Sidef/Types/String/String.pod
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@ Return the

=cut

=head2 contain

String.contain() -> I<Obj>

Return the

Aliases: I<include>, I<contains>, I<includes>

=cut

=head2 count

String.count() -> I<Obj>
Expand Down Expand Up @@ -570,16 +580,6 @@ Return the

=cut

=head2 include

String.include() -> I<Obj>

Return the

Aliases: I<contains>

=cut

=head2 index

String.index() -> I<Obj>
Expand Down
4 changes: 2 additions & 2 deletions scripts/Games/asciiplanes.sf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Find the planes' positions on a grid. (text-based game)

var asciitable = (
try { require 'Text::ASCIITable' }
try { require('Text::ASCIITable') }
catch { STDERR.print("Can't load the 'Text::ASCIITable' Perl module...\n"); Sys.exit(2) }
);

Expand Down Expand Up @@ -64,7 +64,7 @@ func ver {

if (!ARGV.is_empty) {
var getopt = (
try { frequire 'Getopt::Long' }
try { frequire('Getopt::Long') }
catch { STDERR.print("Can't load the 'Getopt::Long' Perl module...\n"); Sys.exit(2) }
);

Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/color_bars.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/Colour_bars/Display
#

require 'GD';
require('GD');

var colors = Hash.new(
white => [255, 255, 255],
Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/fractal_tree.sf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func tree(img, x, y, scale=6/10, len=400, angle=270) {
tree(img, x1, y1, scale, len*scale, angle-35);
}

require 'GD::Simple';
require('GD::Simple');

var (width=1000, height=1000);
var img = %s'GD::Simple'.new(width, height);
Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/hello_world_graphical.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/Hello_world/Graphical
#

var tk = require 'Tk';
var tk = require('Tk');
var main = 'MainWindow'.to_caller.new;
main.Button(
'-text' => 'Goodbye, World!',
Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/munching_squares.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/Munching_squares
#

require 'GD';
require('GD');
 
var img = %s'GD::Image'.new(256, 256, 1);

Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/sierpinski_triangle_graphical.sf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Array {
method to_png(scale=1, bgcolor='white', fgcolor='black') {

var gd = (
try { require 'GD::Simple' }
try { require('GD::Simple') }
catch { warn "GD::Simple is not installed!"; return };
);

Expand Down
2 changes: 1 addition & 1 deletion scripts/Graphical/window_creation.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
## http://rosettacode.org/wiki/Window_creation
#

var tk = require 'Tk';
var tk = require('Tk');
'MainWindow'.to_caller.new;
tk.MainLoop;
2 changes: 1 addition & 1 deletion scripts/Interactive/socket_inet.sf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/ruby

var inet = require 'IO::Socket::INET';
var inet = require('IO::Socket::INET');

var sock = inet.new(
LocalAddr => "127.0.0.1:8080",
Expand Down
2 changes: 1 addition & 1 deletion scripts/Project Euler/0019-counting_sundays.sf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Answer: 171

var dt = require 'DateTime';
var dt = require('DateTime');

func first_sundays(year) {
var count = 0;
Expand Down
2 changes: 1 addition & 1 deletion scripts/Project Euler/0077-prime_summations.sf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Answer: 71

require 'ntheory';
require('ntheory');

var primes = [];

Expand Down
2 changes: 1 addition & 1 deletion scripts/RosettaCode/five_weekends.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/Five_weekends
#

require 'DateTime';
require('DateTime');

var happymonths = [];
var workhardyears = [];
Expand Down
2 changes: 1 addition & 1 deletion scripts/RosettaCode/last_sunday_of_each_month.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/Find_the_last_Sunday_of_each_month
#

require 'DateTime';
require('DateTime');

var year = (ARGV[0] \\ 2015);

Expand Down
14 changes: 7 additions & 7 deletions scripts/RosettaCode/real_constants_and_functions.sf
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ say x.ceil; # ceiling
say x**y; # exponentiation

## Alternative syntax:
say sqrt x; # square root
say log x; # natural logarithm
say log10 x; # base 10 logarithm
say exp x; # e raised to the power of x
say abs x; # absolute value
say floor x; # floor
say ceil x; # ceiling
say sqrt(x); # square root
say log(x); # natural logarithm
say log10(x); # base 10 logarithm
say exp(x); # e raised to the power of x
say abs(x); # absolute value
say floor(x); # floor
say ceil(x); # ceiling
2 changes: 1 addition & 1 deletion scripts/RosettaCode/the_isaac_cipher.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## http://rosettacode.org/wiki/The_ISAAC_Cipher
#

require 'Math::Random::ISAAC';
require('Math::Random::ISAAC');

func xor_isaac(key, msg) {
var rng = %s'Math::Random::ISAAC'.new(unpack('C*', key));
Expand Down
Loading

0 comments on commit e31de40

Please sign in to comment.