<commit_before>#include "casemethod.h"
#include "sugar.h"
#include "../ast/astbuild.h"
#include "../ast/id.h"
#include "../pkg/package.h"
#include <assert.h>


/* The following sugar handles case methods.

Case methods are the methods appearing in the source code appearing to be
overloaded. A match method is the set of case methods for a given method name
within a single entity, treated as a single overall method.

We remove case methods from an entity and replace them with notionally a single
function. In practice we actually create 2 functions, to allow us to make
parameter and capture names work sensibly. These 2 functions are:

1. The wrapper function.
Uses the parameter names specified in the case methods, which allows calling
with named arguments to work as expected.
All this function does is call the worker function.
For case behaviours this is a behaviour, for case functions it is a function.

2. The worker method.
This uses hygenic parameter names, which leaves the names used by the case
methods free to be used for captures.
Contains the match expression that selects which case body to use.

Example source code:

class C
  fun foo(0): U64 => 0
  fun foo(x: U64 where x < 3): U64 => 1
  fun foo(x: U64): U64 => 2

would be translated to:

class C
  fun foo(x: U64): U64 => // wrapper
    $1(x)

  fun $1($2: U64): U64 => // worker
    match $2
    | 0 => 0
    | let x: U64 where x < 3 => 1
    | let x: U64 => 2
    end

*/


// Make a skeleton wrapper method based on the given case method.
// Returns: created method, NULL on error.
static ast_t* make_match_wrapper(ast_t* case_method)
{
  assert(case_method != NULL);

  if(ast_id(case_method) == TK_FUN)
    fun_defaults(case_method);

  AST_GET_CHILDREN(case_method, cap, id, t_params, params, ret_type, question,<commit_msg>fix comment to use updated syntax<commit_after>#include "casemethod.h"
#include "sugar.h"
#include "../ast/astbuild.h"
#include "../ast/id.h"
#include "../pkg/package.h"
#include <assert.h>


/* The following sugar handles case methods.

Case methods are the methods appearing in the source code appearing to be
overloaded. A match method is the set of case methods for a given method name
within a single entity, treated as a single overall method.

We remove case methods from an entity and replace them with notionally a single
function. In practice we actually create 2 functions, to allow us to make
parameter and capture names work sensibly. These 2 functions are:

1. The wrapper function.
Uses the parameter names specified in the case methods, which allows calling
with named arguments to work as expected.
All this function does is call the worker function.
For case behaviours this is a behaviour, for case functions it is a function.

2. The worker method.
This uses hygenic parameter names, which leaves the names used by the case
methods free to be used for captures.
Contains the match expression that selects which case body to use.

Example source code:

class C
  fun foo(0): U64 => 0
  fun foo(x: U64): U64 if x < 3 => 1
  fun foo(x: U64): U64 => 2

would be translated to:

class C
  fun foo(x: U64): U64 => // wrapper
    $1(x)

  fun $1($2: U64): U64 => // worker
    match $2
    | 0 => 0
    | let x: U64 if x < 3 => 1
    | let x: U64 => 2
    end

*/


// Make a skeleton wrapper method based on the given case method.
// Returns: created method, NULL on error.
static ast_t* make_match_wrapper(ast_t* case_method)
{
  assert(case_method != NULL);

  if(ast_id(case_method) == TK_FUN)
    fun_defaults(case_method);

  AST_GET_CHILDREN(case_method, cap, id, t_params, params, ret_type, question,
{'fraction_non_alphanumeric': 0.08379272326350606, 'fraction_numerical': 0.022050716648291068, 'mean_word_length': 3.6525641025641025, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '2338437', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1071, 'n_words': 528}