Skip to content

Commit

Permalink
Updated cli example
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 9, 2017
1 parent 555f750 commit 300c434
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/cpp/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5)
project(cliMiniQbt)
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_C_FLAGS_RELEASE "-O3")

find_library(MiniQbt REQUIRED)

SET(srcfiles
Main.cpp
Expand Down
32 changes: 20 additions & 12 deletions examples/cpp/cli/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,31 @@ int main(int argc, char** argv){
printInfo(
"Interactive shell for the Qasm language\n",
"Exit program -> exit \n",
"Show version -> about \n"
"Show version -> about \n",
"show a classic register (and collapse the quantum)-> collapse <classic register name> \n",
"Reset the quantum register to super position -> reset <quantum register name>\n"
);
continue;
}

if(command == "collapse"){
for(const std::string& reg: intepreter.getRegisters()){
std::vector<bool> result = intepreter.readClassicRegister(reg);
std::cout << "Result registery " << reg << ": ";
for(const bool& r : result){
std::cout << r;
}
std::cout << "\n";
}
continue;
}
std::regex resetQuantum("\\s*(reset|collapse)\\s*([a-z|A-Z|0-9]*)", std::regex_constants::ECMAScript);
std::smatch m;
std::regex_match(command, m,resetQuantum);
if (m[1] == "reset") {
intepreter.resetSuperPosition(m[2]);
continue;
}
else if (m[1] == "collapse") {
std::vector<bool> result = intepreter.readClassicRegister(m[2]);
std::cout << "Result registery " << m[1] << ": ";
for (const bool& r : result) {
std::cout << r;
}
std::cout << "\n";
continue;
}


if(command[0] == '!'){
std::cout.flush();
system(command.substr(1).c_str());
Expand Down
56 changes: 43 additions & 13 deletions examples/cpp/cli/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,46 @@
#include <string>
#include <iostream>

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif

template<typename T>
constexpr void printInfo(T& msg){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 2);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << "\033[1;32m" << msg << "\033[0m";
#endif
}

template<typename T, typename... arguments>
constexpr void printInfo(T& msg, arguments... args) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 2);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << "\033[1;32m" << msg;
#endif
printInfo(args...);
}


template<typename T>
constexpr void printWarning(T& msg){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
std::cout << msg;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << "\033[1;33m" << msg << "\033[0m";
#endif
Expand All @@ -25,27 +52,26 @@ constexpr void printWarning(T& msg){
template<typename T, typename... arguments>
constexpr void printWarning(T& msg, arguments... args){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
std::cout<< msg;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << "\033[1;33m" << msg;
#endif
printWarning(args...);
}

template<typename T, typename... arguments>
constexpr void printInfo(T& msg, arguments... args){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
std::cout<< msg;
#else
std::cout << "\033[1;32m" << msg;
#endif
printInfo(args...);
}

template<typename T>
constexpr void printError(T& msg){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
std::cout << msg;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 4);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << msg << "\033[0m";
#endif
Expand All @@ -56,7 +82,11 @@ constexpr void printError(T& msg){
template<typename T, typename... arguments>
constexpr void printError(T& msg, arguments... args){
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
std::cout<< msg;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 4);
std::cout << msg;
SetConsoleTextAttribute(hConsole, 7);
#else
std::cout << "\033[1;31m" << msg;
#endif
Expand Down
2 changes: 1 addition & 1 deletion modules/core/tests/IntepreterTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 3.5)
project(IntepreterTests)
set (CMAKE_CXX_STANDARD 14)

find_library(MiniQbt REQUIRED)
SET(srcfiles
Main.cpp
SimpleAsyncTests.cpp
Expand Down

0 comments on commit 300c434

Please sign in to comment.