summaryrefslogtreecommitdiff
path: root/graphics/asymptote/callable.h
blob: f870d48d97b3775d968b712924b8aca50b722ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*****
 * callable.h
 * Tom Prince 2005/06/19
 *
 * Runtime representation of functions.
 *****/

#ifndef CALLABLE_H
#define CALLABLE_H

#include "common.h"
#include "item.h"
#include "inst.h"

namespace vm {

class stack;
typedef void (*bltin)(stack *s);

struct callable : public gc
{
  virtual void call(stack *) = 0;
  virtual ~callable();
  virtual bool compare(callable*) { return false; }

  // For debugging:
  virtual void print(ostream& out) = 0;
};

class nullfunc : public callable
{
private:
  nullfunc() {}
  static nullfunc func;
public:
  virtual void call (stack*);
  virtual bool compare(callable*);
  static callable* instance() { return &func; }

  void print(ostream& out);
};

// How a function reference to a non-builtin function is stored.
struct func : public callable {
  lambda *body;
  frame *closure;
  func () : body(), closure() {}
  virtual void call (stack*);
  virtual bool compare(callable*);

  void print(ostream& out);
};

class bfunc : public callable
{
public:
  bfunc(bltin b) : func(b) {}
  virtual void call (stack *s) { func(s); }
  virtual bool compare(callable*);

  void print(ostream& out);
private:
  bltin func;
};

class thunk : public callable
{
public:
  thunk(callable *f, item i) : func(f), arg(i) {}
  virtual void call (stack*);

  void print(ostream& out);
private:
  callable *func;
  item arg;
};

inline ostream& operator<< (ostream& out, callable &c) {
  c.print(out);
  return out;
}

} // namespace vm

GC_DECLARE_PTRFREE(vm::nullfunc);

// I believe this is safe, as pointers to C++ functions do not point to
// the heap.
GC_DECLARE_PTRFREE(vm::bfunc);

#endif // CALLABLE_H