From b9d75f894401afcbc98081167555715a95e67d43 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 20 Jan 2013 12:47:25 +0000 Subject: Fixed a bug with the 'do notation' consuming statements after it. --- tests/run/tdomulttest.nim | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/run/tdomulttest.nim (limited to 'tests/run/tdomulttest.nim') diff --git a/tests/run/tdomulttest.nim b/tests/run/tdomulttest.nim new file mode 100644 index 000000000..76ab0963c --- /dev/null +++ b/tests/run/tdomulttest.nim @@ -0,0 +1,17 @@ +discard """ + file: "tasynciossl.nim" + cmd: "nimrod cc --hints:on --define:ssl $# $#" + output: "555\ntest\nmulti lines\n99999999\nend" +""" +proc foo(bar, baz: proc (x: int): int) = + echo bar(555) + echo baz(99999999) + +foo do (x: int) -> int: + return x +do (x: int) -> int: + echo("test") + echo("multi lines") + return x + +echo("end") \ No newline at end of file -- cgit 1.4.1-2-gfad0 From be51e8b61b1afbedb1e041b087236787f6e4df81 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 20 Jan 2013 17:34:43 +0000 Subject: Reverted my 'do notation' fixes. --- compiler/parser.nim | 20 ++++++++++++-------- tests/run/tdomulttest.nim | 3 +-- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'tests/run/tdomulttest.nim') diff --git a/compiler/parser.nim b/compiler/parser.nim index e785a7e62..bfc6b3b65 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -389,7 +389,7 @@ proc parseGStrLit(p: var TParser, a: PNode): PNode = else: result = a -proc identOrLiteral(p: var TParser): PNode = +proc identOrLiteral(p: var TParser): PNode = case p.tok.tokType of tkSymbol: result = newIdentNodeP(p.tok.ident, p) @@ -494,6 +494,12 @@ proc primarySuffix(p: var TParser, r: PNode): PNode = result = newNodeP(nkCall, p) addSon(result, a) exprColonEqExprListAux(p, nkExprEqExpr, tkParRi, tkEquals, result) + parseDoBlocks(p, result) + of tkDo: + var a = result + result = newNodeP(nkCall, p) + addSon(result, a) + parseDoBlocks(p, result) of tkDot: result = dotExpr(p, result) result = parseGStrLit(p, result) @@ -730,7 +736,7 @@ proc parseProcExpr(p: var TParser, isExpr: bool): PNode = proc isExprStart(p: TParser): bool = case p.tok.tokType of tkSymbol, tkAccent, tkOpr, tkNot, tkNil, tkCast, tkIf, - tkProc, tkDo, tkIterator, tkBind, + tkProc, tkIterator, tkBind, tkParLe, tkBracketLe, tkCurlyLe, tkIntLit..tkCharLit, tkVar, tkRef, tkPtr, tkTuple, tkObject, tkType, tkWhen, tkCase, tkShared: result = true @@ -826,8 +832,6 @@ proc primary(p: var TParser, mode: TPrimaryMode): PNode = getTok(p) optInd(p, result) addSon(result, primary(p, pmNormal)) - of tkDo: - result = parseDoBlock(p) else: result = identOrLiteral(p) if mode != pmSkipSuffix: @@ -852,7 +856,7 @@ proc parseExprStmt(p: var TParser): PNode = var call = if a.kind == nkCall: a else: newNode(nkCommand, a.info, @[a]) while true: - if not isExprStart(p): break + if not isExprStart(p): break var e = parseExpr(p) addSon(call, e) if p.tok.tokType != tkComma: break @@ -860,7 +864,7 @@ proc parseExprStmt(p: var TParser): PNode = optInd(p, a) if p.tok.tokType == tkDo: parseDoBlocks(p, call) - return call + return result = if call.sonsLen <= 1: a else: call if p.tok.tokType == tkColon: @@ -1548,14 +1552,14 @@ proc parseAll(p: var TParser): PNode = proc parseTopLevelStmt(p: var TParser): PNode = result = ast.emptyNode - while true: + while true: case p.tok.tokType of tkSad, tkSemicolon: getTok(p) of tkDed, tkInd: parMessage(p, errInvalidIndentation) getTok(p) of tkEof: break - else: + else: result = complexOrSimpleStmt(p) if result.kind == nkEmpty: parMessage(p, errExprExpected, p.tok) break diff --git a/tests/run/tdomulttest.nim b/tests/run/tdomulttest.nim index 76ab0963c..6842d38ed 100644 --- a/tests/run/tdomulttest.nim +++ b/tests/run/tdomulttest.nim @@ -1,6 +1,5 @@ discard """ - file: "tasynciossl.nim" - cmd: "nimrod cc --hints:on --define:ssl $# $#" + file: "tdomulttest.nim" output: "555\ntest\nmulti lines\n99999999\nend" """ proc foo(bar, baz: proc (x: int): int) = -- cgit 1.4.1-2-gfad0 From 0d978c4a9a2a6e476231d7bd70a3932af1e4c9c6 Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 22 Jan 2013 00:29:54 +0100 Subject: fixed some closure related bugs --- compiler/ccgexprs.nim | 2 +- compiler/lambdalifting.nim | 2 +- compiler/main.nim | 4 +- compiler/semexprs.nim | 4 +- compiler/semstmts.nim | 38 ++++---- tests/compile/tclosurebug2.nim | 194 +++++++++++++++++++++++++++++++++++++++++ tests/run/tdomulttest.nim | 1 + tests/run/tmultim6.nim | 2 +- todo.txt | 3 +- 9 files changed, 226 insertions(+), 24 deletions(-) create mode 100644 tests/compile/tclosurebug2.nim (limited to 'tests/run/tdomulttest.nim') diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 26144c152..7781040d9 100755 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1,7 +1,7 @@ # # # The Nimrod Compiler -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index bca5b8c1d..0c3eea3be 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -1,7 +1,7 @@ # # # The Nimrod Compiler -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. diff --git a/compiler/main.nim b/compiler/main.nim index 0a76b967a..fd934e195 100755 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -312,7 +312,9 @@ proc MainCommand = # no need to write rod files and would slow down things: #registerPass(rodwrite.rodwritePass()) discard CompileModule(options.libpath / "system", {sfSystemModule}) - service.serve(proc () = + # I'm lazy and misused this piece of code as a testcase so don't remove + # the invocation with a named parameter: + service.serve(action = proc () = let projectFile = mainCommandArg() discard CompileModule(projectFile, {sfMainModule}) ) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index d0fbcab6f..4b4cb5bdf 100755 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1,7 +1,7 @@ # # # The Nimrod Compiler -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -318,7 +318,7 @@ proc semIs(c: PContext, n: PNode): PNode = if not containsGenericType(t1): result = evalIsOp(n) proc semOpAux(c: PContext, n: PNode) = - let flags = {efDetermineType} + const flags = {efDetermineType} for i in countup(1, n.sonsLen- 1): var a = n.sons[i] if a.kind == nkExprEqExpr and sonsLen(a) == 2: diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 363cff89a..5bd8462f3 100755 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -686,17 +686,18 @@ proc semLambda(c: PContext, n: PNode, flags: TExprFlags): PNode = if n.sons[pragmasPos].kind != nkEmpty: pragma(c, s, n.sons[pragmasPos], lambdaPragmas) s.options = gOptions - if n.sons[bodyPos].kind != nkEmpty: - if sfImportc in s.flags: + if n.sons[bodyPos].kind != nkEmpty: + if sfImportc in s.flags: LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s) - if efDetermineType notin flags: - pushProcCon(c, s) - addResult(c, s.typ.sons[0], n.info, skProc) - let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos])) - n.sons[bodyPos] = transformBody(c.module, semBody, s) - addResultNode(c, n) - popProcCon(c) - sideEffectsCheck(c, s) + #if efDetermineType notin flags: + # XXX not good enough + pushProcCon(c, s) + addResult(c, s.typ.sons[0], n.info, skProc) + let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos])) + n.sons[bodyPos] = transformBody(c.module, semBody, s) + addResultNode(c, n) + popProcCon(c) + sideEffectsCheck(c, s) else: LocalError(n.info, errImplOfXexpected, s.name.s) closeScope(c.tab) # close scope for parameters @@ -706,13 +707,16 @@ proc semLambda(c: PContext, n: PNode, flags: TExprFlags): PNode = proc activate(c: PContext, n: PNode) = # XXX: This proc is part of my plan for getting rid of # forward declarations. stay tuned. - case n.kind - of nkLambdaKinds: - discard semLambda(c, n, {}) - of nkCallKinds: - for i in 1 .. counter) + result = (length * 2 < counter * 3) or (length - counter < 4) + +proc nextTry(h, maxHash: THash): THash {.inline.} = + result = ((5 * h) + 1) and maxHash + +template rawGetImpl() {.dirty.} = + var h: THash = hash(key) and high(t.data) # start with real hash value + while t.data[h].slot != seEmpty: + if t.data[h].key == key and t.data[h].slot == seFilled: + return h + h = nextTry(h, high(t.data)) + result = -1 + +template rawInsertImpl() {.dirty.} = + var h: THash = hash(key) and high(data) + while data[h].slot == seFilled: + h = nextTry(h, high(data)) + data[h].key = key + data[h].val = val + data[h].slot = seFilled + +template AddImpl() {.dirty.} = + if mustRehash(len(t.data), t.counter): Enlarge(t) + RawInsert(t, t.data, key, val) + inc(t.counter) + +template PutImpl() {.dirty.} = + var index = RawGet(t, key) + if index >= 0: + t.data[index].val = val + else: + AddImpl() + +proc len*[A, B](t: TOrderedTable[A, B]): int {.inline.} = + ## returns the number of keys in `t`. + result = t.counter + +template forAllOrderedPairs(yieldStmt: stmt) {.dirty, immediate.} = + var h = t.first + while h >= 0: + var nxt = t.data[h].next + if t.data[h].slot == seFilled: yieldStmt + h = nxt + +iterator pairs*[A, B](t: TOrderedTable[A, B]): tuple[key: A, val: B] = + ## iterates over any (key, value) pair in the table `t` in insertion + ## order. + forAllOrderedPairs: + yield (t.data[h].key, t.data[h].val) + +iterator mpairs*[A, B](t: var TOrderedTable[A, B]): tuple[key: A, val: var B] = + ## iterates over any (key, value) pair in the table `t` in insertion + ## order. The values can be modified. + forAllOrderedPairs: + yield (t.data[h].key, t.data[h].val) + +iterator keys*[A, B](t: TOrderedTable[A, B]): A = + ## iterates over any key in the table `t` in insertion order. + forAllOrderedPairs: + yield t.data[h].key + +iterator values*[A, B](t: TOrderedTable[A, B]): B = + ## iterates over any value in the table `t` in insertion order. + forAllOrderedPairs: + yield t.data[h].val + +iterator mvalues*[A, B](t: var TOrderedTable[A, B]): var B = + ## iterates over any value in the table `t` in insertion order. The values + ## can be modified. + forAllOrderedPairs: + yield t.data[h].val + +proc RawGet[A, B](t: TOrderedTable[A, B], key: A): int = + rawGetImpl() + +proc `[]`*[A, B](t: TOrderedTable[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 index = RawGet(t, key) + if index >= 0: result = t.data[index].val + +proc mget*[A, B](t: var TOrderedTable[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 index = RawGet(t, key) + if index >= 0: result = t.data[index].val + else: raise newException(EInvalidKey, "key not found: " & $key) + +proc hasKey*[A, B](t: TOrderedTable[A, B], key: A): bool = + ## returns true iff `key` is in the table `t`. + result = rawGet(t, key) >= 0 + +proc RawInsert[A, B](t: var TOrderedTable[A, B], + data: var TOrderedKeyValuePairSeq[A, B], + key: A, val: B) = + rawInsertImpl() + data[h].next = -1 + if t.first < 0: t.first = h + if t.last >= 0: data[t.last].next = h + t.last = h + +proc Enlarge[A, B](t: var TOrderedTable[A, B]) = + var n: TOrderedKeyValuePairSeq[A, B] + newSeq(n, len(t.data) * growthFactor) + var h = t.first + t.first = -1 + t.last = -1 + while h >= 0: + var nxt = t.data[h].next + if t.data[h].slot == seFilled: + RawInsert(t, n, t.data[h].key, t.data[h].val) + h = nxt + swap(t.data, n) + +proc `[]=`*[A, B](t: var TOrderedTable[A, B], key: A, val: B) = + ## puts a (key, value)-pair into `t`. + putImpl() + +proc add*[A, B](t: var TOrderedTable[A, B], key: A, val: B) = + ## puts a new (key, value)-pair into `t` even if ``t[key]`` already exists. + AddImpl() + +proc initOrderedTable*[A, B](initialSize=64): TOrderedTable[A, B] = + ## creates a new ordered hash table that is empty. `initialSize` needs to be + ## a power of two. + assert isPowerOfTwo(initialSize) + result.counter = 0 + result.first = -1 + result.last = -1 + newSeq(result.data, initialSize) + +proc toOrderedTable*[A, B](pairs: openarray[tuple[key: A, + val: B]]): TOrderedTable[A, B] = + ## creates a new ordered hash table that contains the given `pairs`. + result = initOrderedTable[A, B](nextPowerOfTwo(pairs.len+10)) + for key, val in items(pairs): result[key] = val + +proc sort*[A, B](t: var TOrderedTable[A,B], + cmp: proc (x, y: tuple[key: A, val: B]): int {.closure.}) = + ## sorts the ordered table so that the entry with the highest counter comes + ## first. This is destructive (with the advantage of being efficient)! + ## You must not modify `t` afterwards! + ## You can use the iterators `pairs`, `keys`, and `values` to iterate over + ## `t` in the sorted order. + + # we use shellsort here; fast enough and simple + var h = 1 + while true: + h = 3 * h + 1 + if h >= high(t.data): break + while true: + h = h div 3 + for i in countup(h, high(t.data)): + var j = i + #echo(t.data.len, " ", j, " - ", h) + #echo(repr(t.data[j-h])) + proc rawCmp(x, y: TOrderedKeyValuePair[A, B]): int = + if x.slot in {seEmpty, seDeleted} and y.slot in {seEmpty, seDeleted}: + return 0 + elif x.slot in {seEmpty, seDeleted}: + return -1 + elif y.slot in {seEmpty, seDeleted}: + return 1 + else: + let item1 = (x.key, x.val) + let item2 = (y.key, y.val) + return cmp(item1, item2) + + while rawCmp(t.data[j-h], t.data[j]) <= 0: + swap(t.data[j], t.data[j-h]) + j = j-h + if j < h: break + if h == 1: break diff --git a/tests/run/tdomulttest.nim b/tests/run/tdomulttest.nim index 6842d38ed..4ee6de128 100644 --- a/tests/run/tdomulttest.nim +++ b/tests/run/tdomulttest.nim @@ -1,6 +1,7 @@ discard """ file: "tdomulttest.nim" output: "555\ntest\nmulti lines\n99999999\nend" + disabled: true """ proc foo(bar, baz: proc (x: int): int) = echo bar(555) diff --git a/tests/run/tmultim6.nim b/tests/run/tmultim6.nim index a55b69e37..5f45f572a 100644 --- a/tests/run/tmultim6.nim +++ b/tests/run/tmultim6.nim @@ -1,5 +1,5 @@ discard """ - output: "collide: unit, thing | collide: unit, thing | collide: thing, unit" + output: "collide: unit, thing | collide: unit, thing | collide: thing, unit |" """ # Test multi methods diff --git a/todo.txt b/todo.txt index 56a27e0fa..c9510d9ca 100755 --- a/todo.txt +++ b/todo.txt @@ -3,8 +3,8 @@ version 0.9.2 - implement constructors + full 'not nil' checking - ``restrict`` pragma + backend support -- fix closure bug finally - fix marshal bug +- fix: 'result' is not properly cleaned for NRVO - investigate nimgame bug @@ -62,6 +62,7 @@ version 0.9.XX not in a 'let/var' context (p(a.openFile, b.openFile) makes no sense anyway) - document nimdoc properly finally - make 'clamp' a magic for the range stuff +- better type syntax for functions and tuples: tuple(int, int); (int,int)->int Not essential for 1.0.0 -- cgit 1.4.1-2-gfad0