create compilers directory, found and renamed salama.rb
This commit is contained in:
25
lib/melon/compilers/locals_collector.rb
Normal file
25
lib/melon/compilers/locals_collector.rb
Normal file
@ -0,0 +1,25 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
class LocalsCollector < TotalProcessor
|
||||
|
||||
def initialize
|
||||
@locals = {}
|
||||
end
|
||||
|
||||
def collect(statement)
|
||||
process statement
|
||||
@locals
|
||||
end
|
||||
|
||||
def on_lvasgn statement
|
||||
add_local( statement )
|
||||
end
|
||||
|
||||
def add_local(statement)
|
||||
var = statement.children[0]
|
||||
@locals[var] = :Object #not really used right now
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
38
lib/melon/compilers/method_collector.rb
Normal file
38
lib/melon/compilers/method_collector.rb
Normal file
@ -0,0 +1,38 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
|
||||
class MethodCollector < TotalProcessor
|
||||
|
||||
def initialize
|
||||
@methods = []
|
||||
end
|
||||
|
||||
def collect(statement)
|
||||
process statement
|
||||
@methods
|
||||
end
|
||||
|
||||
def on_def(statement)
|
||||
name , args , body = *statement
|
||||
args_type = make_type(args)
|
||||
locals_type = make_locals(body)
|
||||
@methods << RubyMethod.new(name , args_type , locals_type , body )
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def make_type( statement )
|
||||
type_hash = {}
|
||||
statement.children.each do |arg|
|
||||
type_hash[arg.children[0]] = :Object
|
||||
end
|
||||
Parfait::NamedList.type_for( type_hash )
|
||||
end
|
||||
|
||||
def make_locals(body)
|
||||
type_hash = LocalsCollector.new.collect(body)
|
||||
Parfait::NamedList.type_for( type_hash )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
lib/melon/compilers/total_processor.rb
Normal file
14
lib/melon/compilers/total_processor.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module Melon
|
||||
|
||||
module Compilers
|
||||
class TotalProcessor < AST::Processor
|
||||
|
||||
def handler_missing(node)
|
||||
node.children.each do |kid |
|
||||
process(kid) if kid.is_a?(AST::Node)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
29
lib/melon/compilers/type_collector.rb
Normal file
29
lib/melon/compilers/type_collector.rb
Normal file
@ -0,0 +1,29 @@
|
||||
module Melon
|
||||
module Compilers
|
||||
class TypeCollector < TotalProcessor
|
||||
|
||||
def initialize
|
||||
@ivars = {}
|
||||
end
|
||||
|
||||
def collect(statement)
|
||||
process statement
|
||||
@ivars
|
||||
end
|
||||
|
||||
def on_ivar(statement)
|
||||
add_ivar(statement)
|
||||
end
|
||||
|
||||
def on_ivasgn( statement )
|
||||
add_ivar(statement)
|
||||
end
|
||||
|
||||
def add_ivar(statement)
|
||||
var = statement.children[0].to_s[1..-1].to_sym
|
||||
@ivars[var] = :Object #guess, can maybe guess better
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user