Skip to content

Commit b067163

Browse files
committed
copy control checks in CI & better comments
1 parent a04f332 commit b067163

2 files changed

Lines changed: 64 additions & 17 deletions

File tree

CI.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ def build(build_type: str) -> None:
2424
sys.exit(f"make failed\nresult = {res}")
2525
print(f"OK ({end - start:.3f} seconds)", flush = True)
2626

27+
def check_copy_controls(err: str) -> bool:
28+
start = """\
29+
[debug]constructor
30+
[debug]copy constructor
31+
[debug]move constructor
32+
[debug]constructor
33+
[debug]copy assignment operator
34+
[debug]constructor
35+
[debug]move assignment operator
36+
"""
37+
end = """\
38+
[debug]destructor
39+
[debug]destructor
40+
[debug]destructor
41+
[debug]destructor
42+
[debug]destructor
43+
"""
44+
# there may be calls to `.eval` so we only check the first layer of states
45+
return err.startswith(start) and err.endswith(end)
46+
2747
def test() -> None:
2848
for dirpath, _, filenames in os.walk("test/"):
2949
for filename in filenames:
@@ -37,7 +57,11 @@ def test() -> None:
3757
end = time.time()
3858
with open(filepath, "r") as f:
3959
src = f.read()
40-
if (res[0] == 0 and res[1] == src + "<end-of-stdout>\n( vval )\n"):
60+
if (
61+
res[0] == 0 and
62+
res[1] == src + "<end-of-stdout>\n( vval )\n" and
63+
check_copy_controls(res[2])
64+
):
4165
print(f"OK ({end - start:.3f} seconds)", flush = True)
4266
else:
4367
sys.exit(f'test failed\nresult = {res}\n')
@@ -48,7 +72,11 @@ def test() -> None:
4872
start = time.time()
4973
res = execute(["bin/clo", filepath], io["in"])
5074
end = time.time()
51-
if (res[0] == 0 and res[1] == io["out"]):
75+
if (
76+
res[0] == 0 and
77+
res[1] == io["out"] and
78+
check_copy_controls(res[2])
79+
):
5280
print(f"OK ({end - start:.3f} seconds)", flush = True)
5381
else:
5482
sys.exit(f'test failed\nresult = {res}\n')

src/main.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#include <vector>
2222

2323
#define DBG(x) \
24-
std::cerr << "[+] LINE " << __LINE__ \
25-
<< ": " << (x) << std::endl
24+
std::cerr << "[debug]" << (x) << std::endl
25+
#define DBG_WITH_LINE(x) \
26+
std::cerr << "[debug] (LINE " << __LINE__ << "): " << (x) << std::endl
2627
// TODO: add DBG everywhere and use a macro to control all of them
2728

2829
namespace utils {
@@ -226,8 +227,8 @@ namespace syntax {
226227
if (!hasDigit) {
227228
utils::panic("lexer", "incomplete integer literal", startsl);
228229
}
229-
// string literal
230230
}
231+
// string literal
231232
else if (ss.peekNext() == '"') {
232233
text += ss.popNext();
233234
bool complete = false;
@@ -252,26 +253,26 @@ namespace syntax {
252253
if (!complete) {
253254
utils::panic("lexer", "incomplete string literal", startsl);
254255
}
255-
// variable / keyword
256256
}
257+
// variable / keyword
257258
else if (std::isalpha(ss.peekNext()) || ss.peekNext() == '_') {
258259
while (ss.hasNext() && (
259260
std::isalpha(ss.peekNext()) || std::isdigit(ss.peekNext()) ||
260261
ss.peekNext() == '_')) {
261262
text += ss.popNext();
262263
}
263-
// intrinsic
264264
}
265+
// intrinsic
265266
else if (ss.peekNext() == '.') {
266267
while (ss.hasNext() && !(std::isspace(ss.peekNext()) || ss.peekNext() == ')')) {
267268
text += ss.popNext();
268269
}
269-
// special symbol
270270
}
271+
// special symbol
271272
else if (std::string("(){}@").find(ss.peekNext()) != std::string::npos) {
272273
text += ss.popNext();
273-
// comment
274274
}
275+
// comment
275276
else if (ss.peekNext() == '#') {
276277
while (ss.hasNext() && ss.peekNext() != '\n') {
277278
ss.popNext();
@@ -1039,8 +1040,8 @@ namespace syntax {
10391040
}
10401041
else if (tokens[0].text == "if") {
10411042
return parseIf();
1042-
// check keywords before var to avoid recognizing keywords as vars
10431043
}
1044+
// check keywords before var to avoid recognizing keywords as vars
10441045
else if (isVariableToken(tokens[0])) {
10451046
return parseVariable();
10461047
}
@@ -1789,6 +1790,9 @@ class State {
17891790
}
17901791
public:
17911792
State(std::string origin) {
1793+
#ifdef DEBUG
1794+
DBG("constructor");
1795+
#endif
17921796
if (origin.size() == 0) {
17931797
utils::panic("state constructor", "empty origin string");
17941798
}
@@ -1956,9 +1960,15 @@ class State {
19561960
heap(state.heap),
19571961
numLiterals(state.numLiterals),
19581962
resultLoc(state.resultLoc) {
1963+
#ifdef DEBUG
1964+
DBG("copy constructor");
1965+
#endif
19591966
_migrateAST(state, *this);
19601967
}
19611968
State& operator=(const State& state) {
1969+
#ifdef DEBUG
1970+
DBG("copy assignment operator");
1971+
#endif
19621972
if (this != &state) {
19631973
source = state.source;
19641974
delete expr;
@@ -1978,9 +1988,15 @@ class State {
19781988
heap(std::move(state.heap)),
19791989
numLiterals(state.numLiterals),
19801990
resultLoc(state.resultLoc) {
1991+
#ifdef DEBUG
1992+
DBG("move constructor");
1993+
#endif
19811994
state.expr = nullptr;
19821995
}
19831996
State& operator=(State&& state) {
1997+
#ifdef DEBUG
1998+
DBG("move assignment operator");
1999+
#endif
19842000
if (this != &state) {
19852001
source = std::move(state.source);
19862002
delete expr;
@@ -1994,6 +2010,9 @@ class State {
19942010
return *this;
19952011
}
19962012
~State() {
2013+
#ifdef DEBUG
2014+
DBG("destructor");
2015+
#endif
19972016
if (expr != nullptr) {
19982017
delete expr;
19992018
}
@@ -2740,18 +2759,18 @@ int main(int argc, char** argv) {
27402759
<< serialization::join(
27412760
serialization::valueToSentence(state.getResult(), state.getExpr())) << std::endl;
27422761
#else
2743-
// test the (copy & move) (constructors & assignment operators) for State
2762+
// test the copy control operators for State
27442763
std::string source = readSource(argv[1]);
2745-
State state1(source);
2746-
State state2(state1); // copy constructor
2764+
State state1(source); // constructor
2765+
State state2(state1); // copy constructor
27472766
state1.clear();
27482767
State state3(std::move(state2)); // move constructor
27492768
state2.clear();
2750-
State state4(source);
2751-
state4 = state3; // copy assignment operator
2769+
State state4(source); // constructor
2770+
state4 = state3; // copy assignment operator
27522771
state3.clear();
2753-
State state5(source);
2754-
state5 = std::move(state4); // move assignment operator
2772+
State state5(source); // constructor
2773+
state5 = std::move(state4); // move assignment operator
27552774
state4.clear();
27562775
state5.execute();
27572776
std::cout << "<end-of-stdout>\n"

0 commit comments

Comments
 (0)