Skip to content

Instantly share code, notes, and snippets.

@atg
Created November 17, 2015 00:26
Show Gist options
  • Save atg/da2563e301c2bd2f85a9 to your computer and use it in GitHub Desktop.
Save atg/da2563e301c2bd2f85a9 to your computer and use it in GitHub Desktop.
// A symbol represents a component of a scope e.g. "js" or "string"
// Symbols are stored in the SHSymbolRegistry in an array
// Components can never be removed, they last the life of the process.
using SHSymbol = uint32_t;
struct SHSymbolRecord {
SHSymbol key;
std::string value;
SHSymbolRecord(SHSymbol key, std::string value) : key(key), value(value) { }
};
struct SHSymbolRegistry {
public:
using ReverseMapType = std::unordered_map<std::string, SHSymbolRecord*>;
using RecordsType = std::vector<SHSymbolRecord*>;
SHSymbolRegistry& sharedInstance() {
static SHSymbolRegistry _shared;
return shared;
}
const RecordsType& getRecords() const { return records };
const ReverseMapType& getReverseMap() const { return reverseMap };
// This function is slow
SHSymbol addSymbol(const std::string& str) {
const auto& it = records.find(str);
if (it != records.end())
return it->key;
SHSymbol key = records.size();
auto* record = new SHSymbolRecord(key, str);
records.append(record);
reverseMap[str] = record;
return key;
}
private:
SHSymbolRegistry() { }
RecordsType records;
ReverseMapType reverseMap;
};
// A scope is a sequence of components
// It may own its storage, or have borrowed it from another scope, or perhaps
struct SHSymbolArray {
SHSymbol* symbols;
uint32_t refcount;
};
// We need different kinds of scope types and then conversion functions between them??
/*struct SHScope {
SHSymbolArray* symbolArray;
uint16_t n : 15;
bool owned : 1;
~SHScope() {
if (owned) delete symbols;
}
void sort() {
// insertion sort
}
};*/
// What can you do with a scope?
// union: x | y
// add: x | {y}
// to string
// from string
// a == b?
// contains symbol?
// contains scope?
// Some scopes are transient, others are permanent
using SHScopeLength = int;
struct SHPermaScope {
static SHPermaScope FromString(const char* str) { }
};
struct SHTransientScope {
static SHTransientScope FromString(const char* str) { }
};
namespace SH {
template<typename T>
bool containsSymbol(const T& scope, SHSymbol symbol) {
for (SHSymbol x : scope) {
if (x == symbol) return true;
}
return false;
}
SHScopeLength len(const SHPermaScope& scope) { }
SHScopeLength len(const SHTransientScope& scope) { }
template<typename A, typename B>
SHTransientScope union(const A& scopeA, const B& scopeB) {
}
template<typename A, typename B>
SHTransientScope add(const A& scope, SHSymbol symbol) {
}
template<typename A, typename B>
bool operator == (const A& scopeA, const B& scopeB) {
}
template<typename T>
std::string toCPPString(const T& scope) { }
template<typename T>
NSString* toNSString(const T& scope) { }
}
// --- Lines and zones ---
struct SHZone {
};
struct SHContext {
std::vector<SHZone> zones;
};
// Line marker
struct SHLine {
// A line is a list of zones
std::vector<SHZone> zone;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment