Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make add_simple_op support ops with a variable numer of arguments, us…
…e it for nqp::ord and nqp::index.
  • Loading branch information
pmurias committed Aug 17, 2014
1 parent 74fa9cf commit e49d2b1
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -193,8 +193,8 @@ class QAST::OperationsJS {
my @exprs;
my @setup;
my $i := 0;
for @argument_types -> $type {
my $chunk := $comp.as_js($node[$i], :want($type));
for $node.list -> $arg {
my $chunk := $comp.as_js($arg, :want(@argument_types[$i]));
@exprs.push($chunk.expr);
@setup.push($chunk);
$i := $i + 1;
Expand Down Expand Up @@ -244,33 +244,19 @@ class QAST::OperationsJS {

add_simple_op('join', $T_STR, [$T_STR, $T_OBJ], sub ($delim, $list) {"$list.join($delim)"});

add_op('index', sub ($comp, $node, :$want) {
my $string := $comp.as_js($node[0], :want($T_STR));
my $pattern := $comp.as_js($node[1], :want($T_STR));
if +$node.list == 2 {
Chunk.new($T_INT, "{$string.expr}.indexOf({$pattern.expr})" , [$string, $pattern], :$node);
} else {
my $starting := $comp.as_js($node[2], :want($T_INT));
Chunk.new($T_INT, "{$string.expr}.indexOf({$pattern.expr},{$starting.expr})" , [$string, $pattern, $starting], :$node);
}
add_simple_op('index', $T_INT, [$T_STR, $T_STR, $T_INT], sub ($string, $pattern, $from?) {
nqp::defined($from) ?? "$string.indexOf($pattern,$from)" !! "$string.indexOf($pattern)";
});


add_simple_op('chr', $T_STR, [$T_INT], sub ($code) {"String.fromCharCode($code)"});

add_simple_op('lc', $T_STR, [$T_STR], sub ($string) {"$string.toLowerCase()"});
add_simple_op('uc', $T_STR, [$T_STR], sub ($string) {"$string.toUpperCase()"});

add_simple_op('flip', $T_STR, [$T_STR], sub ($string) {"$string.split('').reverse().join('')"});

add_op('ord', sub ($comp, $node, :$want) {
my $string := $comp.as_js($node[0], :want($T_STR));
if +$node.list == 1 {
Chunk.new($T_INT, "{$string.expr}.charCodeAt(0)" , [$string], :$node);
} else {
my $pos := $comp.as_js($node[1], :want($T_INT));
Chunk.new($T_INT, "{$string.expr}.charCodeAt({$pos.expr})" , [$string, $pos], :$node);
}
});
add_simple_op('ord', $T_INT, [$T_STR, $T_INT], sub ($string, $pos='0') {"$string.charCodeAt($pos)"});

add_simple_op('null', $T_OBJ, [], sub () {"null"});

Expand Down

0 comments on commit e49d2b1

Please sign in to comment.