summaryrefslogtreecommitdiff
path: root/src/theory/evaluator.h
blob: cee8a0d210bdf1c02f245ed2fb499bfdbd81524a (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
/*********************                                                        */
/*! \file evaluator.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Andres Noetzli
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief The Evaluator class
 **
 ** The Evaluator class.
 **/

#include "cvc4_private.h"

#pragma once

#include <utility>
#include <vector>

#include "base/output.h"
#include "expr/node.h"
#include "util/bitvector.h"
#include "util/rational.h"
#include "util/regexp.h"

namespace CVC4 {
namespace theory {

struct EvalResult
{
  enum
  {
    BOOL,
    BITVECTOR,
    RATIONAL,
    STRING,
    INVALID
  } d_tag;

  union
  {
    bool d_bool;
    BitVector d_bv;
    Rational d_rat;
    String d_str;
  };

  EvalResult(const EvalResult& other)
  {
    d_tag = other.d_tag;
    switch (d_tag)
    {
      case BOOL: d_bool = other.d_bool; break;
      case BITVECTOR:
        new (&d_bv) BitVector;
        d_bv = other.d_bv;
        break;
      case RATIONAL:
        new (&d_rat) Rational;
        d_rat = other.d_rat;
        break;
      case STRING:
        new (&d_str) String;
        d_str = other.d_str;
        break;
      case INVALID: break;
    }
  }

  EvalResult() : d_tag(INVALID) {}

  EvalResult(bool b) : d_tag(BOOL), d_bool(b) {}

  EvalResult(const BitVector& bv) : d_tag(BITVECTOR), d_bv(bv) {}

  EvalResult(const Rational& i) : d_tag(RATIONAL), d_rat(i) {}

  EvalResult(const String& str) : d_tag(STRING), d_str(str) {}

  EvalResult& operator=(const EvalResult& other)
  {
    if (this != &other)
    {
      d_tag = other.d_tag;
      switch (d_tag)
      {
        case BOOL: d_bool = other.d_bool; break;
        case BITVECTOR:
          new (&d_bv) BitVector;
          d_bv = other.d_bv;
          break;
        case RATIONAL:
          new (&d_rat) Rational;
          d_rat = other.d_rat;
          break;
        case STRING:
          new (&d_str) String;
          d_str = other.d_str;
          break;
        case INVALID: break;
      }
    }
    return *this;
  }

  ~EvalResult()
  {
    switch (d_tag)
    {
      case BITVECTOR:
      {
        d_bv.~BitVector();
        break;
      }
      case RATIONAL:
      {
        d_rat.~Rational();
        break;
      }
      case STRING:
      {
        d_str.~String();
        break;

        default: break;
      }
    }
  }

  Node toNode()
  {
    NodeManager* nm = NodeManager::currentNM();
    switch (d_tag)
    {
      case EvalResult::BOOL: return nm->mkConst(d_bool);
      case EvalResult::BITVECTOR: return nm->mkConst(d_bv);
      case EvalResult::RATIONAL: return nm->mkConst(d_rat);
      case EvalResult::STRING: return nm->mkConst(d_str);
      default:
      {
        Trace("evaluator") << "Missing conversion from " << d_tag << " to node"
                           << std::endl;
        return Node();
      }
    }

    return Node();
  }
};

class Evaluator
{
 public:
  Node eval(TNode n,
            const std::vector<Node>& args,
            const std::vector<Node>& vals);

 private:
  EvalResult evalInternal(TNode n,
                          const std::vector<Node>& args,
                          const std::vector<Node>& vals);
};

}  // namespace theory
}  // namespace CVC4
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback