moved all the normalize stuff over to the ruby layer
Which is how it should have been from the start
This commit is contained in:
parent
38350dd198
commit
ae3d64eb53
@ -23,7 +23,7 @@ guard :minitest , all_on_start: false do # with Minitest::Unit
|
|||||||
watch(%r{^lib/parfait/type.rb}) { Dir["test/parfait/type/test_*.rb"] }
|
watch(%r{^lib/parfait/type.rb}) { Dir["test/parfait/type/test_*.rb"] }
|
||||||
|
|
||||||
# ruby compiler tests have a whole directory
|
# ruby compiler tests have a whole directory
|
||||||
watch(%r{^lib/rubyx/ruby_compiler.rb}) { Dir["test/rubyx/ruby_compiler/test_*.rb"] }
|
watch(%r{^lib/ruby/ruby_compiler.rb}) { Dir["test/ruby/test_*.rb"] }
|
||||||
|
|
||||||
watch(%r{^lib/vool/statements/send_statement.rb}) {
|
watch(%r{^lib/vool/statements/send_statement.rb}) {
|
||||||
[ Dir["test/vool/send/test_*.rb"] ] }
|
[ Dir["test/vool/send/test_*.rb"] ] }
|
||||||
|
10
lib/rubyx.rb
10
lib/rubyx.rb
@ -1,12 +1,3 @@
|
|||||||
require "parser/ruby22"
|
|
||||||
require "ast"
|
|
||||||
|
|
||||||
AST::Node.class_eval do
|
|
||||||
def first
|
|
||||||
children.first
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require "rx-file"
|
require "rx-file"
|
||||||
|
|
||||||
require "util/logging"
|
require "util/logging"
|
||||||
@ -15,5 +6,6 @@ require_relative "risc"
|
|||||||
require_relative "arm/arm_machine"
|
require_relative "arm/arm_machine"
|
||||||
require_relative "arm/arm_platform"
|
require_relative "arm/arm_platform"
|
||||||
require_relative "vool/statement"
|
require_relative "vool/statement"
|
||||||
|
require_relative "ruby"
|
||||||
require_relative "rubyx/rubyx_compiler"
|
require_relative "rubyx/rubyx_compiler"
|
||||||
require_relative "mom/mom"
|
require_relative "mom/mom"
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
require_relative "ruby_compiler"
|
|
||||||
|
|
||||||
module RubyX
|
module RubyX
|
||||||
class RubyXCompiler
|
class RubyXCompiler
|
||||||
attr_reader :source
|
attr_reader :source
|
||||||
@ -9,8 +7,8 @@ module RubyX
|
|||||||
end
|
end
|
||||||
|
|
||||||
def ruby_to_vool
|
def ruby_to_vool
|
||||||
vool = RubyCompiler.compile( source )
|
ruby = Ruby::RubyCompiler.compile( source )
|
||||||
vool = vool.normalize
|
vool = ruby.to_vool
|
||||||
vool
|
vool
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -6,36 +6,6 @@ module Vool
|
|||||||
@name , @value = name , value
|
@name , @value = name , value
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize()
|
|
||||||
raise "not named left #{name.class}" unless name.is_a?(Symbol)
|
|
||||||
case value
|
|
||||||
when Named , Constant
|
|
||||||
return copy
|
|
||||||
when SendStatement
|
|
||||||
return normalize_send
|
|
||||||
else
|
|
||||||
raise "unsupported right #{value}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy(value = nil)
|
|
||||||
value ||= @value
|
|
||||||
self.class.new(name,value)
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_send
|
|
||||||
statements = value.normalize()
|
|
||||||
return copy( statements ) if statements.is_a?(SendStatement)
|
|
||||||
assign = statements.statements.pop
|
|
||||||
statements << copy(assign)
|
|
||||||
statements
|
|
||||||
end
|
|
||||||
|
|
||||||
def chain_assign(assign , compiler)
|
|
||||||
return assign unless @value.is_a?(SendStatement)
|
|
||||||
@value.to_mom(compiler) << assign
|
|
||||||
end
|
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
block.call(self)
|
block.call(self)
|
||||||
@value.each(&block)
|
@value.each(&block)
|
||||||
@ -49,11 +19,6 @@ module Vool
|
|||||||
|
|
||||||
class IvarAssignment < Assignment
|
class IvarAssignment < Assignment
|
||||||
|
|
||||||
def normalize()
|
|
||||||
super()
|
|
||||||
return IvarAssignment.new(@name , @value)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_mom( compiler )
|
def to_mom( compiler )
|
||||||
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
|
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
|
||||||
from = @value.slot_definition(compiler)
|
from = @value.slot_definition(compiler)
|
||||||
|
@ -33,10 +33,6 @@ module Vool
|
|||||||
@body.each(&block)
|
@body.each(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
BlockStatement.new( @args , @body.normalize)
|
|
||||||
end
|
|
||||||
|
|
||||||
# create the parfait block (parfait representation of the block, a Callable similar
|
# create the parfait block (parfait representation of the block, a Callable similar
|
||||||
# to CallableMethod)
|
# to CallableMethod)
|
||||||
def parfait_block(compiler)
|
def parfait_block(compiler)
|
||||||
|
@ -17,11 +17,6 @@ module Vool
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
meths = body.statements.collect{|meth| meth.normalize}
|
|
||||||
ClassStatement.new(@name , @super_class_name, Statements.new(meths) )
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_mom( _ )
|
def to_mom( _ )
|
||||||
create_class_object
|
create_class_object
|
||||||
method_compilers = body.statements.collect do |node|
|
method_compilers = body.statements.collect do |node|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require_relative "normalizer"
|
|
||||||
module Vool
|
module Vool
|
||||||
class IfStatement < Statement
|
class IfStatement < Statement
|
||||||
include Normalizer
|
|
||||||
|
|
||||||
attr_reader :condition , :if_true , :if_false
|
attr_reader :condition , :if_true , :if_false
|
||||||
|
|
||||||
@ -11,15 +10,6 @@ module Vool
|
|||||||
@if_false = if_false
|
@if_false = if_false
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
cond , rest = *normalize_name(@condition)
|
|
||||||
fals = @if_false ? @if_false.normalize : nil
|
|
||||||
me = IfStatement.new(cond , @if_true.normalize, fals)
|
|
||||||
return me unless rest
|
|
||||||
rest << me
|
|
||||||
rest
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_mom( compiler )
|
def to_mom( compiler )
|
||||||
if_false ? full_if(compiler) : simple_if(compiler)
|
if_false ? full_if(compiler) : simple_if(compiler)
|
||||||
end
|
end
|
||||||
|
@ -23,10 +23,6 @@ module Vool
|
|||||||
@body.each(&block)
|
@body.each(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
MethodStatement.new( @name , @args , @body.normalize)
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_yield?
|
def has_yield?
|
||||||
each{|statement| return true if statement.is_a?(YieldStatement)}
|
each{|statement| return true if statement.is_a?(YieldStatement)}
|
||||||
return false
|
return false
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
module Vool
|
|
||||||
module Normalizer
|
|
||||||
# given a something, determine if it is a Name
|
|
||||||
#
|
|
||||||
# Return a Name, and a possible rest that has a hoisted part of the statement
|
|
||||||
#
|
|
||||||
# eg if( @var % 5) is not normalized
|
|
||||||
# but if(tmp_123) is with tmp_123 = @var % 5 hoisted above the if
|
|
||||||
#
|
|
||||||
# also constants count, though they may not be so useful in ifs, but returns
|
|
||||||
def normalize_name( condition )
|
|
||||||
if( condition.is_a?(ScopeStatement) and condition.single?)
|
|
||||||
condition = condition.first
|
|
||||||
end
|
|
||||||
return [condition] if condition.is_a?(Named) or condition.is_a?(Constant)
|
|
||||||
condition = condition.normalize
|
|
||||||
local = "tmp_#{object_id}".to_sym
|
|
||||||
assign = Statements.new [LocalAssignment.new( local , condition)]
|
|
||||||
[LocalVariable.new(local) , assign]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,6 +1,5 @@
|
|||||||
module Vool
|
module Vool
|
||||||
class ReturnStatement < Statement
|
class ReturnStatement < Statement
|
||||||
include Normalizer
|
|
||||||
|
|
||||||
attr_reader :return_value
|
attr_reader :return_value
|
||||||
|
|
||||||
@ -8,14 +7,6 @@ module Vool
|
|||||||
@return_value = value
|
@return_value = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
val , rest = *normalize_name(@return_value)
|
|
||||||
me = ReturnStatement.new(val)
|
|
||||||
return me unless rest
|
|
||||||
rest << me
|
|
||||||
rest
|
|
||||||
end
|
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
block.call(@return_value)
|
block.call(@return_value)
|
||||||
end
|
end
|
||||||
|
@ -27,30 +27,6 @@ module Vool
|
|||||||
@arguments << block
|
@arguments << block
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
statements = Statements.new([])
|
|
||||||
arguments = []
|
|
||||||
@arguments.each_with_index do |arg , index |
|
|
||||||
normalize_arg(arg , arguments , statements)
|
|
||||||
end
|
|
||||||
if statements.empty?
|
|
||||||
return SendStatement.new(@name, @receiver , @arguments)
|
|
||||||
else
|
|
||||||
statements << SendStatement.new(@name, @receiver , arguments)
|
|
||||||
return statements
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_arg(arg , arguments , statements)
|
|
||||||
if arg.respond_to?(:slot_definition) and !arg.is_a?(SendStatement)
|
|
||||||
arguments << arg
|
|
||||||
return
|
|
||||||
end
|
|
||||||
assign = LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg)
|
|
||||||
statements << assign
|
|
||||||
arguments << LocalVariable.new(assign.name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
||||||
end
|
end
|
||||||
|
@ -25,12 +25,6 @@ module Vool
|
|||||||
#
|
#
|
||||||
class Statement
|
class Statement
|
||||||
|
|
||||||
# after creation vool normalizes to ensure valid syntax and simplify
|
|
||||||
# also throw errors if violation
|
|
||||||
def normalize()
|
|
||||||
raise self.class.name
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_mom( _ )
|
def to_mom( _ )
|
||||||
# temporary warning to find unimplemented kids
|
# temporary warning to find unimplemented kids
|
||||||
raise "Not implemented for #{self}"
|
raise "Not implemented for #{self}"
|
||||||
|
@ -44,13 +44,6 @@ module Vool
|
|||||||
@statements.each{|a| a.each(&block)}
|
@statements.each{|a| a.each(&block)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
if( single? )
|
|
||||||
first.normalize
|
|
||||||
else
|
|
||||||
Statements.new(@statements.collect{|s| s.normalize})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def to_s(depth = 0)
|
def to_s(depth = 0)
|
||||||
at_depth(depth , *@statements.collect{|st| st.to_s(depth)})
|
at_depth(depth , *@statements.collect{|st| st.to_s(depth)})
|
||||||
end
|
end
|
||||||
@ -58,12 +51,5 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ScopeStatement < Statements
|
class ScopeStatement < Statements
|
||||||
def normalize
|
|
||||||
if( single? )
|
|
||||||
first.normalize
|
|
||||||
else
|
|
||||||
ScopeStatement.new(@statements.collect{|s| s.normalize})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
require_relative "normalizer"
|
|
||||||
|
|
||||||
module Vool
|
module Vool
|
||||||
class WhileStatement < Statement
|
class WhileStatement < Statement
|
||||||
include Normalizer
|
|
||||||
attr_reader :condition , :body , :hoisted
|
attr_reader :condition , :body , :hoisted
|
||||||
|
|
||||||
def initialize( condition , body , hoisted = nil)
|
def initialize( condition , body , hoisted = nil)
|
||||||
@ -11,11 +9,6 @@ module Vool
|
|||||||
@body = body
|
@body = body
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
cond , rest = *normalize_name(@condition)
|
|
||||||
WhileStatement.new(cond , @body.normalize , rest)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_mom( compiler )
|
def to_mom( compiler )
|
||||||
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
|
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
|
||||||
cond_label = Mom::Label.new( "cond_label_#{object_id.to_s(16)}")
|
cond_label = Mom::Label.new( "cond_label_#{object_id.to_s(16)}")
|
||||||
|
@ -8,30 +8,6 @@ module Vool
|
|||||||
@arguments ||= []
|
@arguments ||= []
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
|
||||||
statements = Statements.new([])
|
|
||||||
arguments = []
|
|
||||||
@arguments.each_with_index do |arg , index |
|
|
||||||
normalize_arg(arg , arguments , statements)
|
|
||||||
end
|
|
||||||
if statements.empty?
|
|
||||||
return YieldStatement.new(@name, @receiver , @arguments)
|
|
||||||
else
|
|
||||||
statements << YieldStatement.new(@name, @receiver , arguments)
|
|
||||||
return statements
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_arg(arg , arguments , statements)
|
|
||||||
if arg.respond_to?(:slot_definition) and !arg.is_a?(YieldStatement)
|
|
||||||
arguments << arg
|
|
||||||
return
|
|
||||||
end
|
|
||||||
assign = LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg)
|
|
||||||
statements << assign
|
|
||||||
arguments << LocalVariable.new(assign.name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user