Skip to content

Commit 115b38c

Browse files
committed
Implement nqp::radix_I.
1 parent 2ac90f3 commit 115b38c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,7 @@ QAST::OperationsJAST.map_classlib_core_op('ln_n', $TYPE_MATH, 'log', [$RT_NUM],
15801580
QAST::OperationsJAST.map_classlib_core_op('sqrt_n', $TYPE_MATH, 'sqrt', [$RT_NUM], $RT_NUM);
15811581
QAST::OperationsJAST.map_classlib_core_op('exp_n', $TYPE_MATH, 'exp', [$RT_NUM], $RT_NUM);
15821582
QAST::OperationsJAST.map_classlib_core_op('radix', $TYPE_OPS, 'radix', [$RT_INT, $RT_STR, $RT_INT, $RT_INT], $RT_OBJ, :tc);
1583+
QAST::OperationsJAST.map_classlib_core_op('radix_I', $TYPE_OPS, 'radix_I', [$RT_INT, $RT_STR, $RT_INT, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
15831584

15841585
# trig opcodes
15851586
QAST::OperationsJAST.map_classlib_core_op('sin_n', $TYPE_MATH, 'sin', [$RT_NUM], $RT_NUM);

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,59 @@ public static SixModelObject abs_I(SixModelObject a, SixModelObject type, Thread
33953395
return makeBI(tc, type, getBI(tc, a).abs());
33963396
}
33973397

3398+
public static SixModelObject radix_I(long radix_l, String str, long zpos, long flags, SixModelObject type, ThreadContext tc) {
3399+
BigInteger zvalue = BigInteger.ZERO;
3400+
BigInteger zbase = BigInteger.ONE;
3401+
int chars = str.length();
3402+
BigInteger value = zvalue;
3403+
BigInteger base = zbase;
3404+
long pos = -1;
3405+
char ch;
3406+
boolean neg = false;
3407+
BigInteger radix = BigInteger.valueOf(radix_l);
3408+
3409+
if (radix_l > 36) {
3410+
throw ExceptionHandling.dieInternal(tc, "Cannot convert radix of " + radix_l + " (max 36)");
3411+
}
3412+
3413+
ch = (zpos < chars) ? str.charAt((int)zpos) : 0;
3414+
if ((flags & 0x02) != 0 && (ch == '+' || ch == '-')) {
3415+
neg = (ch == '-');
3416+
zpos++;
3417+
ch = (zpos < chars) ? str.charAt((int)zpos) : 0;
3418+
}
3419+
while (zpos < chars) {
3420+
if (ch >= '0' && ch <= '9') ch = (char)(ch - '0');
3421+
else if (ch >= 'a' && ch <= 'z') ch = (char)(ch - 'a' + 10);
3422+
else if (ch >= 'A' && ch <= 'Z') ch = (char)(ch - 'A' + 10);
3423+
else break;
3424+
if (ch >= radix_l) break;
3425+
zvalue = zvalue.multiply(radix).add(BigInteger.valueOf(ch));
3426+
zbase = zbase.multiply(radix);
3427+
zpos++; pos = zpos;
3428+
if (ch != 0 || (flags & 0x04) == 0) { value=zvalue; base=zbase; }
3429+
if (zpos >= chars) break;
3430+
ch = str.charAt((int)zpos);
3431+
if (ch != '_') continue;
3432+
zpos++;
3433+
if (zpos >= chars) break;
3434+
ch = str.charAt((int)zpos);
3435+
}
3436+
3437+
if (neg || (flags & 0x01) != 0) { value = value.negate(); }
3438+
3439+
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
3440+
SixModelObject result = hllConfig.slurpyArrayType.st.REPR.allocate(tc,
3441+
hllConfig.slurpyArrayType.st);
3442+
result.initialize(tc);
3443+
3444+
result.push_boxed(tc, makeBI(tc, type, value));
3445+
result.push_boxed(tc, makeBI(tc, type, base));
3446+
result.push_boxed(tc, makeBI(tc, type, BigInteger.valueOf(pos)));
3447+
3448+
return result;
3449+
}
3450+
33983451
public static SixModelObject bitor_I(SixModelObject a, SixModelObject b, SixModelObject type, ThreadContext tc) {
33993452
return makeBI(tc, type, getBI(tc, a).or(getBI(tc, b)));
34003453
}

0 commit comments

Comments
 (0)