Parent child linkage
parent
c3e285c9bd
commit
ada235c71d
|
@ -8,21 +8,24 @@
|
|||
# speed: "literal"
|
||||
# }, ["if_statement", "exec_sequence"])
|
||||
class AstNode
|
||||
attr_reader :args, :body, :comments, :kind
|
||||
def initialize(args:, body: nil, comment: "", kind:)
|
||||
@comment, @kind = comment, kind
|
||||
attr_reader :args, :body, :comments, :kind, :parent
|
||||
def initialize(parent = nil, args:, body: nil, comment: "", kind:)
|
||||
@comment, @kind, @parent = comment, kind, parent
|
||||
|
||||
@args = args.map {|m,e| [m, maybe_initialize(e)] }.to_h if args
|
||||
@body = body.map {|e| maybe_initialize(e) } if body
|
||||
rescue => e
|
||||
binding.pry
|
||||
@args = args.map do |key, value|
|
||||
[key, maybe_initialize(key, value)]
|
||||
end.to_h if args
|
||||
|
||||
@body = body.map do |e|
|
||||
maybe_initialize(self, e)
|
||||
end if body
|
||||
end
|
||||
|
||||
def maybe_initialize(hash)
|
||||
well_formed?(hash) ? AstNode.new(**hash) : hash
|
||||
def maybe_initialize(parent, hash)
|
||||
is_node?(hash) ? AstNode.new(self, **hash) : hash
|
||||
end
|
||||
|
||||
def well_formed?(hash)
|
||||
def is_node?(hash)
|
||||
hash.is_a?(Hash) && hash.has_key?(:kind) && hash.has_key?(:args)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue