function returns and modifying squaak
Jay Emerson
jayemerson at gmail.com
Sat Jun 25 16:46:00 UTC 2011
I'm now trying to use 3.3.0; previously, using 2.8.0, I was able to
modify squaak to add
function returns (successfully); since then, I found the 2008 advice
on parrotblog.org which
paralleled what I had to do:
http://www.parrotblog.org/2008_06_11_archive.html
I was unable to succeed with 3.3.0, and wanted to describe the steps I
used and seek advice.
1. Added a return_statement to Grammar.pm, and a return to
00-sanity.t. Result: build and test passed. Question: at this point there
is no action associated with the return, why wouldn't this give
some sort of error?
# JWE NEW: return_statement, in Grammar.pm
rule statement:sym<return_statement> {
'return' <EXPR>
}
# test subroutines # NEW JWE in 00-sanity.t
sub foo(a)
print("ok ", a)
return a
end
2. Added to Actions.pm only the return_statement method.
Result: build succeeded, test failed. Possibly because I need
something else (see 3).
# JWE NEW in Actions.pm
method statement:sym<return_statement>($/) {
my $expr := $<EXPR>.ast;
make PAST::Op.new( $expr, :pasttype('return'), :node($/) );
}
3. Added to Actions.pm, the next to last line of sub_definition.
Result: build ok, test ok, may have fixed issue with item 2 above.
# JWE NEW in Actions.pm, almost at end of sub_definition
$past.control('return_pir');
4. At this point, I may still need to add something as was recommended on
http://www.parrotblog.org/2008_06_11_archive.html for rule, term, but
perhaps not (unsure about this Longest Token Matching comment). So I'll
expand the test:
# JWE NEW in 00-sanity.t
myresult = foo(5)
RESULT: this generated a parse error. See 5, next.
5. To attempt to recover, I wanted to do something similar to what was
recommended, a current version of this, which I had used previously in
2.8.0 squaak:
rule term {
...
| <sub_call> #= sub_call
| <primary> #= primary
...
}
So, in Grammar.pm I added:
'return' to the keyword list, just in case; the following right before the
token term:sym<primary> { line:
# JWE NEW in Grammar.pm:
token term:sym<sub_call> { <sub_call> }
This built, but the tests failed, with
Parse errors: Unknown TAP token: "Method 'sub_call' not found for
invocant of class 'Squaak;Grammar'"
Now, I'm thinking that here the method for sub_call is a statement, not a term,
and is causing this problem? I tried adding a method like sub_call
but as a term
rather than as a statement, with no luck.
CONCLUSION:
I'm at a loss. I hope I'm close, and this is something minor. Advice on how
to do this (e.g. a modern version of that parrotblog post) would be most
welcome.
Thanks,
Jay
--
John W. Emerson (Jay)
Associate Professor of Statistics
Department of Statistics
Yale University
http://www.stat.yale.edu/~jay
More information about the parrot-dev
mailing list