Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <cstdlib>
- #include <windows.h>
- #define PUSH 0x0 //Implicit command for pushing signed 64 bit integers onto the stack
- #define POP 0x1 //Pop the top item off of the stack
- #define PULL 0x2 //Pull arbitrary item off of the stack
- #define SWAP 0x3 //Swap the first two items on the stack
- #define SWITCH 0x4 //Switch out two arbitrary items on the stack
- #define GET 0x5 //Put arbitrary item on top of the stack
- #define INPUT 0x6 //Get arbitrary input
- #define PRINT_A 0x7 //Print the top item of the stack as a character and pop it off of the stack
- #define PRINT_I 0x8 //Print the top item of the stack as a number and pop it off of the stack
- #define DUP 0x9 //Duplicate the first item on the stack
- #define EMPTY 0xA //Empty the program's memory
- #define JUMP 0xB //Jump to an arbitrary position
- #define IF 0xC //If the top item of the stack is nonzero, pop it off. Continue as normal. Otherwise, pop it off then jump to an arbitrary code location specified by the top item of the stack
- #define EXIT 0xD //Stop execution and return the top item of the stack to the intepretter.
- #define ADD 0xE
- #define SUB 0xF
- #define MUL 0x10
- #define DIV 0x11
- #define MOD 0x12
- #define GREATER 0x13
- #define LESS 0x14
- #define EQ 0x15
- #define LESSEQ 0x16
- #define GREATEREQ 0x17
- #define BAND 0x18
- #define BNOT 0x19
- #define BOR 0x1A
- #define LOR 0x1B
- #define LAND 0x1C
- #define LNOT 0x1D
- #define MAX_MEM 100000 //100,000 * 8 = 800kb Memory max
- struct CMD;
- struct Program;
- signed long long Execute(Program* PRG);
- bool Parse(std::string Data, Program* PRG);
- std::string LowerCase(std::string Data);
- struct CMD{
- unsigned long OP;
- signed long long v;
- };
- struct Program{
- std::vector<CMD> commands;
- std::vector<signed long long> memory;
- };
- int main()
- {
- while(1){
- Program PRG;
- char* buffer;
- system("cls");
- std::cout << "\n-- Esoteric CMDLine> ";
- std::cin.getline(buffer,4096);
- std::string Input = std::string(buffer);
- if(LowerCase(Input) == "quit"){
- return 1;
- }
- else if(Parse(LowerCase(Input), &PRG)){
- std::cout << "\nThe process returned: " << Execute(&PRG) << "\nWith stack size: " << PRG.memory.size() << "\nPress enter to continue..." << std::endl;
- std::cin.ignore();
- std::cin.get();
- }
- else{
- std::cout << "\nThe Program was not formatted correctly..." << std::endl;
- std::cin.get();
- }
- }
- return 0; //Crazy freaking error
- }
- bool Parse(std::string Data, Program* PRG){
- std::cout << "\nParsing..." << std::endl;
- std::string buffer = "";
- CMD temp;
- for(unsigned int i = 0;i<Data.length();++i){
- if(Data[i] != ' '){
- buffer += Data[i];
- }
- if(Data[i] == ' ' || i == (Data.length()-1)){
- if(buffer.length() > 0){
- if(buffer == "pop"){
- temp.OP = POP;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "pull"){
- temp.OP = PULL;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "swap"){
- temp.OP = SWAP;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "switch"){
- temp.OP = SWITCH;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "get"){
- temp.OP = GET;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "input"){
- temp.OP = INPUT;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "print-a"){
- temp.OP = PRINT_A;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "print-i"){
- temp.OP = PRINT_I;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "dup"){
- temp.OP = DUP;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "empty"){
- temp.OP = EMPTY;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "jump"){
- temp.OP = JUMP;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "if"){
- temp.OP = IF;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "exit"){
- temp.OP = EXIT;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "+"){
- temp.OP = ADD;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "-"){
- temp.OP = SUB;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "*"){
- temp.OP = MUL;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "/"){
- temp.OP = DIV;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "%"){
- temp.OP = MOD;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == ">"){
- temp.OP = GREATER;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "<"){
- temp.OP = LESS;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "="){
- temp.OP = EQ;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == ">="){
- temp.OP = GREATEREQ;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "<="){
- temp.OP = LESSEQ;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "&"){
- temp.OP = BAND;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "~"){
- temp.OP = BNOT;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "|"){
- temp.OP = BOR;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "||"){
- temp.OP = LOR;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "&&"){
- temp.OP = LAND;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer == "!"){
- temp.OP = LNOT;
- temp.v = 0;
- PRG->commands.push_back(temp);
- }
- else if(buffer[0] == '0' || buffer[0] == '1' || buffer[0] == '2' || buffer[0] == '3' ||
- buffer[0] == '4' || buffer[0] == '5' || buffer[0] == '6' || buffer[0] == '7' ||
- buffer[0] == '8' || buffer[0] == '9'){
- temp.OP = PUSH;
- temp.v = atoi(buffer.c_str());
- PRG->commands.push_back(temp);
- }
- else{
- return 0;
- }
- }
- buffer = "";
- }
- }
- if(PRG->commands.size() < 1){
- return 0;
- }
- return 1;
- }
- signed long long Execute(Program* PRG){
- std::cout << "\nExecuting..." << std::endl;
- for(unsigned int i = 0;i<PRG->commands.size();++i){
- switch (PRG->commands[i].OP){
- case PUSH:
- if(PRG->memory.size() < MAX_MEM){
- PRG->memory.insert(PRG->memory.begin(),PRG->commands[i].v);
- }
- break;
- case POP:
- if(PRG->memory.size() > 0){
- PRG->memory.erase(PRG->memory.begin());
- }
- break;
- case PULL:
- if(PRG->memory.size() > PRG->memory[PRG->memory[0]]){
- PRG->memory.erase(PRG->memory.begin()+PRG->memory[PRG->memory[0]]);
- }
- break;
- case SWAP:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0];
- PRG->memory[0] = PRG->memory[1];
- PRG->memory[1] = t;
- }
- break;
- case SWITCH:
- if(PRG->memory.size() > PRG->memory[0] && PRG->memory.size() > PRG->memory[1]){
- signed long long t = PRG->memory[PRG->memory[0]];
- PRG->memory[PRG->memory[0]] = PRG->memory[PRG->memory[1]];
- PRG->memory[PRG->memory[1]] = t;
- }
- break;
- case GET:
- if(PRG->memory.size() > PRG->memory[0]){
- signed long long t = PRG->memory[PRG->memory[0]];
- PRG->memory.erase(PRG->memory.begin()+PRG->memory[0]);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case INPUT:
- signed long long t;
- std::cout << "> ";
- std::cin >> t;
- PRG->memory.insert(PRG->memory.begin(), t);
- std::cout << "\n";
- break;
- case PRINT_A:
- std::cout << (char)PRG->memory[0];
- PRG->memory.erase(PRG->memory.begin());
- break;
- case PRINT_I:
- std::cout << PRG->memory[0];
- PRG->memory.erase(PRG->memory.begin());
- break;
- case DUP:
- PRG->memory.insert(PRG->memory.begin(), PRG->memory[0]);
- break;
- case EMPTY:
- PRG->memory.empty();
- break;
- case JUMP:
- if(PRG->commands.size() > PRG->memory[0]){
- i = PRG->memory[0];
- }
- break;
- case IF:
- if(PRG->memory[0] == 0){
- if(PRG->commands.size() > PRG->memory[0]){
- i = PRG->memory[0];
- }
- }
- break;
- case EXIT:
- if(PRG->memory.size() > 0){
- return PRG->memory[0];
- }
- else{
- return 0x0;
- }
- break;
- case ADD:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] + PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case SUB:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] - PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case MUL:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] * PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case DIV:
- if(PRG->memory.size() > 1 && PRG->memory[1] != 0){
- signed long long t = PRG->memory[0] / PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case MOD:
- if(PRG->memory.size() > 1 && PRG->memory[1] != 0){
- signed long long t = PRG->memory[0] % PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case GREATER:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] > PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case LESS:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] < PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case EQ:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] == PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case LESSEQ:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] <= PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case GREATEREQ:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] >= PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case BAND:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] & PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case BNOT:
- if(PRG->memory.size() > 0){
- signed long long t = ~PRG->memory[0];
- PRG->memory.erase(PRG->memory.begin());
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case BOR:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] | PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case LOR:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] || PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case LAND:
- if(PRG->memory.size() > 1){
- signed long long t = PRG->memory[0] && PRG->memory[1];
- PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- case LNOT:
- if(PRG->memory.size() > 0){
- signed long long t = !PRG->memory[0];
- PRG->memory.erase(PRG->memory.begin());
- PRG->memory.insert(PRG->memory.begin(), t);
- }
- break;
- }
- }
- }
- std::string LowerCase(std::string Data){
- for(unsigned int i = 0;i<Data.length();++i){
- Data[i] = tolower(Data[i]);
- }
- return Data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement