File tree 7 files changed +85
-51
lines changed
7 files changed +85
-51
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ cc_binary(
37
37
deps = [
38
38
":command" ,
39
39
":expression" ,
40
+ ":loop" ,
40
41
":parser" ,
41
42
":stream" ,
42
43
":value" ,
@@ -93,6 +94,16 @@ cc_library(
93
94
hdrs = ["command.h" ],
94
95
)
95
96
97
+ cc_library (
98
+ name = "loop" ,
99
+ srcs = ["loop.cc" ],
100
+ hdrs = ["loop.h" ],
101
+ deps = [
102
+ ":command" ,
103
+ ":expression" ,
104
+ ],
105
+ )
106
+
96
107
sh_test (
97
108
name = "test" ,
98
109
srcs = ["test.sh" ],
Original file line number Diff line number Diff line change 12
12
13
13
#include " command.h"
14
14
#include " expression.h"
15
+ #include " loop.h"
15
16
#include " stream.h"
16
17
#include " variable.h"
17
18
@@ -65,56 +66,6 @@ class SuccessCommand : public Command {
65
66
void run (std::string_view&) {}
66
67
};
67
68
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
-
118
69
class Reader : public Command {
119
70
public:
120
71
Reader (checktestdataParser::ReadContext* ctx, std::optional<Expression> min,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ IFS=$'\n'
3
3
progs=$( find tests/ | grep testprog | sort -V)
4
4
for prog in $progs
5
5
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/' ) " .* )
7
7
do
8
8
echo checktestdata $prog $data
9
9
./checktestdata $prog $data
Original file line number Diff line number Diff line change
1
+ 0 1
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments