Skip to content

Commit 25167ea

Browse files
committed
[truffle] Implement str to int coercion
1 parent cdea879 commit 25167ea

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/vm/jvm/Truffle.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@ class QAST::TruffleCompiler does SerializeOnce {
818818
if $got == $NUM {
819819
return TAST.new($INT, ['coerce-num-to-int', $tast.tree]);
820820
}
821+
if $got == $STR {
822+
return TAST.new($INT, ['coerce-str-to-int', $tast.tree]);
823+
}
821824
}
822825

823826
# TODO - Perl 6 proper does it differently than nqp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.perl6.nqp.truffle.nodes.expression;
2+
import com.oracle.truffle.api.frame.VirtualFrame;
3+
import com.oracle.truffle.api.nodes.NodeInfo;
4+
import org.perl6.nqp.truffle.nodes.NQPNode;
5+
import org.perl6.nqp.truffle.nodes.NQPIntNode;
6+
import org.perl6.nqp.dsl.Deserializer;
7+
8+
@NodeInfo(shortName = "coerce an str to int")
9+
public final class NQPCoerceStrToIntNode extends NQPIntNode {
10+
@Child private NQPNode argNode;
11+
12+
@Deserializer("coerce-str-to-int")
13+
public NQPCoerceStrToIntNode(NQPNode argNode) {
14+
this.argNode = argNode;
15+
}
16+
17+
@Override
18+
public long executeInt(VirtualFrame frame) {
19+
String value = argNode.executeStr(frame);
20+
try {
21+
return Long.parseLong(value);
22+
}
23+
catch (NumberFormatException e) {
24+
return 0;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)