From d42c4bd97982c1252c5ad638a11aea5319c4be7f Mon Sep 17 00:00:00 2001 From: Teddy Date: Sun, 4 Aug 2013 10:15:13 +0800 Subject: replaced many `dynamic_cast` to `static_cast` --- builtin.cpp | 23 +++++++++++++---------- eval.cpp | 14 +++++++++----- model.cpp | 8 +++++--- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 75f470e..1ac0cf2 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -53,7 +53,8 @@ void SpecialOptIf::prepare(Cons *pc) { void SpecialOptIf::pre_call(ArgList *args, Cons *pc, Environment *envt) { - pc = dynamic_cast(pc->car); + // static_cast because it's a call invocation + pc = static_cast(pc->car); // Condition evaluated and the decision is made state = 1; if (args->cdr->car->is_true()) @@ -82,7 +83,7 @@ EvalObj *SpecialOptIf::post_call(ArgList *args, Cons *pc, Cons *SpecialOptIf::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; + Cons *ret_addr = static_cast(*top_ptr)->addr; if (state) { *top_ptr++ = post_call(args, ret_addr, envt); @@ -93,7 +94,8 @@ Cons *SpecialOptIf::call(ArgList *args, Environment * &envt, pre_call(args, ret_addr, envt); top_ptr += 2; // Undo pop and invoke again - return dynamic_cast(ret_addr->car)->next; + // static_cast because it's a call invocation + return static_cast(ret_addr->car)->next; } } @@ -116,8 +118,8 @@ void SpecialOptLambda::prepare(Cons *pc) { Cons *SpecialOptLambda::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; - Cons *pc = dynamic_cast(ret_addr->car); + Cons *ret_addr = static_cast(*top_ptr)->addr; + Cons *pc = static_cast(ret_addr->car); SymbolList *para_list = dynamic_cast(pc->cdr->car); // parameter list // Clear the flag to avoid side-effects (e.g. proc calling) FILL_MARKS(pc, false); @@ -150,8 +152,8 @@ void SpecialOptDefine::prepare(Cons *pc) { Cons *SpecialOptDefine::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; - Cons *pc = dynamic_cast(ret_addr->car); + Cons *ret_addr = static_cast(*top_ptr)->addr; + Cons *pc = static_cast(ret_addr->car); EvalObj *obj; SymObj *id; // TODO: check identifier @@ -162,7 +164,8 @@ Cons *SpecialOptDefine::call(ArgList *args, Environment * &envt, } else { - Cons *plst = dynamic_cast(pc->cdr->car); + // static_cast because of is_simple_obj() is false + Cons *plst = static_cast(pc->cdr->car); id = dynamic_cast(plst->car); ArgList *para_list = plst->cdr; // Clear the flag to avoid side-effects (e.g. proc calling) @@ -192,8 +195,8 @@ void SpecialOptSet::prepare(Cons *pc) { Cons *SpecialOptSet::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; - Cons *pc = dynamic_cast(ret_addr->car); + Cons *ret_addr = static_cast(*top_ptr)->addr; + Cons *pc = static_cast(ret_addr->car); SymObj *id = dynamic_cast(pc->cdr->car); bool flag = envt->add_binding(id, args->cdr->car, false); // TODO: throw an exc "unbound variable" diff --git a/eval.cpp b/eval.cpp index 44569c1..e536f8f 100644 --- a/eval.cpp +++ b/eval.cpp @@ -34,14 +34,16 @@ void push(Cons * &pc, FrameObj ** &top_ptr, Environment *envt) { if (pc->car->is_simple_obj()) // Not an opt invocation { *top_ptr = envt->get_obj(pc->car); // Objectify the symbol - dynamic_cast(*top_ptr)->prepare(pc); + // static_cast because of is_simple_obj() is true + static_cast(*top_ptr)->prepare(pc); top_ptr++; pc = pc->next; // Move to the next instruction } else // Operational Invocation { *top_ptr++ = new RetAddr(pc); // Push the return address - pc = dynamic_cast(pc->car); // Go deeper to enter the call + // static_cast because of is_simple_obj() is false + pc = static_cast(pc->car); // Go deeper to enter the call } } @@ -67,8 +69,9 @@ EvalObj *Evaluator::run_expr(Cons *prog) { { Cons *args = empty_list; while (!(*(--top_ptr))->is_ret_addr()) - args = new Cons(dynamic_cast(*top_ptr), args); - RetAddr *ret_addr = dynamic_cast(*top_ptr); + args = new Cons(static_cast(*top_ptr), args); + //< static_cast because the while condition + RetAddr *ret_addr = static_cast(*top_ptr); if (!ret_addr->addr) { Cons *nexp = cont->proc_body->cdr; @@ -87,5 +90,6 @@ EvalObj *Evaluator::run_expr(Cons *prog) { pc = dynamic_cast(args->car)->call(args, envt, cont, top_ptr); } } - return dynamic_cast(*(eval_stack)); + // static_cast because the previous while condition + return static_cast(*(eval_stack)); } diff --git a/model.cpp b/model.cpp index 95f70c6..9ecd5f7 100644 --- a/model.cpp +++ b/model.cpp @@ -88,14 +88,16 @@ ProcObj::ProcObj(ASTList *_body, Cons *ProcObj::call(ArgList *args, Environment * &_envt, Continuation * &cont, FrameObj ** &top_ptr) { // Create a new continuation - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; + // static_cast see `call` invocation in eval.cpp + Cons *ret_addr = static_cast(*top_ptr)->addr; Continuation *ncont = new Continuation(_envt, ret_addr, cont, body); cont = ncont; // Add to the cont chain _envt = new Environment(envt); // Create local env and recall the closure // TODO: Compare the arguments to the parameters + // static_cast because the para_list is already checked for (Cons *ptr = args->cdr, *ppar = para_list; ptr != empty_list; ptr = ptr->cdr, ppar = ppar->cdr) - _envt->add_binding(dynamic_cast(ppar->car), ptr->car); + _envt->add_binding(static_cast(ppar->car), ptr->car); *top_ptr++ = new RetAddr(NULL); // Mark the entrance of a cont return body; // Move pc to the proc entry point } @@ -116,7 +118,7 @@ BuiltinProcObj::BuiltinProcObj(BuiltinProc f, string _name) : Cons *BuiltinProcObj::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = dynamic_cast(*top_ptr)->addr; + Cons *ret_addr = static_cast(*top_ptr)->addr; *top_ptr++ = handler(args->cdr); return ret_addr->next; // Move to the next instruction } -- cgit v1.2.3-70-g09d2