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.
23 #include "mala_types.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
};
36 mala_program_acogc_initize (void* o
)
38 MalaProgram self
= (MalaProgram
)o
;
43 mala_program_acogc_mark (void* o
)
45 MalaProgram self
= (MalaProgram
)o
;
46 acogc_object_lastmark (self
->program
);
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
;
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");
95 mala_program_eval_arg (MalaProgram self
, int n
, mala_state dest
)
97 if (self
->state
> MALA_EFAULT
)
100 MalaStringList opptr
= self
->pptr
;
101 self
->pptr
= (MalaStringList
) llist_get_nth_stop (&self
->pptr
->node
, n
, llist_get_prev (&self
->program
->node
));
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
);
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
);
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
;
143 state
= MALA_LITERAL
;
147 if (self
->state
!= MALA_REMOVE
)
156 // c-file-style: "gnu"
158 // arch-tag: fd8544bd-4144-44fe-8a9e-635aa0d3c393