Skip to content

Instantly share code, notes, and snippets.

@pastagatsan
Created July 2, 2014 13:12
Show Gist options
  • Save pastagatsan/ccff553d3848156bbd00 to your computer and use it in GitHub Desktop.
Save pastagatsan/ccff553d3848156bbd00 to your computer and use it in GitHub Desktop.
#alloc 1
load a 5
print a
#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";
}
}
#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;
}
#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
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;
}
}
#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