-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainstorm.h
105 lines (82 loc) · 3.06 KB
/
brainstorm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Brainstorm *
* by Mark Lagodych *
* *
* Brainstorm is a Brainf*ck extension *
* New: ! - write number (numeric analog of .) *
* ? - read number (numeric analog of ,) *
* %...% - comment (+-<>[],.!?$& are ignored inside) *
* $ - switch input/output to a virtual command line *
* & - switch i/o back to console window *
* *
* Full list: < > + - [ ] . , ! ? %% $ & *
* *
* Some optimisation ideas: https://habr.com/ru/post/321630/ *
* *
* See https://en.wikipedia.org/wiki/Brainfuck *
* https://esolangs.org/wiki/Brainfuck *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef BRAINSTORM_LANG
#define BRAINSTORM_LANG
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <map>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
typedef unsigned char cmd_t; // Command
typedef unsigned long ptr_t; // Pointer
typedef stringstream& cmdline_t; // Virtual command line
typedef void(*exe_t)(cmdline_t); // Virtual command executor
inline void clearCommandLine(cmdline_t commandLine) {
commandLine.str(string()); // Clear content
commandLine.clear(); // Clear flags
}
inline string commandLineToString(cmdline_t commandLine) {
return commandLine.str();
}
inline void setCommandLineContent(cmdline_t commandLine, string content) {
commandLine.str(content);
}
inline bool commandLineIsEmpty(cmdline_t commandLine) {
return commandLine.str().empty();
}
template<class mem_t>
class BrainstormInterpreter {
public:
// For source code
ptr_t ssize = 0;
char *src = nullptr;
// For compiled code
ptr_t csize = 0; // code size
cmd_t *cmd = nullptr; // cmd and arg must have same sizes - csize
ptr_t *arg = nullptr;
// For memory
ptr_t msize; // memory size
mem_t *mem = nullptr;
// For queries to a virtual command line
bool commandLineMode = false;
stringstream commandLine;
map<string, exe_t> executors;
void setCommand(string name, exe_t executor);
void delCommand(string name);
void exeCommand();
// Usual functions
BrainstormInterpreter();
~BrainstormInterpreter();
void loadCStr(const char* source);
int loadFile(string fileName);
int compile();
void allocMem(ptr_t memSize);
void run();
};
template class BrainstormInterpreter<unsigned char >;
template class BrainstormInterpreter<unsigned short>;
template class BrainstormInterpreter<unsigned long >;
#endif // BRAINSTORM_LANG