enforces data encapsulation in vool tree

gets rid of attraccessor
This commit is contained in:
Torsten Ruger 2017-04-06 16:06:51 +03:00
parent b9caad937a
commit 3fe286b5ed
9 changed files with 41 additions and 45 deletions

View File

@ -1,6 +1,6 @@
module Vool
class Assignment < Statement
attr_accessor :name , :value
attr_reader :name , :value
def initialize(name , value )
@name , @value = name , value
end

View File

@ -1,12 +1,12 @@
module Vool
class IntegerStatement < Statement
attr_accessor :value
attr_reader :value
def initialize(value)
@value = value
end
end
class FloatStatement < Statement
attr_accessor :value
attr_reader :value
def initialize(value)
@value = value
end
@ -22,7 +22,7 @@ module Vool
class SuperStatement < Statement
end
class StringStatement < Statement
attr_accessor :value
attr_reader :value
def initialize(value)
@value = value
end

View File

@ -1,11 +1,17 @@
module Vool
class IfStatement < Statement
attr_accessor :condition , :if_true , :if_false
attr_reader :condition , :if_true , :if_false
def initialize( cond = nil)
def initialize( cond , if_true , if_false = [])
@condition = cond
@if_true = []
@if_false = []
@if_true = if_true
@if_false = if_false
simplify_condition
end
def simplify_condition
return unless @condition.is_a?(ScopeStatement)
@condition = @condition.first if @condition.single?
end
def has_false?

View File

@ -1,6 +1,6 @@
module Vool
class ReturnStatement < Statement
attr_accessor :return_value
attr_reader :return_value
def initialize(value)
@return_value = value

View File

@ -137,30 +137,22 @@ module Vool
def on_while statement
condition , statements = *statement
w = WhileStatement.new( process(condition) )
simplify_condition(w)
w.statements = process(statements)
w
WhileStatement.new( process(condition) , process(statements))
end
def on_if statement
condition , if_true , if_false = *statement
w = IfStatement.new( process(condition) )
simplify_condition(w)
w.if_true = process(if_true)
w.if_false = process(if_false)
w
if_true = process(if_true)
if_false = process(if_false)
IfStatement.new( process(condition) , if_true , if_false )
end
def on_send statement
kids = statement.children.dup
receiver = kids.shift
receiver = process(kids.shift) || SelfStatement.new
name = kids.shift
arguments = kids
w = SendStatement.new( name )
w.receiver = process(receiver) || SelfStatement.new
w.arguments = process_all(arguments)
w
arguments = process_all(kids)
SendStatement.new( name , receiver , arguments )
end
def on_and expression
@ -173,18 +165,14 @@ module Vool
# this is a call to super without args (z = zero arity)
def on_zsuper exp
w = SendStatement.new( nil )
w.receiver = SuperStatement.new
w
SendStatement.new( nil , SuperStatement.new )
end
# this is a call to super with args and
# same name as current method, which is set later
def on_super( statement )
w = SendStatement.new( nil )
w.receiver = SuperStatement.new
w.arguments = process_all(statement.children)
w
arguments = process_all(statement.children)
SendStatement.new( nil , SuperStatement.new , arguments)
end
def on_assignment statement
@ -197,12 +185,6 @@ module Vool
private
def simplify_condition( cond )
condition = cond.condition
return unless condition.is_a?(ScopeStatement)
cond.condition = condition.first if condition.single?
end
def instance_name sym
sym.to_s[1 .. -1].to_sym
end

View File

@ -1,9 +1,9 @@
module Vool
class SendStatement < Statement
attr_accessor :name , :receiver , :arguments
def initialize(name)
@name = name
@arguments = []
attr_reader :name , :receiver , :arguments
def initialize(name , receiver , arguments = [])
@name , @receiver , @arguments = name , receiver , arguments
end
end
end

View File

@ -1,6 +1,6 @@
module Vool
class Statements < Statement
attr_accessor :statements
attr_reader :statements
def initialize(statements)
@statements = statements
end

View File

@ -1,6 +1,6 @@
module Vool
module Named
attr_accessor :name
attr_reader :name
def initialize name
@name = name
end

View File

@ -1,9 +1,17 @@
module Vool
class WhileStatement < Statement
attr_accessor :condition , :statements
attr_reader :condition , :statements
def initialize( condition )
def initialize( condition , statements )
@condition = condition
end
@statements = statements
simplify_condition
end
def simplify_condition
return unless @condition.is_a?(ScopeStatement)
@condition = @condition.first if @condition.single?
end
end
end