1 /***** 2 * callable.cc 3 * Tom Prince 2005/06/19 4 * 5 * Runtime representation of functions. 6 *****/ 7 8 #include "stack.h" 9 #include "callable.h" 10 11 namespace vm { 12 ~callable()13callable::~callable() 14 {} 15 call(stack * s)16void func::call(stack *s) 17 { 18 s->run(this); 19 } 20 print(ostream & out)21void nullfunc::print(ostream& out) { 22 out << "nullfunc"; 23 } 24 compare(callable * F)25bool func::compare(callable* F) 26 { 27 if (func* f=dynamic_cast<func*>(F)) 28 return (body == f->body) && (closure == f->closure); 29 else return false; 30 } 31 print(ostream & out)32void func::print(ostream& out) { 33 out << "func with lambda"; 34 #ifdef DEBUG_FRAME 35 out << " " << body->name; 36 #endif 37 } 38 compare(callable * F)39bool bfunc::compare(callable* F) 40 { 41 if (bfunc* f=dynamic_cast<bfunc*>(F)) 42 return (func == f->func); 43 else return false; 44 } 45 print(ostream & out)46void bfunc::print(ostream& out) { 47 out << "bltin"; 48 #ifdef DEBUG_BLTIN 49 out << " " << lookupBltin(func); 50 #endif 51 } 52 call(stack * s)53void thunk::call(stack *s) 54 { 55 s->push(arg); 56 func->call(s); 57 } 58 print(ostream & out)59void thunk::print(ostream& out) { 60 out << "thunk on " << arg << " with "; 61 func->print(out); 62 } 63 64 nullfunc nullfunc::func; call(stack *)65void nullfunc::call(stack *) 66 { 67 error("dereference of null function"); 68 } 69 compare(callable * f)70bool nullfunc::compare(callable* f) 71 { 72 return f == &func; 73 } 74 75 } // namespace vm 76