Class: Vangrail::Colang::Program

Inherits:
Struct
  • Object
show all
Defined in:
lib/vangrail/colang/ast.rb

Overview

The whole grammar this gem executes, as data. Everything the parser can produce is one of these, which is also the honest statement of what a Colang file may contain here: anything else is refused at parse time rather than skipped at run time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bot_messagesObject

Returns the value of attribute bot_messages

Returns:

  • (Object)

    the current value of bot_messages



11
12
13
# File 'lib/vangrail/colang/ast.rb', line 11

def bot_messages
  @bot_messages
end

#flowsObject

Returns the value of attribute flows

Returns:

  • (Object)

    the current value of flows



11
12
13
# File 'lib/vangrail/colang/ast.rb', line 11

def flows
  @flows
end

#user_messagesObject

Returns the value of attribute user_messages

Returns:

  • (Object)

    the current value of user_messages



11
12
13
# File 'lib/vangrail/colang/ast.rb', line 11

def user_messages
  @user_messages
end

Instance Method Details

#bot_message(name) ⇒ Object



20
21
22
# File 'lib/vangrail/colang/ast.rb', line 20

def bot_message(name)
  bot_messages[name.to_s]
end

#check!Object

Every bot <name> in every flow has a define bot. The walk covers unreached if branches and flows no config names: a half-loaded guardrail must not load, so a dead-branch bot in a merged .co file fails the folder. Called after parse/merge, not during parse, so a bot defined in another file is visible once the programs are joined.



37
38
39
40
# File 'lib/vangrail/colang/ast.rb', line 37

def check!
  flows.each { |name, flow| check_body(flow.body, name) }
  self
end

#check_body(body, flow_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vangrail/colang/ast.rb', line 42

def check_body(body, flow_name)
  Array(body).each do |node|
    case node
    when Bot
      next if bot_message(node.message)

      raise ColangError, "no `define bot #{node.message}` for flow #{flow_name.inspect}"
    when If
      check_body(node.then_body, flow_name)
      check_body(node.else_body, flow_name)
    end
  end
end

#flow(name) ⇒ Object



12
13
14
# File 'lib/vangrail/colang/ast.rb', line 12

def flow(name)
  flows[name.to_s]
end

#flow_namesObject



16
17
18
# File 'lib/vangrail/colang/ast.rb', line 16

def flow_names
  flows.keys
end

#merge(other) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/vangrail/colang/ast.rb', line 24

def merge(other)
  Program.new(
    flows: flows.merge(other.flows),
    bot_messages: bot_messages.merge(other.bot_messages),
    user_messages: user_messages.merge(other.user_messages),
  )
end