Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't overload numeric constant 0. #6870

Closed
p5pRT opened this issue Oct 27, 2003 · 4 comments
Closed

Can't overload numeric constant 0. #6870

p5pRT opened this issue Oct 27, 2003 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 27, 2003

Migrated from rt.perl.org#24313 (status was 'resolved')

Searchable as RT24313$

@p5pRT
Copy link
Author

p5pRT commented Oct 27, 2003

From @Abigail

Created by @Abigail

The following program overloads numeric constants, and should turn
all numbers into '17'​:

  #!/usr/bin/perl

  use strict;
  use warnings;

  BEGIN {
  $^H = 0x21000;
  $^H {integer} = sub {print "[", $_ [0], "]\n"; 17};
  }

  BEGIN {print "print 0;\n"}
  print 0, "\n";

  BEGIN {print "print 1;\n"}
  print 1, "\n";

  __END__
  print 0;
  print 1;
  [1]
  0
  17

It doesn't modify 0 though. I would have expected the result to be​:

  print 0;
  [0]
  print 1;
  [1]
  17
  17

Perl Info

Flags:
    category=core
    severity=low

Site configuration information for perl v5.8.1:

Configured by abigail at Thu Oct 23 13:09:17 CEST 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 1) configuration:
  Platform:
    osname=linux, osvers=2.4.18-bf2.4, archname=i686-linux-64int-ld
    uname='linux alexandra 2.4.18-bf2.4 #1 son apr 14 09:53:28 cest 2002 i686 unknown '
    config_args='-des -Dusedevel -Dusemorebits -Uversiononly -Dmydomain=.abigail.nl -Dcf_email=abigail@abigail.nl -Dperladmin=camel@abigail.nl -Doptimize=-g -Dcc=gcc -Dprefix=/opt/perl/5.8.1'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=define use64bitall=undef uselongdouble=define
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc', ccflags ='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-g',
    cppflags='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='3.0.4', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long long', ivsize=8, nvtype='long double', nvsize=12, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -ldl -lm -lcrypt -lutil -lc
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.2.5'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.8.1:
    /home/abigail/Perl
    /opt/perl/5.8.1/lib/5.8.1/i686-linux-64int-ld
    /opt/perl/5.8.1/lib/5.8.1
    /opt/perl/5.8.1/lib/site_perl/5.8.1/i686-linux-64int-ld
    /opt/perl/5.8.1/lib/site_perl/5.8.1
    /opt/perl/5.8.1/lib/site_perl
    .


Environment for perl v5.8.1:
    HOME=/home/abigail
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/home/abigail/Lib:/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib
    LOGDIR (unset)
    PATH=/home/abigail/Bin:/opt/perl/bin:/usr/local/bin:/usr/local/X11/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/games:/usr/share/texmf/bin:/opt/Acrobat/bin:/opt/java/blackdown/j2sdk1.3.1/bin:/usr/local/games/bin
    PERL5LIB=/home/abigail/Perl
    PERLDIR=/opt/perl
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Oct 27, 2003

From rick@bort.ca

On Mon, Oct 27, 2003 at 01​:05​:37PM +0100, Abigail wrote​:

[snip tests]

The original bug is "Can't overload numeric constant 0.", specifically
with overload​::constant('integer'). This is because a plain "0" is
being treated as a binary constant. The following patch treats a plain
zero as an integer constant under overload​::constant('integer'). For
backwards compatibility it will still treat it as a binary constant
under overload​::constant('binary') but if both are specified 'integer'
takes precedence.

--
Rick Delaney
rick@​bort.ca

Inline Patch
--- toke.c.orig	Mon Oct 27 11:04:56 2003
+++ toke.c	Mon Oct 27 12:00:13 2003
@@ -7252,6 +7252,7 @@
 	    UV u = 0;
 	    I32 shift;
 	    bool overflowed = FALSE;
+	    bool just_zero  = TRUE;	/* just plain 0 or binary number? */
 	    static NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
 	    static char* bases[5] = { "", "binary", "", "octal",
 				      "hexadecimal" };
@@ -7268,9 +7269,11 @@
 	    if (s[1] == 'x') {
 		shift = 4;
 		s += 2;
+		just_zero = FALSE;
 	    } else if (s[1] == 'b') {
 		shift = 1;
 		s += 2;
+		just_zero = FALSE;
 	    }
 	    /* check for a decimal in disguise */
 	    else if (s[1] == '.' || s[1] == 'e' || s[1] == 'E')
@@ -7342,6 +7345,7 @@
 		    */
 
 		  digit:
+		    just_zero = FALSE;
 		    if (!overflowed) {
 			x = u << shift;	/* make room for the digit */
 
@@ -7400,7 +7404,10 @@
 #endif
 		sv_setuv(sv, u);
 	    }
-	    if (PL_hints & HINT_NEW_BINARY)
+	    if (just_zero && (PL_hints & HINT_NEW_INTEGER))
+		sv = new_constant(start, s - start, "integer", 
+				  sv, Nullsv, NULL);
+	    else if (PL_hints & HINT_NEW_BINARY)
 		sv = new_constant(start, s - start, "binary", sv, Nullsv, NULL);
 	}
 	break;

@p5pRT
Copy link
Author

p5pRT commented Oct 28, 2003

From @rgarcia

Rick Delaney wrote​:

On Mon, Oct 27, 2003 at 01​:05​:37PM +0100, Abigail wrote​:

[snip tests]

The original bug is "Can't overload numeric constant 0.", specifically
with overload​::constant('integer'). This is because a plain "0" is
being treated as a binary constant. The following patch treats a plain
zero as an integer constant under overload​::constant('integer'). For
backwards compatibility it will still treat it as a binary constant
under overload​::constant('binary') but if both are specified 'integer'
takes precedence.

Thanks, applied as #21566, with Abigail's deTODOified tests.

--- toke.c.orig Mon Oct 27 11​:04​:56 2003
+++ toke.c Mon Oct 27 12​:00​:13 2003

@p5pRT p5pRT closed this as completed Oct 28, 2003
@p5pRT
Copy link
Author

p5pRT commented Oct 28, 2003

@rgs - Status changed from 'new' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant