Skip to content

Commit

Permalink
Second tag attempt!
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/tags/jruby-1_1_4@7564 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
enebo committed Aug 28, 2008
2 parents 9d9bd54 + 5f8365a commit 5744d3a
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 126 deletions.
111 changes: 14 additions & 97 deletions lib/ruby/site_ruby/1.8/duby/c_compiler.rb
@@ -1,15 +1,16 @@
require 'duby'
require 'duby/compiler'

module Duby
module Compiler
class C
class MathCompiler
def call(compiler, name, recv, args)
recv.call
recv.compile(compiler)

compiler.src << " #{name} "

args.call
args.each {|arg| arg.compile(compiler)}
end
end

Expand All @@ -25,17 +26,17 @@ def initialize(filename)
end

def compile(ast)
ast.compile(this)
ast.compile(self)
end

def define_method(name, signature, args, body)
@src << "#{type_mapper[signature[:return]]} #{name}("

args.call
args.compile(self)

@src << ") {"

body.call
body.compile(self)

@src << "}\n\n"
end
Expand All @@ -44,26 +45,26 @@ def declare_argument(name, type)
@src << "#{type_mapper[type]} #{name}"
end

def branch(condition, body_proc, else_proc)
def branch(condition, body, els)
@src << "if ("

condition.call
condition.compile(self)

@src << ") {"

body_proc.call
body.compile(self)

if else_proc
if els
@src << "} else {"

else_proc.call
els.compile(self)
end

@src << "}"
end

def call(name, recv_type, recv, args)
call_compilers[recv_type].call(self, name, recv, args)
def call(name, recv, args)
call_compilers[recv.inferred_type].call(self, name, recv, args)
end

def call_compilers
Expand All @@ -73,7 +74,7 @@ def call_compilers
def self_call(name, args)
@src << "#{name}("

args.call
args.each {|arg| arg.compile(self)}

@src << ")"
end
Expand Down Expand Up @@ -105,90 +106,6 @@ def type_mapper
end
end
end

module AST
class Script
def compile(compiler)
# preparations for the .c file would go here
body.compile(compiler)
end
end

class Body
def compile(compiler)
last = children[-1]
children.each do |child|
child.compile(compiler)
compiler.newline
end
end
end

class MethodDefinition
def compile(compiler)
args_callback = proc {arguments.compile(compiler)}
body_callback = proc {body.compile(compiler)}
compiler.define_method(name, signature, args_callback, body_callback)
end
end

class Arguments
def compile(compiler)
args.each {|arg| compiler.declare_argument(arg.name, arg.inferred_type)} if args
end
end

class Noop
def compile(compiler)
# nothing
end
end

class Fixnum
def compile(compiler)
compiler.fixnum(literal)
end
end

class If
def compile(compiler)
cond_callback = proc { condition.compile(compiler) }
body_callback = proc { body.compile(compiler) }
else_callback = proc { self.else.compile(compiler)}

compiler.branch(cond_callback, body_callback, else_callback)
end
end

class Condition
def compile(compiler)
predicate.compile(compiler)
end
end

class FunctionalCall
def compile(compiler)
args_callback = proc { parameters.each {|param| param.compile(compiler)}}

compiler.self_call(name, args_callback)
end
end

class Call
def compile(compiler)
recv_callback = proc { target.compile(compiler) }
args_callback = proc { parameters.each {|param| param.compile(compiler)}}

compiler.call(name, target.inferred_type, recv_callback, args_callback)
end
end

class Local
def compile(compiler)
compiler.local(name)
end
end
end
end

if __FILE__ == $0
Expand Down
23 changes: 6 additions & 17 deletions lib/ruby/site_ruby/1.8/duby/compiler.rb
Expand Up @@ -34,13 +34,13 @@ def compile(compiler)

class Local
def compile(compiler)
compiler.local(inferred_type, name)
compiler.local(name)
end
end

class LocalAssignment
def compile(compiler)
compiler.local_assign(inferred_type, name) {
compiler.local_assign(name) {
value.compile(compiler)
}
end
Expand All @@ -54,9 +54,7 @@ def compile(compiler)

class MethodDefinition
def compile(compiler)
args_callback = proc {arguments.compile(compiler)}
body_callback = proc {body.compile(compiler)}
compiler.define_method(name, signature, args_callback, body_callback)
compiler.define_method(name, signature, arguments, body)
end
end

Expand All @@ -74,11 +72,7 @@ def compile(compiler)

class If
def compile(compiler)
cond_callback = proc { condition.compile(compiler) }
body_callback = proc { body.compile(compiler) }
else_callback = proc { self.else.compile(compiler)}

compiler.branch(cond_callback, body_callback, else_callback)
compiler.branch(condition, body, self.else)
end
end

Expand All @@ -90,18 +84,13 @@ def compile(compiler)

class FunctionalCall
def compile(compiler)
args_callback = proc { parameters.each {|param| param.compile(compiler)}}

compiler.self_call(name, args_callback)
compiler.self_call(name, parameters)
end
end

class Call
def compile(compiler)
recv_callback = proc { target.compile(compiler) }
args_callback = proc { parameters.each {|param| param.compile(compiler)}}

compiler.call(name, target.inferred_type, recv_callback, args_callback)
compiler.call(name, target, parameters)
end
end
end
Expand Down

0 comments on commit 5744d3a

Please sign in to comment.