From 33ec161b45c3436f43268e264dcd6d4c2b2737da Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 28 Feb 2007 23:00:54 +0100 Subject: [PATCH] basics for new error handling --- engine/program.c | 73 ++++++++++++++++++++++---------------------------------- engine/program.h | 29 +++++++++++----------- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/engine/program.c b/engine/program.c index 050d6c2..8dd95fd 100644 --- a/engine/program.c +++ b/engine/program.c @@ -87,15 +87,15 @@ mala_program_run (MalaProgram self, mala_state dest) mala_state state = MALA_START; if (++self->nesting_level >= self->nesting_limit) - state = mala_program_commonexception (self, MALA_STRING_ERROR_NESTING_LIMIT_EXCEED); + state = mala_program_commonexception (self, MALA_STRING_ERROR_NESTING_LIMIT_EXCEED, mala_program_pptr (self)); if (self->state == MALA_REMOVE) dest = MALA_REMOVE; else if (dest == MALA_REMOVE) self->state = MALA_REMOVE; - while (mala_stringlist_next (self->pptr) != self->program && - state > dest && state < MALA_EFAULT) + while (mala_program_pptr (self) != self->program && + state > dest && state < MALA_EXCEPTION) { state = mala_program_step (self); if (state == MALA_LITERAL) @@ -103,7 +103,7 @@ mala_program_run (MalaProgram self, mala_state dest) if (dest != MALA_REMOVE) mala_stringlist_fwd (&self->pptr); else - mala_stringlist_next (self->pptr)->string = self->engine->common_string[MALA_STRING_PASS]; + mala_program_pptr (self)->string = self->engine->common_string[MALA_STRING_PASS]; } } @@ -115,9 +115,8 @@ mala_program_run (MalaProgram self, mala_state dest) } mala_state -mala_program_eval_arg (MalaProgram self, int n, mala_state dest, MalaStringList_ref dst) +mala_program_eval_arg (MalaProgram self, int n, mala_state final, MalaStringList_ref node) { - TRACE (mala_program, "%d %s", n, mala_engine_states[dest]); if (self->state > MALA_EFAULT) return self->state; @@ -126,18 +125,23 @@ mala_program_eval_arg (MalaProgram self, int n, mala_state dest, MalaStringList_ MalaStringList pptr = (MalaStringList) llist_get_nth_stop (&self->pptr->node, n, llist_get_prev (&self->program->node)); + + TRACE (mala_program, "eval arg %d '%s' to %s", n, + pptr?mala_stringlist_cstr (pptr):"(missing)", + mala_engine_states[final]); + if (pptr) { mala_state ostate = self->state; self->pptr = pptr; - if (dst) - *dst = pptr; + if (node) + *node = pptr; - mala_program_run (self, dest); + mala_program_run (self, final); - if (dst) - *dst = mala_stringlist_next (*dst); + if (node) + *node = mala_stringlist_next (*node); nstate = self->state; self->state = ostate; @@ -145,14 +149,9 @@ mala_program_eval_arg (MalaProgram self, int n, mala_state dest, MalaStringList_ else { ERROR (mala_program, "Missing Argument"); - - self->pptr = mala_stringlist_prev (self->program); - mala_program_commonexception (self, MALA_STRING_ERROR_MISSING_ARGUMENT); - - nstate = mala_program_step (self); - - if (dst) - *dst = NULL; + nstate = mala_program_commonexception (self, MALA_STRING_ERROR_MISSING_ARGUMENT, self->program); + if (node) + *node = NULL; } self->pptr = opptr; @@ -160,26 +159,22 @@ mala_program_eval_arg (MalaProgram self, int n, mala_state dest, MalaStringList_ } mala_state -mala_program_eval_arg_fmt (MalaProgram self, int n, const char* fmt, void* dst) +mala_program_eval_arg_fmt (MalaProgram self, int n, const char* fmt, void* data, MalaStringList_ref node) { - MalaStringList node; + MalaStringList nde; mala_state state; - retry: - state = mala_program_eval_arg (self, n, MALA_LITERAL, &node); - if (state != MALA_LITERAL) - return state; + state = mala_program_eval_arg (self, n, MALA_LITERAL, &nde); + if (node) + *node = nde; int r; - r = mala_string_scan (mala_stringlist_string (node), fmt, dst); + r = mala_string_scan (mala_stringlist_string (nde), fmt, data); if (r != 1) - { - mala_program_commonexception_at (self, MALA_STRING_ERROR_WRONG_TYPE, node); - goto retry; - } + return mala_program_commonexception (self, MALA_STRING_ERROR_WRONG_TYPE, nde); - return self->state; + return state; } mala_state @@ -188,7 +183,7 @@ mala_program_step (MalaProgram self) TODO(" handle --LITERAL"); mala_state state; - MalaStringList pptr = mala_stringlist_next (self->pptr); + MalaStringList pptr = mala_program_pptr (self); TRACE (mala_program, "%s", mala_stringlist_cstr (pptr)); @@ -197,18 +192,8 @@ mala_program_step (MalaProgram self) MalaAction action = mala_program_pptr_action (self); state = action->parser (self); } - else - { - if (self->state == MALA_EXCEPTION) - { - WARN (mala, "EXCEPTION"); - state = MALA_ENOACTION; - } - else - { - state = MALA_LITERAL; - } - } + else if (self->state != MALA_EXCEPTION) + state = MALA_LITERAL; return state; } diff --git a/engine/program.h b/engine/program.h index 28c383d..d6103f9 100644 --- a/engine/program.h +++ b/engine/program.h @@ -28,6 +28,7 @@ struct mala_program_struct { MalaStringList program; // list containing the actual program MalaStringList pptr; // points to the position *before* the next to be evaluated string + MalaStringList exception; // when a exception is pending then this points to the exception MalaEngine engine; int nesting_limit; int nesting_level; @@ -51,10 +52,10 @@ void mala_program_run (MalaProgram self, mala_state dest); mala_state -mala_program_eval_arg (MalaProgram self, int n, mala_state dest, MalaStringList_ref dst); +mala_program_eval_arg (MalaProgram self, int n, mala_state final, MalaStringList_ref node); mala_state -mala_program_eval_arg_fmt (MalaProgram self, int n, const char* fmt, void* dst); +mala_program_eval_arg_fmt (MalaProgram self, int n, const char* fmt, void* data, MalaStringList_ref node); static inline int mala_program_exists_arg (MalaProgram self, int n) @@ -63,20 +64,11 @@ mala_program_exists_arg (MalaProgram self, int n) } static inline mala_state -mala_program_commonexception (MalaProgram self, mala_common_string idx) -{ - mala_stringlist_insert_after (self->pptr, - mala_stringlist_node_new (self->engine->common_string[idx], - self->engine)); - return self->state = MALA_EXCEPTION; -} - -static inline mala_state -mala_program_commonexception_at (MalaProgram self, mala_common_string idx, MalaStringList node) +mala_program_commonexception (MalaProgram self, mala_common_string idx, MalaStringList node) { mala_stringlist_insert_before (node, - mala_stringlist_node_new (self->engine->common_string[idx], - self->engine)); + self->exception = mala_stringlist_node_new (self->engine->common_string[idx], + self->engine)); return self->state = MALA_EXCEPTION; } @@ -99,6 +91,9 @@ mala_program_pptr_string (MalaProgram self); static inline const char* mala_program_pptr_cstr (MalaProgram self); +static inline MalaStringList +mala_program_pptr (MalaProgram self); + /* @@ -143,6 +138,12 @@ mala_program_pptr_cstr (MalaProgram self) return mala_string_cstr (mala_stringlist_next (self->pptr)->string); } +static inline MalaStringList +mala_program_pptr (MalaProgram self) +{ + return mala_stringlist_next (self->pptr); +} + #endif /*MALA_PROGRAM_H*/ /* // Local Variables: -- 2.11.4.GIT