From 2fda95a4d630aa8b293f16e4f21471f7ee8e743a Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 13 Oct 2015 03:03:05 +0200 Subject: [PATCH] added getOrDefault; bootstrapping works again --- compiler/importer.nim | 4 +- compiler/main.nim | 2 +- compiler/nimblecmd.nim | 2 +- compiler/options.nim | 2 +- compiler/vmmarshal.nim | 2 +- lib/pure/collections/intsets.nim | 2 + lib/pure/collections/tables.nim | 71 +++++++++++++++++--------------- lib/pure/strtabs.nim | 5 +++ lib/pure/xmltree.nim | 2 +- 9 files changed, 52 insertions(+), 40 deletions(-) diff --git a/compiler/importer.nim b/compiler/importer.nim index 657f41eab..c121059fd 100644 --- a/compiler/importer.nim +++ b/compiler/importer.nim @@ -121,7 +121,7 @@ proc importAllSymbolsExcept(c: PContext, fromMod: PSym, exceptSet: IntSet) = if s.kind != skEnumField: if s.kind notin ExportableSymKinds: internalError(s.info, "importAllSymbols: " & $s.kind) - if s.name.id notin exceptSet: + if exceptSet.isNil or s.name.id notin exceptSet: rawImportSymbol(c, s) s = nextIter(i, fromMod.tab) @@ -138,7 +138,7 @@ proc importForwarded(c: PContext, n: PNode, exceptSet: IntSet) = let s = a.sym if s.kind == skModule: importAllSymbolsExcept(c, s, exceptSet) - elif s.name.id notin exceptSet: + elif exceptSet.isNil or s.name.id notin exceptSet: rawImportSymbol(c, s) of nkExportExceptStmt: localError(n.info, errGenerated, "'export except' not implemented") diff --git a/compiler/main.nim b/compiler/main.nim index 3bb6fc343..2ee07a443 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -15,7 +15,7 @@ import wordrecg, sem, semdata, idents, passes, docgen, extccomp, cgen, jsgen, json, nversion, platform, nimconf, importer, passaux, depends, vm, vmdef, types, idgen, - tables, docgen2, service, parser, modules, ccgutils, sigmatch, ropes, lists + docgen2, service, parser, modules, ccgutils, sigmatch, ropes, lists from magicsys import systemModule, resetSysTypes diff --git a/compiler/nimblecmd.nim b/compiler/nimblecmd.nim index 9b647e67d..c6c2ab058 100644 --- a/compiler/nimblecmd.nim +++ b/compiler/nimblecmd.nim @@ -48,7 +48,7 @@ proc addPackage(packages: StringTableRef, p: string) = let name = p.substr(0, x-1) if x < p.len: let version = p.substr(x+1) - if packages[name] <. version: + if packages.getOrDefault(name) <. version: packages[name] = version else: packages[name] = latest diff --git a/compiler/options.nim b/compiler/options.nim index 23c76acc5..98224a11d 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -173,7 +173,7 @@ proc existsConfigVar*(key: string): bool = result = hasKey(gConfigVars, key) proc getConfigVar*(key: string): string = - result = gConfigVars[key] + result = gConfigVars.getOrDefault key proc setConfigVar*(key, val: string) = gConfigVars[key] = val diff --git a/compiler/vmmarshal.nim b/compiler/vmmarshal.nim index 1670dd4c4..c08c5d249 100644 --- a/compiler/vmmarshal.nim +++ b/compiler/vmmarshal.nim @@ -239,7 +239,7 @@ proc loadAny(p: var JsonParser, t: PType, result = newNode(nkNilLit) next(p) of jsonInt: - result = tab[p.getInt] + result = tab.getOrDefault(p.getInt) if result.isNil: raiseParseErr(p, "cannot load object with address " & $p.getInt) next(p) diff --git a/lib/pure/collections/intsets.nim b/lib/pure/collections/intsets.nim index 38bc9d462..603a4b595 100644 --- a/lib/pure/collections/intsets.nim +++ b/lib/pure/collections/intsets.nim @@ -138,6 +138,8 @@ proc initIntSet*: IntSet = result.counter = 0 result.head = nil +proc isNil*(x: IntSet): bool {.inline.} = x.head.isNil + proc assign*(dest: var IntSet, src: IntSet) = ## copies `src` to `dest`. `dest` does not need to be initialized by ## `initIntSet`. diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index bb6440652..37d52b6de 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -96,18 +96,10 @@ proc len*[A, B](t: Table[A, B]): int = ## returns the number of keys in `t`. result = t.counter -proc `[]`*[A, B](t: Table[A, B], key: A): B = - ## retrieves the value at ``t[key]``. If `key` is not in `t`, - ## default empty value for the type `B` is returned - ## and no exception is raised. One can check with ``hasKey`` whether the key - ## exists. - var hc: Hash - var index = rawGet(t, key, hc) - if index >= 0: result = t.data[index].val - -proc mget*[A, B](t: var Table[A, B], key: A): var B = +template get(t, key): untyped {.immediate.} = ## retrieves the value at ``t[key]``. The value can be modified. ## If `key` is not in `t`, the ``KeyError`` exception is raised. + mixin rawGet var hc: Hash var index = rawGet(t, key, hc) if index >= 0: result = t.data[index].val @@ -117,6 +109,12 @@ proc mget*[A, B](t: var Table[A, B], key: A): var B = else: raise newException(KeyError, "key not found") +template getOrDefaultImpl(t, key): untyped {.immediate.} = + mixin rawGet + var hc: Hash + var index = rawGet(t, key, hc) + if index >= 0: result = t.data[index].val + proc `[]`*[A, B](t: Table[A, B], key: A): B = ## retrieves the value at ``t[key]``. If `key` is not in `t`, the ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether @@ -134,6 +132,8 @@ proc mget*[A, B](t: var Table[A, B], key: A): var B {.deprecated.} = ## instead. get(t, key) +proc getOrDefault*[A, B](t: Table[A, B], key: A): B = getOrDefaultImpl(t, key) + iterator allValues*[A, B](t: Table[A, B]; key: A): B = ## iterates over any value in the table `t` that belongs to the given `key`. var h: Hash = hash(key) and high(t.data) @@ -305,6 +305,8 @@ proc mget*[A, B](t: TableRef[A, B], key: A): var B {.deprecated.} = ## Use ```[]``` instead. t[][key] +proc getOrDefault*[A, B](t: TableRef[A, B], key: A): B = getOrDefault(t[], key) + proc mgetOrPut*[A, B](t: TableRef[A, B], key: A, val: B): var B = ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way ## returning a value which can be modified. @@ -416,20 +418,6 @@ proc rawGetDeep[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int {.inline proc rawGet[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int = rawGetImpl() -proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B = - ## retrieves the value at ``t[key]``. If `key` is not in `t`, - ## default empty value for the type `B` is returned - ## and no exception is raised. One can check with ``hasKey`` whether the key - ## exists. - var hc: Hash - var index = rawGet(t, key, hc) - if index >= 0: result = t.data[index].val - else: - when compiles($key): - raise newException(KeyError, "key not found: " & $key) - else: - raise newException(KeyError, "key not found") - proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B = ## retrieves the value at ``t[key]``. If `key` is not in `t`, the ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether @@ -438,11 +426,18 @@ proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B = proc `[]`*[A, B](t: var OrderedTable[A, B], key: A): var B = ## retrieves the value at ``t[key]``. The value can be modified. - ## If `key` is not in `t`, the ``EInvalidKey`` exception is raised. - var hc: Hash - var index = rawGet(t, key, hc) - if index >= 0: result = t.data[index].val - else: raise newException(KeyError, "key not found: " & $key) + ## If `key` is not in `t`, the ``KeyError`` exception is raised. + get(t, key) + +proc mget*[A, B](t: var OrderedTable[A, B], key: A): var B {.deprecated.} = + ## retrieves the value at ``t[key]``. The value can be modified. + ## If `key` is not in `t`, the ``KeyError`` exception is raised. + ## Use ```[]``` instead. + get(t, key) + +proc getOrDefault*[A, B](t: OrderedTable[A, B], key: A): B = + getOrDefaultImpl(t, key) + proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool = ## returns true iff `key` is in the table `t`. @@ -612,6 +607,9 @@ proc mget*[A, B](t: OrderedTableRef[A, B], key: A): var B {.deprecated.} = ## Use ```[]``` instead. result = t[][key] +proc getOrDefault*[A, B](t: OrderedTableRef[A, B], key: A): B = + getOrDefault(t[], key) + proc mgetOrPut*[A, B](t: OrderedTableRef[A, B], key: A, val: B): var B = ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way ## returning a value which can be modified. @@ -711,7 +709,7 @@ proc rawGet[A](t: CountTable[A], key: A): int = h = nextTry(h, high(t.data)) result = -1 - h # < 0 => MISSING; insert idx = -1 - result -template get[A](t: CountTable[A], key: A): int {.immediate.} = +template ctget(t, key: untyped): untyped {.immediate.} = var index = rawGet(t, key) if index >= 0: result = t.data[index].val else: @@ -724,18 +722,22 @@ proc `[]`*[A](t: CountTable[A], key: A): int = ## retrieves the value at ``t[key]``. If `key` is not in `t`, ## the ``KeyError`` exception is raised. One can check with ``hasKey`` ## whether the key exists. - get(t, key) + ctget(t, key) proc `[]`*[A](t: var CountTable[A], key: A): var int = ## retrieves the value at ``t[key]``. The value can be modified. ## If `key` is not in `t`, the ``KeyError`` exception is raised. - get(t, key) + ctget(t, key) proc mget*[A](t: var CountTable[A], key: A): var int {.deprecated.} = ## retrieves the value at ``t[key]``. The value can be modified. ## If `key` is not in `t`, the ``KeyError`` exception is raised. ## Use ```[]``` instead. - get(t, key) + ctget(t, key) + +proc getOrDefault*[A](t: CountTable[A], key: A): int = + var index = rawGet(t, key) + if index >= 0: result = t.data[index].val proc hasKey*[A](t: CountTable[A], key: A): bool = ## returns true iff `key` is in the table `t`. @@ -882,6 +884,9 @@ proc mget*[A](t: CountTableRef[A], key: A): var int {.deprecated.} = ## Use ```[]``` instead. result = t[][key] +proc getOrDefault*[A](t: CountTableRef[A], key: A): int = + getOrDefaultImpl(t, key) + proc hasKey*[A](t: CountTableRef[A], key: A): bool = ## returns true iff `key` is in the table `t`. result = t[].hasKey(key) diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim index 2127c7f10..e477c6f07 100644 --- a/lib/pure/strtabs.nim +++ b/lib/pure/strtabs.nim @@ -122,6 +122,11 @@ proc mget*(t: StringTableRef, key: string): var string {.deprecated.} = ## ``KeyError`` exception is raised. Use ```[]``` instead. get(t, key) +proc getOrDefault*(t: StringTableRef; key: string): string = + var index = rawGet(t, key) + if index >= 0: result = t.data[index].val + else: result = "" + proc hasKey*(t: StringTableRef, key: string): bool {.rtl, extern: "nst$1".} = ## returns true iff `key` is in the table `t`. result = rawGet(t, key) >= 0 diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index c60da0239..4a506546b 100644 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -144,7 +144,7 @@ proc `[]`* (n: var XmlNode, i: int): var XmlNode {.inline.} = assert n.k == xnElement result = n.s[i] -proc mget* (n: var XmlNode, i: int): var XmlNode {.inline, deprecated.} = +proc mget*(n: var XmlNode, i: int): var XmlNode {.inline, deprecated.} = ## returns the `i`'th child of `n` so that it can be modified. Use ```[]``` ## instead. n[i]