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.
25 #include "std_statements.h"
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("--", ("")),
152 mala_pass_parser (MalaProgram prg
)
154 // mala_stringlist_elem_delete_fwd (&eng->program, pptr);
155 mala_program_action_done (prg
, 0);
157 return MALA_CONTINUE
;
162 mala_defined_parser (MalaEngine eng
,
163 MalaStringListNode_ref pptr
,
166 MalaStringListNode result
;
170 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
175 mala_engine_command_done (eng
, pptr
, 1);
177 // if (desc && 0 /*TODO mala_actiondesc_top (desc)*/)
178 // return MALA_SUCCESS;
185 mala_ifelse_parser (MalaEngine eng
,
186 MalaStringListNode_ref pptr
,
189 MalaStringListNode result
;
193 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
197 MALA_SIDEEFFECT_BEGIN
199 //int negated = mala_engine_negated_getclear (eng);
202 if (eng->state == MALA_LITERAL)
205 mala_engine_string_istrue (eng, mala_stringlistnode_string (result)))
206 eng->state = MALA_SUCCESS;
208 eng->state = MALA_FAILURE;
211 if (eng
->state
== MALA_SUCCESS
)
213 result
= mala_engine_arg_eval (eng
, pptr
, 2, MALA_MACRO
);
217 result
= mala_engine_arg_remove (eng
, pptr
, 3);
223 result
= mala_engine_arg_remove (eng
, pptr
, 2);
227 result
= mala_engine_arg_eval (eng
, pptr
, 3, MALA_MACRO
);
234 //if (eng->state == MALA_LITERAL)
235 // mala_engine_command_done (eng, pptr, 1);
237 mala_engine_command_done (eng
, pptr
, 1);
238 return MALA_CONTINUE
;
242 mala_trace_parser (MalaEngine eng
,
243 MalaStringListNode_ref pptr
,
248 /*TODO trace levels*/
249 MALA_SIDEEFFECT_BEGIN
250 eng
->engine_trace
= eng
->negated
? MALA_NOTRACE
:MALA_TRACE
;
253 mala_engine_command_done (eng
, pptr
, 0);
254 return MALA_CONTINUE
;
259 mala_gctrace_parser (MalaEngine eng
,
260 MalaStringListNode_ref pptr
,
265 /*TODO trace levels*/
266 MALA_SIDEEFFECT_BEGIN
267 *eng
->gc_trace
= eng
->negated
? MALA_NOTRACE
:MALA_TRACE
;
270 mala_engine_command_done (eng
, pptr
, 0);
271 return MALA_CONTINUE
;
275 /*TODO to predicates*/
277 mala_predicate_greaterorequal_double (double * a
, double * b
)
283 mala_sleep_parser (MalaEngine eng
,
284 MalaStringListNode_ref pptr
,
293 if (mala_engine_arg_eval_fmt (eng
, pptr
, 1, "%lf", &duration
,
294 (MalaPredicate
) mala_predicate_greaterorequal_double
, &zero
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
);
307 mala_engine_command_done (eng
, pptr
, 1);
312 mala_literal_parser (MalaEngine eng
,
313 MalaStringListNode_ref 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
]);
329 //TODO --PROGRAM mala_stringlist_elem_delete_fwd (&eng->program, pptr);
334 mala_not_parser (MalaEngine eng
,
335 MalaStringListNode_ref pptr
,
338 MalaStringListNode result
;
342 MALA_SIDEEFFECT_BEGIN
343 eng
->negated
= !eng
->negated
;
346 result
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
350 mala_engine_command_done (eng
, pptr
, 1);
355 mala_exception_parser (MalaEngine eng
,
356 MalaStringListNode_ref pptr
,
359 // TODO needs better semantics --EXCEPTION n error -> --ERROR-error 1 .. n --HERE
363 mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
365 //TODO MALA_SIDEEFFECT_BEGIN
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
]);
372 if (eng
->state
> MALA_EFAULT
)
375 MalaString ex
= mala_string_new_print (&eng
->words
, NULL
, //TODO refs
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;
391 mala_foreach_word_parser (MalaEngine eng
,
392 MalaStringListNode_ref pptr
,
396 MalaStringListNode first
;
397 MalaStringListNode second
;
398 MalaStringListNode last
;
399 MalaStringListNode itr
;
402 /*TODO bugs, rename to --APPLY fixit etc*/
404 first
= mala_engine_arg_eval (eng
, pptr
, 1, -1, NULL
, NULL
);
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
);
417 last
= mala_stringlistnode_next (second
);
419 act
= mala_actiondesc_top ((MalaActionDesc
) mala_stringlistnode_user_get (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
,
430 mala_stringlistnode_string (itr
)))
432 if (!mala_stringlist_after_new (&eng
->program
,
434 mala_stringlistnode_string (first
)))
441 if (!mala_stringlist_after_new (&eng
->program
,
443 mala_stringlistnode_string (second
)))
445 if (!mala_stringlist_after_new (&eng
->program
,
447 mala_stringlistnode_string (first
)))
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);
461 for (itr
= mala_stringlistnode_next (second
);
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
479 reserve_string (char ** s
, size_t actual
, size_t needed
)
484 for (n
= actual
>64?actual
:64; n
<= needed
; n
+= (n
>>1)); /*n = n * 1.5*/
489 /* that was to much, try conservatively */
490 r
= realloc (*s
, n
= needed
);
504 mala_module_std_statements_init (MalaEngine self
)
506 return mala_engine_actions_register (self
, std_statements
);
512 // c-file-style: "gnu"
514 // arch-tag: 7e2dc784-1527-458a-8881-e87d3fb22cef