Skip to content

Commit ded494f

Browse files
committed
Merge loopvar test from #7 and fix it.
Loop counter should be incremented after the last iteration.
1 parent ff1092d commit ded494f

File tree

7 files changed

+85
-51
lines changed

7 files changed

+85
-51
lines changed

BUILD

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cc_binary(
3737
deps = [
3838
":command",
3939
":expression",
40+
":loop",
4041
":parser",
4142
":stream",
4243
":value",
@@ -93,6 +94,16 @@ cc_library(
9394
hdrs = ["command.h"],
9495
)
9596

97+
cc_library(
98+
name = "loop",
99+
srcs = ["loop.cc"],
100+
hdrs = ["loop.h"],
101+
deps = [
102+
":command",
103+
":expression",
104+
],
105+
)
106+
96107
sh_test(
97108
name = "test",
98109
srcs = ["test.sh"],

checktestdata.cc

+1-50
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "command.h"
1414
#include "expression.h"
15+
#include "loop.h"
1516
#include "stream.h"
1617
#include "variable.h"
1718

@@ -65,56 +66,6 @@ class SuccessCommand : public Command {
6566
void run(std::string_view&) {}
6667
};
6768

68-
class Loop : public Command {
69-
public:
70-
Command* after_ = nullptr;
71-
Command* separator_ = nullptr;
72-
std::optional<Expression> i_;
73-
74-
protected:
75-
void set_i(int64_t i) {
76-
if (i_) i_->assign(Value{i});
77-
}
78-
};
79-
80-
class WhileLoop : public Loop {
81-
virtual void run(std::string_view& in) override {
82-
if (next_) {
83-
for (int64_t i = 0; std::get<bool>(condition_.eval().value_); ++i) {
84-
set_i(i);
85-
if (i)
86-
separator_->run(in);
87-
else
88-
next_->run(in);
89-
}
90-
}
91-
after_->run(in);
92-
}
93-
94-
public:
95-
Expression condition_;
96-
};
97-
98-
class ForLoop : public Loop {
99-
virtual void run(std::string_view& in) override {
100-
if (next_) {
101-
Value v = count_.eval();
102-
int64_t count = v.toInt<uint32_t>();
103-
for (int64_t i = 0; i < count; ++i) {
104-
set_i(i);
105-
if (i)
106-
separator_->run(in);
107-
else
108-
next_->run(in);
109-
}
110-
}
111-
after_->run(in);
112-
}
113-
114-
public:
115-
Expression count_;
116-
};
117-
11869
class Reader : public Command {
11970
public:
12071
Reader(checktestdataParser::ReadContext* ctx, std::optional<Expression> min,

loop.cc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "loop.h"
2+
3+
void WhileLoop::run(std::string_view& in) {
4+
if (next_) {
5+
int64_t i;
6+
for (i = 0; std::get<bool>(condition_.eval().value_); ++i) {
7+
set_i(i);
8+
if (i)
9+
separator_->run(in);
10+
else
11+
next_->run(in);
12+
}
13+
set_i(i);
14+
}
15+
after_->run(in);
16+
}
17+
18+
void ForLoop::run(std::string_view& in) {
19+
if (next_) {
20+
Value v = count_.eval();
21+
int64_t count = v.toInt<uint32_t>();
22+
int64_t i;
23+
for (i = 0; i < count; ++i) {
24+
set_i(i);
25+
if (i)
26+
separator_->run(in);
27+
else
28+
next_->run(in);
29+
}
30+
set_i(i);
31+
}
32+
after_->run(in);
33+
}

loop.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
5+
#include "command.h"
6+
#include "expression.h"
7+
8+
class Loop : public Command {
9+
public:
10+
Command* after_ = nullptr;
11+
Command* separator_ = nullptr;
12+
std::optional<Expression> i_;
13+
14+
protected:
15+
void set_i(int64_t i) {
16+
if (i_) i_->assign(Value{i});
17+
}
18+
};
19+
20+
class WhileLoop : public Loop {
21+
void run(std::string_view& in) override;
22+
23+
public:
24+
Expression condition_;
25+
};
26+
27+
class ForLoop : public Loop {
28+
void run(std::string_view& in) override;
29+
30+
public:
31+
Expression count_;
32+
};

test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ IFS=$'\n'
33
progs=$(find tests/ | grep testprog | sort -V)
44
for prog in $progs
55
do
6-
for data in $(ls "$(echo $prog | sed -e 's/testprog\([0-9]*\)\..*/testdata\1/')".*)
6+
for data in $(ls "$(echo $prog | sed -e 's/testprog\([^.]*\)\..*/testdata\1/')".*)
77
do
88
echo checktestdata $prog $data
99
./checktestdata $prog $data

tests/testdataloopvar.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 1

tests/testprogloopvar.in

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Read at least 2 bits (space-separated)
2+
WHILEI(i,!MATCH("\n"), SPACE)
3+
INT(0,1)
4+
END
5+
ASSERT(i>=2)
6+
NEWLINE

0 commit comments

Comments
 (0)