helper.c

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 186 SCAR 0
CC
0
Cov
0%
CRAP 0
« TC.xs ↑ Summary tokenizer.c »
1 #ifndef _HELPER_C_
2 #define _HELPER_C_
3  
4 #include "helper.h"
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8  
9 0 char * call_load_parameterizable_type_class( char * class, char * word ) {
10  
11 0 dSP;
12 0 ENTER;
13 0 SAVETMPS;
14  
15 0 ▶ PUSHMARK( SP );
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK( SP ); 0 0 0
16  
17 0 ▶ XPUSHs( sv_2mortal( newSVpvn( class, strlen( class ) ) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( class, strlen( class ) ) ) ); 0 0 0
18 0 ▶ XPUSHs( sv_2mortal( newSVpvn( word, strlen( word ) ) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( word, strlen( word ) ) ) ); 0 0 0
19  
20 0 PUTBACK;
21  
22 0 const char * suffix = "::load_parameterizable_type_class\0";
23 0 char * path = malloc( strlen( class ) + strlen( suffix ) + 1 );
24  
25 0 strcpy( path, class );
26 0 strcat( path, suffix );
27  
28 0 int count = call_pv( path, G_SCALAR );
29  
30 0 free( path );
31  
32 0 SPAGAIN;
33  
34 0 ▶ if ( count != 1 ) croak( "Can't call load_parameterizable_type_class()\n" );
Branch: 1 branch
BranchTrueFalseTotal
if ( count != 1 ) croak( "Can't call load_parameterizable_type_class()\n" ); 0 0 0
35  
36 0 char * out = strdup( POPp );
37  
38 0 PUTBACK;
39 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
40 0 LEAVE;
41  
42 0 return out;
43 }
44  
45 static HV * token_to_perl( intptr_t token );
46  
47 0 AV * tokens_to_perl( my_stack_t * stack ) {
48  
49 0 ▶ if( stack == 0 ) croak( "Parser error\n" );
Branch: 1 branch
BranchTrueFalseTotal
if( stack == 0 ) croak( "Parser error\n" ); 0 0 0
50  
51 0 int size = stack -> size;
52  
53 0 AV * perl_tokens = newAV();
54  
55 0 ▶ for( int i = 0; i < size; ++i ) {
Branch: 1 branch
BranchTrueFalseTotal
for( int i = 0; i < size; ++i ) { 0 0 0
56  
57 0 HV * perl_token = token_to_perl( stack -> data[ i ] );
58  
59 0 av_push( perl_tokens, newRV_noinc( (SV*)perl_token ) );
60 }
61  
62 0 return perl_tokens;
63 }
64  
65 0 static inline void hash_set_sv( HV * hash, char * key, SV * value ) {
66  
67 0 hv_store( hash, key, strlen( key ), value, 0 );
68 0 }
69  
70 0 static inline void hash_set_str( HV * hash, char * key, char * value ) {
71  
72 0 hash_set_sv( hash, key, newSVpvn( value, strlen( value ) ) );
73 0 }
74  
75 0 static inline void hash_set_short( HV * hash, char * key, short value ) {
76  
77 0 hash_set_sv( hash, key, newSViv( value ) );
78 0 }
79  
80 0 static HV * token_to_perl( intptr_t token ) {
81  
82 0 HV * perl_token = newHV();
83 0 short token_type = ((abstract_type_t*)token) -> token_type;
84  
85 0 ▶ if( token_type == TOKEN_TYPE_BASIC ) {
Branch: 1 branch
BranchTrueFalseTotal
if( token_type == TOKEN_TYPE_BASIC ) { 0 0 0
86  
87 0 hash_set_str( perl_token, "type\0", ((basic_type_t*)token) -> type );
88  
89 0 ▶ } else if( token_type == TOKEN_TYPE_MAYBE ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_MAYBE ) { 0 0 0
90  
91 0 AV * stack = tokens_to_perl( ((maybe_type_t*)token) -> stack );
92 0 hash_set_sv( perl_token, "maybe\0", newRV_noinc( (SV*)stack ) );
93  
94 0 ▶ } else if( token_type == TOKEN_TYPE_PARAMETRIZABLE ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_PARAMETRIZABLE ) { 0 0 0
95  
96 0 hash_set_str( perl_token, "class\0", ((parameterizable_type_t*)token) -> class );
97  
98 0 AV * param = tokens_to_perl( ((parameterizable_type_t*)token) -> param );
99 0 hash_set_sv( perl_token, "param\0", newRV_noinc( (SV*)param ) );
100  
101 0 AV * base = tokens_to_perl( ((parameterizable_type_t*)token) -> stack );
102 0 hash_set_sv( perl_token, "base\0", newRV_noinc( (SV*)base ) );
103  
104 0 ▶ } else if( token_type == TOKEN_TYPE_SIGNED ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_SIGNED ) { 0 0 0
105  
106 0 HV * perl_param = newHV();
107  
108 0 hash_set_str( perl_param, "source\0", ((signed_type_t*)token) -> source );
109  
110 0 HV * type = token_to_perl( ((signed_type_t*)token) -> type );
111 0 hash_set_sv( perl_param, "type\0", newRV_noinc( (SV*)type ) );
112  
113 0 AV * signature = tokens_to_perl( ((signed_type_t*)token) -> signature );
114 0 hash_set_sv( perl_param, "signature\0", newRV_noinc( (SV*)signature ) );
115  
116 0 hash_set_sv( perl_token, "signed\0", newRV_noinc( (SV*)perl_param ) );
117  
118 0 ▶ } else if( token_type == TOKEN_TYPE_SIGNATURE_ITEM ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_SIGNATURE_ITEM ) { 0 0 0
119  
120 0 signature_param_t * param = ((signature_item_t*)token) -> param;
121  
122 0 AV * type = tokens_to_perl( ((signature_item_t*)token) -> type );
123 0 hash_set_sv( perl_token, "type\0", newRV_noinc( (SV*)type ) );
124  
125 0 HV * perl_param = newHV();
126  
127 0 hash_set_str( perl_param, "name\0", param -> name );
128 0 hash_set_short( perl_param, "named\0", param -> named );
129 0 hash_set_short( perl_param, "positional\0", param -> positional );
130 0 hash_set_short( perl_param, "required\0", param -> required );
131 0 hash_set_short( perl_param, "optional\0", param -> optional );
132  
133 0 hash_set_sv( perl_token, "param\0", newRV_noinc( (SV*)perl_param ) );
134  
135 0 ▶ } else if ( token_type == TOKEN_TYPE_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( token_type == TOKEN_TYPE_LENGTH ) { 0 0 0
136  
137 0 HV * perl_param = newHV();
138  
139 0 HV * type = token_to_perl( ((length_type_t*)token) -> type );
140 0 hash_set_sv( perl_param, "type\0", newRV_noinc( (SV*)type ) );
141  
142 0 ▶ if( ((length_type_t*)token) -> has_min == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ((length_type_t*)token) -> has_min == 1 ) { 0 0 0
143  
144 0 hash_set_str( perl_param, "min\0", ((length_type_t*)token) -> min );
145  
146 } else {
147  
148 0 hash_set_sv( perl_param, "min\0", newSVsv( &PL_sv_undef ) );
149 }
150  
151 0 ▶ if( ((length_type_t*)token) -> has_max == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ((length_type_t*)token) -> has_max == 1 ) { 0 0 0
152  
153 0 hash_set_str( perl_param, "max\0", ((length_type_t*)token) -> max );
154  
155 } else {
156  
157 0 hash_set_sv( perl_param, "max\0", newSVsv( &PL_sv_undef ) );
158 }
159  
160 0 hash_set_sv( perl_token, "length\0", newRV_noinc( (SV*)perl_param ) );
161 }
162  
163 0 return perl_token;
164 }
165  
166 0 static inline SV ** hash_get( HV * hash, char * key ) {
167  
168 0 return hv_fetch( hash, key, strlen( key ), 0 );
169 }
170  
171 0 tokenizer_options_t * perl_to_options( HV * options ) {
172  
173 0 tokenizer_options_t * out = malloc( sizeof( *out ) );
174  
175 0 SV ** perl_loose = hash_get( options, "loose\0" );
176  
177 0 ▶ if( perl_loose != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( perl_loose != 0 ) { 0 0 0
178  
179 0 out -> loose = SvIV( *perl_loose );
180  
181 } else {
182  
183 0 out -> loose = 0;
184 }
185  
186 0 return out;
187 }
188  
189 0 AV * mortalize_av( AV * v ) {
190  
191 0 return (AV*)sv_2mortal( (SV*)v );
192 }
193  
194 static void free_token( intptr_t token );
195  
196 0 void free_stack_arr( intptr_t * stack, int size ) {
197  
198 0 ▶ for( int i = 0; i < size; ++i ) {
Branch: 1 branch
BranchTrueFalseTotal
for( int i = 0; i < size; ++i ) { 0 0 0
199  
200 0 free_token( stack[ i ] );
201 }
202  
203 0 free( stack );
204 0 }
205  
206 0 void free_my_stack( my_stack_t * stack ) {
207  
208 0 int size = stack -> size;
209  
210 0 free_stack_arr( stack -> data, stack -> size );
211 0 free( stack );
212 0 }
213  
214 0 static void free_token( intptr_t token ) {
215  
216 0 short token_type = ((abstract_type_t*)token) -> token_type;
217  
218 0 ▶ if( token_type == TOKEN_TYPE_BASIC ) {
Branch: 1 branch
BranchTrueFalseTotal
if( token_type == TOKEN_TYPE_BASIC ) { 0 0 0
219  
220 0 free( ((basic_type_t*)token) -> type );
221 0 free((basic_type_t*)token);
222  
223 0 ▶ } else if( token_type == TOKEN_TYPE_MAYBE ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_MAYBE ) { 0 0 0
224  
225 0 free_my_stack( ((maybe_type_t*)token) -> stack );
226 0 free((maybe_type_t*)token);
227  
228 0 ▶ } else if( token_type == TOKEN_TYPE_PARAMETRIZABLE ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_PARAMETRIZABLE ) { 0 0 0
229  
230 0 free( ((parameterizable_type_t*)token) -> class );
231 0 free_my_stack( ((parameterizable_type_t*)token) -> param );
232 0 free_my_stack( ((parameterizable_type_t*)token) -> stack );
233 0 free((parameterizable_type_t*)token);
234  
235 0 ▶ } else if( token_type == TOKEN_TYPE_SIGNED ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_SIGNED ) { 0 0 0
236  
237 0 free( ((signed_type_t*)token) -> source );
238 0 free_token( ((signed_type_t*)token) -> type );
239 0 free_my_stack( ((signed_type_t*)token) -> signature );
240 0 free((signed_type_t*)token);
241  
242 0 ▶ } else if( token_type == TOKEN_TYPE_SIGNATURE_ITEM ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token_type == TOKEN_TYPE_SIGNATURE_ITEM ) { 0 0 0
243  
244 0 signature_param_t * param = ((signature_item_t*)token) -> param;
245  
246 0 free_my_stack( ((signature_item_t*)token) -> type );
247 0 free( param -> name );
248 0 free( param );
249 0 free((signature_item_t*)token);
250  
251 0 ▶ } else if ( token_type == TOKEN_TYPE_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( token_type == TOKEN_TYPE_LENGTH ) { 0 0 0
252  
253 0 free_token( ((length_type_t*)token) -> type );
254 0 ▶ if( ((length_type_t*)token) -> has_min == 1 ) free( ((length_type_t*)token) -> min );
Branch: 1 branch
BranchTrueFalseTotal
if( ((length_type_t*)token) -> has_min == 1 ) free( ((length_type_t*)token) -> min ); 0 0 0
255 0 ▶ if( ((length_type_t*)token) -> has_max == 1 ) free( ((length_type_t*)token) -> max );
Branch: 1 branch
BranchTrueFalseTotal
if( ((length_type_t*)token) -> has_max == 1 ) free( ((length_type_t*)token) -> max ); 0 0 0
256 0 free((length_type_t*)token);
257 }
258 0 }
259  
260 0 void p_die( char * s ) {
261  
262 0 croak( "%s", s );
263 }
264  
265 #endif /* end of include guard: _HELPER_C_ */
« TC.xs ↑ Summary tokenizer.c »