summaryrefslogtreecommitdiff
path: root/obcl
diff options
context:
space:
mode:
authorMarius Nita <marius@cs.pdx.edu>2003-04-15 00:11:57 +0000
committerMarius Nita <marius@cs.pdx.edu>2003-04-15 00:11:57 +0000
commitf8c712f53470fd651dbbc785f71045645cbcd8ed (patch)
treeb33011b2557add73db5091ba5808845bb18563b7 /obcl
parente6d1ab217f53fcafe418a75ce184463c6e36e31b (diff)
init commit, tree processing code bummy
Diffstat (limited to 'obcl')
-rw-r--r--obcl/process.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/obcl/process.c b/obcl/process.c
new file mode 100644
index 00000000..f79fc631
--- /dev/null
+++ b/obcl/process.c
@@ -0,0 +1,105 @@
+#include "obcl.h"
+
+static void cl_proc_intern_handler(CLNode *node)
+{
+ CL_ASSERT_NODE(node);
+ g_warning("Unhandled node %s on line %d\n",
+ CL_ID(node), CL_LINE(node));
+}
+
+static CLProcHandler *default_handler(void)
+{
+ static CLProcHandler *ph = 0;
+ if (!ph)
+ ph = cl_proc_handler_new_func(cl_proc_intern_handler);
+ return ph;
+}
+
+CLProcHandler *cl_proc_handler_new_func(CLProcFunc f)
+{
+ CLProcHandler *cph = g_new(CLProcHandler,1);
+ cph->type = CLPROC_FUNC;
+ cph->u.func = f;
+ return cph;
+}
+
+CLProcHandler *cl_proc_handler_new_proc(CLProc *cp)
+{
+ CLProcHandler *cph = g_new(CLProcHandler,1);
+ cph->type = CLPROC_PROC;
+ cph->u.proc = cp;
+ return cph;
+}
+
+CLProc *cl_proc_new(void)
+{
+ CLProc *ret = g_new(CLProc,1);
+ ret->table = g_hash_table_new(g_str_hash,g_str_equal);
+ ret->default_h = default_handler();
+ return ret;
+}
+
+void cl_proc_free(CLProc *proc)
+{
+
+}
+
+void cl_proc_add_handler(CLProc *proc, gchar *str,
+ CLProcHandler *handler)
+{
+ g_assert(proc != NULL);
+ g_hash_table_replace(proc->table, str, handler);
+}
+
+void cl_proc_add_handler_func(CLProc *proc, gchar *str,
+ CLProcFunc func)
+{
+ CLProcHandler *ph;
+
+ g_assert(proc != NULL);
+ ph = cl_proc_handler_new_func(func);
+ cl_proc_add_handler(proc, str, ph);
+}
+
+void cl_proc_set_default(CLProc *proc, CLProcHandler *ph)
+{
+ g_assert(proc != NULL);
+ proc->default_h = ph;
+}
+
+void cl_proc_register_keywords(CLProc *proc, ...)
+{
+ va_list args;
+ g_assert(proc != NULL);
+
+ va_start(args,proc);
+ for (;;) {
+ gchar *k = va_arg(args, gchar*);
+ if (k == NULL)
+ break;
+ if (g_hash_table_lookup(proc->table, k) != NULL)
+ g_hash_table_insert(proc->table, k, default_handler());
+ }
+ va_end(args);
+}
+
+void cl_process(GList *tree, CLProc *proc)
+{
+ GList *lst;
+ CLProcHandler *handler;
+
+ g_assert(proc != NULL);
+
+ if (!tree) return;
+
+ for (lst = tree; lst != NULL; lst = lst->next) {
+ CL_ASSERT_NODE(lst->data);
+ handler = g_hash_table_lookup(proc->table, CL_ID(lst->data));
+ if (!handler)
+ handler = default_handler();
+ if (handler->type == CLPROC_FUNC)
+ handler->u.func(CL_NODE(lst->data));
+ else
+ cl_process(CL_BLOCK(lst->data), handler->u.proc);
+ }
+}