Skip to content

Instantly share code, notes, and snippets.

@kristianmandrup
Last active February 29, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristianmandrup/2b05315477b5d9c836751f34d5346650 to your computer and use it in GitHub Desktop.
Save kristianmandrup/2b05315477b5d9c836751f34d5346650 to your computer and use it in GitHub Desktop.
Nim type/id table display
type
TIdTableEntry = object
id: string
genId: string
startIndex: int
endIndex: int
PIdTableEntry = ref TIdTableEntry
TIdLookupTable = object
idMap: TableRef[string, PIdTableEntry]
currentId: string
PIdLookupTable = ref TIdLookupTable
TTypeLookupTable = object
typeMap: TableRef[string, PIdLookupTable]
currentType: string
PTypeLookupTable = ref TTypeLookupTable
proc newIdTableEntry(startIndex: int = 0, id: string = "", genId: string = ""): PIdTableEntry =
new(result)
result.startIndex = startIndex
result.id = id
result.genId = genId
proc newIdLookupTable(): PIdLookupTable =
new(result)
result.idMap = newTable[string, PIdTableEntry]()
proc newTypeLookupTable(): PTypeLookupTable =
new(result)
# I think my problem is bad initialisation of here!?
result.typeMap = newTable[string, PIdLookupTable]()
proc `$`(entry: PIdTableEntry): string =
$(entry.id, entry.genId, entry.startIndex, entry.endIndex)
proc `$`(table: PIdLookupTable): string =
result = "idMap:"
for k, v in table.idMap.pairs:
result.add(k & ": " & v & "\n")
result.add("currentId: " & table.currentId
proc `$`(self: PTypeLookupTable): string =
result = "typeMap:"
for k, v in self.typeMap.pairs:
result.add(k & ":" & v & "\n")
result.add("currentType: " & self.currentType)
var typeTable = newTypeLookupTable()
var idTable = newIdLookupTable()
var entry = newIdTableEntry(startIndex = 1, id = "a", genId = "a123")
idTable["a"] = entry
typeTable["property"] = idTable
# echo idTable
# echo typeTable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment