151
151
import org .truffleruby .language .supercall .ZSuperOutsideMethodNode ;
152
152
import org .truffleruby .language .yield .YieldExpressionNode ;
153
153
154
+ import java .math .BigInteger ;
154
155
import java .util .ArrayDeque ;
155
156
import java .util .ArrayList ;
156
157
import java .util .Deque ;
@@ -1656,16 +1657,7 @@ public RubyNode visitFlipFlopNode(Nodes.FlipFlopNode node) {
1656
1657
1657
1658
@ Override
1658
1659
public RubyNode visitFloatNode (Nodes .FloatNode node ) {
1659
- final String string = toString (node ).replaceAll ("_" , "" );
1660
- double value ;
1661
-
1662
- try {
1663
- value = SafeDoubleParser .parseDouble (string );
1664
- } catch (NumberFormatException e ) {
1665
- value = string .startsWith ("-" ) ? Double .NEGATIVE_INFINITY : Double .POSITIVE_INFINITY ;
1666
- }
1667
-
1668
- final RubyNode rubyNode = new FloatLiteralNode (value );
1660
+ final RubyNode rubyNode = new FloatLiteralNode (node .value );
1669
1661
return assignPositionAndFlags (node , rubyNode );
1670
1662
}
1671
1663
@@ -2360,7 +2352,18 @@ public RubyNode visitInstanceVariableTargetNode(Nodes.InstanceVariableTargetNode
2360
2352
2361
2353
@ Override
2362
2354
public RubyNode visitIntegerNode (Nodes .IntegerNode node ) {
2363
- final RubyNode rubyNode = translateIntegerLiteralString (toString (node ));
2355
+ Object value = node .value ;
2356
+ final RubyNode rubyNode ;
2357
+ if (value instanceof Integer i ) {
2358
+ rubyNode = new IntegerFixnumLiteralNode (i );
2359
+ } else if (value instanceof Long l ) {
2360
+ rubyNode = new LongFixnumLiteralNode (l );
2361
+ } else if (value instanceof BigInteger bigInteger ) {
2362
+ rubyNode = new ObjectLiteralNode (new RubyBignum (bigInteger ));
2363
+ } else {
2364
+ throw CompilerDirectives .shouldNotReachHere (value .getClass ().getName ());
2365
+ }
2366
+
2364
2367
return assignPositionAndFlags (node , rubyNode );
2365
2368
}
2366
2369
@@ -2899,11 +2902,11 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
2899
2902
final RubyNode numeratorNode ;
2900
2903
final RubyNode denominatorNode ;
2901
2904
2902
- // Handle float literals differently and avoid Java float/double types to not lose precision.
2905
+ // Handle rational float literals differently and avoid Java float/double types to not lose precision.
2903
2906
// So normalize numerator and denominator, e.g 3.14r literal is translated into Rational(314, 100).
2904
- // The other option is to represent float literal as a String and rely on parsing String literals in
2905
- // the Kernel#Rational() method. The only downside is worse performance as far as Kernel#Rational()
2906
- // is implemented in Ruby .
2907
+ // The other option is to represent rational float literals as a String and rely on parsing String literals in
2908
+ // the Kernel#Rational() method. The downside is worse performance due to repeated runtime calls vs once at
2909
+ // translation time .
2907
2910
if (node .numeric instanceof Nodes .FloatNode floatNode ) {
2908
2911
// Translate as Rational.convert(numerator, denominator).
2909
2912
@@ -2918,7 +2921,7 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
2918
2921
assert fractionLength > 0 ;
2919
2922
2920
2923
String numerator = string .replace ("." , "" ); // remove float point
2921
- numeratorNode = translateIntegerLiteralString (numerator , 10 ); // string literal may have leading "0" (e.g. for 0.1r) so specify base explicitly
2924
+ numeratorNode = translateIntegerLiteralString (numerator ); // string literal may have leading "0" (e.g. for 0.1r) so specify base explicitly
2922
2925
2923
2926
String denominator = "1" + "0" .repeat (fractionLength );
2924
2927
denominatorNode = translateIntegerLiteralString (denominator );
@@ -2934,6 +2937,27 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
2934
2937
return assignPositionAndFlags (node , rubyNode );
2935
2938
}
2936
2939
2940
+ /** Parse Integer literal ourselves. */
2941
+ private RubyNode translateIntegerLiteralString (String string ) {
2942
+ final RubyNode rubyNode ;
2943
+ TruffleString tstring = toTString (string );
2944
+
2945
+ Object numeratorInteger = ConvertBytes .bytesToInum (RubyLanguage .getCurrentContext (), null , tstring ,
2946
+ sourceEncoding , 10 , true );
2947
+
2948
+ if (numeratorInteger instanceof Integer i ) {
2949
+ rubyNode = new IntegerFixnumLiteralNode (i );
2950
+ } else if (numeratorInteger instanceof Long l ) {
2951
+ rubyNode = new LongFixnumLiteralNode (l );
2952
+ } else if (numeratorInteger instanceof RubyBignum bignum ) {
2953
+ rubyNode = new ObjectLiteralNode (bignum );
2954
+ } else {
2955
+ throw CompilerDirectives .shouldNotReachHere (numeratorInteger .getClass ().getName ());
2956
+ }
2957
+
2958
+ return rubyNode ;
2959
+ }
2960
+
2937
2961
@ Override
2938
2962
public RubyNode visitRedoNode (Nodes .RedoNode node ) {
2939
2963
// The redo operator is invalid outside a proc body or while-loop body.
@@ -3911,32 +3935,4 @@ private Arity createArity(Nodes.ParametersNode parametersNode) {
3911
3935
parametersNode .keyword_rest != null );
3912
3936
}
3913
3937
3914
- /** Parse Integer literal ourselves. See https://github.com/ruby/yarp/issues/1098 */
3915
- private RubyNode translateIntegerLiteralString (String string , int base ) {
3916
- final RubyNode rubyNode ;
3917
- TruffleString tstring = toTString (string );
3918
-
3919
- Object numeratorInteger = ConvertBytes .bytesToInum (RubyLanguage .getCurrentContext (), null , tstring ,
3920
- sourceEncoding ,
3921
- base ,
3922
- true );
3923
-
3924
- if (numeratorInteger instanceof Integer i ) {
3925
- rubyNode = new IntegerFixnumLiteralNode (i );
3926
- } else if (numeratorInteger instanceof Long l ) {
3927
- rubyNode = new LongFixnumLiteralNode (l );
3928
- } else if (numeratorInteger instanceof RubyBignum bignum ) {
3929
- rubyNode = new ObjectLiteralNode (bignum );
3930
- } else {
3931
- throw CompilerDirectives .shouldNotReachHere (numeratorInteger .getClass ().getName ());
3932
- }
3933
-
3934
- return rubyNode ;
3935
- }
3936
-
3937
- /** Parse Integer literal */
3938
- private RubyNode translateIntegerLiteralString (String string ) {
3939
- return translateIntegerLiteralString (string , 0 ); // detect base automatically
3940
- }
3941
-
3942
3938
}
0 commit comments