From 3fe286b5ed1e43f998a16735c79bacae3d2cc842 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 6 Apr 2017 16:06:51 +0300 Subject: [PATCH] enforces data encapsulation in vool tree gets rid of attraccessor --- lib/vool/assignment_statement.rb | 2 +- lib/vool/basic_values.rb | 6 ++--- lib/vool/if_statement.rb | 14 ++++++++---- lib/vool/return_statement.rb | 2 +- lib/vool/ruby_compiler.rb | 38 +++++++++----------------------- lib/vool/send_statement.rb | 8 +++---- lib/vool/statements.rb | 2 +- lib/vool/variables.rb | 2 +- lib/vool/while_statement.rb | 12 ++++++++-- 9 files changed, 41 insertions(+), 45 deletions(-) diff --git a/lib/vool/assignment_statement.rb b/lib/vool/assignment_statement.rb index 7e956968..8b665eb8 100644 --- a/lib/vool/assignment_statement.rb +++ b/lib/vool/assignment_statement.rb @@ -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 diff --git a/lib/vool/basic_values.rb b/lib/vool/basic_values.rb index 8dd2f9cc..d5a20681 100644 --- a/lib/vool/basic_values.rb +++ b/lib/vool/basic_values.rb @@ -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 diff --git a/lib/vool/if_statement.rb b/lib/vool/if_statement.rb index 8d71845d..4774333a 100644 --- a/lib/vool/if_statement.rb +++ b/lib/vool/if_statement.rb @@ -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? diff --git a/lib/vool/return_statement.rb b/lib/vool/return_statement.rb index 176694db..8e014051 100644 --- a/lib/vool/return_statement.rb +++ b/lib/vool/return_statement.rb @@ -1,6 +1,6 @@ module Vool class ReturnStatement < Statement - attr_accessor :return_value + attr_reader :return_value def initialize(value) @return_value = value diff --git a/lib/vool/ruby_compiler.rb b/lib/vool/ruby_compiler.rb index 30a3daac..aab41661 100644 --- a/lib/vool/ruby_compiler.rb +++ b/lib/vool/ruby_compiler.rb @@ -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 diff --git a/lib/vool/send_statement.rb b/lib/vool/send_statement.rb index e733aadb..e986a3cd 100644 --- a/lib/vool/send_statement.rb +++ b/lib/vool/send_statement.rb @@ -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 diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index ffe841e5..13d9e4ab 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -1,6 +1,6 @@ module Vool class Statements < Statement - attr_accessor :statements + attr_reader :statements def initialize(statements) @statements = statements end diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb index 320e0ba6..2e655e06 100644 --- a/lib/vool/variables.rb +++ b/lib/vool/variables.rb @@ -1,6 +1,6 @@ module Vool module Named - attr_accessor :name + attr_reader :name def initialize name @name = name end diff --git a/lib/vool/while_statement.rb b/lib/vool/while_statement.rb index 83970f0a..efba9d6d 100644 --- a/lib/vool/while_statement.rb +++ b/lib/vool/while_statement.rb @@ -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 + @statements = statements + simplify_condition end + + def simplify_condition + return unless @condition.is_a?(ScopeStatement) + @condition = @condition.first if @condition.single? + end + end end