Some docs and to_s testing

somewhat code_climate inspired
This commit is contained in:
Torsten Ruger 2018-09-01 15:54:25 +03:00
parent 2bb6ad5f61
commit d73e1526cd
12 changed files with 111 additions and 18 deletions

View File

@ -1,5 +1,14 @@
module Ruby
# A CallStatement is the abstraction of Send and Yield. The two are really
# much more similar than different.
#
# A CallStatement has a name, receiver and arguments
#
# Using the "vool_brother" we can create the right Vool class for it.
# Arguments in vool must be simple, so any complex expressions get
# hoisted and assigned to temporary variables.
#
class CallStatement < Statement
attr_reader :name , :receiver , :arguments

View File

@ -1,6 +1,15 @@
require_relative "normalizer"
module Ruby
# The if must have condition and a true branch, the false is optional
#
# It maps pretty much one to one to a Vool, except for "hoisting"
#
# Ruby may have super complex expressions as the condition, whereas
# Vool may not. Ie of a Statement list all but the last are hoisted to before
# the vool if. This is equivalent, just easier to compile later
#
# The hoisintg code is in Normalizer, as it is also useed in return and while
class IfStatement < Statement
include Normalizer
@ -28,8 +37,8 @@ module Ruby
end
def to_s(depth = 0)
parts = ["if (#{@condition})" , @body.to_s(depth + 1) ]
parts += ["else" , "@if_false.to_s(depth + 1)"] if(@false)
parts = ["if(#{@condition})" , @if_true.to_s(depth + 1) ]
parts += ["else" , @if_false.to_s(depth + 1)] if(@if_false)
parts << "end"
at_depth(depth , *parts )
end

View File

@ -1,5 +1,8 @@
module Ruby
# Send and yield are very very similar, so they have a base class CallStatement
#
# The SendStatement really only provides to_s, so see CallStatement
#
class SendStatement < CallStatement
def to_s
"#{receiver}.#{name}(#{arguments.join(', ')})"

View File

@ -7,17 +7,26 @@ module Ruby
#
class Statement
# Many statements exist in the vool layer in quite a similar arrangement
# Especially for different types of assignment we can abstract the creation
# of the vool, by using the right class to instantiate, the "vool_brother"
# Ie same class_name, but in the Vool module
def vool_brother
eval "Vool::#{class_name}"
end
# return the class name without the module
# used to evaluate the vool_brother
def class_name
self.class.name.split("::").last
end
# helper method for formatting source code
# depth is the depth in the tree (os the ast)
# and the string are the ones to be indented (2 spaces)
def at_depth(depth , *strings)
prefix = " " * 2 * depth
strings.collect{|str| prefix + str}.join("\n")
end
def vool_brother
eval "Vool::#{class_name}"
end
def class_name
self.class.name.split("::").last
end
end
end

View File

@ -1,7 +1,14 @@
module Ruby
# Send and yield are very very similar, so they have a base class CallStatement
#
# The YieldStatement really only provides to_s, and has slightly
# different constructor. See CallStatement
#
class YieldStatement < CallStatement
# We give the instance of the yield and auto generated name
# Also, a yield is always (for now) on self
def initialize(arguments)
super("yield_#{object_id}".to_sym , SelfExpression.new , arguments)
end

View File

@ -1,16 +1,18 @@
module Vool
#Marker class for different constants
class Constant < Expression
#gobble it up
def each(&block)
end
end
# An integer at the vool level
class IntegerConstant < Constant
attr_reader :value
def initialize(value)
@value = value
end
def slot_definition(compiler)
def slot_definition(_)
return Mom::SlotDefinition.new(Mom::IntegerConstant.new(@value) , [])
end
def ct_type
@ -22,6 +24,7 @@ module Vool
def each(&block)
end
end
# An float at the vool level
class FloatConstant < Constant
attr_reader :value
def initialize(value)
@ -30,40 +33,48 @@ module Vool
def ct_type
true
end
def to_s
value.to_s
end
end
# True at the vool level
class TrueConstant < Constant
def ct_type
Parfait.object_space.get_type_by_class_name(:True)
end
def slot_definition(compiler)
def slot_definition(_)
return Mom::SlotDefinition.new(Parfait.object_space.true_object , [])
end
def to_s(depth = 0)
"true"
end
end
# False at the vool level
class FalseConstant < Constant
def ct_type
Parfait.object_space.get_type_by_class_name(:False)
end
def slot_definition(compiler)
def slot_definition(_)
return Mom::SlotDefinition.new(Parfait.object_space.false_object , [])
end
def to_s(depth = 0)
"false"
end
end
# Nil at the vool level
class NilConstant < Constant
def ct_type
Parfait.object_space.get_type_by_class_name(:Nil)
end
def slot_definition(compiler)
def slot_definition(_)
return Mom::SlotDefinition.new(Parfait.object_space.nil_object , [])
end
def to_s(depth = 0)
"nil"
end
end
# Self at the vool level
class SelfExpression < Expression
attr_reader :my_type
def initialize(type = nil)
@ -90,7 +101,7 @@ module Vool
def initialize(value)
@value = value
end
def slot_definition(compiler)
def slot_definition(_)
return Mom::SlotDefinition.new(Mom::StringConstant.new(@value),[])
end
def ct_type

View File

@ -10,7 +10,7 @@ module Vool
# 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 slot_definition(compiler)
def slot_definition(_)
Mom::SlotDefinition.new(:message ,[ :return_value])
end

View File

@ -21,7 +21,7 @@ module Vool
class InstanceVariable < Expression
include Named
def slot_definition(compiler)
def slot_definition(_)
Mom::SlotDefinition.new(:message , [ :receiver , @name] )
end
# used to collect type information

View File

@ -60,6 +60,9 @@ module Ruby
def test_integer
assert_equal IntegerConstant , compile_const( "123")
end
def test_float
assert_equal FloatConstant , compile_const( "123.1")
end
def test_string
assert_equal StringConstant , compile_const( "'string'")
end
@ -76,4 +79,36 @@ module Ruby
assert_equal TrueConstant , compile_const( "true")
end
end
class TestBasicTypesVool < MiniTest::Test
include RubyTests
def setup
Parfait.boot!
end
def compile_const( input )
lst = compile( input )
lst.to_vool.to_s
end
def test_integer
assert_equal "123" , compile_const( "123")
end
def test_float
assert_equal "123.0" , compile_const( "123.0")
end
def test_string
assert_equal "'string'" , compile_const( "'string'")
end
def test_sym
assert_equal "'symbol'" , compile_const( ":symbol")
end
def test_nil
assert_equal "nil" , compile_const( "nil")
end
def test_false
assert_equal "false" , compile_const( "false")
end
def test_true
assert_equal "true" , compile_const( "true")
end
end
end

View File

@ -37,5 +37,9 @@ module Ruby
assert_equal TrueConstant , lst.if_true.class
assert_equal FalseConstant, lst.if_false.class
end
def test_to_s
lst = compile( double_if )
assert_equal "if(false);true;else;false;end" , lst.to_s.gsub("\n",";")
end
end
end

View File

@ -18,6 +18,9 @@ module Ruby
def test_simple_args
assert_equal [] , @lst.arguments
end
def test_tos
assert_equal "self.foo()" , @lst.to_s
end
end
class TestSendBar < MiniTest::Test
include RubyTests

View File

@ -28,5 +28,8 @@ module Ruby
def test_block_args
assert_equal IntegerConstant , @lst.arguments.first.class
end
def test_tos
assert_equal "yield(0)" , @lst.to_s
end
end
end