Skip to content

Commit

Permalink
[java] eliminate many (mis)uses of the C# var data type
Browse files Browse the repository at this point in the history
  • Loading branch information
mberends committed Sep 28, 2010
1 parent a40c29f commit cffcdc8
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 206 deletions.
22 changes: 15 additions & 7 deletions java/compiler/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ sub package($/) {
my $name := ~$<package_def><name>;

# Prefix the class initialization with initial setup. Also install it
# in the symbol table right away.
# in the symbol table right away, and also into $?CLASS.
$*PACKAGE-SETUP.unshift(PAST::Stmts.new(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name('type_obj'), :scope('register'), :isdecl(1) ),
Expand All @@ -363,7 +363,11 @@ sub package($/) {
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name($name) ),
PAST::Var.new( :name('type_obj'), :scope('register') )
)
),
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name('$?CLASS') ),
PAST::Var.new( :name('type_obj'), :scope('register') )
),
# XXX name
# XXX is parent
));
Expand All @@ -382,16 +386,20 @@ sub package($/) {
),
PAST::Var.new( :name('type_obj'), :scope('register') )
));

# Run this at loadinit time.
@BLOCK[0].loadinit.push(PAST::Block.new( :blocktype('immediate'), $*PACKAGE-SETUP ));

# Set up lexical for this to live in.
# Set up lexical for the type object to live in.
@BLOCK[0][0].unshift(PAST::Var.new( :name($name), :scope('lexical'), :isdecl(1) ));
@BLOCK[0].symbol($name, :scope('lexical'));

# Just evaluate anything else in the package in-line.
# Evaluate anything else in the package in-line; also give it a $?CLASS
# lexical.
my $past := $<package_def>.ast;
$past.unshift(PAST::Var.new( :name('$?CLASS'), :scope('lexical'), :isdecl(1) ));
$past.symbol('$?CLASS', :scope('lexical'));

# Attach the class code to run at loadinit time.
$past.loadinit.push(PAST::Block.new( :blocktype('immediate'), $*PACKAGE-SETUP ));

return $past;
}

Expand Down
12 changes: 10 additions & 2 deletions java/compiler/JST.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The Java Syntax Tree set of nodes is designed to represent fundemental
# JVM concepts. This allows most of a PAST compiler for JVM to be
# written and used to generate Java for now, but later we can generate IL.
# written and used to generate Java for now, but later we can generate
# bytecode IL.
# A tree must have the form:
#
# JST::CompilationUnit
Expand Down Expand Up @@ -174,6 +175,7 @@ class JST::MethodCall is JST::Node {
has $!on;
has $!name;
has $!void;
has $!type;

method on($set?) {
if $set { $!on := $set }
Expand All @@ -190,11 +192,17 @@ class JST::MethodCall is JST::Node {
$!void
}

method new(:$name!, :$on, :$void, *@children) {
method type($set?) {
if $set { $!type := $set }
$!type
}

method new(:$name!, :$on, :$void, :$type, *@children) {
my $obj := self.CREATE;
if $on { $obj.on($on); }
$obj.name($name);
$obj.void($void);
$obj.type($type);
$obj.set_children(@children);
$obj;
}
Expand Down
44 changes: 38 additions & 6 deletions java/compiler/JST2Java.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ our multi sub java_for(JST::Using $using) {
our multi sub java_for(JST::Class $class) {
my $code := '';
if $class.namespace {
$code := $code ~ 'namespace ' ~ $class.namespce ~ " \{\n";
$code := $code ~ 'package ' ~ $class.namespce ~ ";\n";
}
$code := $code ~ 'class ' ~ $class.name ~ " \{ // JST::Class\n";
for @($class) {
$code := $code ~ java_for($_);
}
$code := $code ~ "}\n";
if $class.namespace {
$code := $code ~ "}\n";
}
return $code;
}




our multi sub java_for(JST::Attribute $attr) {
return ' private static ' ~ $attr.type ~ ' ' ~ $attr.name ~ "; // JST:Attribute\n";
}
Expand Down Expand Up @@ -111,7 +111,39 @@ our multi sub java_for(JST::MethodCall $mc) {
$code := $code ~ ' ';
unless $mc.void {
$*LAST_TEMP := get_unique_id('result');
$code := $code ~ "RakudoObject $*LAST_TEMP = ";
my $ret_type := $mc.type || 'var';
my $method_name := "$invocant." ~ $mc.name;
if $ret_type eq 'var' && $method_name eq 'Ops.unbox_str'
{
$ret_type := 'String';
}
if $ret_type eq 'var' && (
$method_name eq 'CaptureHelper.FormWith' ||
$method_name eq 'Ops.add_int' ||
$method_name eq 'Ops.box_str' ||
$method_name eq 'Ops.coerce_int_to_num' ||
$method_name eq 'Ops.coerce_int_to_str' ||
$method_name eq 'Ops.coerce_num_to_int' ||
$method_name eq 'Ops.coerce_num_to_str' ||
$method_name eq 'Ops.concat' ||
$method_name eq 'Ops.div_int' ||
$method_name eq 'Ops.equal_ints' ||
$method_name eq 'Ops.equal_nums' ||
$method_name eq 'Ops.equal_strs' ||
$method_name eq 'Ops.get_how' ||
$method_name eq 'Ops.get_what' ||
$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'
)
{
$ret_type := 'RakudoObject';
}
$code := $code ~ "$ret_type $*LAST_TEMP = ";
}
$code := $code ~ "$invocant." ~ $mc.name ~
"(" ~ pir::join(', ', @arg_names) ~ "); // JST::MethodCall\n";
Expand All @@ -132,7 +164,7 @@ our multi sub java_for(JST::Call $mc) {
$code := $code ~ ' ';
unless $mc.void {
$*LAST_TEMP := get_unique_id('result');
$code := $code ~ "var $*LAST_TEMP = ";
$code := $code ~ "RakudoObject $*LAST_TEMP = ";
}
$code := $code ~ $mc.name ~
"(" ~ pir::join(', ', @arg_names) ~ "); // JST::Call\n";
Expand Down
9 changes: 9 additions & 0 deletions java/compiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ clean:
Actions.pm: ../../dotnet/compiler/Actions.pm
@echo "todo: $@ is older than $<"

JST.pm: ../../dotnet/compiler/DNST.pm
@echo "todo: $@ is older than $<"

JST2Java.pm: ../../dotnet/compiler/DNST2CSharp.pm
@echo "todo: $@ is older than $<"

PAST2JSTCompiler.pm: ../../dotnet/compiler/PAST2DNSTCompiler.pm
@echo "todo: $@ is older than $<"


Loading

0 comments on commit cffcdc8

Please sign in to comment.