Skip to content

Commit

Permalink
test conditional compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 2, 2024
1 parent 9630034 commit 80fa67e
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions compiler/layeredtable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import std/tables
import ast

type
LayeredIdTable* {.acyclic.} = ref object
LayeredIdTableObj* {.acyclic.} = object
topLayer*: TypeMapping
nextLayer*: LayeredIdTable
nextLayer*: ref LayeredIdTableObj
previousLen*: int # used to track if bindings were added

const useRef = defined(gcMarkAndSweep)

when useRef:
type LayeredIdTable* = ref LayeredIdTableObj
else:
type LayeredIdTable* = LayeredIdTableObj

proc initLayeredTypeMap*(pt: sink TypeMapping = initTypeMapping()): LayeredIdTable =
result = LayeredIdTable(topLayer: pt, nextLayer: nil)

Expand All @@ -17,21 +24,41 @@ proc currentLen*(pt: LayeredIdTable): int =
pt.previousLen + pt.topLayer.len

proc newTypeMapLayer*(pt: LayeredIdTable): LayeredIdTable =
result = LayeredIdTable(topLayer: initTable[ItemId, PType](), nextLayer: pt, previousLen: pt.currentLen)
result = LayeredIdTable(topLayer: initTable[ItemId, PType](), previousLen: pt.currentLen)
when useRef:
result.nextLayer = pt
else:
new(result.nextLayer)
result.nextLayer[] = pt

proc setToPreviousLayer*(pt: var LayeredIdTable) {.inline.} =
pt = pt.nextLayer
when useRef:
pt = pt.nextLayer
else:
when defined(gcDestructors):
pt = pt.nextLayer[]
else:
pt = shallowCopy(pt.nextLayer[])

proc lookup(typeMap: LayeredIdTable, key: ItemId): PType =
proc lookup(typeMap: ref LayeredIdTableObj, key: ItemId): PType =
result = nil
var tm = typeMap
while tm != nil:
result = getOrDefault(tm.topLayer, key)
if result != nil: return
tm = tm.nextLayer

template lookup*(typeMap: LayeredIdTable, key: PType): PType =
template lookup*(typeMap: ref LayeredIdTableObj, key: PType): PType =
lookup(typeMap, key.itemId)

proc put*(typeMap: LayeredIdTable, key, value: PType) {.inline.} =
when not useRef:
proc lookup(typeMap: LayeredIdTableObj, key: ItemId): PType {.inline.} =
result = getOrDefault(typeMap.topLayer, key)
if result == nil and typeMap.nextLayer != nil:
result = lookup(typeMap.nextLayer, key)

template lookup*(typeMap: LayeredIdTableObj, key: PType): PType =
lookup(typeMap, key.itemId)

proc put*(typeMap: var LayeredIdTable, key, value: PType) {.inline.} =
typeMap.topLayer[key.itemId] = value

0 comments on commit 80fa67e

Please sign in to comment.