<commit_before>/*
 * Copyright 2012 <NAME> <<EMAIL>>
 * See COPYING for terms of redistribution.
 */

#include <jive/types/function/fctapply.h>

#include <jive/vsdg/node-private.h>
#include <jive/types/function/fcttype.h>

#include <string.h>

static void
jive_apply_node_init_(jive_apply_node * self, struct jive_region * region, jive_output * function,
	size_t narguments, jive_output * const arguments[]);

static jive_node *
jive_apply_node_create_(struct jive_region * region, const jive_node_attrs * attrs,
	size_t noperands, struct jive_output * const operands[]);

static char *
jive_apply_node_get_label_(const jive_node * self_);

const jive_node_class JIVE_APPLY_NODE = {
	.parent = &JIVE_NODE,
	.name = "APPLY",
	.fini = jive_node_fini_, /* inherit */
	.get_default_normal_form = jive_node_get_default_normal_form_, /* inherit */
	.get_label = jive_apply_node_get_label_, /* override */
	.get_attrs = jive_node_get_attrs_, /* inherit */
	.match_attrs = jive_node_match_attrs_, /* inherit */
	.create = jive_apply_node_create_, /* override */
	.get_aux_rescls = jive_node_get_aux_rescls_ /* inherit */
};

static char *
jive_apply_node_get_label_(const jive_node * self_)
{
	return strdup("APPLY");
}

static jive_node *
jive_apply_node_create_(struct jive_region * region, const jive_node_attrs * attrs,
	size_t noperands, struct jive_output * const operands[])
{
	return jive_apply_node_create(region, operands[0], noperands - 1, &operands[1]);
}

static void
jive_apply_node_init_(
	jive_apply_node * self,
	struct jive_region * region,
	jive_output * function,
	size_t narguments,
	jive_output * const arguments[])
{
	if (function->class_ != &JIVE_FUNCTION_OUTPUT) {
		jive_context_fatal_error(region->graph->context, "Type mismatch: need 'function' type as input to 'apply' node");
	}
	jive_function_output * fct = (jive_function_output *) function;

	if (fct->type.narguments != narguments) {
		jive_context_fatal_error(region->graph->context, "Type mismatch: number of parameters to function does not match signature");
	}

	jive_output * args[narguments+1];
	const jive_type * argument_types[narguments+1];
	args[0] = function;
	argument_types[0] = &fct->type.base.base; 
 
	size_t i;
	for(i = 0; i < fct->type.narguments; i++){
		argument_types[i+1] = fct->type.argument_types[i];
		args[i+1] = arguments[i];
	}
	
	jive_node_init_(self, region,
		narguments + 1, argument_types, args,
		fct->type.nreturns, (const jive_type * const *) fct->type.return_types);
}

jive_node *
jive_apply_node_create(struct jive_region * region, jive_output * function,
	size_t narguments, jive_output * const arguments[])
{
	jive_apply_node * node = jive_context_malloc(region->graph->context, sizeof( * node));
<commit_msg>function: small cleanups for apply node<commit_after>/*
 * Copyright 2012 <NAME> <<EMAIL>>
 * See COPYING for terms of redistribution.
 */

#include <jive/types/function/fctapply.h>

#include <jive/vsdg/node-private.h>
#include <jive/types/function/fcttype.h>

#include <string.h>

static void
jive_apply_node_init_(jive_apply_node * self, struct jive_region * region, jive_output * function,
	size_t narguments, jive_output * const arguments[]);

static jive_node *
jive_apply_node_create_(struct jive_region * region, const jive_node_attrs * attrs,
	size_t noperands, struct jive_output * const operands[]);

const jive_node_class JIVE_APPLY_NODE = {
	.parent = &JIVE_NODE,
	.name = "APPLY",
	.fini = jive_node_fini_, /* inherit */
	.get_default_normal_form = jive_node_get_default_normal_form_, /* inherit */
	.get_label = jive_node_get_label_, /* inherit */
	.get_attrs = jive_node_get_attrs_, /* inherit */
	.match_attrs = jive_node_match_attrs_, /* inherit */
	.create = jive_apply_node_create_, /* override */
	.get_aux_rescls = jive_node_get_aux_rescls_ /* inherit */
};

static jive_node *
jive_apply_node_create_(struct jive_region * region, const jive_node_attrs * attrs,
	size_t noperands, struct jive_output * const operands[])
{
	return jive_apply_node_create(region, operands[0], noperands - 1, &operands[1]);
}

static void
jive_apply_node_init_(
	jive_apply_node * self,
	struct jive_region * region,
	jive_output * function,
	size_t narguments,
	jive_output * const arguments[])
{
	if (!jive_output_isinstance(function, &JIVE_FUNCTION_OUTPUT)) {
		jive_context_fatal_error(region->graph->context, "Type mismatch: need 'function' type as input to 'apply' node");
	}
	jive_function_output * fct = (jive_function_output *) function;

	if (fct->type.narguments != narguments) {
		jive_context_fatal_error(region->graph->context, "Type mismatch: number of parameters to function does not match signature");
	}

	jive_output * args[narguments+1];
	const jive_type * argument_types[narguments+1];
	args[0] = function;
	argument_types[0] = &fct->type.base.base; 
 
	size_t i;
	for(i = 0; i < fct->type.narguments; i++){
		argument_types[i+1] = fct->type.argument_types[i];
		args[i+1] = arguments[i];
	}
	
	jive_node_init_(self, region,
		narguments + 1, argument_types, args,
		fct->type.nreturns, (const jive_type * const *) fct->type.return_types);
}

jive_node *
jive_apply_node_create(struct jive_region * region, jive_output * function,
	size_t narguments, jive_output * const arguments[])
{
	jive_apply_node * node = jive_context_malloc(region->graph->context, sizeof( * node));

{'fraction_non_alphanumeric': 0.12157083803081549, 'fraction_numerical': 0.005636978579481398, 'mean_word_length': 5.563501849568434, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 42, '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': '6175610', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1946, 'n_words': 397}