<commit_before>%% Helper module for dispatching names(module/function/macro/var) and their references.
-module(kapok_dispatch).
-export([default_requires/0,
         default_functions/0,
         default_macros/0,
         find_local_macro/3,
         find_remote_macro/4,
         find_local/2,
         find_local_function/3,
         get_remote_function/4,
         find_remote_function/4,
         format_error/1]).
-import(kapok_config, [get_compiler_opt/1]).
-include("kapok.hrl").

default_requires() ->
  [{kapok_macro, kapok_macro}].

default_functions() ->
  [].

default_macros() ->
  [{kapok_macro, get_optional_macros(kapok_macro)}].


%% find local/remote macro/function

find_local_macro(Meta, FunArity, #{namespace := Namespace} = Env) ->
  %% check whether the FunArity refers to a macro in current namespace
  %% which is defined previously.
  Macros = kapok_symbol_table:namespace_macros(Namespace),
  case filter_fa(FunArity, Macros) of
    [{F, A, P}] ->
      {{F, A, P}, Env};
    [] ->
      find_import_macro(Meta, FunArity, Env)
  end.

find_import_macro(Meta, FunArity, Env) ->
  {D, Env1} = find_dispatch(Meta, FunArity, Env),
  R = case D of
        {macro, {M, F, A, P}} -> {M, F, A, P};
        {function, _} -> false;
        false -> false
      end,
  {R, Env1}.

find_remote_macro(Meta, Module, FunArity, Env) ->
  Requires = ?m(Env, requires),
  Uses = ?m(Env, uses),
  Module1 = case orddict:find(Module, Requires) of
              {ok, M1} -> M1;
              error -> Module
            end,
  case orddict:find(Module1, Uses) of
    {ok, _} ->
      {D, Env1} = find_dispatch(Meta, Module1, FunArity, Env),
      R = case D of
            {macro, {M, F, A, P}} -> {M, F, A, P};
            {function, _} -> false;
            false -> false
          end,
      {R, Env1};
    error ->
      {D, Env1} = find_optional_dispatch(Meta, Module1, FunArity, Env),
      R = case D of
            {macro, {M, F, A, P}} -> {M, F, A, P};
            {function, _} -> false;
            false -> false
          end,
      {R, Env1}
  end.

find_local(FunArity, #{def_fap := FAP} = Env) ->
  case filter_fa(FunArity, [FAP]) of
    [{F, A, P}] ->
      %% match current function definition
      {F, A, P};
    [] ->
      %% find in macros/functions of current namespace
      Namespace = maps:get(namespace, Env),
      Locals = kapok_symbol_table:namespace_locals(Namespace),
      case filter_fa(FunArity, Locals) of
        [{F, A, P}] -> {F, A, P};
        [] -> false
      end
  end.
<commit_msg>rename var<commit_after>%% Helper module for dispatching names(module/function/macro/var) and their references.
-module(kapok_dispatch).
-export([default_requires/0,
         default_functions/0,
         default_macros/0,
         find_local_macro/3,
         find_remote_macro/4,
         find_local/2,
         find_local_function/3,
         get_remote_function/4,
         find_remote_function/4,
         format_error/1]).
-import(kapok_config, [get_compiler_opt/1]).
-include("kapok.hrl").

default_requires() ->
  [{kapok_macro, kapok_macro}].

default_functions() ->
  [].

default_macros() ->
  [{kapok_macro, get_optional_macros(kapok_macro)}].


%% find local/remote macro/function

find_local_macro(Meta, FunArity, #{namespace := Namespace} = Env) ->
  %% check whether the FunArity refers to a macro in current namespace
  %% which is defined previously.
  Macros = kapok_symbol_table:namespace_macros(Namespace),
  case filter_fa(FunArity, Macros) of
    [{F, A, P}] ->
      {{F, A, P}, Env};
    [] ->
      find_import_macro(Meta, FunArity, Env)
  end.

find_import_macro(Meta, FunArity, Env) ->
  {D, Env1} = find_dispatch(Meta, FunArity, Env),
  R = case D of
        {macro, {M, F, A, P}} -> {M, F, A, P};
        {function, _} -> false;
        false -> false
      end,
  {R, Env1}.

find_remote_macro(Meta, Module, FunArity, Env) ->
  Requires = ?m(Env, requires),
  Uses = ?m(Env, uses),
  %% get the original module name in case Module is a alias or rename.
  Original = case orddict:find(Module, Requires) of
               {ok, M1} -> M1;
               error -> Module
             end,
  case orddict:find(Original, Uses) of
    {ok, _} ->
      %% Original is declared in ns use clause.
      %% Load all the import macros/functions from the specified module if necessary,
      %% and then find the specified FunArity.
      {D, Env1} = find_dispatch(Meta, Original, FunArity, Env),
      R = case D of
            {macro, {M, F, A, P}} -> {M, F, A, P};
            {function, _} -> false;
            false -> false
          end,
      {R, Env1};
    error ->
      {D, Env1} = find_optional_dispatch(Meta, Original, FunArity, Env),
      R = case D of
            {macro, {M, F, A, P}} -> {M, F, A, P};
            {function, _} -> false;
            false -> false
          end,
      {R, Env1}
  end.

find_local(FunArity, #{def_fap := FAP} = Env) ->
  case filter_fa(FunArity, [FAP]) of
    [{F, A, P}] ->
      %% match current function definition
      {F, A, P};
    [] ->
      %% find in macros/functions of current namespace
      Namespace = maps:get(namespace, Env),
      Locals = kapok_symbol_table:namespace_locals(Namespace),
      case filter_fa(FunArity, Locals) of
        [{F, A, P}] -> {F, A, P};
        [] -> false
      end
  end.

{'fraction_non_alphanumeric': 0.14406461307287755, 'fraction_numerical': 0.007888805409466567, 'mean_word_length': 2.3745247148288975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1664016', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1833, 'n_words': 478}