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.
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("--", ("")),
150 mala_defined_parser (MalaEngine eng
,
151 MalaStringListNode_ref pptr
,
154 MalaStringListNode result
;
158 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
163 mala_engine_command_done (eng
, pptr
, 1);
165 // if (desc && 0 /*TODO mala_actiondesc_top (desc)*/)
166 // return MALA_SUCCESS;
173 mala_ifelse_parser (MalaEngine eng
,
174 MalaStringListNode_ref pptr
,
177 MalaStringListNode result
;
181 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
185 MALA_SIDEEFFECT_BEGIN
187 //int negated = mala_engine_negated_getclear (eng);
190 if (eng->state == MALA_LITERAL)
193 mala_engine_string_istrue (eng, mala_stringlistnode_string (result)))
194 eng->state = MALA_SUCCESS;
196 eng->state = MALA_FAILURE;
199 if (eng
->state
== MALA_SUCCESS
)
201 result
= mala_engine_arg_eval (eng
, pptr
, 2, MALA_MACRO
);
205 result
= mala_engine_arg_remove (eng
, pptr
, 3);
211 result
= mala_engine_arg_remove (eng
, pptr
, 2);
215 result
= mala_engine_arg_eval (eng
, pptr
, 3, MALA_MACRO
);
222 //if (eng->state == MALA_LITERAL)
223 // mala_engine_command_done (eng, pptr, 1);
225 mala_engine_command_done (eng
, pptr
, 1);
226 return MALA_CONTINUE
;
230 mala_pass_parser (MalaEngine eng
,
231 MalaStringListNode_ref pptr
,
237 // mala_stringlist_elem_delete_fwd (&eng->program, pptr);
243 mala_trace_parser (MalaEngine eng
,
244 MalaStringListNode_ref pptr
,
249 /*TODO trace levels*/
250 MALA_SIDEEFFECT_BEGIN
251 eng
->engine_trace
= eng
->negated
? MALA_NOTRACE
:MALA_TRACE
;
254 mala_engine_command_done (eng
, pptr
, 0);
255 return MALA_CONTINUE
;
260 mala_gctrace_parser (MalaEngine eng
,
261 MalaStringListNode_ref pptr
,
266 /*TODO trace levels*/
267 MALA_SIDEEFFECT_BEGIN
268 *eng
->gc_trace
= eng
->negated
? MALA_NOTRACE
:MALA_TRACE
;
271 mala_engine_command_done (eng
, pptr
, 0);
272 return MALA_CONTINUE
;
276 /*TODO to predicates*/
278 mala_predicate_greaterorequal_double (double * a
, double * b
)
284 mala_sleep_parser (MalaEngine eng
,
285 MalaStringListNode_ref pptr
,
294 if (mala_engine_arg_eval_fmt (eng
, pptr
, 1, "%lf", &duration
,
295 (MalaPredicate
) mala_predicate_greaterorequal_double
, &zero
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
);
308 mala_engine_command_done (eng
, pptr
, 1);
313 mala_literal_parser (MalaEngine eng
,
314 MalaStringListNode_ref 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
]);
330 //TODO --PROGRAM mala_stringlist_elem_delete_fwd (&eng->program, pptr);
335 mala_not_parser (MalaEngine eng
,
336 MalaStringListNode_ref pptr
,
339 MalaStringListNode result
;
343 MALA_SIDEEFFECT_BEGIN
344 eng
->negated
= !eng
->negated
;
347 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
351 mala_engine_command_done (eng
, pptr
, 1);
356 mala_exception_parser (MalaEngine eng
,
357 MalaStringListNode_ref pptr
,
360 // TODO needs better semantics --EXCEPTION n error -> --ERROR-error 1 .. n --HERE
364 mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
366 //TODO MALA_SIDEEFFECT_BEGIN
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
]);
373 if (eng
->state
> MALA_EFAULT
)
376 MalaString ex
= mala_string_new_print (&eng
->words
, NULL
, //TODO refs
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;
392 mala_foreach_word_parser (MalaEngine eng
,
393 MalaStringListNode_ref pptr
,
397 MalaStringListNode first
;
398 MalaStringListNode second
;
399 MalaStringListNode last
;
400 MalaStringListNode itr
;
403 /*TODO bugs, rename to --APPLY fixit etc*/
405 first
= mala_engine_arg_eval (eng
, pptr
, 1, -1, NULL
, NULL
);
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
);
418 last
= mala_stringlistnode_next (second
);
420 act
= mala_actiondesc_top ((MalaActionDesc
) mala_stringlistnode_user_get (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
,
431 mala_stringlistnode_string (itr
)))
433 if (!mala_stringlist_after_new (&eng
->program
,
435 mala_stringlistnode_string (first
)))
442 if (!mala_stringlist_after_new (&eng
->program
,
444 mala_stringlistnode_string (second
)))
446 if (!mala_stringlist_after_new (&eng
->program
,
448 mala_stringlistnode_string (first
)))
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);
462 for (itr
= mala_stringlistnode_next (second
);
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
480 reserve_string (char ** s
, size_t actual
, size_t needed
)
485 for (n
= actual
>64?actual
:64; n
<= needed
; n
+= (n
>>1)); /*n = n * 1.5*/
490 /* that was to much, try conservatively */
491 r
= realloc (*s
, n
= needed
);
502 mala_module_std_statements_init (MalaEngine self
)
504 return mala_engine_actions_register (self
, std_statements
);
510 // c-file-style: "gnu"
512 // arch-tag: 7e2dc784-1527-458a-8881-e87d3fb22cef