Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81568 / BF_Interpreter / bf_interpreter.cpp
diff options
context:
space:
mode:
Commitaeb0d4f887a0c18c36b59acc7b5df08e62020c50 (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:32:48 +0300
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:33:38 +0300
Filesb6abdc6e1810326b587f2a160f39570f1f568f27 /homework/81568/BF_Interpreter/bf_interpreter.cpp
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar.gz
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.zip
Parent154b7cd30f21af1554ca26986ff1643e28857727 (diff)
Removed old homeworks
1 files changed, 0 insertions, 114 deletions
-rw-r--r--homework/81568/BF_Interpreter/bf_interpreter.cpp114
diff --git a/homework/81568/BF_Interpreter/bf_interpreter.cpp b/homework/81568/BF_Interpreter/bf_interpreter.cpp
deleted file mode 100644
index 28d4c18..0000000
--- a/homework/81568/BF_Interpreter/bf_interpreter.cpp
+++ /dev/null
@@ -1,114 +0,0 @@
-#include "bf_interpreter.h"
-
-#include <cmath>
-#include <cstring>
-#include <iostream>
-#include <stack>
-
-#ifndef MEMSIZE
-#define MEMSIZE 30000
-#endif
-
-BFInterpreter::BFInterpreter(const char *program, std::istream &input, std::ostream &output) :
- memory_blocks(nullptr), p(nullptr),
- program(nullptr), input(input), output(output),
- memory_size(MEMSIZE) {
-
- this->program = new char[strlen(program) + 1];
- strncpy(this->program, program, strlen(program) + 1);
- pp = this->program;
-
- memory_blocks = new uchar[memory_size];
- for (int i = 0; i < memory_size; ++i) {
- memory_blocks[i] = 0;
- }
- p = memory_blocks;
-}
-
-BFInterpreter::~BFInterpreter() {
- delete [] memory_blocks;
- delete [] program;
-}
-
-void BFInterpreter::addMemory() {
- uchar *new_memory = new uchar[memory_size + ((size_t) log2(memory_size))];
- for (int i = 0; i < memory_size; ++i) {
- new_memory[i] = memory_blocks[i];
- }
- delete [] memory_blocks;
- memory_blocks = new_memory;
- memory_size += log2(memory_size);
-}
-
-void BFInterpreter::compile() {
- while (*pp) {
- switch (*pp) {
- case '[':
- ++pp;
- loop();
- break;
- case ']': // that will only happen if code is wrong
- printf("Syntax error!\n");
- return;
- default:
- proccessNonLoopCmd(*pp);
- }
- ++pp;
- }
- output.put('\n');
-}
-
-void BFInterpreter::loop() {
- std::stack<char*> loops;
- loops.push(pp);
-
- char *body = loops.top();
- while (!loops.empty()) {
- while (*body != ']' && *body != '[') {
- proccessNonLoopCmd(*body);
- ++body;
- }
- if (*body == ']') {
- if (!*p) { /* End of a loop */
- loops.pop();
- ++body;
- } else { /* Loop has not ended. Return to the beginning of the current loop */
- body = loops.top();
- }
- } else { /* Start of a new loop */
- loops.push(body + 1);
- ++body;
- }
- }
- pp = --body;
- return;
-}
-
-void BFInterpreter::proccessNonLoopCmd(char c) {
- switch (c) {
- case '+':
- ++*p;
- break;
- case '-':
- --*p;
- break;
- case '.':
- output.put(*p);
- break;
- case ',':
- *p = input.get();
- break;
- case '<':
- if (p == memory_blocks)
- p = memory_blocks + (memory_size - 1);
- else
- --p;
- break;
- case '>':
- if (p == memory_blocks + (memory_size - 1))
- p = memory_blocks;
- else
- ++p;
- break;
- }
-}