Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_value_expression_definition.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 Expression 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 
22 /* Inf specific is always data type. */
23 #define AFW_IMPLEMENTATION_SPECIFIC (const void *)&afw_data_type_expression_direct
24 
25 /* Define inf variables for data_type and fully_evaluated_data_type. */
26 #define AFW_IMPLEMENTATION_INF_VARIABLES \
27  (const void *)&afw_data_type_expression_direct, \
28  NULL
29 
30 #define impl_afw_value_get_evaluated_meta \
31  afw_value_internal_get_evaluated_meta_default
32 
33 #define impl_afw_value_get_evaluated_metas \
34  afw_value_internal_get_evaluated_metas_default
35 
36 /* Declares and rti/inf defines for interface afw_value */
37 #define AFW_IMPLEMENTATION_ID "expression_definition"
38 #define AFW_IMPLEMENTATION_INF_SPECIFIER AFW_DEFINE_CONST_DATA
39 #define AFW_IMPLEMENTATION_INF_LABEL afw_value_expression_definition_inf
41 
42 
43 /* Create function for expression definition value. */
44 AFW_DEFINE(const afw_value_t *)
46  const afw_compile_value_contextual_t *contextual,
47  const afw_value_assignment_t * const * assignments,
48  const afw_value_t * value,
49  const afw_pool_t *p,
50  afw_xctx_t *xctx)
51 {
53 
56  result->contextual = contextual;
57  result->assignments = assignments;
58  result->value = value;
59  return (afw_value_t *)result;
60 }
61 
62 
63 
64 /*
65  * Implementation of method optional_evaluate for interface afw_value.
66  */
67 const afw_value_t *
69  const afw_value_t * instance,
70  const afw_pool_t * p,
71  afw_xctx_t *xctx)
72 {
74  (const afw_value_expression_definition_t *)instance;
75 
76  const afw_value_assignment_t * const *assignment;
77  int local_top;
78  const afw_value_t *v;
79  const afw_value_t *result;
80  const afw_compile_value_contextual_t *saved_contextual;
81 
82  /* Push value on evaluation stack. */
84  saved_contextual = xctx->error->contextual;
85  xctx->error->contextual = self->contextual;
86 
87  /* Save stack top which will be restored on return. */
88  local_top = afw_xctx_begin_stack_frame(xctx);
89  result = NULL;
90  AFW_TRY {
91 
92  /* Push all assignments on to xctx stack. */
93  if (self->assignments) {
94  for (assignment = self->assignments; *assignment; assignment++) {
95  v = afw_value_evaluate((*assignment)->value, p, xctx);
96  afw_xctx_set_local_variable((*assignment)->name, v, xctx);
97  }
98  }
99 
100  /* Evaluate expression value. */
101  result = afw_value_evaluate(self->value, p, xctx);
102  }
103 
104  AFW_FINALLY{
105 
106  /* Restore xctx stack top to what it was on entry. */
107  afw_xctx_end_stack_frame(local_top, xctx);
108 
109  }
110 
111  AFW_ENDTRY;
112 
113  /* Pop value from evaluation stack and return result. */
115  xctx->error->contextual = saved_contextual;
116  return result;
117 }
118 
119 /*
120  * Implementation of method get_data_type for interface afw_value.
121  */
122 const afw_data_type_t *
123 impl_afw_value_get_data_type(
124  const afw_value_t * instance,
125  afw_xctx_t *xctx)
126 {
128 }
129 
130 /*
131  * Implementation of method get_evaluated_data_type for interface afw_value.
132  */
133 const afw_data_type_t *
134 impl_afw_value_get_evaluated_data_type(
135  const afw_value_t * instance,
136  afw_xctx_t *xctx)
137 {
138  return NULL;
139 }
140 
141 
142 /*
143  * Implementation of method get_evaluated_data_type_parameter for interface
144  * afw_value.
145  */
146 const afw_utf8_t *
147 impl_afw_value_get_evaluated_data_type_parameter(
148  const afw_value_t * instance,
149  afw_xctx_t *xctx)
150 {
151  return NULL;
152 }
153 
154 
155 
156 /*
157  * Implementation of method compiler_listing for interface afw_value.
158  */
159 void
160 impl_afw_value_produce_compiler_listing(
161  const afw_value_t *instance,
162  const afw_writer_t *writer,
163  afw_xctx_t *xctx)
164 {
166  (const afw_value_expression_definition_t *)instance;
167  const afw_value_assignment_t *const *a;
168 
169  afw_value_compiler_listing_begin_value(writer, instance,
170  self->contextual, xctx);
171  afw_writer_write_z(writer, ": [", xctx);
172  afw_writer_write_eol(writer, xctx);
173  afw_writer_increment_indent(writer, xctx);
174 
175  if (self->assignments) for (a = self->assignments; *a; a++) {
176  afw_writer_write_utf8(writer, (*a)->name, xctx);
177  afw_writer_write_z(writer, " = ", xctx);
178  afw_value_compiler_listing_value((*a)->value, writer, xctx);
179  }
180  afw_value_compiler_listing_value(self->value, writer, xctx);
181 
182  afw_writer_decrement_indent(writer, xctx);
183  afw_writer_write_z(writer, "]", xctx);
184  afw_writer_write_eol(writer, xctx);
185 }
186 
187 /*
188  * Implementation of method decompile for interface afw_value.
189  */
190 void
191 impl_afw_value_decompile(
192  const afw_value_t * instance,
193  const afw_writer_t * writer,
194  afw_xctx_t *xctx)
195 {
197  (const afw_value_expression_definition_t *)instance;
198  const afw_value_assignment_t * const *a;
199 
200  if (writer->tab) {
201  afw_writer_write_eol(writer, xctx);
202  }
203  if (self->assignments) for (a = self->assignments; *a; a++) {
204  afw_writer_write_utf8(writer, (*a)->name, xctx);
205  if (writer->tab) {
206  afw_writer_write_z(writer, " = ", xctx);
207  }
208  else {
209  afw_writer_write_z(writer, "=", xctx);
210  }
211  afw_value_decompile_value((*a)->value, writer, xctx);
212  afw_writer_write_z(writer, ",", xctx);
213  if (writer->tab) {
214  afw_writer_write_eol(writer, xctx);
215  }
216  }
217  afw_value_decompile_value(self->value, writer, xctx);
218  if (writer->tab) {
219  afw_writer_write_eol(writer, xctx);
220  }
221 }
222 
223 /*
224  * Implementation of method get_info for interface afw_value.
225  */
226 void
227 impl_afw_value_get_info(
228  const afw_value_t *instance,
229  afw_value_info_t *info,
230  const afw_pool_t *p,
231  afw_xctx_t *xctx)
232 {
234  (const afw_value_expression_definition_t *)instance;
235 
236  afw_memory_clear(info);
237  info->value_inf_id = &instance->inf->rti.implementation_id;
238  info->contextual = self->contextual;
239 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
afw_data_type_expression
Data type struct for expression.
#define AFW_FINALLY
Always executed regardless of error.
Definition: afw_error.h:702
#define AFW_ENDTRY
Ends an AFW try block.
Definition: afw_error.h:727
#define AFW_TRY
Begin an AFW TRY block.
Definition: afw_error.h:634
#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)
afw_value_expression_definition_create(const afw_compile_value_contextual_t *contextual, const afw_value_assignment_t *const *assignments, const afw_value_t *value, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for expression definition value.
#define afw_value_evaluate(value, p, xctx)
Evaluate value if needed using specific pool.
Definition: afw_value.h:841
afw_value_expression_definition_inf
Value expression inf.
Definition: afw_value.h:255
afw_value_decompile_value(const afw_value_t *instance, const afw_writer_t *writer, afw_xctx_t *xctx)
Decompile Value::.
#define afw_writer_increment_indent(instance, xctx)
Call method increment_indent 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
int afw_xctx_begin_stack_frame(afw_xctx_t *xctx)
Begin stack frame.
Definition: afw_xctx.h:329
afw_xctx_set_local_variable(const afw_utf8_t *name, const afw_value_t *value, afw_xctx_t *xctx)
Set a variable then current xctx frame.
Definition: afw_xctx.c:289
#define afw_xctx_evaluation_stack_push_value(VALUE, xctx)
Push VALUE onto execution stack.
Definition: afw_xctx.h:78
#define afw_xctx_evaluation_stack_pop_value(xctx)
Pop top VALUE off execution stack.
Definition: afw_xctx.h:113
void afw_xctx_end_stack_frame(int top, afw_xctx_t *xctx)
Set stack top.
Definition: afw_xctx.h:343
Contextual information provided in some values.
Interface afw_data_type public struct.
const afw_compile_value_contextual_t * contextual
Contextual information or NULL.
Definition: afw_error.h:83
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Struct for value assignment.
Struct for expression value.
Filled in by afw_value get_info method.
Definition: afw_value.h:49
Interface afw_value public struct.
Interface afw_writer public struct.
Interface afw_xctx public struct.