From 1b8d6149dd68069ff477f8cec1331fd09369f9d1 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 23 Dec 2016 21:31:31 +0200 Subject: [PATCH] adding to_s to code for debugging --- lib/typed/tree.rb | 5 +++++ lib/typed/tree/basic_values.rb | 14 ++++++++++++++ lib/typed/tree/call_site.rb | 5 +++++ lib/typed/tree/field_access.rb | 3 +++ lib/typed/tree/if_statement.rb | 5 +++++ lib/typed/tree/operator_expression.rb | 3 +++ lib/typed/tree/return_statement.rb | 4 ++++ lib/typed/tree/while_statement.rb | 4 ++++ 8 files changed, 43 insertions(+) diff --git a/lib/typed/tree.rb b/lib/typed/tree.rb index 37f4bdc7..8f7cc2ee 100644 --- a/lib/typed/tree.rb +++ b/lib/typed/tree.rb @@ -5,6 +5,11 @@ module Typed class Statement < Code ; end class Expression < Code ; end + module ValuePrinter + def to_s + @value.to_s + end + end end require_relative "tree/while_statement" diff --git a/lib/typed/tree/basic_values.rb b/lib/typed/tree/basic_values.rb index 79361138..3b744423 100644 --- a/lib/typed/tree/basic_values.rb +++ b/lib/typed/tree/basic_values.rb @@ -1,30 +1,43 @@ module Typed module Tree class IntegerExpression < Expression + include ValuePrinter attr_accessor :value def initialize(value) @value = value end end class FloatExpression < Expression + include ValuePrinter attr_accessor :value def initialize(value) @value = value end end class TrueExpression < Expression + def to_s + "true" + end end class FalseExpression < Expression + def to_s + false + end end class NilExpression < Expression + def to_s + "nil" + end end class StringExpression < Expression + include ValuePrinter attr_accessor :value def initialize(value) @value = value end end class NameExpression < Expression + include ValuePrinter attr_accessor :value alias :name :value def initialize(value) @@ -32,6 +45,7 @@ module Typed end end class ClassExpression < Expression + include ValuePrinter attr_accessor :value def initialize(value) @value = value diff --git a/lib/typed/tree/call_site.rb b/lib/typed/tree/call_site.rb index fff848b3..00c8785e 100644 --- a/lib/typed/tree/call_site.rb +++ b/lib/typed/tree/call_site.rb @@ -2,6 +2,11 @@ module Typed module Tree class CallSite < Expression attr_accessor :name , :receiver , :arguments + + def to_s + str = receiver ? "#{receiver}.#{name}" : name.to_s + str + arguments.collect{|a| a.to_s }.join(",") + end end end end diff --git a/lib/typed/tree/field_access.rb b/lib/typed/tree/field_access.rb index ae91cecc..5e192273 100644 --- a/lib/typed/tree/field_access.rb +++ b/lib/typed/tree/field_access.rb @@ -2,6 +2,9 @@ module Typed module Tree class FieldAccess < Expression attr_accessor :receiver , :field + def to_s + "#{receiver}.#{field}" + end end end end diff --git a/lib/typed/tree/if_statement.rb b/lib/typed/tree/if_statement.rb index a07a8827..241c45df 100644 --- a/lib/typed/tree/if_statement.rb +++ b/lib/typed/tree/if_statement.rb @@ -2,6 +2,11 @@ module Typed module Tree class IfStatement < Statement attr_accessor :branch_type , :condition , :if_true , :if_false + def to_s + str = "if_#{branch_type}(#{condition}) \n #{if_true}\n" + str += "else\n #{if_false}\n" if if_false + str + "end\n" + end end end end diff --git a/lib/typed/tree/operator_expression.rb b/lib/typed/tree/operator_expression.rb index 2d17c52f..e371bc16 100644 --- a/lib/typed/tree/operator_expression.rb +++ b/lib/typed/tree/operator_expression.rb @@ -2,6 +2,9 @@ module Typed module Tree class OperatorExpression < Expression attr_accessor :operator , :left_expression , :right_expression + def to_s + "#{left_expression} #{operator} #{right_expression}" + end end end end diff --git a/lib/typed/tree/return_statement.rb b/lib/typed/tree/return_statement.rb index 703202b1..3f6d4c9f 100644 --- a/lib/typed/tree/return_statement.rb +++ b/lib/typed/tree/return_statement.rb @@ -2,6 +2,10 @@ module Typed module Tree class ReturnStatement < Statement attr_accessor :return_value + + def to_s + "return #{return_value}" + end end end end diff --git a/lib/typed/tree/while_statement.rb b/lib/typed/tree/while_statement.rb index 8a2a447c..e73602f9 100644 --- a/lib/typed/tree/while_statement.rb +++ b/lib/typed/tree/while_statement.rb @@ -2,6 +2,10 @@ module Typed module Tree class WhileStatement < Statement attr_accessor :branch_type , :condition , :statements + def to_s + str = "while_#{branch_type}(#{condition}) do\n" + str + statements.join(" ") + "\nend\n" + end end end end