Test recursiveness of AST traversal

pull/281/head
Rick Carlino 2016-10-12 03:27:23 -05:00
parent ada235c71d
commit 9fce5432a3
4 changed files with 59 additions and 17 deletions

View File

@ -1,12 +1,3 @@
# 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, :parent
def initialize(parent = nil, args:, body: nil, comment: "", kind:)
@ -26,6 +17,47 @@ class AstNode
end
def is_node?(hash)
hash.is_a?(Hash) && hash.has_key?(:kind) && hash.has_key?(:args)
hash.is_a?(Hash) &&
hash.has_key?(:kind) &&
hash.has_key?(:args) &&
(hash[:body].is_a?(Array) || hash[:body] == nil) &&
(hash[:comment].is_a?(String) || hash[:comment] == nil) &&
(hash[:args].is_a?(Hash)) &&
(hash[:kind].is_a?(String))
end
end
# Temp stub.
class Parser
def define(name, arg_types, body_types = nil)
self
end
end
Parser.new
.define(
# Define node name
"sequence", {
#define node args and sub args
imports: ["import_statement"]
}, [
"var_set",
"var_get",
"move_absolute",
"move_relative",
"write_pin",
"read_pin",
"wait",
"send_message",
"execute",
"if_statement"
]) {
}
.define("literal", {
# Literal has "terminal" args (args that are not nodes).
# Pass in the class you expect instead of a string.
data_value: [String, "var_get"],
data_type: [String]
})

View File

@ -8,6 +8,11 @@ describe AstNode do
it "initializes" do
node = AstNode.new(**hash)
binding.pry
expect(node.kind).to eq("sequence")
expect(node.body.length).to eq(2)
expect(node.body[0].kind).to eq("other")
expect(node.body[1].kind).to eq("whatever")
expect(node.args[:x].kind).to eq("blah")
expect(node.args[:x].args[:data_value]).to be_kind_of(AstNode)
end
end

View File

@ -1,9 +1,9 @@
require 'spec_helper'
describe Sequences::AstParser do
let(:nodes) {
let(:nodes) do
JSON.parse(File.read("./spec/mutations/sequences/ast_fixture.json"))
}
end
it 'validates the type of all key/value pairs in the AST' do
result = Sequences::AstParser.run!(body: nodes)

View File

@ -2,23 +2,28 @@
"kind": "sequence",
"args": {
"x": {
"kind": "literal",
"kind": "blah",
"args": {
"data_value": "5",
"data_value": {
"kind": "var_get",
"args": {
"data_label": "wow"
}
},
"data_type": "integer"
}
}
},
"body": [
{
"kind": "literal",
"kind": "other",
"args": {
"data_type": "integer",
"data_value": 1000
}
},
{
"kind": "literal",
"kind": "whatever",
"args": {
"data_type": "integer",
"data_value": 1000