summaryrefslogtreecommitdiff
path: root/src/ops/express.cc
blob: 2cd31146e41f9396653b40988f6d3993a08399e5 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*************************************************************************
 *
 * 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
 */

#include <ops/express.h>

const mstring sp_tag="SP";

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

uniheap<sizeof(expr_t)> expr_t::tree;
void* expr_t::operator new(size_t n) { return tree.push(); }
void expr_t::operator delete(void* p) { tree.pop(p); }

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

inline int precedence(const expr_t* root,const op_t* op)
{
  if(!root->op) return 1;
  else if(!op) return 0;
  else if(root->left) {
    if(root->right) return op->pre2&&root->op->pre2>=op->pre2;
    else return root->op->pre2>=op->pre1;
  }
  else {
    if(root->right) return op->pre2&&root->op->pre1>=op->pre2;
    else return root->op->pre1>=op->pre1;
  }
}

static int ins_node_sp(expr_t*& root,op_env& env,
                       const sref& in,const op_t* op) throw(merror_t)
{
  if(root==0) { root=new expr_t(in,op); root->expand(env); return 1; }
  else if(precedence(root,op)) {
    if(op) { root=new expr_t(in,op,root); return 1; } else return 0;
  }
  else return ins_node_sp(root->right,env,in,op);
}

static void ins_node(expr_t*& root,op_env& env,const sref& in) throw(merror_t)
{
  const op_t* op=env.ops->get(in);
  if(!ins_node_sp(root,env,in,op)) {
    if(!ins_node_sp(root,env,sp_tag,env.ops->get(sp_tag)))
      THROW("ops: ("<<root->expr<<" ["<<in<<"]): operator expected");
    ins_node_sp(root,env,in,op);
  }
}

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

void expr_t::clear()
{
  expr.clear();
  op=0;
  status=OP_ALL;
  result.clear();
  delete left; left=0;
  delete right; right=0;
}

void expr_t::copy(const expr_t& x)
{
  expr=x.expr;
  op=x.op;
  if(x.left) left=new expr_t(*x.left);
  if(x.right) right=new expr_t(*x.right);
}

void expr_t::trav(mstream& out,int n)
{
  if(n&&left) left->trav(out,n-1);
  for(int i=0;i<3*n;i++) out.put(' ');
  out<<"["<<expr<<"]("<<status<<") -> "<<result<<"\n";
  if(n&&right) right->trav(out,n-1);
}

void expr_t::expand(op_env& env) throw(merror_t)
{
  if(!op) {
    sref first,rest=expr;
    env.split(first,rest);
    if(first!=expr) {
      do ins_node(right,env,first);
      while(env.split(first,rest));
    }
  }
}

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

void expr_t::eval(mstream& out,op_env& env,int opt) throw(merror_t)
{
  if(op) {
    if(left) {
      if(op->left>1) {
        if(left->status&opt) {
          swrite sw(left->result);
          left->eval(sw,env,opt);
        }
      }
      else if(!op->left)
        THROW("ops: ("<<left->expr<<" ["<<expr<<"]): not an L operator")
      else if(left->op)
        THROW("ops: (["<<left->expr<<"] "<<expr<<"): Lvalue required")
      else {
        left->status=OP_LOCAL;
        left->result=left->expr;
      }
    }
    if(right) {
      if(op->right>1) {
        if(right->status&opt) {
          swrite sw(right->result);
          right->eval(sw,env,opt);
        }
      }
      else if(!op->right)
        THROW("ops: ("<<right->expr<<" ["<<expr<<"]): not an R operator")
      else if(right->op)
        THROW("ops: (["<<right->expr<<"] "<<expr<<"): Lvalue required")
      else {
        right->status=OP_LOCAL;
        right->result=right->expr;
      }
    }
    if(left) {
      if(right) {
        status=left->status|right->status;
        if((status&OP_NINIT)==0) op->f2(out,env,left->result,right->result);
      }
      else if(!op->f1)
        THROW("ops: ("<<left->expr<<" "<<expr<<" [R]): missing R argument")
      else {
        status=left->status;
        if((status&OP_NINIT)==0) op->f1(out,env,left->result);
      }
    }
    else if(right) {
      if(!op->f1)
        THROW("ops: ([L] "<<expr<<" "<<right->expr<<"): missing L argument")
      else {
        status=right->status;
        if((status&OP_NINIT)==0) op->f1(out,env,right->result);
      }
    }
    else THROW("ops: ["<<expr<<"]: operator not expected");
  }
  else {
    if(right) {
      right->eval(out,env,opt);
      status=right->status;
    }
    else env.term(out,this);
  }
  if(status&OP_NINIT) THROW("ops: unfinished expression ["<<expr<<"]");
}