std shelf merge, (disabled many things) much to do
[mala.git] / std / std_statements.c
blobd0ce2794229e837bdc21ff2cbb628c86c540c937
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_statements.h"
28 static
29 mala_actioninit std_statements[] =
31 //MALA_PARSER_PASS("--PASS"),
32 //MALA_PARSER_BRIEF("--PASS","does nothing"),
33 //TODO MALA_PARSER_HELP("--PASS",("--HELP-ACTION","blah")),
34 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
35 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
36 // MALA_PARSER_RESULT_TYPE("--", ("")),
37 // MALA_PARSER_RESULT_USAGE("--", ("")),
41 MALA_PARSER("--", mala__parser, NULL, NULL, NULL),
42 MALA_PARSER_BRIEF("--", ""),
43 // MALA_PARSER_HELP("--", ("TODO")),
44 MALA_PARSER_SIGNATURE_TYPE("--", ("")),
45 MALA_PARSER_SIGNATURE_USAGE("--", ("")),
46 MALA_PARSER_RESULT_TYPE("--", ("")),
47 MALA_PARSER_RESULT_USAGE("--", ("")),
49 MALA_PARSER("--CHILDOF", mala__parser, NULL, NULL, NULL),
50 MALA_PARSER_BRIEF("--", ""),
51 // MALA_PARSER_HELP("--", ("TODO")),
52 MALA_PARSER_SIGNATURE_TYPE("--", ("")),
53 MALA_PARSER_SIGNATURE_USAGE("--", ("")),
54 MALA_PARSER_RESULT_TYPE("--", ("")),
55 MALA_PARSER_RESULT_USAGE("--", ("")),
60 //MALA_PARSER("--EXCEPTION", mala_exception_parser, NULL, NULL, NULL),
61 //MALA_PARSER_BRIEF("--EXCEPTION", "defines a new macro"),
62 // MALA_PARSER_HELP("--EXCEPTION", ("TODO")),
63 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
64 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
65 // MALA_PARSER_RESULT_TYPE("--", ("")),
66 // MALA_PARSER_RESULT_USAGE("--", ("")),
68 //TODO MALA_EXPAND_PARSER("--IFDEF", ("--IF", "--DEFINED")),
69 //MALA_PARSER_BRIEF("--IFDEF", "tests if a word is defined as macro"),
70 // MALA_PARSER_HELP("--NOT", ("TODO")),
71 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
72 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
73 // MALA_PARSER_RESULT_TYPE("--", ("")),
74 // MALA_PARSER_RESULT_USAGE("--", ("")),
76 //MALA_PARSER("--DEFINED", mala_defined_parser, NULL, NULL, NULL),
77 //MALA_PARSER_BRIEF("--DEFINED", "tests if a word is defined as macro"),
78 // MALA_PARSER_HELP("--NOT", ("TODO")),
79 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
80 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
81 // MALA_PARSER_RESULT_TYPE("--", ("")),
82 // MALA_PARSER_RESULT_USAGE("--", ("")),
84 //TODO MALA_MACRO_PARSER("--IF", ("--IF-ELSE", "%1", "%2", "--PASS")),
85 //MALA_PARSER_BRIEF("--IF", "executes some code if a condition succeeded"),
86 // MALA_PARSER_HELP("--NOT", ("TODO")),
87 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
88 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
89 // MALA_PARSER_RESULT_TYPE("--", ("")),
90 // MALA_PARSER_RESULT_USAGE("--", ("")),
92 //MALA_PARSER("--IF-ELSE", mala_ifelse_parser, NULL, NULL, NULL),
93 // MALA_PARSER_BRIEF("--ELSE", "executes some code when the former macro failed"),
94 // MALA_PARSER_HELP("--NOT", ("TODO")),
95 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
96 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
97 // MALA_PARSER_RESULT_TYPE("--", ("")),
98 // MALA_PARSER_RESULT_USAGE("--", ("")),
100 //MALA_PARSER("--NOT", mala_not_parser, NULL, NULL, NULL),
101 //MALA_PARSER_BRIEF("--NOT", "negates logic"),
102 // MALA_PARSER_HELP("--NOT", ("TODO")),
103 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
104 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
105 // MALA_PARSER_RESULT_TYPE("--", ("")),
106 // MALA_PARSER_RESULT_USAGE("--", ("")),
109 //MALA_PARSER("--LITERAL", mala_literal_parser, NULL, NULL, NULL),
110 //MALA_PARSER_BRIEF("--LITERAL", "Treats the next word literally"),
111 // MALA_PARSER_HELP("--LITERAL", ("TODO")),
112 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
113 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
114 // MALA_PARSER_RESULT_TYPE("--", ("")),
115 // MALA_PARSER_RESULT_USAGE("--", ("")),
118 //MALA_PARSER("--SLEEP",mala_sleep_parser, NULL, NULL, NULL),
119 //MALA_PARSER_BRIEF("--SLEEP", "delays execution for some time"),
120 //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")),
121 //TODO MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
122 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
123 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
125 //MALA_PARSER("--TRACE",mala_trace_parser, NULL, NULL, NULL),
126 //MALA_PARSER_BRIEF("--TRACE", "controls the trace mode of the MaLa VM"),
127 //MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
128 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
129 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
131 //MALA_PARSER("--GCTRACE",mala_gctrace_parser, NULL, NULL, NULL),
132 //MALA_PARSER_BRIEF("--GCTRACE", "controls the trace mode of the MaLa garbagge collector"),
133 //MALA_PARSER_SIGNATURE_USAGE("--SLEEP",("Seconds to sleep")),
134 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
135 //MALA_PARSER_SIGNATURE_TYPE("--SLEEP", ("DURATION")),
137 // MALA_PARSER("--EVAL",mala_eval_parser, NULL, NULL, NULL),
138 // MALA_PARSER_BRIEF("--EVAL",""),
139 // MALA_PARSER_HELP("--EVAL",("blah")),
140 // MALA_PARSER_SIGNATURE_TYPE("--", ("")),
141 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
142 // MALA_PARSER_RESULT_TYPE("--", ("")),
143 // MALA_PARSER_RESULT_USAGE("--", ("")),
146 MALA_ACTIONS_END
149 #if 0
152 mala_pass_parser (MalaProgram prg)
154 // mala_stringlist_elem_delete_fwd (&eng->program, pptr);
155 mala_program_action_done (prg, 0);
156 //return eng->state;
157 return MALA_CONTINUE;
162 mala_defined_parser (MalaEngine eng,
163 MalaStringListNode_ref pptr,
164 void * data)
166 MalaStringListNode result;
168 (void) data;
170 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
171 if (!result)
172 return eng->state;
175 mala_engine_command_done (eng, pptr, 1);
177 // if (desc && 0 /*TODO mala_actiondesc_top (desc)*/)
178 // return MALA_SUCCESS;
179 //else
180 return MALA_FAILURE;
185 mala_ifelse_parser (MalaEngine eng,
186 MalaStringListNode_ref pptr,
187 void * data)
189 MalaStringListNode result;
191 (void) data;
193 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
194 if (!result)
195 return eng->state;
197 MALA_SIDEEFFECT_BEGIN
199 //int negated = mala_engine_negated_getclear (eng);
201 /* TODO
202 if (eng->state == MALA_LITERAL)
204 if (negated ^
205 mala_engine_string_istrue (eng, mala_stringlistnode_string (result)))
206 eng->state = MALA_SUCCESS;
207 else
208 eng->state = MALA_FAILURE;
211 if (eng->state == MALA_SUCCESS)
213 result = mala_engine_arg_eval (eng, pptr, 2, MALA_MACRO);
214 if (!result)
215 return eng->state;
217 result = mala_engine_arg_remove (eng, pptr, 3);
218 if (!result)
219 return eng->state;
221 else
223 result = mala_engine_arg_remove (eng, pptr, 2);
224 if (!result)
225 return eng->state;
227 result = mala_engine_arg_eval (eng, pptr, 3, MALA_MACRO);
228 if (!result)
229 return eng->state;
232 MALA_SIDEEFFECT_END;
234 //if (eng->state == MALA_LITERAL)
235 // mala_engine_command_done (eng, pptr, 1);
236 //else
237 mala_engine_command_done (eng, pptr, 1);
238 return MALA_CONTINUE;
242 mala_trace_parser (MalaEngine eng,
243 MalaStringListNode_ref pptr,
244 void * data)
246 (void) data;
248 /*TODO trace levels*/
249 MALA_SIDEEFFECT_BEGIN
250 eng->engine_trace = eng->negated ? MALA_NOTRACE:MALA_TRACE;
251 MALA_SIDEEFFECT_END;
253 mala_engine_command_done (eng, pptr, 0);
254 return MALA_CONTINUE;
257 #if 0
259 mala_gctrace_parser (MalaEngine eng,
260 MalaStringListNode_ref pptr,
261 void * data)
263 (void) data;
265 /*TODO trace levels*/
266 MALA_SIDEEFFECT_BEGIN
267 *eng->gc_trace = eng->negated ? MALA_NOTRACE:MALA_TRACE;
268 MALA_SIDEEFFECT_END;
270 mala_engine_command_done (eng, pptr, 0);
271 return MALA_CONTINUE;
273 #endif
275 /*TODO to predicates*/
276 static int
277 mala_predicate_greaterorequal_double (double * a, double * b)
279 return *a >= *b;
283 mala_sleep_parser (MalaEngine eng,
284 MalaStringListNode_ref pptr,
285 void * data)
287 double duration;
288 double zero = 0.0;
289 struct timespec req;
291 (void) data;
293 if (mala_engine_arg_eval_fmt (eng, pptr, 1, "%lf", &duration,
294 (MalaPredicate) mala_predicate_greaterorequal_double, &zero
295 ) >= MALA_EXCEPTION)
296 return eng->state;
298 MALA_SIDEEFFECT_BEGIN
300 req.tv_sec = (time_t) duration;
301 req.tv_nsec = (long) 1000000000 * (duration - ((double) req.tv_sec));
303 (void) nanosleep (&req, NULL);
305 MALA_SIDEEFFECT_END;
307 mala_engine_command_done (eng, pptr, 1);
308 return MALA_SUCCESS;
312 mala_literal_parser (MalaEngine eng,
313 MalaStringListNode_ref pptr,
314 void * data)
316 (void) data;
317 (void) pptr;
319 MALA_SIDEEFFECT_BEGIN
321 #if 0 // TODO --PROGRAM
322 if (mala_stringlist_is_tail (&eng->program, *pptr))
323 return mala_engine_exception (eng, pptr, *pptr,
324 eng->common_string[MALA_STRING_ERROR_MISSING_ARGUMENT]);
325 #endif
327 MALA_SIDEEFFECT_END;
329 //TODO --PROGRAM mala_stringlist_elem_delete_fwd (&eng->program, pptr);
330 return MALA_LITERAL;
334 mala_not_parser (MalaEngine eng,
335 MalaStringListNode_ref pptr,
336 void * data)
338 MalaStringListNode result;
340 (void) data;
342 MALA_SIDEEFFECT_BEGIN
343 eng->negated = !eng->negated;
344 MALA_SIDEEFFECT_END;
346 result = mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
347 if (!result)
348 return eng->state;
350 mala_engine_command_done (eng, pptr, 1);
351 return eng->state;
355 mala_exception_parser (MalaEngine eng,
356 MalaStringListNode_ref pptr,
357 void * data)
359 // TODO needs better semantics --EXCEPTION n error -> --ERROR-error 1 .. n --HERE
361 (void) data;
363 mala_engine_arg_eval (eng, pptr, 1, MALA_LITERAL);
365 //TODO MALA_SIDEEFFECT_BEGIN
366 // {
367 #if 0 // TODO --PROGRAM
368 if (mala_stringlist_is_tail (&eng->program, *pptr))
369 return mala_engine_exception (eng, pptr, *pptr,
370 eng->common_string[MALA_STRING_ERROR_MISSING_ARGUMENT]);
371 #endif
372 if (eng->state > MALA_EFAULT)
373 return eng->state;
375 MalaString ex = mala_string_new_print (&eng->words, NULL, //TODO refs
376 "--ERROR-%s",
377 mala_stringlistnode_cstr (mala_stringlistnode_next (*pptr)));
379 // TODO mala_stringlist_elem_delete_fwd (&eng->program, pptr);
380 int state = mala_engine_exception (eng, pptr, *pptr, ex);
381 // TODO mala_stringlist_elem_delete (&eng->program, mala_stringlistnode_next (*pptr));
383 // TODO review mala_string_free (ex);
384 // MALA_SIDEEFFECT_END;
386 return state;
389 #if 0
391 mala_foreach_word_parser (MalaEngine eng,
392 MalaStringListNode_ref pptr,
393 void * data)
395 MalaAction act;
396 MalaStringListNode first;
397 MalaStringListNode second;
398 MalaStringListNode last;
399 MalaStringListNode itr;
400 (void) data;
402 /*TODO bugs, rename to --APPLY fixit etc*/
404 first = mala_engine_arg_eval (eng, pptr, 1, -1, NULL, NULL);
405 if (!first)
406 return eng->state;
407 act = mala_actiondesc_top ((MalaActionDesc) mala_stringlistnode_user_get (first));
408 // TODO allow blocks as first arg (define macro and delete it at later)
409 if (act && act->parser == mala_block_parser)
410 return mala_engine_exception (eng, pptr, first,
411 eng->common_string[MALA_STRING_ERROR_BLOCK_NOT_ALLOWED]);
413 second = mala_engine_arg_eval (eng, pptr, 2, -1, (MalaDataFactory) mala_stringlist_factory, NULL);
414 if (!second)
415 return eng->state;
417 last = mala_stringlistnode_next (second);
419 act = mala_actiondesc_top ((MalaActionDesc) mala_stringlistnode_user_get (second));
421 // expand second
422 if (eng->state != MALA_LITERAL)
423 for (itr = mala_stringlist_tail ((MalaStringList) act->data);
424 !mala_stringlist_is_end ((MalaStringList) act->data, itr);
425 mala_stringlistnode_rev (&itr))
427 #if 0 // TODO --PROGRAM
428 if (!mala_stringlist_after_new (&eng->program,
429 second,
430 mala_stringlistnode_string (itr)))
431 goto ealloc_node;
432 if (!mala_stringlist_after_new (&eng->program,
433 second,
434 mala_stringlistnode_string (first)))
435 goto ealloc_node;
436 #endif
438 else
440 #if 0
441 if (!mala_stringlist_after_new (&eng->program,
442 second,
443 mala_stringlistnode_string (second)))
444 goto ealloc_node;
445 if (!mala_stringlist_after_new (&eng->program,
446 second,
447 mala_stringlistnode_string (first)))
448 goto ealloc_node;
449 #endif
453 // was a block? delete it
454 if (act && act->parser == mala_block_parser)
455 mala_actiondesc_pop_delete (mala_stringlistnode_user_get (second));
457 mala_engine_command_done (eng, pptr, 2);
458 return MALA_SUCCESS;
460 ealloc_node:
461 for (itr = mala_stringlistnode_next (second);
462 itr != last;
463 mala_stringlistnode_fwd (&itr))
465 // mala_stringlist_elem_delete (&eng->program, itr);
468 return MALA_EALLOC; // TODO exception instead (needs pools, no allocation possible further)
475 realloc a string to at least needed size
476 return the amount really reserved or 0 on error
478 static size_t
479 reserve_string (char ** s, size_t actual, size_t needed)
481 size_t n;
482 char * r;
484 for (n = actual>64?actual:64; n <= needed; n += (n>>1)); /*n = n * 1.5*/
486 r = realloc (*s, n);
487 if (!r)
489 /* that was to much, try conservatively */
490 r = realloc (*s, n = needed);
491 if (!r)
492 return 0;
494 *s = r;
495 return n;
497 #endif
500 #endif
504 mala_module_std_statements_init (MalaEngine self)
506 return mala_engine_actions_register (self, std_statements);
510 // Local Variables:
511 // mode: C
512 // c-file-style: "gnu"
513 // End:
514 // arch-tag: 7e2dc784-1527-458a-8881-e87d3fb22cef
515 // end_of_file