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()13 callable::~callable()
14 {}
15 
call(stack * s)16 void func::call(stack *s)
17 {
18   s->run(this);
19 }
20 
print(ostream & out)21 void nullfunc::print(ostream& out) {
22   out << "nullfunc";
23 }
24 
compare(callable * F)25 bool 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)32 void func::print(ostream& out) {
33   out << "func with lambda";
34 #ifdef DEBUG_FRAME
35   out << " " << body->name;
36 #endif
37 }
38 
compare(callable * F)39 bool 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)46 void bfunc::print(ostream& out) {
47   out << "bltin";
48 #ifdef DEBUG_BLTIN
49   out << " " << lookupBltin(func);
50 #endif
51 }
52 
call(stack * s)53 void thunk::call(stack *s)
54 {
55   s->push(arg);
56   func->call(s);
57 }
58 
print(ostream & out)59 void thunk::print(ostream& out) {
60   out << "thunk on " << arg << " with ";
61   func->print(out);
62 }
63 
64 nullfunc nullfunc::func;
call(stack *)65 void nullfunc::call(stack *)
66 {
67   error("dereference of null function");
68 }
69 
compare(callable * f)70 bool nullfunc::compare(callable* f)
71 {
72   return f == &func;
73 }
74 
75 } // namespace vm
76