2018-07-19 13:46:51 +02:00
|
|
|
module Ruby
|
|
|
|
|
|
|
|
# Base class for all statements in the tree. Derived classes correspond to known language
|
|
|
|
# constructs
|
|
|
|
#
|
|
|
|
# Compilers or compiler passes are written by implementing methods.
|
|
|
|
#
|
|
|
|
class Statement
|
|
|
|
|
2018-09-01 14:54:25 +02:00
|
|
|
# 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"
|
2019-08-19 10:33:12 +02:00
|
|
|
# Ie same class_name, but in the Vool module
|
2018-07-19 19:59:15 +02:00
|
|
|
def vool_brother
|
|
|
|
eval "Vool::#{class_name}"
|
|
|
|
end
|
2018-09-01 14:54:25 +02:00
|
|
|
|
|
|
|
# return the class name without the module
|
|
|
|
# used to evaluate the vool_brother
|
2018-07-19 19:59:15 +02:00
|
|
|
def class_name
|
|
|
|
self.class.name.split("::").last
|
|
|
|
end
|
2018-07-19 13:46:51 +02:00
|
|
|
|
2018-09-01 14:54:25 +02:00
|
|
|
# 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)
|
2019-09-19 19:48:21 +02:00
|
|
|
def at_depth(depth , lines)
|
2018-09-01 14:54:25 +02:00
|
|
|
prefix = " " * 2 * depth
|
2019-09-19 19:48:21 +02:00
|
|
|
strings = lines.split("\n")
|
2018-09-01 14:54:25 +02:00
|
|
|
strings.collect{|str| prefix + str}.join("\n")
|
|
|
|
end
|
|
|
|
end
|
2018-07-19 13:46:51 +02:00
|
|
|
end
|