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
|
|
|
|
|
2019-10-03 23:36:49 +02:00
|
|
|
# Many statements exist in the sol layer in quite a similar arrangement
|
2018-09-01 14:54:25 +02:00
|
|
|
# Especially for different types of assignment we can abstract the creation
|
2019-10-03 23:36:49 +02:00
|
|
|
# of the sol, by using the right class to instantiate, the "sol_brother"
|
|
|
|
# Ie same class_name, but in the Sol module
|
|
|
|
def sol_brother
|
|
|
|
eval "Sol::#{class_name}"
|
2018-07-19 19:59:15 +02:00
|
|
|
end
|
2018-09-01 14:54:25 +02:00
|
|
|
|
|
|
|
# return the class name without the module
|
2019-10-03 23:36:49 +02:00
|
|
|
# used to evaluate the sol_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
|