Created
July 2, 2014 13:12
-
-
Save pastagatsan/ccff553d3848156bbd00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#alloc 1 | |
load a 5 | |
print a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <string> | |
#include <sstream> | |
#include "Pleb.h" | |
#include "plebscript.h" | |
int main(int argc, char * argv[]) | |
{ | |
if (argc < 2) | |
{ | |
std::cout << "Too few arguments. Usage: plebs [file]\n"; | |
return 1; | |
} | |
std::ifstream file(argv[1]); | |
std::string line; | |
Pleb pleb(); | |
if (file.is_open()) | |
{ | |
int linec = 0; | |
while (getline(file, line)) | |
{ | |
if (linec == 0) | |
{ | |
std::string tokens[2]; | |
split(line, tokens); | |
if (tokens[0] == "#alloc") | |
{ | |
pleb.init(atoi(std::string::c_str(tokens[1])); | |
} | |
continue; | |
} | |
exec(pleb, line); | |
} | |
} | |
else | |
{ | |
std::cout << "The file could not be opened.\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Pleb.h" | |
Pleb::Pleb() | |
{ | |
PRINT_INT = 0; | |
PRINT_HEX = 1; | |
PRINT_CHAR = 2; | |
} | |
Pleb::init(int mem) | |
{ | |
memory = new unsigned char[mem]; | |
} | |
Pleb::~Pleb() | |
{ | |
delete[] memory; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PLEB | |
#define PLEB | |
class Pleb | |
{ | |
public: | |
/* These print modes control how the program prints. | |
* PRINT_INT = Print integers. | |
* PRINT_HEX = Print hexadecimal integers. | |
* PRINT_CHAR = Print characters. | |
*/ | |
int PRINT_INT, | |
PRINT_HEX, | |
PRINT_CHAR; | |
/* Print mode. */ | |
int print_mode; | |
/* These are 4 general purpose variables. */ | |
unsigned char a, b, c, d; | |
/* Memory. This can be used for increased space. | |
* The size will have to be initialized at the | |
* beginning of the script. | |
*/ | |
unsigned char * memory; | |
Pleb(); | |
void init(int); | |
~Pleb(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void split(const std::string str, std::string tokens[]) | |
{ | |
std::string buf; // Have a buffer string | |
std::stringstream ss(str); // Insert the string into a stream | |
int i = 0; | |
while (ss >> buf) | |
{ | |
std::cout << buf << "\n"; | |
tokens[i] = buf; | |
i++; | |
} | |
} | |
void exec(Pleb::Pleb pleb, std::string line) | |
{ | |
std::string tokens[3]; | |
split(line, tokens); | |
if (tokens[0] == "load") | |
{ | |
/* Load instruction expects 2 parameters. */ | |
load(pleb, tokens[1], tokens[2]); | |
} | |
else if (tokens[0] == "print") | |
{ | |
print(pleb, tokens[1]); | |
} | |
} | |
void load(Pleb::Pleb pleb, std::string s, std::string value) | |
{ | |
int val = atoi(c_str(value)); | |
if (s == "A") | |
{ | |
pleb.a = val; | |
} | |
else if (s == "B") | |
{ | |
pleb.b = val; | |
} | |
else if (s == "C") | |
{ | |
pleb.c = val; | |
} | |
else if (s == "D") | |
{ | |
pleb.d = val; | |
} | |
} | |
void print(Pleb::Pleb pleb, unsigned char x) | |
{ | |
if (x == 'A') | |
{ | |
std::cout << pleb.a; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PLEBSCRIPT_H | |
#define PLEBSCRIPT_H | |
#include <string> | |
#include <vector> | |
#include <iostream> | |
#include <vector> | |
#include <sstream> | |
#include "plebscript.h" | |
#include "Pleb.h" | |
void split(const std::string&, std::string[]); | |
void exec(Pleb, std::string); | |
void load(Pleb, std::string, std::string[]); | |
void print(Pleb, std::string); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment