Parent child linkage

pull/281/head
Rick Carlino 2016-10-12 02:50:26 -05:00
parent c3e285c9bd
commit ada235c71d
1 changed files with 13 additions and 10 deletions

View File

@ -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