Skip to content

Commit

Permalink
Add some more nqp:: ops and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Jun 22, 2011
1 parent 1572977 commit ba48021
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/nqp-opcode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ General notes:
of the opcode. Stick to lowercase letters here...
* ... except that bigints might be a _I suffix :-)
* Perhaps reserve _b for a boolean native type
* Might want to reserve _a and _h for "native" list/hash versions of opcodes
* Might want to reserve _l and _h for "native" list/hash versions of opcodes
* Might want to reserve _o for object/instance versions of opcodes
* aim for consistency with underscores and the like
(i.e., unlike parrot, don't mix "get_foo" and "getbar")
Expand Down
63 changes: 49 additions & 14 deletions src/PAST/NQP.pir
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ entry to produce the node to be returned.
maphash['print'] = 'print'
maphash['say'] = 'say'

# terms
maphash['time_i'] = 'time__I'
maphash['time_n'] = 'time__N'

# arithmetic opcodes
maphash['add_i'] = 'add__Iii'
maphash['add_n'] = 'add__Nnn'
Expand All @@ -217,6 +221,16 @@ entry to produce the node to be returned.
maphash['div_n'] = 'div__Nnn'
maphash['mod_i'] = 'mod__Iii'
maphash['mod_n'] = 'mod__Nnn'
maphash['pow_n'] = 'pow__Nnn'
maphash['neg_i'] = 'neg__Ii'
maphash['neg_n'] = 'neg__Nn'
maphash['abs_i'] = 'abs__Ii'
maphash['abs_n'] = 'abs__Nn'

maphash['ceil_n'] = 'ceil__Nn'
maphash['floor_n'] = 'floor__NN'
maphash['ln_n'] = 'ln__Nn'
maphash['sqrt_n'] = 'sqrt__Nn'

# string opcodes
maphash['chars'] = 'length__Is'
Expand All @@ -228,15 +242,19 @@ entry to produce the node to be returned.
maphash['ord'] = 'ord__Is'
maphash['lc'] = 'downcase__Ss'
maphash['uc'] = 'upcase__Ss'
maphash['substr'] = 'substr__Ssii'
maphash['x'] = 'repeat__Ssi'

# relational opcodes
maphash['cmp_i'] = 'cmp_Iii'
maphash['iseq_i'] = 'iseq__Iii'
maphash['isne_i'] = 'isne__Iii'
maphash['islt_i'] = 'islt__Iii'
maphash['isle_i'] = 'isle__Iii'
maphash['isgt_i'] = 'isgt__Iii'
maphash['isge_i'] = 'isge__Iii'

maphash['cmp_n'] = 'cmp_Inn'
maphash['iseq_n'] = 'iseq__Inn'
maphash['isne_n'] = 'isne__Inn'
maphash['islt_n'] = 'islt__Inn'
Expand All @@ -251,39 +269,56 @@ entry to produce the node to be returned.
maphash['isgt_s'] = 'isgt__Iss'
maphash['isge_s'] = 'isge__Iss'

# boolean opcodes
maphash['not_i'] = 'not__Ii'

# aggregate opcodes
maphash['atkey'] = 'set__PQs'
maphash['atpos'] = 'set__PQi'
maphash['bindkey'] = 'set__1QsP'
maphash['bindpos'] = 'set__1QiP'
maphash['deletekey'] = 'delete__0Qs'
maphash['deletepos'] = 'delete__0Qi'
maphash['existskey'] = 'exists__IQs'
maphash['existspos'] = 'exists__IQi'
maphash['elems'] = 'elements__IP'
maphash['iterator'] = 'iter__PP'
maphash['push'] = 'push__0PP'
maphash['pop'] = 'pop__PP'
maphash['shift'] = 'shift__PP'
maphash['unshift'] = 'unshift__0PP'
maphash['splice'] = 'splice__0PPii'
maphash['atpos'] = 'set__PQi'
maphash['bindpos'] = 'set__1QiP'
maphash['existspos'] = 'exists__IQi'
maphash['deletepos'] = 'delete__0Qi'
maphash['atkey'] = 'set__PQs'
maphash['bindkey'] = 'set__1QsP'
maphash['existskey'] = 'exists__IQs'
maphash['deletekey'] = 'delete__0Qs'
$P0 = new ['Hash']
$P0['pasttype'] = 'list'
maphash['list'] = $P0

# object opcodes
maphash['unbox_i'] = 'repr_unbox_int__IP'
maphash['unbox_n'] = 'repr_unbox_num__NP'
maphash['unbox_s'] = 'repr_unbox_str__SP'
maphash['bindattr'] = 'setattribute__3PPsP'
maphash['getattr'] = 'getattribute__PPPs'
maphash['create'] = 'repr_instance_of'
maphash['clone'] = 'clone__PP'
maphash['isconcrete'] = 'repr_defined__IP'
maphash['iscont'] = 'is_container__IP'
maphash['isnull'] = 'isnull__IP'
maphash['istrue'] = 'istrue__IP'
maphash['null'] = 'null__P'
maphash['unbox_i'] = 'repr_unbox_int__IP'
maphash['unbox_n'] = 'repr_unbox_num__NP'
maphash['unbox_s'] = 'repr_unbox_str__SP'
maphash['where'] = 'get_addr__IP'

# control opcodes
$P0 = new ['Hash']
$P0['pasttype'] = 'if'
maphash['if'] = $P0
maphash['if'] = $P0

$P0 = new ['Hash']
$P0['pasttype'] = 'unless'
maphash['unless'] = $P0
maphash['unless'] = $P0

$P0 = new ['Hash']
$P0['pasttype'] = 'while'
maphash['while'] = $P0
maphash['while'] = $P0

.return (maphash)
.end
49 changes: 48 additions & 1 deletion t/nqp/59-nqpop.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Test nqp::op pseudo-functions.

plan(25);
plan(64);


ok( nqp::add_i(5,2) == 7, 'nqp::add_i');
Expand Down Expand Up @@ -40,4 +40,51 @@ my $a := 10;
ok( nqp::if(0, ($a++), ($a--)) == 10, 'nqp::if shortcircuit');
ok( $a == 9, 'nqp::if shortcircuit');

ok( nqp::pow_n(2.0, 4) == 16.0, 'nqp::pow_n');
ok( nqp::neg_i(5) == -5, 'nqp::neg_i');
ok( nqp::neg_i(-10) == 10, 'nqp::neg_i');
ok( nqp::neg_n(5.2) == -5.2, 'nqp::neg_n');
ok( nqp::neg_n(-10.3) == 10.3, 'nqp::neg_n');
ok( nqp::abs_i(5) == 5, 'nqp::abs_i');
ok( nqp::abs_i(-10) == 10, 'nqp::abs_i');
ok( nqp::abs_n(5.2) == 5.2, 'nqp::abs_n');
ok( nqp::abs_n(-10.3) == 10.3, 'nqp::abs_n');

ok( nqp::ceil_n(5.2) == 6.0, 'nqp::ceil_n');
ok( nqp::ceil_n(-5.2) == -5.0, 'nqp::ceil_n');
ok( nqp::ceil_n(5.0) == 5.0, 'nqp::ceil_n');
ok( nqp::ceil_n(-5.0) == -5.0, 'nqp::ceil_n');
ok( nqp::floor_n(5.2) == 5.0, 'nqp::floor_n');
ok( nqp::floor_n(-5.2) == -6.0, 'nqp::floor_n');
ok( nqp::floor_n(5.0) == 5.0, 'nqp::floor_n');
ok( nqp::floor_n(-5.0) == -5.0, 'nqp::floor_n');

ok( nqp::substr('rakudo', 1, 3) eq 'aku', 'nqp::substr');
ok( nqp::substr('rakudo', 1) eq 'akudo', 'nqp::substr');
ok( nqp::substr('rakudo', 6, 3) eq '', 'nqp::substr');
ok( nqp::substr('rakudo', 6) eq '', 'nqp::substr');
ok( nqp::substr('rakudo', 0, 4) eq 'raku', 'nqp::substr');
ok( nqp::substr('rakudo', 0) eq 'rakudo', 'nqp::substr');

ok( nqp::x('abc', 5) eq 'abcabcabcabcabc', 'nqp::x');
ok( nqp::x('abc', 0) eq '', 'nqp::x');

ok( nqp::not_i(0) == 1, 'nqp::not_i');
ok( nqp::not_i(1) == 0, 'nqp::not_i');
ok( nqp::not_i(-1) == 0, 'nqp::not_i');

ok( nqp::isnull(nqp::null()) == 1, 'nqp::isnull/nqp::null' );

ok( nqp::istrue(0) == 0, 'nqp::istrue');
ok( nqp::istrue(1) == 1, 'nqp::istrue');
ok( nqp::istrue('') == 0, 'nqp::istrue');
ok( nqp::istrue('0') == 0, 'nqp::istrue');
ok( nqp::istrue('no') == 1, 'nqp::istrue');
ok( nqp::istrue(0.0) == 0, 'nqp::istrue');
ok( nqp::istrue(0.1) == 1, 'nqp::istrue');

my $list := nqp::list(0, 'a', 'b', 3.0);
ok( nqp::elems($list) == 4, 'nqp::elems');
ok( nqp::atpos($list, 0) == 0, 'nqp::atpos');
ok( nqp::atpos($list, 2) eq 'b', 'nqp::atpos');

0 comments on commit ba48021

Please sign in to comment.