This repository was archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.hpp
114 lines (91 loc) · 2.14 KB
/
lambda.hpp
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
#ifndef YODA_LAMBDA_HPP
#define YODA_LAMBDA_HPP
#include <string>
#include <vector>
#include <inttypes.h>
#include "string_stack.hpp"
// this really should just be a struct defined somewhere else...
class Lambda {
public:
std::vector<std::string> params;
StrStack src;
Lambda(){};
Lambda(StrStack body, std::vector<std::string> params):
params(params), src(body) {}
//~Lambda(){};
// are the params they gave acceptable?
int16_t validParams(){
if (!params.size())
return -1;
bool defaultVal = false;
bool va_arg = false;
for (uint8_t i = 0; i < params.size(); i++) {
int sps = countSpaces(i);
if (sps == 2) {
if (va_arg)
return i;
defaultVal = true;
} else if (sps == 1) // default arg
va_arg = true;
else if (defaultVal) // normal arg after a default val
return i;
}
return -1;
}
int16_t requiredArgs(){
int ret = 0;
for (const std::string& param : params)
if (param.at(0) != ' ')
ret++;
else
return -ret;
return ret;
}
int max_args() {
int max = 0;
for (unsigned i = 0; i < params.size(); i++) {
if (countSpaces(i) == 1) { // lambdas with va_args can take infinite arguments
return -1;
}
max++;
}
return max;
}
// returns a vector containing the parameter each argument will be bound to
// one element per argument
std::vector<int16_t> bindArgs(const uint8_t argc){
std::vector<int16_t> ret;
if (!validParams()) {
ret.push_back(-1);
return ret;
}
//if ()
ret.reserve(argc);
uint8_t param = 0;
for (uint8_t i = 0; i < argc; i++)
if (param + 1U < (unsigned) params.size())
ret.push_back(param++);
else
ret.push_back(param);
return ret;
}
// this doesnt actually count spaces correctly... it conserves CPU
int countSpaces(int param) {
// 0 spaces = normal arg
// 1 space = va_args
// 2 spaces = arg + handler
//if (params.size() - 1 < param)
// return -2;
// "a"
if (params[param].at(0) != ' ')
return 0;
// " a process to runn if $a not provided"
for (uint8_t i = 1; i < params[param].size(); i++) {
if (params[param].at(i) == ' ')
return 2;
}
// " a"
return 1;
}
};
#endif //YODA_LAMBDA_HPP