:(after "Types")
typedef size_t recipe_number;
:(before "End Globals")
map<string, recipe_number> Recipe_number;
map<recipe_number, recipe> Recipe;
recipe_number Next_recipe_number = 1;
:(before "End Types")
struct recipe {
string name;
vector<instruction> steps;
};
:(before "struct recipe")
struct instruction {
bool is_label;
string label;
string name;
recipe_number operation;
vector<reagent> ingredients;
vector<reagent> products;
instruction();
void clear();
string to_string() const;
};
:(before "struct instruction")
struct reagent {
vector<pair<string, vector<string> > > properties;
string name;
long long int value;
bool initialized;
vector<type_number> types;
reagent(string s);
reagent();
void set_value(long long int v) { value = v; initialized = true; }
string to_string() const;
};
:(before "struct reagent")
struct property {
vector<string> values;
};
:(before "End Globals")
map<index_t, long long int> Memory;
:(before "End Setup")
Memory.clear();
:(after "Types")
typedef size_t type_number;
:(before "End Globals")
map<string, type_number> Type_number;
map<type_number, type_info> Type;
type_number Next_type_number = 1;
:(code)
void setup_types() {
Type.clear(); Type_number.clear();
Type_number["literal"] = 0;
Next_type_number = 1;
type_number integer = Type_number["integer"] = Next_type_number++;
Type_number["location"] = Type_number["integer"];
Type[integer].name = "integer";
type_number address = Type_number["address"] = Next_type_number++;
Type[address].name = "address";
type_number boolean = Type_number["boolean"] = Next_type_number++;
Type[boolean].name = "boolean";
type_number character = Type_number["character"] = Next_type_number++;
Type[character].name = "character";
type_number array = Type_number["array"] = Next_type_number++;
Type[array].name = "array";
}
:(before "End One-time Setup")
setup_types();
:(before "End Types")
enum kind_of_type {
primitive,
container,
exclusive_container
};
struct type_info {
string name;
kind_of_type kind;
size_t size;
vector<vector<type_number> > elements;
vector<string> element_names;
type_info() :kind(primitive), size(0) {}
};
enum primitive_recipes {
IDLE = 0,
COPY,
MAX_PRIMITIVE_RECIPES,
};
:(code)
void setup_recipes() {
Recipe.clear(); Recipe_number.clear();
Recipe_number["idle"] = IDLE;
Recipe_number["copy"] = COPY;
}
:(before "End One-time Setup")
setup_recipes();
assert(MAX_PRIMITIVE_RECIPES < 100);
Next_recipe_number = 100;
:(before "End Test Run Initialization")
assert(Next_recipe_number < 1000);
:(before "End Setup")
Next_recipe_number = 1000;
^L
:(code)
instruction::instruction() :is_label(false), operation(IDLE) {}
void instruction::clear() { is_label=false; label.clear(); operation=IDLE; ingredients.clear(); products.clear(); }
reagent::reagent(string s) :value(0), initialized(false) {
istringstream in(s);
in >> std::noskipws;
while (!in.eof()) {
istringstream row(slurp_until(in, '/'));
row >> std::noskipws;
string name = slurp_until(row, ':');
vector<string> values;
while (!row.eof())
values.push_back(slurp_until(row, ':'));
properties.push_back(pair<string, vector<string> >(name, values));
}
name = properties[0].first;
for (index_t i = 0; i < properties[0].second.size(); ++i) {
types.push_back(Type_number[properties[0].second[i]]);
}
if (name == "_" && types.empty()) {
types.push_back(0);
properties[0].second.push_back("dummy");
}
}
reagent::reagent() :value(0), initialized(false) {
properties.push_back(pair<string, vector<string> >("", vector<string>()));
}
string reagent::to_string() const {
ostringstream out;
out << "{name: \"" << name << "\", value: " << value << ", type: ";
for (index_t i = 0; i < types.size(); ++i) {
out << types[i];
if (i < types.size()-1) out << "-";
}
if (!properties.empty()) {
out << ", properties: [";
for (index_t i = 0; i < properties.size(); ++i) {
out << "\"" << properties[i].first << "\": ";
for (index_t j = 0; j < properties[i].second.size(); ++j) {
out << "\"" << properties[i].second[j] << "\"";
if (j < properties[i].second.size()-1) out << ":";
}
if (i < properties.size()-1) out << ", ";
else out << "]";
}
}
out << "}";
return out.str();
}
string instruction::to_string() const {
if (is_label) return label;
ostringstream out;
for (index_t i = 0; i < products.size(); ++i) {
if (i > 0) out << ", ";
out << products[i].to_string();
}
if (!products.empty()) out << " <- ";
out << name << '/' << operation << ' ';
for (index_t i = 0; i < ingredients.size(); ++i) {
if (i > 0) out << ", ";
out << ingredients[i].to_string();
}
return out.str();
}
string slurp_until(istream& in, char delim) {
ostringstream out;
char c;
while (in >> c) {
if (c == delim) {
break;
}
out << c;
}
return out.str();
}
void dump_memory() {
map<int, int> ordered(Memory.begin(), Memory.end());
for (map<int, int>::iterator p = ordered.begin(); p != ordered.end(); ++p) {
cout << p->first << ": " << p->second << '\n';
}
}
:(before "End Includes")
#include <map>
using std::map;