A good start on the macro idea
I call it macro because it lets you insert basically arbitrary risc code into the ruby level. The way it works: Reserve namespace X map any X.some_call to a Mom instruction by the name SomeCall which must take the same args in constructor as given And obviously produce whatever risc it wants Hoping to rewrite builtin around this idea (with the existing Mom builtn instructions)
This commit is contained in:
@ -33,6 +33,7 @@ module Ruby
|
||||
ast = Parser::CurrentRuby.parse( input )
|
||||
rescue => e
|
||||
puts "Error parsing #{input}"
|
||||
raise e
|
||||
end
|
||||
begin
|
||||
self.new.process(ast)
|
||||
|
@ -4,6 +4,12 @@ module Ruby
|
||||
# The SendStatement really only provides to_s, so see CallStatement
|
||||
#
|
||||
class SendStatement < CallStatement
|
||||
|
||||
def to_vool
|
||||
return super unless @receiver.is_a?(ModuleName) and @receiver.name == :X
|
||||
args = @arguments.collect { |arg| arg.to_vool }
|
||||
Vool::MacroExpression.new(name , args)
|
||||
end
|
||||
def to_s(depth = 0)
|
||||
at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})")
|
||||
end
|
||||
|
@ -15,12 +15,11 @@ module Vool
|
||||
false_label = Mom::Label.new( self , "false_label_#{object_id.to_s(16)}")
|
||||
merge_label = Mom::Label.new( self , "merge_label_#{object_id.to_s(16)}")
|
||||
|
||||
check = Mom::TruthCheck.new(condition.to_slot(compiler) , false_label)
|
||||
if @condition.is_a?(CallStatement)
|
||||
head = @condition.to_mom(compiler)
|
||||
head << check
|
||||
head << check_slot(compiler , false_label)
|
||||
else
|
||||
head = check
|
||||
head = check_slot(compiler , false_label)
|
||||
end
|
||||
head << true_label
|
||||
head << if_true.to_mom(compiler) if @if_true
|
||||
@ -31,6 +30,11 @@ module Vool
|
||||
head
|
||||
end
|
||||
|
||||
# create the slot lazily, so to_mom gets called first
|
||||
def check_slot(compiler , false_label)
|
||||
Mom::TruthCheck.new(@condition.to_slot(compiler) , false_label)
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
block.call(condition)
|
||||
@if_true.each(&block) if @if_true
|
||||
|
27
lib/vool/macro_expression.rb
Normal file
27
lib/vool/macro_expression.rb
Normal file
@ -0,0 +1,27 @@
|
||||
module Vool
|
||||
|
||||
class MacroExpression < CallStatement
|
||||
|
||||
def initialize(name , arguments )
|
||||
super(name , SelfExpression.new , arguments)
|
||||
end
|
||||
|
||||
def to_mom(compiler)
|
||||
parts = name.to_s.split("_")
|
||||
class_name = "Mom::#{parts.collect{|s| s.capitalize}.join}"
|
||||
eval(class_name).new( self , *arguments)
|
||||
end
|
||||
|
||||
# When used as right hand side, this tells what data to move to get the result into
|
||||
# a varaible. It is (off course) the return value of the message
|
||||
def to_slot(_)
|
||||
Mom::SlotDefinition.new(:message ,[ :return_value])
|
||||
end
|
||||
|
||||
def to_s(depth = 0)
|
||||
sen = "X.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})"
|
||||
at_depth(depth , sen)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -17,13 +17,11 @@ module Vool
|
||||
# - store the given return value, this is a SlotMove
|
||||
# - activate return sequence (reinstantiate old message and jump to return address)
|
||||
def to_mom( compiler )
|
||||
load = Mom::SlotLoad.new( self , [:message , :return_value] ,
|
||||
@return_value.to_slot(compiler) )
|
||||
if @return_value.is_a?(CallStatement)
|
||||
ret = @return_value.to_mom(compiler)
|
||||
ret << load
|
||||
ret << slot_load(compiler)
|
||||
else
|
||||
ret = load
|
||||
ret = slot_load(compiler)
|
||||
end
|
||||
ret << Mom::ReturnJump.new(self , compiler.return_label )
|
||||
end
|
||||
@ -31,5 +29,10 @@ module Vool
|
||||
def to_s(depth = 0)
|
||||
at_depth(depth , "return #{@return_value.to_s}")
|
||||
end
|
||||
|
||||
def slot_load(compiler)
|
||||
Mom::SlotLoad.new( self , [:message , :return_value] ,
|
||||
@return_value.to_slot(compiler) )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -70,6 +70,7 @@ require_relative "if_statement"
|
||||
require_relative "ivar_assignment"
|
||||
require_relative "lambda_expression"
|
||||
require_relative "local_assignment"
|
||||
require_relative "macro_expression"
|
||||
require_relative "method_expression"
|
||||
require_relative "class_method_expression"
|
||||
require_relative "return_statement"
|
||||
|
Reference in New Issue
Block a user