adding to_s to code for debugging

This commit is contained in:
Torsten Ruger 2016-12-23 21:31:31 +02:00
parent 6c9bd7e476
commit 1b8d6149dd
8 changed files with 43 additions and 0 deletions

View File

@ -5,6 +5,11 @@ module Typed
class Statement < Code ; end class Statement < Code ; end
class Expression < Code ; end class Expression < Code ; end
module ValuePrinter
def to_s
@value.to_s
end
end
end end
require_relative "tree/while_statement" require_relative "tree/while_statement"

View File

@ -1,30 +1,43 @@
module Typed module Typed
module Tree module Tree
class IntegerExpression < Expression class IntegerExpression < Expression
include ValuePrinter
attr_accessor :value attr_accessor :value
def initialize(value) def initialize(value)
@value = value @value = value
end end
end end
class FloatExpression < Expression class FloatExpression < Expression
include ValuePrinter
attr_accessor :value attr_accessor :value
def initialize(value) def initialize(value)
@value = value @value = value
end end
end end
class TrueExpression < Expression class TrueExpression < Expression
def to_s
"true"
end
end end
class FalseExpression < Expression class FalseExpression < Expression
def to_s
false
end
end end
class NilExpression < Expression class NilExpression < Expression
def to_s
"nil"
end
end end
class StringExpression < Expression class StringExpression < Expression
include ValuePrinter
attr_accessor :value attr_accessor :value
def initialize(value) def initialize(value)
@value = value @value = value
end end
end end
class NameExpression < Expression class NameExpression < Expression
include ValuePrinter
attr_accessor :value attr_accessor :value
alias :name :value alias :name :value
def initialize(value) def initialize(value)
@ -32,6 +45,7 @@ module Typed
end end
end end
class ClassExpression < Expression class ClassExpression < Expression
include ValuePrinter
attr_accessor :value attr_accessor :value
def initialize(value) def initialize(value)
@value = value @value = value

View File

@ -2,6 +2,11 @@ module Typed
module Tree module Tree
class CallSite < Expression class CallSite < Expression
attr_accessor :name , :receiver , :arguments 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 end
end end

View File

@ -2,6 +2,9 @@ module Typed
module Tree module Tree
class FieldAccess < Expression class FieldAccess < Expression
attr_accessor :receiver , :field attr_accessor :receiver , :field
def to_s
"#{receiver}.#{field}"
end
end end
end end
end end

View File

@ -2,6 +2,11 @@ module Typed
module Tree module Tree
class IfStatement < Statement class IfStatement < Statement
attr_accessor :branch_type , :condition , :if_true , :if_false 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 end
end end

View File

@ -2,6 +2,9 @@ module Typed
module Tree module Tree
class OperatorExpression < Expression class OperatorExpression < Expression
attr_accessor :operator , :left_expression , :right_expression attr_accessor :operator , :left_expression , :right_expression
def to_s
"#{left_expression} #{operator} #{right_expression}"
end
end end
end end
end end

View File

@ -2,6 +2,10 @@ module Typed
module Tree module Tree
class ReturnStatement < Statement class ReturnStatement < Statement
attr_accessor :return_value attr_accessor :return_value
def to_s
"return #{return_value}"
end
end end
end end
end end

View File

@ -2,6 +2,10 @@ module Typed
module Tree module Tree
class WhileStatement < Statement class WhileStatement < Statement
attr_accessor :branch_type , :condition , :statements 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 end
end end