fork(1) download
  1. #include <cstdint>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. const std::string program = R"(
  7. +++++ +++++ initialize counter (cell #0) to 10
  8. [ use loop to set the next four cells to 70/100/30/10
  9. > +++++ ++ add 7 to cell #1
  10. > +++++ +++++ add 10 to cell #2
  11. > +++ add 3 to cell #3
  12. > + add 1 to cell #4
  13. <<<< - decrement counter (cell #0)
  14. ]
  15. > ++ . print 'H'
  16. > + . print 'e'
  17. +++++ ++ . print 'l'
  18. . print 'l'
  19. +++ . print 'o'
  20. > ++ . print ' '
  21. << +++++ +++++ +++++ . print 'W'
  22. > . print 'o'
  23. +++ . print 'r'
  24. ----- - . print 'l'
  25. ----- --- . print 'd'
  26. > + . print '!'
  27. > . print '\n'
  28. )";
  29.  
  30. uint8_t memory[30000];
  31.  
  32. void execute(const std::string& program) {
  33. auto it = program.begin(), eof = program.end();
  34. uint8_t* ptr = memory;
  35. while (it != eof) {
  36. if (*it == '>') ptr++;
  37. if (*it == '<') ptr--;
  38. if (*it == '+') (*ptr)++;
  39. if (*it == '-') (*ptr)--;
  40. if (*it == '.') std::cout << *ptr;
  41. if (*it == ',') std::cin >> *ptr;
  42. if (*it == '[') {
  43. if (*ptr == 0) {
  44. ++it;
  45. int balance = 0;
  46. while (*it != ']' || balance != 0) {
  47. if (*it == '[') ++balance;
  48. if (*it == ']') --balance;
  49. ++it;
  50. }
  51. }
  52. }
  53. if (*it == ']') {
  54. if (*ptr != 0) {
  55. --it;
  56. int balance = 0;
  57. while (*it != '[' || balance != 0) {
  58. if (*it == ']') ++balance;
  59. if (*it == '[') --balance;
  60. --it;
  61. }
  62. }
  63. }
  64. ++it;
  65. }
  66. }
  67.  
  68. int main(int argc, char* argv[]) {
  69. execute(program);
  70. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
Hello World!