Files
jailbasic/types.cpp

60 lines
2.3 KiB
C++

#include "types.h"
#include "variables.h"
#include <vector>
struct t_struct {
std::string name;
int size;
std::vector<t_variable> members;
};
static std::vector<t_struct> types;
void types_init() {
types.clear();
const t_struct type_number {"number", 4}; types.push_back(type_number);
const t_struct type_string {"string", 4}; types.push_back(type_string);
}
const int types_add(const std::string name) {
for (auto t : types) if (t.name == name) return -1;
t_struct new_type; new_type.name = name;
types.push_back(new_type);
return types.size()-1;
}
const int types_exist(const std::string name) {
for (int i = 0; i < types.size(); i++) if (types[i].name == name) return i;
return -1;
}
const int types_get_length(const uint32_t type) {
if (type >= types.size()) return -1; // ERROR INTERN: No deuria donar-se mai
return types[type].size;
}
const int types_add_variable(const uint32_t type, const std::string name, const int var_type, const int length) {
if (type >= types.size()) return -1; // ERROR INTERN: No deuria donar-se mai
for (auto m : types[type].members) if (m.name == name) return -1;
const t_variable new_var {name, var_type, length};
types[type].members.push_back(new_var);
types[type].size += types_get_length(var_type) + length;
return types[type].members.size()-1;
}
const int types_variable_exists(const uint32_t type, const std::string name) {
if (type >= types.size()) return -1; // ERROR INTERN: No deuria donar-se mai
for (int i = 0; i < types[type].members.size(); i++) if (types[type].members[i].name == name) return i;
return -1;
}
const int types_get_variable_type(const uint32_t type, const uint32_t variable) {
if (type >= types.size()) return -1; // ERROR INTERN: No deuria donar-se mai
if (variable >= types[type].members.size()) return -1; // ERROR INTERN: No deuria donar-se mai
return types[type].members[variable].type;
}
const int types_get_variable_length(const uint32_t type, const uint32_t variable) {
if (type >= types.size()) return -1; // ERROR INTERN: No deuria donar-se mai
if (variable >= types[type].members.size()) return -1; // ERROR INTERN: No deuria donar-se mai
return types[type].members[variable].length;
}