Skip to content

Commit

Permalink
Massive advancement of Duby JVM support:
Browse files Browse the repository at this point in the history
* A simple JVM type inference plugin now works, using JI JavaClass to infer types
* Additional syntax to support Java: constant = class/static reference, import assigns a short name to a long type name, puts is an intrinsic keyword for System.out.println
* Large number of additions and improvements to JVM compiler to compile all new syntax as well as calls to Java classes
...starting to approach a point where it might actually be usable.


git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@7610 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
headius committed Aug 31, 2008
1 parent 9ae84ed commit 260d2eb
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 170 deletions.
31 changes: 23 additions & 8 deletions lib/ruby/site_ruby/1.8/duby/ast.rb
Expand Up @@ -2,6 +2,10 @@

module Duby
module AST
class << self
attr_accessor :verbose
end

# The top of the AST class hierarchy, this represents an abstract AST node.
# It provides accessors for _children_, an array of all child nodes,
# _parent_, a reference to this node's parent (nil if none), and _newline_,
Expand All @@ -21,7 +25,7 @@ def initialize(parent, children = [])
end

def log(message)
puts "* [AST] [#{simple_name}] " + message if $DEBUG
puts "* [AST] [#{simple_name}] " + message if AST.verbose
end

def inspect(indent = 0)
Expand Down Expand Up @@ -107,6 +111,12 @@ def initialize(parent, name)
@name = name
super(parent, [])
end

def infer(typer)
@inferred_type ||= begin
typer.known_types[name] || AST::type(name, false, true)
end
end
end

class Self < Node; end
Expand All @@ -116,16 +126,19 @@ class VoidType < Node; end
class TypeReference < Node
include Named
attr_accessor :array
def initialize(name, array = false)
alias array? array
attr_accessor :meta
alias meta? meta

def initialize(name, array = false, meta = false)
@name = name
@array = array
@meta = meta
super(nil)
end

def array?; @array; end

def to_s
"Type(#{name}#{array? ? ' array' : ''})"
"Type(#{name}#{array? ? ' array' : ''}#{meta? ? ' meta' : ''})"
end

def ==(other)
Expand Down Expand Up @@ -169,8 +182,8 @@ def initialize(name, superclass)
end

# Shortcut method to construct type references
def self.type(typesym, array = false)
TypeReference.new(typesym, array)
def self.type(typesym, array = false, meta = false)
TypeReference.new(typesym, array, meta)
end
end
end
Expand All @@ -181,4 +194,6 @@ def self.type(typesym, array = false)
require 'duby/ast/literal'
require 'duby/ast/method'
require 'duby/ast/class'
require 'duby/ast/structure'
require 'duby/ast/structure'
require 'duby/ast/type'
require 'duby/ast/intrinsics'
16 changes: 16 additions & 0 deletions lib/ruby/site_ruby/1.8/duby/ast/intrinsics.rb
@@ -0,0 +1,16 @@
module Duby::AST
class PrintLine < Node
attr_accessor :parameters

def initialize(parent)
@parameters = children = yield(self)
super(parent, children)
end

def infer(typer)
resolved = parameters.select {|param| param.infer(typer); param.resolved?}
resolved! if resolved.size == parameters.size
TypeReference::NoType
end
end
end
21 changes: 21 additions & 0 deletions lib/ruby/site_ruby/1.8/duby/ast/type.rb
@@ -0,0 +1,21 @@
module Duby::AST
class Import < Node
attr_accessor :short
attr_accessor :long
def initialize(parent, short, long)
@short = short
@long = long
super(parent, [])
end

def to_s
"Import(#{short} = #{long})"
end

def infer(typer)
typer.known_types[short] = TypeReference.new(long, false, true)

TypeReference::NoType
end
end
end
18 changes: 18 additions & 0 deletions lib/ruby/site_ruby/1.8/duby/compiler.rb
Expand Up @@ -31,6 +31,24 @@ def compile(compiler)
children.each {|child| child.compile(compiler)}
end
end

class Import
def compile(compiler)
compiler.import(short, long)
end
end

class Constant
def compile(compiler)
compiler.constant(self)
end
end

class PrintLine
def compile(compiler)
compiler.println(self)
end
end

class Local
def compile(compiler)
Expand Down

0 comments on commit 260d2eb

Please sign in to comment.