Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_value_script_function.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Interface afw_value Implementation for Lambda Definition
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 
18 #define impl_afw_value_optional_release NULL
19 #define impl_afw_value_get_reference NULL
20 
21 /* Inf specific is always data type. */
22 #define AFW_IMPLEMENTATION_SPECIFIC (const void *)&afw_data_type_function_direct
23 
24 /* Define inf variables for data_type and fully_evaluated_data_type. */
25 #define AFW_IMPLEMENTATION_INF_VARIABLES \
26  (const void *)&afw_data_type_function_direct, \
27  NULL
28 
29 #define impl_afw_value_get_evaluated_meta \
30  afw_value_internal_get_evaluated_meta_default
31 
32 #define impl_afw_value_get_evaluated_metas \
33  afw_value_internal_get_evaluated_metas_default
34 
35 /* Declares and rti/inf defines for interface afw_value */
36 #define AFW_IMPLEMENTATION_ID "script_function"
37 #define AFW_IMPLEMENTATION_INF_SPECIFIER AFW_DEFINE_CONST_DATA
38 #define AFW_IMPLEMENTATION_INF_LABEL afw_value_script_function_definition_inf
40 
41 
42 /* Create function for lambda definition value. */
43 AFW_DEFINE(const afw_value_t *)
45  const afw_compile_value_contextual_t *contextual,
46  const afw_value_type_t *returns,
47  afw_size_t count,
48  const afw_value_script_function_parameter_t **parameters,
49  const afw_value_t *body,
50  const afw_pool_t *p,
51  afw_xctx_t *xctx)
52 {
54 
59  result->contextual = contextual;
60  result->returns = returns;
61  result->count = count;
62  result->parameters = parameters;
63  result->body = body;
64 
65  return (afw_value_t *)result;
66 }
67 
68 
69 /*
70  * Implementation of method optional_evaluate for interface afw_value.
71  */
72 const afw_value_t *
74  const afw_value_t * instance,
75  const afw_pool_t * p,
76  afw_xctx_t *xctx)
77 {
78  return instance;
79 }
80 
81 /*
82  * Implementation of method get_data_type for interface afw_value.
83  */
84 const afw_data_type_t *
85 impl_afw_value_get_data_type(
86  const afw_value_t * instance,
87  afw_xctx_t *xctx)
88 {
90 }
91 
92 /*
93  * Implementation of method get_evaluated_data_type for interface afw_value.
94  */
95 const afw_data_type_t *
96 impl_afw_value_get_evaluated_data_type(
97  const afw_value_t * instance,
98  afw_xctx_t *xctx)
99 {
100  return NULL;
101 }
102 
103 
104 /*
105  * Implementation of method get_evaluated_data_type_parameter for interface
106  * afw_value.
107  */
108 const afw_utf8_t *
109 impl_afw_value_get_evaluated_data_type_parameter(
110  const afw_value_t * instance,
111  afw_xctx_t *xctx)
112 {
113  return NULL;
114 }
115 
116 
117 static void
118 impl_decompile_type(
119  const afw_writer_t *writer,
120  const afw_value_type_t *type,
121  afw_xctx_t *xctx)
122 {
123  afw_writer_write_z(writer, "(", xctx);
124  if (type) {
125  if (type->data_type) {
126  afw_writer_write_utf8(writer,
127  &type->data_type->data_type_id, xctx);
128  if (type->data_type_parameter_contextual) {
129  afw_writer_write_z(writer, " ", xctx);
130  afw_writer_write(writer,
132  full_source->s +
135  xctx);
136  }
137  }
138  else {
139  afw_writer_write_z(writer, "any", xctx);
140  }
141  }
142  afw_writer_write_z(writer, ") ", xctx);
143 }
144 
145 
146 /*
147  * Implementation of method compiler_listing for interface afw_value.
148  */
149 void
150 impl_afw_value_produce_compiler_listing(
151  const afw_value_t *instance,
152  const afw_writer_t *writer,
153  afw_xctx_t *xctx)
154 {
156  (const afw_value_script_function_definition_t *)instance;
157  afw_size_t i;
158 
159  afw_value_compiler_listing_begin_value(writer, instance,
160  self->contextual, xctx);
161  afw_writer_write_z(writer, ": [", xctx);
162  afw_writer_write_eol(writer, xctx);
163  afw_writer_increment_indent(writer, xctx);
164 
165  afw_writer_write_z(writer, "returns: ", xctx);
166  impl_decompile_type(writer, self->returns, xctx);
167  afw_writer_write_eol(writer, xctx);
168 
169  for (i = 0; i < self->count; i++) {
170  afw_writer_write_z(writer, "parameter: ", xctx);
171  impl_decompile_type(writer, self->parameters[i]->type, xctx);
172  afw_writer_write_z(writer, " ", xctx);
173  afw_writer_write_utf8(writer, self->parameters[i]->name, xctx);
174  afw_writer_write_eol(writer, xctx);
175  }
176 
177  afw_value_produce_compiler_listing(self->body, writer, xctx);
178 
179  afw_writer_decrement_indent(writer, xctx);
180  afw_writer_write_z(writer, "]", xctx);
181  afw_writer_write_eol(writer, xctx);
182 }
183 
184 
185 /*
186  * Implementation of method decompile for interface afw_value.
187  */
188 void
189 impl_afw_value_decompile(
190  const afw_value_t * instance,
191  const afw_writer_t * writer,
192  afw_xctx_t *xctx)
193 {
195  (const afw_value_script_function_definition_t *)instance;
196  afw_size_t i;
197 
198  afw_writer_write_z(writer, "function ", xctx);
199  impl_decompile_type(writer, self->returns, xctx);
200 
201  afw_writer_write_z(writer, "(", xctx);
202  if (writer->tab) {
203  afw_writer_write_eol(writer, xctx);
204  afw_writer_increment_indent(writer, xctx);
205  }
206 
207  for (i = 0; i < self->count; i++) {
208  if (i != 0) {
209  afw_writer_write_z(writer, ",", xctx);
210  if (writer->tab) {
211  afw_writer_write_eol(writer, xctx);
212  }
213  }
214  impl_decompile_type(writer, self->parameters[i]->type, xctx);
215  afw_writer_write_utf8(writer, self->parameters[i]->name, xctx);
216  }
217  afw_writer_write_z(writer, ")", xctx);
218 
219  if (writer->tab) {
220  afw_writer_write_eol(writer, xctx);
221  afw_writer_decrement_indent(writer, xctx);
222  }
223 
224  afw_writer_write_z(writer, "(", xctx);
225  if (writer->tab) {
226  afw_writer_write_eol(writer, xctx);
227  afw_writer_increment_indent(writer, xctx);
228  }
229  afw_value_decompile(self->body, writer, xctx);
230  if (writer->tab) {
231  afw_writer_write_eol(writer, xctx);
232  afw_writer_decrement_indent(writer, xctx);
233  }
234  afw_writer_write_z(writer, ")", xctx);
235 }
236 
237 
238 /*
239  * Implementation of method get_info for interface afw_value.
240  */
241 void
242 impl_afw_value_get_info(
243  const afw_value_t *instance,
244  afw_value_info_t *info,
245  const afw_pool_t *p,
246  afw_xctx_t *xctx)
247 {
249  (const afw_value_script_function_definition_t *)instance;
250 
251  afw_memory_clear(info);
252  info->value_inf_id = &instance->inf->rti.implementation_id;
253  info->contextual = self->contextual;
254 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
afw_data_type_function
Data type struct for function.
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define afw_memory_clear(to)
Clear preallocated memory for sizeof(*(to)).
Definition: afw_memory.h:47
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
const afw_value_t * impl_afw_value_optional_evaluate(const afw_value_t *instance, const afw_pool_t *p, afw_xctx_t *xctx)
#define afw_value_produce_compiler_listing(instance, writer, xctx)
Call method produce_compiler_listing of interface afw_value.
#define afw_value_decompile(instance, writer, xctx)
Call method decompile of interface afw_value.
afw_value_script_function_definition_create(const afw_compile_value_contextual_t *contextual, const afw_value_type_t *returns, afw_size_t count, const afw_value_script_function_parameter_t **parameters, const afw_value_t *body, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for lambda definition value.
afw_value_script_function_definition_inf
Value lambda inf.
Definition: afw_value.h:273
#define afw_writer_increment_indent(instance, xctx)
Call method increment_indent of interface afw_writer.
#define afw_writer_write(instance, buffer, size, xctx)
Call method write of interface afw_writer.
#define afw_writer_write_eol(instance, xctx)
Call method write_eol of interface afw_writer.
#define afw_writer_decrement_indent(instance, xctx)
Call method decrement_indent of interface afw_writer.
#define afw_writer_write_z(writer, s_z, xctx)
Call afw_writer_write() with zero terminated string.
Definition: afw_writer.h:35
#define afw_writer_write_utf8(writer, S, xctx)
Call afw_writer_write() with a afw_utf8_t string.
Definition: afw_writer.h:45
Contextual information provided in some values.
afw_size_t value_size
Size in full_source of value source.
const afw_value_compiled_value_t * compiled_value
Compiled value this value is part of.
afw_size_t value_offset
Offset in full source of compiled value to this value.
Interface afw_data_type public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Filled in by afw_value get_info method.
Definition: afw_value.h:49
Interface afw_value public struct.
Struct for script function parameter.
Type meta (data type, data type parameters, and value meta object.
const afw_data_type_t * data_type
data type or NULL.
const afw_compile_value_contextual_t * data_type_parameter_contextual
contextual for data type parameter or NULL.
Interface afw_writer public struct.
Interface afw_xctx public struct.