Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[java/compiler] generating anonymous funcs to compile the Setting
  • Loading branch information
mberends committed Oct 2, 2010
1 parent cffcdc8 commit f3bd7b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
44 changes: 30 additions & 14 deletions java/compiler/JST2Java.pm
Expand Up @@ -35,17 +35,14 @@ our multi sub java_for(JST::Class $class) {
if $class.namespace {
$code := $code ~ 'package ' ~ $class.namespce ~ ";\n";
}
$code := $code ~ 'class ' ~ $class.name ~ " \{ // JST::Class\n";
$code := $code ~ 'public class ' ~ $class.name ~ " \{ // JST::Class\n"; # the C# version omits public
for @($class) {
$code := $code ~ java_for($_);
}
$code := $code ~ "}\n";
return $code;
}




our multi sub java_for(JST::Attribute $attr) {
return ' private static ' ~ $attr.type ~ ' ' ~ $attr.name ~ "; // JST:Attribute\n";
}
Expand All @@ -54,7 +51,7 @@ our multi sub java_for(JST::Method $meth) {
my $*LAST_TEMP := '';

# Method header.
my $code := ' private static ' ~
my $code := ' public static ' ~ # the C# version has private
$meth.return_type ~ ' ' ~
$meth.name ~ '(' ~
pir::join(', ', $meth.params) ~
Expand Down Expand Up @@ -109,9 +106,10 @@ our multi sub java_for(JST::MethodCall $mc) {

# Code-gen the call.
$code := $code ~ ' ';
my $ret_type := $mc.type || 'var'; # TODO: delete
unless $mc.void {
$*LAST_TEMP := get_unique_id('result');
my $ret_type := $mc.type || 'var';
# TODO my $ret_type := $mc.type || 'var';
my $method_name := "$invocant." ~ $mc.name;
if $ret_type eq 'var' && $method_name eq 'Ops.unbox_str'
{
Expand All @@ -132,13 +130,15 @@ our multi sub java_for(JST::MethodCall $mc) {
$method_name eq 'Ops.equal_strs' ||
$method_name eq 'Ops.get_how' ||
$method_name eq 'Ops.get_what' ||
$method_name eq 'Ops.instance_of' ||
$method_name eq 'Ops.lllist_elems' ||
$method_name eq 'Ops.lllist_get_at_pos' ||
$method_name eq 'Ops.logical_not_int' ||
$method_name eq 'Ops.multi_dispatch_over_lexical_candidates' ||
$method_name eq 'Ops.mod_int' ||
$method_name eq 'Ops.mul_int' ||
$method_name eq 'Ops.sub_int'
$method_name eq 'Ops.sub_int' ||
$method_name eq 'StaticBlockInfo[1].StaticLexPad.SetByName' # WTF?
)
{
$ret_type := 'RakudoObject';
Expand All @@ -147,6 +147,7 @@ our multi sub java_for(JST::MethodCall $mc) {
}
$code := $code ~ "$invocant." ~ $mc.name ~
"(" ~ pir::join(', ', @arg_names) ~ "); // JST::MethodCall\n";
if $ret_type eq 'var' { $code := $code ~ "// var should be " ~ $invocant ~ "." ~ $mc.name ~ "\n"; }

return $code;
}
Expand Down Expand Up @@ -183,9 +184,14 @@ our multi sub java_for(JST::New $new) {

# Code-gen the constructor call.
$*LAST_TEMP := get_unique_id('new');
$code := $code ~ " " ~ $new.type ~ " $*LAST_TEMP = new " ~
$new.type ~ "(" ~ pir::join(', ', @arg_names) ~ "); // JST::New\n";

$code := $code ~ " " ~ $new.type ~ " $*LAST_TEMP = new ";
if $new.type eq 'RakudoCodeRef.IFunc_Body' {
$code := $code ~ $new.type ~ "() \{ public RakudoObject Invoke( ThreadContext TC, RakudoObject Obj, RakudoObject Cap ) \{ return ((RakudoCodeRef.Instance)Obj).Body.Invoke(TC, Obj, Cap);\}\}";
}
else {
$code := $code ~ $new.type ~ "(" ~ pir::join(', ', @arg_names) ~ ")";
}
$code := $code ~ "; // JST::New\n";
return $code;
}

Expand Down Expand Up @@ -245,10 +251,20 @@ our multi sub java_for(JST::Bind $bind) {
}

our multi sub java_for(JST::Literal $lit) {
$*LAST_TEMP := $lit.escape ??
# XXX Need to really escape stuff in there.
('"' ~ ~$lit.value ~ '"') !! # CSharp began with @" here
$lit.value;
if $lit.escape {
my $str_in := $lit.value;
my $str_out := '';
while pir::length($str_in) {
my $char := pir::substr($str_in, 0, 1);
$str_in := pir::substr($str_in, 1);
if $char eq "\n" { $char := "\\n"; }
if $char eq "\t" { $char := "\\t"; }
$str_out := $str_out ~ $char;
}
$*LAST_TEMP := '"' ~ $str_out ~ '"'
} else {
$*LAST_TEMP := $lit.value;
}
return '';
}

Expand Down
15 changes: 12 additions & 3 deletions java/compiler/Makefile
@@ -1,6 +1,13 @@
# This works if you have parrot, parrot-nqp and javac in your path.

all: gen_actions.pir gen_grammar.pir gen_jst.pir gen_past2jst.pir gen_jst2java.pir gen_nqpsetting.class
CLASSES = classes/

JAVAC = javac -d $(CLASSES)

all: $(CLASSES) gen_actions.pir gen_grammar.pir gen_jst.pir gen_past2jst.pir gen_jst2java.pir NQPSetting.jar

$(CLASSES):
perl -MExtUtils::Command -e mkpath $(CLASSES)

gen_actions.pir: Actions.pm
parrot-nqp --target=pir Actions.pm > gen_actions.pir
Expand All @@ -17,20 +24,22 @@ gen_jst.pir: JST.pm
gen_jst2java.pir: JST2Java.pm
parrot-nqp --target=pir JST2Java.pm > gen_jst2java.pir

gen_nqpsetting.class: ../../common/NQP/NQPSetting.pm \
NQPSetting.jar: ../../common/NQP/NQPSetting.pm \
gen_actions.pir \
gen_grammar.pir \
gen_jst.pir \
gen_past2jst.pir \
gen_jst2java.pir
parrot compile.pir ../../common/NQP/NQPSetting.pm --setting > gen_nqpsetting.java
javac -classpath ../runtime/RakudoRuntime.jar gen_nqpsetting.java
$(JAVAC) -classpath ../runtime/RakudoRuntime.jar gen_nqpsetting.java
jar cf NQPSetting.jar -C $(CLASSES) .

test: all
prove -r --exec try.sh ..\..\t\nqp

clean:
perl -MExtUtils::Command -e rm_f gen_*.pir *.class *.jar *.java
perl -MExtUtils::Command -e rm_rf $(CLASSES)

# Java source code files "depend" on files in the dotnet/* directories,
# in the sense that they were manually translated.
Expand Down
12 changes: 7 additions & 5 deletions java/compiler/PAST2JSTCompiler.pm
@@ -1,6 +1,6 @@
# This is the beginings of a PAST to Java Syntax Tree translator. It'll
# no doubt evolve somewhat over time, and get filled out as we support more
# and more of PAST.
# no doubt evolve somewhat over time, and get filled out as we support
# more and more of PAST.
class PAST2JSTCompiler;

# Entry point for the compiler.
Expand Down Expand Up @@ -75,7 +75,7 @@ method compile(PAST::Node $node) {
$class.push(JST::Method.new(
:name('LoadSetting'),
:return_type('Context'),
JST::Temp.new( :name('TC'), :type('Context'),
JST::Temp.new( :name('TC'), :type('ThreadContext'),
JST::MethodCall.new(
:on('Rakudo.Init'), :name('Initialize'),
:type('ThreadContext'),
Expand Down Expand Up @@ -110,7 +110,7 @@ method compile(PAST::Node $node) {
}
else {
$class.push(JST::Method.new(
:name('Main'),
:name('main'), # Main in the C# version
:return_type('void'),
JST::Temp.new( :name('TC'), :type('ThreadContext'),
JST::MethodCall.new(
Expand Down Expand Up @@ -142,6 +142,7 @@ method compile(PAST::Node $node) {
JST::Using.new( :namespace('Rakudo.Metamodel.Representations.P6int') ),
JST::Using.new( :namespace('Rakudo.Metamodel.Representations.RakudoCodeRef') ),
JST::Using.new( :namespace('Rakudo.Metamodel.Representations.RakudoCodeRef.Instance') ),
JST::Using.new( :namespace('Rakudo.Metamodel.REPRRegistry') ),
JST::Using.new( :namespace('Rakudo.Runtime.CaptureHelper') ),
JST::Using.new( :namespace('Rakudo.Runtime.CodeObjectUtility') ),
JST::Using.new( :namespace('Rakudo.Runtime.Context') ),
Expand Down Expand Up @@ -368,7 +369,8 @@ our multi sub jst_for(PAST::Block $block) {

# Finish geneating code setup block call.
$our_sbi_setup.push(JST::New.new(
:type('Func<ThreadContext, RakudoObject, RakudoObject, RakudoObject>'),
:type('RakudoCodeRef.IFunc_Body'),
# :type('Func<ThreadContext, RakudoObject, RakudoObject, RakudoObject>'),
$result.name
));
$our_sbi_setup.push("StaticBlockInfo[$outer_sbi]");
Expand Down
9 changes: 4 additions & 5 deletions java/compiler/try.sh
@@ -1,12 +1,11 @@
#!/bin/sh
# This version of try.sh for 6model/java on Unix
# cp ../runtime/RakudoRuntime.jar . # rather reference the original
# This version of try.sh is for 6model/java on Unix.
if expr "$1" = "" > /dev/null; then
echo Usage: ./try.sh '<Perl 6 source file>'
exit 1
fi
make || exit 2
parrot compile.pir $1 > x.java || exit 3
javac -classpath ../runtime/RakudoRuntime.jar x.java || exit 4
parrot compile.pir $1 > RakudoOutput.java || exit 3
javac -classpath ../runtime/RakudoRuntime.jar RakudoOutput.java || exit 4
echo ---
java -classpath ../runtime/RakudoRuntime.jar x.class
java -classpath ../runtime/RakudoRuntime.jar RakudoOutput.class
6 changes: 2 additions & 4 deletions java/runtime/Makefile
Expand Up @@ -90,10 +90,8 @@ $(CLASSES):

RakudoRuntime.jar: $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS) $(INNER_CLASSES)
jar cf RakudoRuntime.jar -C $(CLASSES) .
#RakudoRuntime.jar: $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS) $(INNER_CLASSES)
# jar cf RakudoRuntime.jar $(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS) '$(INNER_CLASSES)'
# For troubleshooting, use `jar cvf RakudoRuntime.jar` instead.
# Use `jar tf RakudoRuntime.jar` to tell what was done, or `jar tvf` instead.
# Use `jar tvf RakudoRuntime.jar` to tell what was done.

$(CLASSES)Rakudo/Metamodel/Hints.class: Rakudo/Metamodel/Hints.java
$(JAVAC) Rakudo/Metamodel/Hints.java
Expand Down Expand Up @@ -371,7 +369,7 @@ todolist: $(TRANSLATED_SOURCE_FILES)
clean:
perl -MExtUtils::Command -e rm_f RakudoRuntime.jar \
$(ALL_BUILD_TARGETS) $(OTHER_DEPENDENT_TARGETS)
perl -MExtUtils::Command -e rm_rf classes
perl -MExtUtils::Command -e rm_rf $(CLASSES)

help:
@echo "In 6model/java/runtime you can make the following:"
Expand Down

0 comments on commit f3bd7b1

Please sign in to comment.