Skip to content

Commit

Permalink
Fix switch #325
Browse files Browse the repository at this point in the history
  • Loading branch information
nkchuykin committed Aug 28, 2022
1 parent 69a9762 commit 9980639
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
36 changes: 34 additions & 2 deletions project/src/transpiler/transpile_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,20 @@ EOObject GetSwitchEOObject(const SwitchStmt *p_stmt) {
eq_obj.nested.push_back(GetStmtEOObject(case_stmt->getLHS()));
cond_obj.nested.push_back(
GetCaseCondEOObject(all_cases, switch_expr_object, 0));
if_obj.nested.push_back(cond_obj);
if (nested != nullptr &&
nested->getStmtClass() == Stmt::DefaultStmtClass) {
EOObject always_true_obj{"or"};
always_true_obj.nested.push_back(cond_obj);
always_true_obj.nested.emplace_back("TRUE", EOObjectType::EO_LITERAL);
if_obj.nested.push_back(always_true_obj);
const auto *def_stmt = dyn_cast<DefaultStmt>(nested);
nested = def_stmt->getSubStmt();
} else {
if_obj.nested.push_back(cond_obj);
}
EOObject buffer_obj{"seq"};
if (nested != nullptr) {
if (nested != nullptr &&
nested->getStmtClass() != Stmt::DefaultStmtClass) {
buffer_obj.nested.push_back(GetStmtEOObject(nested));
}
auto tmp = stmt;
Expand All @@ -614,6 +625,27 @@ EOObject GetSwitchEOObject(const SwitchStmt *p_stmt) {
buffer_obj.nested.emplace_back("TRUE", EOObjectType::EO_LITERAL);
if_obj.nested.push_back(buffer_obj);
seq_object.nested.push_back(if_obj);

if (nested != nullptr &&
nested->getStmtClass() == Stmt::DefaultStmtClass) {
const auto *default_stmt = dyn_cast<DefaultStmt>(nested);
EOObject buffer_obj_def{"seq"};
if (default_stmt->getSubStmt() != nullptr) {
buffer_obj_def.nested.push_back(
GetStmtEOObject(default_stmt->getSubStmt()));
}
auto tmp_def = stmt;
tmp_def++;
while (tmp_def != end &&
(*tmp_def)->getStmtClass() != Stmt::CaseStmtClass &&
(*tmp_def)->getStmtClass() != Stmt::DefaultStmtClass) {
buffer_obj_def.nested.push_back(GetStmtEOObject(*tmp_def));
tmp_def++;
}
buffer_obj_def.nested.push_back(set_flag_object);
buffer_obj_def.nested.emplace_back("TRUE", EOObjectType::EO_LITERAL);
seq_object.nested.push_back(buffer_obj_def);
}
} else if ((*stmt)->getStmtClass() == Stmt::DefaultStmtClass) {
const auto *default_stmt = dyn_cast<DefaultStmt>(*stmt);
EOObject buffer_obj{"seq"};
Expand Down
73 changes: 73 additions & 0 deletions project/tests/main/switch_and_if_else/switch/switch01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>

int x;
int t;

void op1() {
t = 1;
printf("%d\n", t);
}
void op23() {
t = 23;
printf("%d\n", t);
}
void op4() {
t = 4;
printf("%d\n", t);
}
void op5() {
t = 5;
printf("%d\n", t);
}
void op10() {
t = 10;
printf("%d\n", t);
}

void test_switch() {
switch (x) {
case 1:
op1();
break;
case 2:
case 3:
op23();
break;
case 4:
op4();
case 5:
op5();
break;
case 6:
default:
op10();
break;
}
}

int main() {
x = 0;
printf("%d\n", x);
test_switch();
x = 1;
printf("%d\n", x);
test_switch();
x = 2;
printf("%d\n", x);
test_switch();
x = 3;
printf("%d\n", x);
test_switch();
x = 4;
printf("%d\n", x);
test_switch();
x = 5;
printf("%d\n", x);
test_switch();
x = 6;
printf("%d\n", x);
test_switch();
x = 7;
printf("%d\n", x);
test_switch();
}
69 changes: 69 additions & 0 deletions project/tests/main/switch_and_if_else/switch/switch02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>

int x;
int t;

void op1() {
t = 1;
printf("%d\n", t);
}
void op23() {
t = 23;
printf("%d\n", t);
}
void op4() {
t = 4;
printf("%d\n", t);
}
void op5() {
t = 5;
printf("%d\n", t);
}
void op10() {
t = 10;
printf("%d\n", t);
}

void test_switch() {
switch (x) {
case 1:
op1();
case 2:
case 3:
op23();
case 4:
op4();
case 5:
op5();
case 6:
default:
op10();
}
}

int main() {
x = 0;
printf("%d\n", x);
test_switch();
x = 1;
printf("%d\n", x);
test_switch();
x = 2;
printf("%d\n", x);
test_switch();
x = 3;
printf("%d\n", x);
test_switch();
x = 4;
printf("%d\n", x);
test_switch();
x = 5;
printf("%d\n", x);
test_switch();
x = 6;
printf("%d\n", x);
test_switch();
x = 7;
printf("%d\n", x);
test_switch();
}

1 comment on commit 9980639

@0pdd
Copy link
Member

@0pdd 0pdd commented on 9980639 Aug 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to retrieve PDD puzzles from the code base and submit them to github. If you think that it's a bug on our side, please submit it to yegor256/0pdd:

set -x && set -e && set -o pipefail && cd /tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ && pdd -v -f /tmp/20220828-2998-1grerjt [1]: + set -e + set -o pipefail + cd /tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ + pdd -v -f...

Please, copy and paste this stack trace to GitHub:

UserError
set -x && set -e && set -o pipefail && cd /tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ && pdd -v -f /tmp/20220828-2998-1grerjt [1]:
+ set -e
+ set -o pipefail
+ cd /tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ
+ pdd -v -f /tmp/20220828-2998-1grerjt

My version is 0.21.3
Ruby version is 2.7.5 at x86_64-linux
Reading from root dir /tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ
/tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ/project/scripts/data/skips/test.txt is a binary file (0 bytes)
/tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ/project/tests/in_progress/for_main/.gitkeep is a binary file (0 bytes)
/tmp/0pdd20220828-13-hy67j0/Z2l0QGdpdGh1Yi5jb206cG9seXN0YXQvYzJlby5naXQ/result/eo/c2eo/src/.gitkeep is a binary file (0 bytes)
Reading .gitignore ...
Reading .gitattributes ...
Reading Dockerfile ...
Reading .rultor.yml ...
Reading project/CMakeLists.txt ...
Reading project/scripts/build_c2eo.py ...
Reading project/scripts/build_eo.py ...
Reading project/scripts/code_lines.py ...
Reading project/scripts/c2eo-all.py ...
Reading project/scripts/clang_tidy.py ...
Reading project/scripts/compile.py ...
Reading project/scripts/readme.md ...
Reading project/scripts/clean_before_transpilation.py ...
Reading project/scripts/tools.py ...
Reading project/scripts/update-release.py ...
Reading project/scripts/test.py ...
Reading project/scripts/transpile.py ...
Reading project/scripts/codecov.py ...
Reading project/scripts/data/settings.yml ...
Reading project/scripts/data/meta/run.sh.txt ...
Reading project/scripts/data/meta/plug.txt ...
Reading project/scripts/data/skips/gcc.txt ...
Reading project/scripts/data/skips/testcuite.txt ...
ERROR: project/scripts/data/skips/testcuite.txt; PDD::Error at project/scripts/data/skips/testcuite.txt:1: TODO found, but puzzle can't be parsed, most probably because TODO is not followed by a puzzle marker, as this page explains: https://github.com/cqfn/pdd#how-to-format
If you can't understand the cause of this issue or you don't know how to fix it, please submit a GitHub issue, we will try to help you: https://github.com/cqfn/pdd/issues. This tool is still in its beta version and we will appreciate your feedback. Here is where you can find more documentation: https://github.com/cqfn/pdd/blob/master/README.md.
Exit code is 1

/app/objects/git_repo.rb:73:in `rescue in block in xml'
/app/objects/git_repo.rb:70:in `block in xml'
/app/vendor/ruby-2.7.5/lib/ruby/2.7.0/tempfile.rb:291:in `open'
/app/objects/git_repo.rb:69:in `xml'
/app/objects/puzzles.rb:41:in `deploy'
/app/objects/jobs/job.rb:38:in `proceed'
/app/objects/jobs/job_starred.rb:32:in `proceed'
/app/objects/jobs/job_recorded.rb:31:in `proceed'
/app/objects/jobs/job_emailed.rb:33:in `proceed'
/app/objects/jobs/job_commiterrors.rb:33:in `proceed'
/app/objects/jobs/job_detached.rb:48:in `exclusive'
/app/objects/jobs/job_detached.rb:36:in `block in proceed'
/app/objects/jobs/job_detached.rb:36:in `fork'
/app/objects/jobs/job_detached.rb:36:in `proceed'
/app/0pdd.rb:518:in `process_request'
/app/0pdd.rb:355:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1686:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1686:in `block in compile!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1023:in `block (3 levels) in route!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1042:in `route_eval'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1023:in `block (2 levels) in route!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1071:in `block in process_route'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1069:in `catch'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1069:in `process_route'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1021:in `block in route!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1018:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1018:in `route!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1140:in `block in dispatch!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `block in invoke'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `catch'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `invoke'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1135:in `dispatch!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:949:in `block in call!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `block in invoke'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `catch'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1112:in `invoke'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:949:in `call!'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:938:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/xss_header.rb:18:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/path_traversal.rb:16:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/json_csrf.rb:26:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/base.rb:50:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/base.rb:50:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-protection-2.2.2/lib/rack/protection/frame_options.rb:31:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.4/lib/rack/logger.rb:17:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.4/lib/rack/common_logger.rb:38:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:255:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:248:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.4/lib/rack/method_override.rb:24:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:218:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1993:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1553:in `block in call'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1769:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/sinatra-2.2.2/lib/sinatra/base.rb:1553:in `call'
/app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.4/lib/rack/handler/webrick.rb:95:in `service'
/app/vendor/ruby-2.7.5/lib/ruby/2.7.0/webrick/httpserver.rb:140:in `service'
/app/vendor/ruby-2.7.5/lib/ruby/2.7.0/webrick/httpserver.rb:96:in `run'
/app/vendor/ruby-2.7.5/lib/ruby/2.7.0/webrick/server.rb:307:in `block in start_thread'

Please sign in to comment.