Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Start getting the coercion infrastructure in place; it complains abou…
…t the (many) cases it doesn't know.
  • Loading branch information
jnthn committed Apr 22, 2012
1 parent 2d62455 commit 37cbc8f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/QAST/Compiler.nqp
Expand Up @@ -146,6 +146,40 @@ class QAST::Compiler is HLL::Compiler {
$ops;
}

method coerce($qast, $desired) {
my $result := self.infer_type($qast.result());
if $result eq $desired {
# Exact match
return $qast;
}
elsif nqp::lc($result) eq $desired {
# The result is in a register, and our desired type allows
# both registers and literals.
return $qast;
}
else {
pir::die("Coercion from '$result' to '$desired' NYI");
}
}

method infer_type($inferee) {
if nqp::substr($inferee, 0, 1) eq '$' {
nqp::substr($inferee, 1, 1)
}
elsif nqp::substr($inferee, 0, 6) eq 'utf8:"' || nqp::substr($inferee, 0, 6) eq 'ucs4:"' {
"s"
}
elsif nqp::index($inferee, ".", 0) > 0 {
"n"
}
elsif +$inferee eq $inferee {
"i"
}
else {
pir::die("Cannot infer type from '$inferee'");
}
}

multi method as_post(QAST::Regex $node) {
my $ops := self.post_new('Ops');
my $prefix := self.unique('rx') ~ '_';
Expand Down
12 changes: 7 additions & 5 deletions src/QAST/Operations.nqp
Expand Up @@ -58,15 +58,17 @@ class QAST::Operations {

# Build the arguments list.
# XXX keyed
# XXX coercions/boxings/unboxings
if +$op.list != +@arg_types {
my $num_args := +$op.list;
my $i := 0;
if +@arg_types != $num_args {
pir::die("Operation '" ~ $op.op ~ "' requires " ~
+@arg_types ~ " operands, but got " ~ +$op.list);
+@arg_types ~ " operands, but got $num_args");
}
for $op.list {
my $post := $qastcomp.as_post($_);
while $i < $num_args {
my $post := $qastcomp.coerce($qastcomp.as_post($op.list[$i]), @arg_types[$i]);
$ops.push($post);
@args.push($post.result);
$i := $i + 1;
}

# If we have an integer as the return type, find the arg that
Expand Down

0 comments on commit 37cbc8f

Please sign in to comment.