summaryrefslogtreecommitdiff
path: root/src/ops/operator.h
blob: d4a7af8badc5540987c5492e78919a51c3eb2273 (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
92
93
94
95
96
97
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef OPERATORH
#define OPERATORH

#include <mt/mstring.h>
#include <mt/mvec.h>

const mstring lt_tag="lt";

///////////////////////////////////////////////

class op_env;
struct op_t {
  typedef void (*op1_t)(mstream&,op_env&,const sref&);
  typedef void (*op2_t)(mstream&,op_env&,const sref&,const sref&);

  mstring name,desc;
  int pre1,pre2;
  int left,right;
  op1_t f1; op2_t f2;

  op_t(const mstring& s,int p1,int p2,int l,int r,op1_t op1,op2_t op2,const mstring& d):
    name(s),pre1(p1),pre2(p2),left(l),right(r),f1(op1),f2(op2),desc(d) {}
  op_t(const mstring& s,int p1,int p2,op1_t op1,op2_t op2,const mstring& d):
    name(s),pre1(p1),pre2(p2),left(2),right(2),f1(op1),f2(op2),desc(d) {}
  op_t(const mstring& s,int p,op1_t op,const mstring& d):
    name(s),pre1(p),pre2(0),left(0),right(2),f1(op),f2(0),desc(d) {}
  op_t(const mstring& s,int p,op2_t op,const mstring& d):
    name(s),pre1(0),pre2(p),left(2),right(2),f1(0),f2(op),desc(d) {}
};

#define OP1_IMPL(n) \
static void impl_##n(mstream& out,op_env& env,const sref& a)
#define OP2_IMPL(n) \
static void impl_##n(mstream& out,op_env& env,const sref& L,const sref& R)

////////////////////////////////////////////////////////////

typedef op_t* opmap_t;

struct opmap : public mrvec<opmap_t> {
  typedef mrvec<opmap_t>::iterator iterator;
  typedef mrvec<opmap_t>::const_iterator const_iterator;

  struct comp_t {
    sref key;
    comp_t(const sref& skey) : key(skey) {}
    bool operator()(const op_t* a,const op_t* b) const {
      return (a?((const sref&)a->name):key)<(b?((const sref&)b->name):key);
    }
  };
  
  opmap() : mrvec<opmap_t>() {}
  ~opmap() { clear(); }

  void clear(); // overrides parent
  void erase(iterator p); // overrides parent
  
  const op_t* get(const sref& t) const;
  const op_t* add(op_t* op);
};

#define OP_ADD(p,f,d) M.add(new op_t(#f,p,impl_##f,d))
#define OP_ADDN(p,f,n,d) M.add(new op_t(#n,p,impl_##f,d))
#define OP_ADD2(p1,p2,f,n,d) M.add(new op_t(#n,p1,p2,impl_##f,impl_##f,d))

///////////////////////////////////////////

void opAddBasic(opmap& map);
void opAddComp(opmap& map);
void opAddString(opmap& map);
inline void opAddHEXP(opmap& map) {
  opAddBasic(map);
  opAddComp(map);
  opAddString(map);
}
void opAddLatex(opmap& map);

#endif