souffle  2.0.2-371-g6315b36
EvaluatorUtil.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved.
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file EvaluatorUtils.h
12  *
13  * Defines utility functions used by synthesised and interpreter code.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "souffle/RamTypes.h"
22 #include <csignal>
23 
24 namespace souffle::evaluator {
25 
26 template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
27 void runRange(A from, A to, A step, F&& go) {
28 #define GO(x) go(Tuple<RamDomain, 1>{ramBitCast(x)})
29  if (0 < step) {
30  for (auto x = from; x < to; x += step) {
31  GO(x);
32  }
33  } else if (step < 0) {
34  for (auto x = from; to < x; x += step) {
35  GO(x);
36  }
37  } else if (from != to) {
38  // `step = 0` edge case, only if non-empty range
39  GO(from);
40  }
41 #undef GO
42 }
43 
44 template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
45 void runRange(A from, A to, F&& go) {
46  return runRange(from, to, A(from <= to ? 1 : -1), std::forward<F>(go));
47 }
48 
49 template <typename A>
50 A symbol2numeric(const std::string& src) {
51  try {
52  if constexpr (std::is_same_v<RamFloat, A>) {
53  return RamFloatFromString(src);
54  } else if constexpr (std::is_same_v<RamSigned, A>) {
55  return RamSignedFromString(src);
56  } else if constexpr (std::is_same_v<RamUnsigned, A>) {
57  return RamUnsignedFromString(src);
58  }
59  } catch (...) {
60  tfm::format(std::cerr, "error: wrong string provided by `to_number(\"%s\")` functor.\n", src);
61  raise(SIGFPE);
62  abort(); // UNREACHABLE: `raise` lacks a no-return attribute
63  }
64 };
65 
66 template <typename A>
67 bool lxor(A x, A y) {
68  return (x || y) && (!x != !y);
69 }
70 
71 // HACK: C++ doesn't have an infix logical xor operator.
72 // C++ doesn't allow defining new infix operators.
73 // C++ isn't a very nice language, but C++ does allow overload-based war crimes.
74 // This particular war crime allows a very verbose infix op for `lxor`.
75 // It should only be used in macro dispatches.
76 struct lxor_infix {
77  template <typename A>
78  struct curry {
79  A x;
80  bool operator+(A y) const {
81  return lxor(x, y);
82  }
83  };
84 };
85 
86 template <typename A>
88  return lxor_infix::curry<A>{x};
89 }
90 
91 } // namespace souffle::evaluator
souffle::RamSignedFromString
RamSigned RamSignedFromString(const std::string &str, std::size_t *position=nullptr, const int base=10)
Converts a string to a RamSigned.
Definition: StringUtil.h:51
souffle::evaluator::operator+
lxor_infix::curry< A > operator+(A x, lxor_infix)
Definition: EvaluatorUtil.h:93
tinyformat::format
void format(std::ostream &out, const char *fmt)
Definition: tinyformat.h:1089
souffle::evaluator::lxor_infix::curry::x
A x
Definition: EvaluatorUtil.h:85
tinyformat.h
souffle::evaluator::symbol2numeric
A symbol2numeric(const std::string &src)
Definition: EvaluatorUtil.h:56
souffle::evaluator::lxor_infix::curry
Definition: EvaluatorUtil.h:84
StringUtil.h
souffle::evaluator
Definition: EvaluatorUtil.h:24
souffle::evaluator::lxor
bool lxor(A x, A y)
Definition: EvaluatorUtil.h:73
RamTypes.h
souffle::evaluator::lxor_infix::curry::operator+
bool operator+(A y) const
Definition: EvaluatorUtil.h:86
souffle::RamUnsignedFromString
RamUnsigned RamUnsignedFromString(const std::string &str, std::size_t *position=nullptr, const int base=10)
Converts a string to a RamUnsigned.
Definition: StringUtil.h:110
GO
#define GO(x)
souffle::RamFloatFromString
RamFloat RamFloatFromString(const std::string &str, std::size_t *position=nullptr)
Converts a string to a RamFloat.
Definition: StringUtil.h:93
souffle::evaluator::lxor_infix
Definition: EvaluatorUtil.h:82
souffle::evaluator::runRange
void runRange(A from, A to, A step, F &&go)
Definition: EvaluatorUtil.h:33