CeleryScript Namespace

pull/281/head
Rick Carlino 2016-10-12 09:27:19 -05:00
parent ffa0012fa9
commit 0f71fc1f2f
5 changed files with 72 additions and 75 deletions

View File

@ -1,72 +0,0 @@
class AstNode
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 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(parent, hash)
is_node?(hash) ? AstNode.new(self, **hash) : hash
end
def is_node?(hash)
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 self.define(name, arg_types, body_types = nil)
self
end
def self.validate(name)
self
end
def self.reset
end
end
Parser
.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"
])
.validate("sequence") do |node, problem|
# Accepts block or anything that respond_to? call()
problem("Expected sequence to have steps.") if node.body.length < 1
end
.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

@ -0,0 +1,31 @@
module CeleryScript
class AstNode
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 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(parent, hash)
is_node?(hash) ? AstNode.new(self, **hash) : hash
end
def is_node?(hash)
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
end

View File

@ -0,0 +1,24 @@
module CeleryScript
class Parser
def self.define(name, arg_types, body_types = nil)
self
end
def self.undefine(name)
self
end
def self.reset
end
def self.each_node(node, &blk)
node.args.map do |key, value|
self.
end.to_h if node.args
node.body.map do |e|
maybe_initialize(self, e)
end if node.body
end
end
end

View File

@ -0,0 +1,14 @@
module CeleryScript
class TreeClimber
def self.each_node(node, &blk)
end
private
def self.traverse(node, &blk)
if node.is_a?(AstNode)
else
end
end
end
end

View File

@ -1,18 +1,18 @@
require 'spec_helper'
describe AstNode do
describe CeleryScript::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)
node = CeleryScript::AstNode.new(**hash)
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)
expect(node.args[:x].args[:data_value]).to be_kind_of(CeleryScript::AstNode)
end
end