Skip to content

Commit a1dc140

Browse files
committed
Begin working on let statements
1 parent c3b09ae commit a1dc140

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

include/cppspec.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#define before_each self.before_each
1515
#define after_all self.after_all
1616
#define after_each self.after_each
17+
#define let(name, body) \
18+
auto name = make_let(#name, body); \
19+
self.let(#name, name)
1720

1821
typedef Description describe;
1922

include/description.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
#include <string>
55
#include <queue>
6-
#include <map>
6+
#include <unordered_map>
77
#include <functional>
88
#include <iostream>
99
#include <memory>
1010
#include "util.hpp"
1111
#include "runnable.hpp"
1212
#include "it.hpp"
13+
#include "let.hpp"
1314

1415
template <class T>
1516
class ClassDescription;
@@ -24,6 +25,7 @@ class Description : public Runnable {
2425
std::deque<rule_block_t> after_alls;
2526
std::deque<rule_block_t> before_eaches;
2627
std::deque<rule_block_t> after_eaches;
28+
std::unordered_map<std::string, Runnable *> lets;
2729

2830
explicit Description(std::string descr) : descr(descr){};
2931

@@ -64,6 +66,8 @@ class Description : public Runnable {
6466
void exec_before_eaches();
6567
void exec_after_eaches();
6668

69+
void let(std::string name, Runnable &body);
70+
6771
bool run();
6872
};
6973

@@ -144,6 +148,11 @@ void Description::exec_after_eaches() {
144148
for (rule_block_t b : after_eaches) b();
145149
}
146150

151+
void Description::let(std::string name, Runnable &body) {
152+
auto p = lets.insert({name, &body});
153+
if (!p.second) p.first->second = &body; // assign if the key is already there
154+
}
155+
147156
bool Description::run() {
148157
std::cout << padding() << descr << std::endl;
149158
body(*this);

include/let.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef LET_H
2+
#define LET_H
3+
#include <functional>
4+
#include <type_traits>
5+
#include <iostream>
6+
#include "runnable.hpp"
7+
8+
template <typename T>
9+
class Let : public Runnable {
10+
typedef std::function<T()> block_t;
11+
12+
std::string name;
13+
block_t body;
14+
15+
public:
16+
explicit Let(std::string name, block_t body) : name(name), body(body){};
17+
bool run();
18+
};
19+
20+
template <typename T>
21+
bool Let<T>::run() {
22+
body();
23+
return true;
24+
}
25+
26+
/**
27+
* @brief Object generator for Let.
28+
*
29+
* @param body the body of the let statement
30+
*
31+
* @return a new Let object
32+
*/
33+
template <typename T>
34+
auto make_let(std::string name, T body) -> Let<decltype(body())> {
35+
return Let<decltype(body())>(name, body);
36+
}
37+
38+
#endif /* LET_H */

0 commit comments

Comments
 (0)