mark function returns collection state
[mala.git] / std / std_statements.c
blob8d458639cb02b56d1c9c707bb62f2827bda909a5
1 /*
2 std_statements.c - MaLa statement like parsers
4 Copyright (C) 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
24 #include "mala.h"
25 #include "std.h"
28 static
29 mala_actioninit std_statements[] =
32 MALA_PARSER("--", mala__parser, NULL, NULL, NULL),
33 MALA_PARSER_BRIEF("--", ""),
34 // MALA_PARSER_HELP("--", ("TODO")),
35 MALA_PARSER_SIGNATURE_TYPE("--", ("")),
36 MALA_PARSER_SIGNATURE_USAGE("--", ("")),
37 MALA_PARSER_RESULT_TYPE("--", ("")),
38 MALA_PARSER_RESULT_USAGE("--", ("")),
40 MALA_PARSER("--CHILDOF", mala__parser, NULL, NULL, NULL),
41 MALA_PARSER_BRIEF("--", ""),
42 // MALA_PARSER_HELP("--", ("TODO")),
43 MALA_PARSER_SIGNATURE_TYPE("--", ("")),
44 MALA_PARSER_SIGNATURE_USAGE("--", ("")),
45 MALA_PARSER_RESULT_TYPE("--", ("")),
46 MALA_PARSER_RESULT_USAGE("--", ("")),
51 MALA_PARSER("--EXCEPTION", mala_exception_parser, NULL, NULL, NULL),
52 MALA_PARSER_BRIEF("--EXCEPTION", "defines a new macro"),
53 // MALA_PARSER_HELP("--EXCEPTION", ("TODO")),
54 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
55 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
56 // MALA_PARSER_RESULT_TYPE("--", ("")),
57 // MALA_PARSER_RESULT_USAGE("--", ("")),
59 //TODO MALA_EXPAND_PARSER("--IFDEF", ("--IF", "--DEFINED")),
60 MALA_PARSER_BRIEF("--IFDEF", "tests if a word is defined as macro"),
61 // MALA_PARSER_HELP("--NOT", ("TODO")),
62 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
63 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
64 // MALA_PARSER_RESULT_TYPE("--", ("")),
65 // MALA_PARSER_RESULT_USAGE("--", ("")),
67 MALA_PARSER("--DEFINED", mala_defined_parser, NULL, NULL, NULL),
68 MALA_PARSER_BRIEF("--DEFINED", "tests if a word is defined as macro"),
69 // MALA_PARSER_HELP("--NOT", ("TODO")),
70 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
71 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
72 // MALA_PARSER_RESULT_TYPE("--", ("")),
73 // MALA_PARSER_RESULT_USAGE("--", ("")),
75 //TODO MALA_MACRO_PARSER("--IF", ("--IF-ELSE", "%1", "%2", "--PASS")),
76 MALA_PARSER_BRIEF("--IF", "executes some code if a condition succeeded"),
77 // MALA_PARSER_HELP("--NOT", ("TODO")),
78 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
79 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
80 // MALA_PARSER_RESULT_TYPE("--", ("")),
81 // MALA_PARSER_RESULT_USAGE("--", ("")),
83 MALA_PARSER("--IF-ELSE", mala_ifelse_parser, NULL, NULL, NULL),
84 // MALA_PARSER_BRIEF("--ELSE", "executes some code when the former macro failed"),
85 // MALA_PARSER_HELP("--NOT", ("TODO")),
86 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
87 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
88 // MALA_PARSER_RESULT_TYPE("--", ("")),
89 // MALA_PARSER_RESULT_USAGE("--", ("")),
91 MALA_PARSER("--NOT", mala_not_parser, NULL, NULL, NULL),
92 MALA_PARSER_BRIEF("--NOT", "negates logic"),
93 // MALA_PARSER_HELP("--NOT", ("TODO")),
94 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
95 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
96 // MALA_PARSER_RESULT_TYPE("--", ("")),
97 // MALA_PARSER_RESULT_USAGE("--", ("")),
100 MALA_PARSER("--LITERAL", mala_literal_parser, NULL, NULL, NULL),
101 MALA_PARSER_BRIEF("--LITERAL", "Treats the next word literally"),
102 // MALA_PARSER_HELP("--LITERAL", ("TODO")),
103 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
104 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
105 // MALA_PARSER_RESULT_TYPE("--", ("")),
106 // MALA_PARSER_RESULT_USAGE("--", ("")),
109 MALA_PARSER("--SLEEP",mala_sleep_parser, NULL, NULL, NULL),
110 MALA_PARSER_BRIEF("--SLEEP", "delays execution for some time"),
111 //TODO MALA_PARSER_HELP("--SLEEP", ("The program will be suspended for some time, the time might be specified as fraction of seconds\t example: 9.5")),
112 //TODO MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
113 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
114 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
116 MALA_PARSER("--TRACE",mala_trace_parser, NULL, NULL, NULL),
117 MALA_PARSER_BRIEF("--TRACE", "controls the trace mode of the MaLa VM"),
118 //MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
119 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
120 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
122 MALA_PARSER("--GCTRACE",mala_gctrace_parser, NULL, NULL, NULL),
123 MALA_PARSER_BRIEF("--GCTRACE", "controls the trace mode of the MaLa garbagge collector"),
124 //MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
125 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
126 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
128 // MALA_PARSER("--EVAL",mala_eval_parser, NULL, NULL, NULL),
129 // MALA_PARSER_BRIEF("--EVAL",""),
130 // MALA_PARSER_HELP("--EVAL",("blah")),
131 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
132 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
133 // MALA_PARSER_RESULT_TYPE("--", ("")),
134 // MALA_PARSER_RESULT_USAGE("--", ("")),
136 MALA_PASS_PARSER("--PASS"),
137 MALA_PARSER_BRIEF("--PASS","does nothing"),
138 //TODO MALA_PARSER_HELP("--PASS",("--HELP-ACTION","blah")),
139 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
140 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
141 // MALA_PARSER_RESULT_TYPE("--", ("")),
142 // MALA_PARSER_RESULT_USAGE("--", ("")),
144 MALA_ACTIONS_END
150 mala_defined_parser (MalaEngine eng,
151 MalaStringListNode_ref pptr,
152 void * data)
154 MalaStringListNode result;
156 (void) data;
158 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
159 if (!result)
160 return eng->state;
163 mala_engine_command_done (eng, pptr, 1);
165 // if (desc && 0 /*TODO mala_actiondesc_top (desc)*/)
166 // return MALA_SUCCESS;
167 //else
168 return MALA_FAILURE;
173 mala_ifelse_parser (MalaEngine eng,
174 MalaStringListNode_ref pptr,
175 void * data)
177 MalaStringListNode result;
179 (void) data;
181 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
182 if (!result)
183 return eng->state;
185 MALA_SIDEEFFECT_BEGIN
187 //int negated = mala_engine_negated_getclear (eng);
189 /* TODO
190 if (eng->state == MALA_LITERAL)
192 if (negated ^
193 mala_engine_string_istrue (eng, mala_stringlistnode_string (result)))
194 eng->state = MALA_SUCCESS;
195 else
196 eng->state = MALA_FAILURE;
199 if (eng->state == MALA_SUCCESS)
201 result = mala_engine_arg_eval (eng, pptr, 2, MALA_MACRO);
202 if (!result)
203 return eng->state;
205 result = mala_engine_arg_remove (eng, pptr, 3);
206 if (!result)
207 return eng->state;
209 else
211 result = mala_engine_arg_remove (eng, pptr, 2);
212 if (!result)
213 return eng->state;
215 result = mala_engine_arg_eval (eng, pptr, 3, MALA_MACRO);
216 if (!result)
217 return eng->state;
220 MALA_SIDEEFFECT_END;
222 //if (eng->state == MALA_LITERAL)
223 // mala_engine_command_done (eng, pptr, 1);
224 //else
225 mala_engine_command_done (eng, pptr, 1);
226 return MALA_CONTINUE;
230 mala_pass_parser (MalaEngine eng,
231 MalaStringListNode_ref pptr,
232 void * data)
234 (void) pptr;
235 (void) data;
237 // mala_stringlist_elem_delete_fwd (&eng->program, pptr);
239 return eng->state;
243 mala_trace_parser (MalaEngine eng,
244 MalaStringListNode_ref pptr,
245 void * data)
247 (void) data;
249 /*TODO trace levels*/
250 MALA_SIDEEFFECT_BEGIN
251 eng->engine_trace = eng->negated ? MALA_NOTRACE:MALA_TRACE;
252 MALA_SIDEEFFECT_END;
254 mala_engine_command_done (eng, pptr, 0);
255 return MALA_CONTINUE;
258 #if 0
260 mala_gctrace_parser (MalaEngine eng,
261 MalaStringListNode_ref pptr,
262 void * data)
264 (void) data;
266 /*TODO trace levels*/
267 MALA_SIDEEFFECT_BEGIN
268 *eng->gc_trace = eng->negated ? MALA_NOTRACE:MALA_TRACE;
269 MALA_SIDEEFFECT_END;
271 mala_engine_command_done (eng, pptr, 0);
272 return MALA_CONTINUE;
274 #endif
276 /*TODO to predicates*/
277 static int
278 mala_predicate_greaterorequal_double (double * a, double * b)
280 return *a >= *b;
284 mala_sleep_parser (MalaEngine eng,
285 MalaStringListNode_ref pptr,
286 void * data)
288 double duration;
289 double zero = 0.0;
290 struct timespec req;
292 (void) data;
294 if (mala_engine_arg_eval_fmt (eng, pptr, 1, "%lf", &duration,
295 (MalaPredicate) mala_predicate_greaterorequal_double, &zero
296 ) >= MALA_EXCEPTION)
297 return eng->state;
299 MALA_SIDEEFFECT_BEGIN
301 req.tv_sec = (time_t) duration;
302 req.tv_nsec = (long) 1000000000 * (duration - ((double) req.tv_sec));
304 (void) nanosleep (&req, NULL);
306 MALA_SIDEEFFECT_END;
308 mala_engine_command_done (eng, pptr, 1);
309 return MALA_SUCCESS;
313 mala_literal_parser (MalaEngine eng,
314 MalaStringListNode_ref pptr,
315 void * data)
317 (void) data;
318 (void) pptr;
320 MALA_SIDEEFFECT_BEGIN
322 #if 0 // TODO --PROGRAM
323 if (mala_stringlist_is_tail (&eng->program, *pptr))
324 return mala_engine_exception (eng, pptr, *pptr,
325 eng->common_string[MALA_STRING_ERROR_MISSING_ARGUMENT]);
326 #endif
328 MALA_SIDEEFFECT_END;
330 //TODO --PROGRAM mala_stringlist_elem_delete_fwd (&eng->program, pptr);
331 return MALA_LITERAL;
335 mala_not_parser (MalaEngine eng,
336 MalaStringListNode_ref pptr,
337 void * data)
339 MalaStringListNode result;
341 (void) data;
343 MALA_SIDEEFFECT_BEGIN
344 eng->negated = !eng->negated;
345 MALA_SIDEEFFECT_END;
347 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
348 if (!result)
349 return eng->state;
351 mala_engine_command_done (eng, pptr, 1);
352 return eng->state;
356 mala_exception_parser (MalaEngine eng,
357 MalaStringListNode_ref pptr,
358 void * data)
360 // TODO needs better semantics --EXCEPTION n error -> --ERROR-error 1 .. n --HERE
362 (void) data;
364 mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
366 //TODO MALA_SIDEEFFECT_BEGIN
367 // {
368 #if 0 // TODO --PROGRAM
369 if (mala_stringlist_is_tail (&eng->program, *pptr))
370 return mala_engine_exception (eng, pptr, *pptr,
371 eng->common_string[MALA_STRING_ERROR_MISSING_ARGUMENT]);
372 #endif
373 if (eng->state > MALA_EFAULT)
374 return eng->state;
376 MalaString ex = mala_string_new_print (&eng->words, NULL, //TODO refs
377 "--ERROR-%s",
378 mala_stringlistnode_cstr (mala_stringlistnode_next (*pptr)));
380 // TODO mala_stringlist_elem_delete_fwd (&eng->program, pptr);
381 int state = mala_engine_exception (eng, pptr, *pptr, ex);
382 // TODO mala_stringlist_elem_delete (&eng->program, mala_stringlistnode_next (*pptr));
384 // TODO review mala_string_free (ex);
385 // MALA_SIDEEFFECT_END;
387 return state;
390 #if 0
392 mala_foreach_word_parser (MalaEngine eng,
393 MalaStringListNode_ref pptr,
394 void * data)
396 MalaAction act;
397 MalaStringListNode first;
398 MalaStringListNode second;
399 MalaStringListNode last;
400 MalaStringListNode itr;
401 (void) data;
403 /*TODO bugs, rename to --APPLY fixit etc*/
405 first = mala_engine_arg_eval (eng, pptr, 1, -1, NULL, NULL);
406 if (!first)
407 return eng->state;
408 act = mala_actiondesc_top ((MalaActionDesc) mala_stringlistnode_user_get (first));
409 // TODO allow blocks as first arg (define macro and delete it at later)
410 if (act && act->parser == mala_block_parser)
411 return mala_engine_exception (eng, pptr, first,
412 eng->common_string[MALA_STRING_ERROR_BLOCK_NOT_ALLOWED]);
414 second = mala_engine_arg_eval (eng, pptr, 2, -1, (MalaDataFactory) mala_stringlist_factory, NULL);
415 if (!second)
416 return eng->state;
418 last = mala_stringlistnode_next (second);
420 act = mala_actiondesc_top ((MalaActionDesc) mala_stringlistnode_user_get (second));
422 // expand second
423 if (eng->state != MALA_LITERAL)
424 for (itr = mala_stringlist_tail ((MalaStringList) act->data);
425 !mala_stringlist_is_end ((MalaStringList) act->data, itr);
426 mala_stringlistnode_rev (&itr))
428 #if 0 // TODO --PROGRAM
429 if (!mala_stringlist_after_new (&eng->program,
430 second,
431 mala_stringlistnode_string (itr)))
432 goto ealloc_node;
433 if (!mala_stringlist_after_new (&eng->program,
434 second,
435 mala_stringlistnode_string (first)))
436 goto ealloc_node;
437 #endif
439 else
441 #if 0
442 if (!mala_stringlist_after_new (&eng->program,
443 second,
444 mala_stringlistnode_string (second)))
445 goto ealloc_node;
446 if (!mala_stringlist_after_new (&eng->program,
447 second,
448 mala_stringlistnode_string (first)))
449 goto ealloc_node;
450 #endif
454 // was a block? delete it
455 if (act && act->parser == mala_block_parser)
456 mala_actiondesc_pop_delete (mala_stringlistnode_user_get (second));
458 mala_engine_command_done (eng, pptr, 2);
459 return MALA_SUCCESS;
461 ealloc_node:
462 for (itr = mala_stringlistnode_next (second);
463 itr != last;
464 mala_stringlistnode_fwd (&itr))
466 // mala_stringlist_elem_delete (&eng->program, itr);
469 return MALA_EALLOC; // TODO exception instead (needs pools, no allocation possible further)
476 realloc a string to at least needed size
477 return the amount really reserved or 0 on error
479 static size_t
480 reserve_string (char ** s, size_t actual, size_t needed)
482 size_t n;
483 char * r;
485 for (n = actual>64?actual:64; n <= needed; n += (n>>1)); /*n = n * 1.5*/
487 r = realloc (*s, n);
488 if (!r)
490 /* that was to much, try conservatively */
491 r = realloc (*s, n = needed);
492 if (!r)
493 return 0;
495 *s = r;
496 return n;
498 #endif
502 mala_module_std_statements_init (MalaEngine self)
504 return mala_engine_actions_register (self, std_statements);
508 // Local Variables:
509 // mode: C
510 // c-file-style: "gnu"
511 // End:
512 // arch-tag: 7e2dc784-1527-458a-8881-e87d3fb22cef
513 // end_of_file