Working AstNode class
parent
f79dd8e739
commit
c3e285c9bd
|
@ -0,0 +1,28 @@
|
|||
# 1. Travers the args
|
||||
# 2. Traverse the body
|
||||
# @body = body.map { |hash| maybe_initialize(hash) } if body
|
||||
# binding.pry
|
||||
# Sequencer
|
||||
# .add_kind("move_rel", {
|
||||
# x: ["literal", "var_get"],
|
||||
# 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
|
||||
|
||||
@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
|
||||
end
|
||||
|
||||
def maybe_initialize(hash)
|
||||
well_formed?(hash) ? AstNode.new(**hash) : hash
|
||||
end
|
||||
|
||||
def well_formed?(hash)
|
||||
hash.is_a?(Hash) && hash.has_key?(:kind) && hash.has_key?(:args)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe AstNode do
|
||||
FIXTURE_FILE = File.read("/home/rick/code/farmbot/api/spec/mutations/sequences/improved_ast_fixture.json")
|
||||
let(:hash) do
|
||||
JSON.parse(FIXTURE_FILE).deep_symbolize_keys
|
||||
end
|
||||
|
||||
it "initializes" do
|
||||
node = AstNode.new(**hash)
|
||||
binding.pry
|
||||
end
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"kind": "sequence",
|
||||
"args": {
|
||||
"x": {
|
||||
"kind": "literal",
|
||||
"args": {
|
||||
"data_value": "5",
|
||||
"data_type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"kind": "literal",
|
||||
"args": {
|
||||
"data_type": "integer",
|
||||
"data_value": 1000
|
||||
}
|
||||
},
|
||||
{
|
||||
"kind": "literal",
|
||||
"args": {
|
||||
"data_type": "integer",
|
||||
"data_value": 1000
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue