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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
// I thought it easier to dump information into a struct
// Rather than std::vector< std::vector<std::string> >
struct instruction
{
std::string command;
std::vector<std::string> args;
};
// Function splits a string into chunks by specify a delimiter,
// The function returns a vector of strings, these strings are instructions
std::vector<std::string> Split(const std::string str, const std::string delimiter, bool StripWS = 1)
{
std::vector<std::string> result;
// Remember positions of delimiters
size_t pos = 0, prev = pos;
pos = str.find_first_of(delimiter, pos);
// If a delimiter was found
while(pos != std::string::npos)
{
// Copy the token and store it
std::string token = str.substr(prev, pos-prev);
result.push_back(token);
//If a whitespace is next to delimiter ignore it, i.e command; command2
//^
if(StripWS && (str[pos+1] == ' ' || str[pos+1] == '\n')) pos++;
//Move past the delimiter
pos++;
// make prev equal start of next instruction
prev = pos;
//Attempt to find next delimiter
pos = str.find_first_of(delimiter, pos);
}
//Cut off last token
std::string token = str.substr(prev, pos-prev);
// If no information discard it
if(token != "" && token != " " && token != "\n") result.push_back(token);
// String is split into instructions, done
return result;
}
// This function, takes a vector of strings(The instructions) and splits them into an instruction struct
std::vector<instruction> Tokenize(const std::vector<std::string> instructions, const std::string delimiter)
{
std::vector<instruction> result;
// For each instruction in the vector
for(auto& i : instructions)
{
instruction ins;
// Split string into words
std::vector<std::string> args = Split(i, delimiter);
if(args.empty()) return result;
//First argument is the command
ins.command = args[0];
//Anything else a command argument
if(args.size() > 1)
for(unsigned int j = 1; j < args.size(); j++)
ins.args.push_back(args[j]);
result.push_back(ins);
}
return result;
}
// processes instructions
int Call(instruction);
int Interpret(std::string s)
{
// Split command into instructions
std::vector<std::string> tokens = Split(s, ";");
if(tokens.size() == 0) return 0;
else
{
// Split instruction into commands and arguments seperated by whitespace
std::vector<instruction> instructions = Tokenize(tokens, " ");
for(unsigned int i = 0; i < instructions.size(); i++)
{
std::cout << "Command = " << instructions[i].command << "\t";
for(unsigned int j = 0; j < instructions[i].args.size(); j++)
std::cout << "Arg(" << j << ") = " << instructions[i].args[j] << "\t";
std::cout << "\n";
if(!Call(instructions[i])) return 0;
}
}
return 1;
}
// Read a file and display it
void Read(std::string filename);
// Run a file as a script
void Run(std::string filename);
// Quit program, or stop script from running(like return)
void Quit();
int Call(instruction command)
{
if(command.command == "Read")
{
if(command.args.size() > 1)
{
std::cout << "Read: Expected 1 argument (filename) received " << command.args.size() << "\n";
}
else Read(command.args[0]);
}
else if(command.command == "Run")
{
if(command.args.size() > 1)
{
std::cout << "Read: Expected 1 argument (filename) received " << command.args.size() << "\n";
}
else Run(command.args[0]);
}
else if(command.command == "Quit") return 0;
return 1;
}
void Read(std::string filename)
{
std::cout << "Attempting to open file: " << filename << "...\n\n";
std::ifstream ifs(filename.c_str());
std::string file = "";
if(!ifs.is_open()) file = "Could not open file " + filename + "\n";
else
{
while(ifs)
{
std::string temp;
std::getline(ifs, temp);
file += temp;
file += "\n";
}
}
ifs.close();
std::cout << file << "\n\n";
}
void Run(std::string filename)
{
std::cout << "Attempting to open file: " << filename << "...\n\n";
std::ifstream ifs(filename.c_str());
std::string file = "";
if(!ifs.is_open()) file = "Could not open file " + filename + "\n";
else
{
std::cout << "Reading file...\n\n";
while(ifs.good())
{
std::string temp;
std::getline(ifs, temp, '\n');
file += temp;
}
}
ifs.close();
std::cout << "Translating...\n\n";
if(!Interpret(file)) return;
}
int main()
{
std::cout << "STSI: Script To Screen Interpreter - Version 0.0.1\n\n";
while(1)
{
std::string command;
std::cout << "STSI> ";
std::getline(std::cin,command,'\n');
if(!Interpret(command)) return 0;
}
return 1;
}
|