hurrah, some semblance of respectability
Hangs on test1.
#!perl -pl
$*='(?:-?\d+(?: (?:[-*\/+]|-?\d+))*)';sub a{$_=~$1?$1:$`;0while s/\(([^()]+)\)/&a/e;s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/()([-+])($*)$/;"$_@_";}s/\s//g+s/(.*)/a/e
|
hurrah, some semblance of respectability
Sorry, the segfault persists. The problematic piece of code is: s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚
#!perl -pl
$*='(?:-?\d+(?: (?:[-*\/+]|-?\d+))*)';sub a{$_=$1;0while s/\(([^()]+)\)/&a/e;s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/^(.*(?:\d| .))([-+])($*)$/;"$_@_";}s/\s//g+s/(.*)/a/e
|
sub 200 would be tolerable
#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+ ]*\d)([*/])([^+ ]+)&a(" $3 $2")⪚$_=a(" $' $2")if/^(.+)([+ ])/;"$_@_";}s/\s//g+s/(\d|\))-/$1 /g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/+y/ /-/
|
sub 200 would be tolerable
#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+ ]*\d)([*/])([-%\d]+)&a(" $3 $2")⪚$_=a(" $' $2")if/^(.+)([+ ])/;"$_@_";}s/\s//g+s/(\d|\))-/$1 /g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/+y/ /-/
|
so much effort, so little progress
Running test 47 ... Program rpn.pl killed by signal (11) consistently dies on test47
#!perl -pl
$*='(?:-?\d+(?> (?:[ *\/+]|-?\d+))*)';sub a{$_=$1;0while s/\(([^()]+)\)/&a/e;s&($t(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/^(.*)([+ ])($*)/;"$_@_";}s/\s//g+s/(\d|\))-/$1 /g+s/(.*)/a/e+y/ /-/
|
I see the light
#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+ ]+)([*/])([-%\d]+)&$0[++$-]=a(" $3 $2");"%$-"⪚$_=a(" $' $2")if/(.+)([+ ])/;"$_@_"}s/\s//g+s/(\d|\))-/$1 /g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/|y/ /-/
|
nearly down to double figures :)
#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+~]+)([*/])([-%\d]+)&$0[++$-]=a(" $3 $2");"%$-"⪚s/(.+)([+~])(.+)/a(" $3 $2")/e;$_.="@_"}s/\s//g;s/(\d|\))-/$1~/g;s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/|y/~/-/
|
guess this approach won't get too close to 104 then...
Segfaults on 5.6.1 on Linux. Probably the (??{}).
#!perl -pl
sub t{$_=pop;s/\((.*)\)/a($1)/e;"$_@_"}$b=qr/\((?:(?>[^()]+)|(??{$b}))*\)/;$p=qr/$b|-?\d+/;sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/$p([-+]($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}y/ //d;$_=a$_
|
guess this approach won't get too close to 104 then...
#!perl -pl
sub t{$_=pop;s/\((.*)\)/a($1)/e;"$_@_"}$b=qr/\((?:(?>[^()]+)|(??{$b}))*\)/;$p=qr/$b|-?\d+/;sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/[\d)]([-+]($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}y/ //d;$_=a$_
|
parsing by regex still sucks
#!perl -pl
sub t{$_=pop;/^\((.*)\)$/?a($1,@_):"$_@_"}$b=qr{\((?:(?>[^()]+)|(??{$b}))*\)};$p=qr{$b|-?\d+};sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/(?<=[\d)])(([-+])($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}s/\s//g;$_=a$_
|
parsing by regex sucks
#!perl -pl
sub t{$_=shift;/^\((.*)\)$/?a($1,@_):"$_@_"}$b=qr{\((?:(?>[^()]+)|(??{$b}))*\)};sub p{$_=shift;/(\*|\/)($b|-?\d+)$/?p("$`",'',t($2," $1@_")):t $_,@_}$p=qr{(?:$b|-?\d+)};sub a{$_=shift;/(?<=[\d)])(([-+])($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}s/\s//g;$_=a$_
|