tests update
[mala.git] / engine / program.c
blobdcab2a4b30350c6cc054c1bf24f13af38c2be538
1 /*
2 program.c - MaLa Program container
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 <stdlib.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include "mala_types.h"
24 #include "program.h"
25 #include "engine.h"
26 #include "action.h"
27 #include "stringlist.h"
29 /* genreate a string array for the states */
30 #define MALA_STATE(s) #s
31 const char * mala_program_states [] = {MALA_STATES};
32 #undef MALA_STATE
35 void
36 mala_program_acogc_initize (void* o)
38 MalaProgram self = (MalaProgram)o;
39 self->program = NULL;
42 acogc_mark_result
43 mala_program_acogc_mark (void* o)
45 MalaProgram self = (MalaProgram)o;
46 acogc_object_lastmark (self->program);
47 return ACOGC_KEEP;
50 MalaProgram
51 mala_program_new_cstr (const char* cmd, MalaEngine engine)
53 ACOGC_STACK_ENTER (&engine->gcroot);
54 ACOGC_STACK_PTR (MalaProgram, self);
55 ACOGC_STACK_PTR (MalaStringList, elem);
57 self.ptr = acogc_factory_alloc (&engine->gcfactory_programs);
59 self.ptr->program = mala_stringlist_new (engine);
61 elem.ptr = mala_stringlist_new (engine);
62 elem.ptr->string = mala_string_new_cstr (cmd, engine->words);
63 llist_insert_tail (&self.ptr->program->node, &elem.ptr->node);
65 self.ptr->pptr = self.ptr->program;
67 self.ptr->engine = engine;
68 self.ptr->negated = 0;
69 self.ptr->state = MALA_START;
71 ACOGC_STACK_LEAVE;
72 return self.ptr;
75 mala_state
76 mala_program_run (MalaProgram self, mala_state dest)
78 TRACE_DBG (mala_program, "run pptr %p pptrnext %p prg %p", self->pptr, mala_stringlist_next (self->pptr), self->program);
80 while (mala_stringlist_next (self->pptr) != self->program &&
81 self->state > dest && self->state < MALA_EFAULT)
83 if (mala_program_step (self) == MALA_LITERAL)
85 mala_stringlist_fwd (&self->pptr);
89 TODO("set state to MALA_FINISH at the end of fully evaluated program");
91 return self->state;
94 mala_state
95 mala_program_eval_arg (MalaProgram self, int n, mala_state dest)
97 if (self->state > MALA_EFAULT)
98 return self->state;
100 MalaStringList opptr = self->pptr;
101 self->pptr = (MalaStringList) llist_get_nth_stop (&self->pptr->node, n, llist_get_prev (&self->program->node));
103 if (!self->pptr)
105 self->pptr = (MalaStringList) llist_get_prev (&self->program->node);
106 mala_program_commonexception (self, MALA_STRING_ERROR_MISSING_ARGUMENT);
109 self->state = mala_program_run (self, dest);
111 self->pptr = opptr;
112 return self->state;
115 mala_state
116 mala_program_step (MalaProgram self)
118 mala_state state = MALA_START;
120 TODO(" handle --LITERAL");
121 NOTICE (mala_program,"step");
123 MalaStringList pptr = mala_stringlist_next (self->pptr);
125 if (pptr->string && pptr->string->action)
127 MalaAction action = mala_program_pptr_action (self);
128 state = action->parser (self);
130 else
132 if (self->state == MALA_REMOVE)
134 mala_stringlist_remove (pptr);
136 else if (self->state == MALA_EXCEPTION)
138 WARN (mala, "EXCEPTION");
139 state = MALA_ENOACTION;
141 else
143 state = MALA_LITERAL;
147 if (self->state != MALA_REMOVE)
148 self->state = state;
150 return state;
154 // Local Variables:
155 // mode: C
156 // c-file-style: "gnu"
157 // End:
158 // arch-tag: fd8544bd-4144-44fe-8a9e-635aa0d3c393
159 // end_of_file