-module(bt_erl_parser). -include("bt_nodes.hrl"). -export([ parse/2, parse/3 ]). %% root. %% has to ALWAYS be a bt_erl_tree! (for now) -spec parse(BtErlTree :: bt_erl_tree(), ArgsAsProplist :: list()) -> {Node :: bt_node(), NewBB :: blackboard()}. parse(BtErlTree, ArgsAsProplist) when is_record(BtErlTree, bt_erl_tree), is_list(ArgsAsProplist) -> parse(BtErlTree#bt_erl_tree{args = maps:from_list(ArgsAsProplist)}, bt_parser_utils:get_namespace(), bt_blackboard:get_empty()). %% TODO: should probably call above fn parse_root so as to not confuse with this one. since this one is anyway internal only %% sequence -spec parse(Node :: bt_node() | {TreeModule :: atom(), Args :: map()}, Namespace :: namespace(), BB :: blackboard()) -> {Node :: bt_node(), NewBB :: blackboard()}. parse(Sequence, Namespace, BB) when is_record(Sequence, sequence) -> erl_parse_sequence:parse(Sequence, Namespace, BB); %% selector (TODO) parse(Selector, Namespace, BB) when is_record(Selector, selector) -> erl_parse_selector:parse(Selector, Namespace, BB); %% decorator parse(Decorator = #decorator{child = Child}, Namespace, BB) when is_record(Decorator, decorator) -> {NewChild, NewBB} = parse(Child, Namespace, BB), {Decorator#decorator{child = NewChild}, NewBB}; %% task parse(Task, Namespace, BB) when is_record(Task, task) -> erl_parse_task:parse(Task, Namespace, BB); %% erl tree parse(BtErlTree = #bt_erl_tree{params = Params, args = Args, bb_keys = BBKeys, generator = {GenModule, GenFunction}}, Namespace, BB) when is_record(BtErlTree, bt_erl_tree) -> NewBB = bt_parser_utils:get_blackboard(BBKeys, Namespace, BB), bt_parser_utils:throw_error_if_args_and_params_dont_match("UnknownTree", Args, Params), BBKeyMap = bt_parser_utils:get_unresolved_key_bb_map(BBKeys, Namespace), parse(erlang:apply(GenModule, GenFunction, [Args, BBKeyMap]), Namespace, NewBB).