Skip to content

Commit cbbc951

Browse files
committed
Use the new IntegerNode#value and FloatNode#value from Prism
1 parent 530fbad commit cbbc951

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/main/java/org/truffleruby/language/literal/LongFixnumLiteralNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.language.literal;
1111

12+
import org.truffleruby.core.CoreLibrary;
1213
import org.truffleruby.language.RubyContextSourceNode;
1314

1415
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -22,6 +23,7 @@ public final class LongFixnumLiteralNode extends RubyContextSourceNode {
2223
private final long value;
2324

2425
public LongFixnumLiteralNode(long value) {
26+
assert !CoreLibrary.fitsIntoInteger(value) : "long in int range : " + value;
2527
this.value = value;
2628
}
2729

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
import org.truffleruby.language.supercall.ZSuperOutsideMethodNode;
152152
import org.truffleruby.language.yield.YieldExpressionNode;
153153

154+
import java.math.BigInteger;
154155
import java.util.ArrayDeque;
155156
import java.util.ArrayList;
156157
import java.util.Deque;
@@ -1656,16 +1657,7 @@ public RubyNode visitFlipFlopNode(Nodes.FlipFlopNode node) {
16561657

16571658
@Override
16581659
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);
16691661
return assignPositionAndFlags(node, rubyNode);
16701662
}
16711663

@@ -2360,7 +2352,18 @@ public RubyNode visitInstanceVariableTargetNode(Nodes.InstanceVariableTargetNode
23602352

23612353
@Override
23622354
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+
23642367
return assignPositionAndFlags(node, rubyNode);
23652368
}
23662369

@@ -2899,11 +2902,11 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
28992902
final RubyNode numeratorNode;
29002903
final RubyNode denominatorNode;
29012904

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.
29032906
// 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.
29072910
if (node.numeric instanceof Nodes.FloatNode floatNode) {
29082911
// Translate as Rational.convert(numerator, denominator).
29092912

@@ -2918,7 +2921,7 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
29182921
assert fractionLength > 0;
29192922

29202923
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
29222925

29232926
String denominator = "1" + "0".repeat(fractionLength);
29242927
denominatorNode = translateIntegerLiteralString(denominator);
@@ -2934,6 +2937,27 @@ public RubyNode visitRationalNode(Nodes.RationalNode node) {
29342937
return assignPositionAndFlags(node, rubyNode);
29352938
}
29362939

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+
29372961
@Override
29382962
public RubyNode visitRedoNode(Nodes.RedoNode node) {
29392963
// The redo operator is invalid outside a proc body or while-loop body.
@@ -3911,32 +3935,4 @@ private Arity createArity(Nodes.ParametersNode parametersNode) {
39113935
parametersNode.keyword_rest != null);
39123936
}
39133937

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-
39423938
}

0 commit comments

Comments
 (0)