bugfixes; added events module, sequtils module
This commit is contained in:
parent
3ac9012361
commit
0e7f2ca3f1
13 changed files with 292 additions and 15 deletions
|
|
@ -859,10 +859,12 @@ proc parseField(p: var TRstParser): PRstNode =
|
||||||
|
|
||||||
proc parseFields(p: var TRstParser): PRstNode =
|
proc parseFields(p: var TRstParser): PRstNode =
|
||||||
result = nil
|
result = nil
|
||||||
if (p.tok[p.idx].kind == tkIndent) and (p.tok[p.idx + 1].symbol == ":"):
|
var atStart = p.idx == 0 and p.tok[0].symbol == ":"
|
||||||
var col = p.tok[p.idx].ival # BUGFIX!
|
if (p.tok[p.idx].kind == tkIndent) and (p.tok[p.idx + 1].symbol == ":") or
|
||||||
|
atStart:
|
||||||
|
var col = if atStart: p.tok[p.idx].col else: p.tok[p.idx].ival
|
||||||
result = newRstNode(rnFieldList)
|
result = newRstNode(rnFieldList)
|
||||||
inc(p.idx)
|
if not atStart: inc(p.idx)
|
||||||
while true:
|
while true:
|
||||||
addSon(result, parseField(p))
|
addSon(result, parseField(p))
|
||||||
if (p.tok[p.idx].kind == tkIndent) and (p.tok[p.idx].ival == col) and
|
if (p.tok[p.idx].kind == tkIndent) and (p.tok[p.idx].ival == col) and
|
||||||
|
|
@ -1307,7 +1309,7 @@ proc parseSection(p: var TRstParser, result: PRstNode) =
|
||||||
of rnParagraph: nil
|
of rnParagraph: nil
|
||||||
of rnDefList: a = parseDefinitionList(p)
|
of rnDefList: a = parseDefinitionList(p)
|
||||||
of rnFieldList:
|
of rnFieldList:
|
||||||
dec(p.idx)
|
if p.idx > 0: dec(p.idx)
|
||||||
a = parseFields(p)
|
a = parseFields(p)
|
||||||
of rnTransition: a = parseTransition(p)
|
of rnTransition: a = parseTransition(p)
|
||||||
of rnHeadline: a = parseHeadline(p)
|
of rnHeadline: a = parseHeadline(p)
|
||||||
|
|
|
||||||
|
|
@ -896,7 +896,7 @@ proc computeSizeAux(typ: PType, a: var biggestInt): biggestInt =
|
||||||
if typ.callConv == ccClosure: result = 2 * ptrSize
|
if typ.callConv == ccClosure: result = 2 * ptrSize
|
||||||
else: result = ptrSize
|
else: result = ptrSize
|
||||||
a = ptrSize
|
a = ptrSize
|
||||||
of tyNil, tyCString, tyString, tySequence, tyPtr, tyRef, tyOpenArray:
|
of tyNil, tyCString, tyString, tySequence, tyPtr, tyRef, tyVar, tyOpenArray:
|
||||||
result = ptrSize
|
result = ptrSize
|
||||||
a = result
|
a = result
|
||||||
of tyArray, tyArrayConstr:
|
of tyArray, tyArrayConstr:
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ Comex
|
||||||
Eric Doughty-Papassideris
|
Eric Doughty-Papassideris
|
||||||
Philippe Lhoste
|
Philippe Lhoste
|
||||||
Mario Ray Mahardhika
|
Mario Ray Mahardhika
|
||||||
|
Alex Mitchell
|
||||||
Dominik Picheta
|
Dominik Picheta
|
||||||
Jonathan Plona
|
Jonathan Plona
|
||||||
Alexander Rødseth
|
Alexander Rødseth
|
||||||
|
|
|
||||||
11
doc/lib.txt
11
doc/lib.txt
|
|
@ -60,6 +60,9 @@ Collections and algorithms
|
||||||
Implementation of a queue. The underlying implementation uses a ``seq``.
|
Implementation of a queue. The underlying implementation uses a ``seq``.
|
||||||
* `intsets <intsets.html>`_
|
* `intsets <intsets.html>`_
|
||||||
Efficient implementation of a set of ints as a sparse bit set.
|
Efficient implementation of a set of ints as a sparse bit set.
|
||||||
|
* `sequtils <sequtils.html>`_
|
||||||
|
This module implements operations for the built-in seq type
|
||||||
|
which were inspired by functional programming languages.
|
||||||
|
|
||||||
|
|
||||||
String handling
|
String handling
|
||||||
|
|
@ -250,6 +253,14 @@ Multimedia support
|
||||||
the ``graphics`` module.
|
the ``graphics`` module.
|
||||||
|
|
||||||
|
|
||||||
|
Miscellaneous
|
||||||
|
-------------
|
||||||
|
|
||||||
|
* `events <events.html>`_
|
||||||
|
This module implements an event system that is not dependant on external
|
||||||
|
graphical toolkits.
|
||||||
|
|
||||||
|
|
||||||
Database support
|
Database support
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,22 @@
|
||||||
template all(container, cond: expr): expr =
|
template all(container, cond: expr): expr =
|
||||||
block:
|
block:
|
||||||
var result = true
|
var result = true
|
||||||
for item in items(container):
|
for it in items(container):
|
||||||
if not cond(item):
|
if not cond(it):
|
||||||
result = false
|
result = false
|
||||||
break
|
break
|
||||||
result
|
result
|
||||||
|
|
||||||
if all("mystring", {'a'..'z'}.contains):
|
template any(container, cond: expr): expr =
|
||||||
|
block:
|
||||||
|
var result = false
|
||||||
|
for it in items(container):
|
||||||
|
if cond(it):
|
||||||
|
result = true
|
||||||
|
break
|
||||||
|
result
|
||||||
|
|
||||||
|
if all("mystring", {'a'..'z'}.contains) and any("myohmy", 'y'.`==`):
|
||||||
echo "works"
|
echo "works"
|
||||||
else:
|
else:
|
||||||
echo "does not work"
|
echo "does not work"
|
||||||
|
|
|
||||||
59
lib/pure/collections/sequtils.nim
Normal file
59
lib/pure/collections/sequtils.nim
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nimrod's Runtime Library
|
||||||
|
# (c) Copyright 2011 Alex Mitchell
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
## :Author: Alex Mitchell
|
||||||
|
##
|
||||||
|
## This module implements operations for the built-in `seq`:idx: type
|
||||||
|
## which were inspired by functional programming languages.
|
||||||
|
|
||||||
|
proc concat*[T](seqs: openarray[seq[T]]): seq[T] =
|
||||||
|
## Takes several sequences' items and returns them inside of one sequence.
|
||||||
|
var L = 0
|
||||||
|
for seqitm in items(seqs): inc(L, len(seqitm))
|
||||||
|
newSeq(result, L)
|
||||||
|
var i = 0
|
||||||
|
for s in items(seqs):
|
||||||
|
for itm in items(s):
|
||||||
|
result[i] = itm
|
||||||
|
inc(i)
|
||||||
|
|
||||||
|
proc distnct*[T](seq1: seq[T]): seq[T] =
|
||||||
|
## Removes duplicates from a sequence and returns it.
|
||||||
|
result = @[]
|
||||||
|
for itm in items(seq1):
|
||||||
|
if not result.contains(itm): result.add(itm)
|
||||||
|
|
||||||
|
proc zip*[S, T](seq1: seq[S], seq2: seq[T]): seq[tuple[a: S, b: T]] =
|
||||||
|
## Combines two sequences. If one sequence is too short,
|
||||||
|
## the remaining items in the longer sequence are discarded.
|
||||||
|
var m = min(seq1.len, seq2.len)
|
||||||
|
newSeq(result, m)
|
||||||
|
for i in 0 .. m-1: result[i] = (seq1[i], seq2[i])
|
||||||
|
|
||||||
|
iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool): T =
|
||||||
|
## Iterates through a sequence and yields every item that fulfills the
|
||||||
|
## predicate.
|
||||||
|
for i in countup(0, len(seq1) -1):
|
||||||
|
var item = seq1[i]
|
||||||
|
if pred(item): yield seq1[i]
|
||||||
|
|
||||||
|
proc filter*[T](seq1: seq[T], pred: proc(item: T): bool): seq[T] =
|
||||||
|
## Returns all items in a sequence that fulfilled the predicate.
|
||||||
|
accumulateResult(filter(seq1, pred))
|
||||||
|
|
||||||
|
template filterIt*(seq1, pred: expr): expr =
|
||||||
|
## Finds a specific item in a sequence as long as the
|
||||||
|
## predicate returns true. The predicate needs to be an expression
|
||||||
|
## containing ``it``: ``filterIt("abcxyz", it == 'x')``.
|
||||||
|
block:
|
||||||
|
var result: type(seq1) = @[]
|
||||||
|
for it in items(seq1):
|
||||||
|
if pred: result.add(it)
|
||||||
|
result
|
||||||
|
|
||||||
83
lib/pure/events.nim
Normal file
83
lib/pure/events.nim
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nimrod's Runtime Library
|
||||||
|
# (c) Copyright 2011 Alex Mitchell
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
## :Author: Alex Mitchell
|
||||||
|
##
|
||||||
|
## This module implements an event system that is not dependant on external
|
||||||
|
## graphical toolkits. It was originally called ``NimEE`` because
|
||||||
|
## it was inspired by Ptyhon's PyEE module.
|
||||||
|
|
||||||
|
type
|
||||||
|
TEventArgs* = object of TObject ## Base object for event arguments
|
||||||
|
## that are passed to callback functions.
|
||||||
|
TEventHandler = tuple[name: string, handlers: seq[proc(e:TEventArgs)]]
|
||||||
|
PEventHandler* = ref TEventHandler ## An eventhandler for an event.
|
||||||
|
|
||||||
|
type
|
||||||
|
TEventEmitter = object {.pure, final.}
|
||||||
|
s: seq[PEventHandler]
|
||||||
|
PEventEmitter* = ref TEventEmitter ## An object that fires events and
|
||||||
|
## holds event handlers for an object.
|
||||||
|
EInvalidEvent* = object of EInvalidValue
|
||||||
|
|
||||||
|
proc newEventHandler*(name: string): PEventHandler =
|
||||||
|
## Initializes an EventHandler with the specified name and returns it.
|
||||||
|
new(result)
|
||||||
|
result.handlers = @[]
|
||||||
|
result.name = name
|
||||||
|
|
||||||
|
proc addHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) =
|
||||||
|
## Adds the callback to the specified event handler.
|
||||||
|
handler.handlers.add(func)
|
||||||
|
|
||||||
|
proc removeHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) =
|
||||||
|
## Removes the callback from the specified event handler.
|
||||||
|
for i in countup(0, len(handler.handlers) -1):
|
||||||
|
if func == handler.handlers[i]:
|
||||||
|
handler.handlers.del(i)
|
||||||
|
break
|
||||||
|
|
||||||
|
proc clearHandlers*(handler: PEventHandler) =
|
||||||
|
## Clears all of the callbacks from the event handler.
|
||||||
|
setLen(handler.handlers, 0)
|
||||||
|
|
||||||
|
proc getEventhandler(emitter: PEventEmitter, event: string): int =
|
||||||
|
for k in 0..high(emitter.s):
|
||||||
|
if emitter.s[k].name == event: return k
|
||||||
|
return -1
|
||||||
|
|
||||||
|
proc on*(emitter: PEventEmitter, event: string, func: proc(e: TEventArgs)) =
|
||||||
|
## Assigns a event handler with the specified callback. If the event
|
||||||
|
## doesn't exist, it will be created.
|
||||||
|
var i = getEventHandler(emitter, event)
|
||||||
|
if i < 0:
|
||||||
|
var eh = newEventHandler(event)
|
||||||
|
addHandler(eh, func)
|
||||||
|
emitter.s.add(eh)
|
||||||
|
else:
|
||||||
|
addHandler(emitter.s[i], func)
|
||||||
|
|
||||||
|
proc emit*(emitter: PEventEmitter, eventhandler: PEventHandler,
|
||||||
|
args: TEventArgs) =
|
||||||
|
## Fires an event handler with specified event arguments.
|
||||||
|
for func in items(eventhandler.handlers): func(args)
|
||||||
|
|
||||||
|
proc emit*(emitter: PEventEmitter, event: string, args: TEventArgs) =
|
||||||
|
## Fires an event handler with specified event arguments.
|
||||||
|
var i = getEventHandler(emitter, event)
|
||||||
|
if i >= 0:
|
||||||
|
emit(emitter, emitter.s[i], args)
|
||||||
|
else:
|
||||||
|
raise newException(EInvalidEvent, "invalid event: " & event)
|
||||||
|
|
||||||
|
proc newEventEmitter*(): PEventEmitter =
|
||||||
|
## Creates and returns a new EventEmitter.
|
||||||
|
new(result)
|
||||||
|
result.s = @[]
|
||||||
|
|
||||||
|
|
@ -45,7 +45,9 @@ proc rawReadLine(f: TFile, result: var string) =
|
||||||
setLen(result, 0) # reuse the buffer!
|
setLen(result, 0) # reuse the buffer!
|
||||||
while True:
|
while True:
|
||||||
var c = fgetc(f)
|
var c = fgetc(f)
|
||||||
if c < 0'i32: raiseEIO("EOF reached")
|
if c < 0'i32:
|
||||||
|
if result.len > 0: break
|
||||||
|
else: raiseEIO("EOF reached")
|
||||||
if c == 10'i32: break # LF
|
if c == 10'i32: break # LF
|
||||||
if c == 13'i32: # CR
|
if c == 13'i32: # CR
|
||||||
c = fgetc(f) # is the next char LF?
|
c = fgetc(f) # is the next char LF?
|
||||||
|
|
@ -89,19 +91,21 @@ proc readFile(filename: string): string =
|
||||||
raiseEIO("error while reading from file")
|
raiseEIO("error while reading from file")
|
||||||
else:
|
else:
|
||||||
raiseEIO("file too big to fit in memory")
|
raiseEIO("file too big to fit in memory")
|
||||||
except EIO:
|
finally:
|
||||||
close(f)
|
close(f)
|
||||||
|
|
||||||
proc writeFile(filename, content: string) =
|
proc writeFile(filename, content: string) =
|
||||||
var f = open(filename, fmWrite)
|
var f = open(filename, fmWrite)
|
||||||
f.write(content)
|
try:
|
||||||
close(f)
|
f.write(content)
|
||||||
|
finally:
|
||||||
|
close(f)
|
||||||
|
|
||||||
proc EndOfFile(f: TFile): bool =
|
proc EndOfFile(f: TFile): bool =
|
||||||
# do not blame me; blame the ANSI C standard this is so brain-damaged
|
# do not blame me; blame the ANSI C standard this is so brain-damaged
|
||||||
var c = fgetc(f)
|
var c = fgetc(f)
|
||||||
ungetc(c, f)
|
ungetc(c, f)
|
||||||
return c == -1'i32
|
return c < 0'i32
|
||||||
|
|
||||||
proc writeln[Ty](f: TFile, x: Ty) =
|
proc writeln[Ty](f: TFile, x: Ty) =
|
||||||
write(f, x)
|
write(f, x)
|
||||||
|
|
|
||||||
48
tests/accept/run/tevents.nim
Normal file
48
tests/accept/run/tevents.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
discard """
|
||||||
|
file: "tevents.nim"
|
||||||
|
output: '''HandlePrintEvent: Output -> Handled print event
|
||||||
|
HandlePrintEvent2: Output -> printing for ME
|
||||||
|
HandlePrintEvent2: Output -> printing for ME'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import events
|
||||||
|
|
||||||
|
type
|
||||||
|
TPrintEventArgs = object of TEventArgs
|
||||||
|
user*: string
|
||||||
|
|
||||||
|
proc handleprintevent*(e: TEventArgs) =
|
||||||
|
write(stdout, "HandlePrintEvent: Output -> Handled print event\n")
|
||||||
|
|
||||||
|
proc handleprintevent2*(e: TEventArgs) =
|
||||||
|
var args: TPrintEventArgs = TPrintEventArgs(e)
|
||||||
|
write(stdout, "HandlePrintEvent2: Output -> printing for " & args.user)
|
||||||
|
|
||||||
|
var ee = newEventEmitter()
|
||||||
|
|
||||||
|
var eventargs: TPrintEventArgs
|
||||||
|
eventargs.user = "ME\n"
|
||||||
|
|
||||||
|
##method one test
|
||||||
|
|
||||||
|
ee.on("print", handleprintevent)
|
||||||
|
ee.on("print", handleprintevent2)
|
||||||
|
|
||||||
|
ee.emit("print", eventargs)
|
||||||
|
|
||||||
|
##method two test
|
||||||
|
|
||||||
|
type
|
||||||
|
TSomeObject = object of TObject
|
||||||
|
PrintEvent: PEventHandler
|
||||||
|
|
||||||
|
var obj: TSomeObject
|
||||||
|
obj.PrintEvent = newEventHandler("print")
|
||||||
|
obj.PrintEvent.addHandler(handleprintevent2)
|
||||||
|
|
||||||
|
ee.emit(obj.PrintEvent, eventargs)
|
||||||
|
|
||||||
|
obj.PrintEvent.removeHandler(handleprintevent2)
|
||||||
|
|
||||||
|
ee.emit(obj.PrintEvent, eventargs)
|
||||||
|
|
||||||
55
tests/accept/run/tsequtils.nim
Normal file
55
tests/accept/run/tsequtils.nim
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
discard """
|
||||||
|
file: "tsequtils.nim"
|
||||||
|
output: '''Zip: [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]
|
||||||
|
Filter Iterator: 3
|
||||||
|
Filter Iterator: 5
|
||||||
|
Filter Iterator: 7
|
||||||
|
Filter: [3, 5, 7]
|
||||||
|
FilterIt: [1, 3, 7]
|
||||||
|
Concat: [1, 3, 5, 7, 2, 4, 6]
|
||||||
|
Distnct: [1, 2, 3, 4, 5, 7]'''
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sequtils, marshal
|
||||||
|
|
||||||
|
proc testFindWhere(item : int) : bool =
|
||||||
|
if item != 1: return true
|
||||||
|
|
||||||
|
var seq1: seq[int] = @[]
|
||||||
|
|
||||||
|
seq1.add(1)
|
||||||
|
seq1.add(3)
|
||||||
|
seq1.add(5)
|
||||||
|
seq1.add(7)
|
||||||
|
|
||||||
|
var seq2: seq[int] = @[2, 4, 6]
|
||||||
|
var final = zip(seq1, seq2)
|
||||||
|
|
||||||
|
echo "Zip: ", $$(final)
|
||||||
|
|
||||||
|
#Test findWhere as a iterator
|
||||||
|
|
||||||
|
for itms in filter(seq1, testFindWhere):
|
||||||
|
echo "Filter Iterator: ", $$(itms)
|
||||||
|
|
||||||
|
|
||||||
|
#Test findWhere as a proc
|
||||||
|
|
||||||
|
var fullseq: seq[int] = filter(seq1, testFindWhere)
|
||||||
|
|
||||||
|
echo "Filter: ", $$(fullseq)
|
||||||
|
|
||||||
|
#Test findIt as a template
|
||||||
|
|
||||||
|
var finditval: seq[int] = filterIt(seq1, it!=5)
|
||||||
|
|
||||||
|
echo "FilterIt: ", $$(finditval)
|
||||||
|
|
||||||
|
var concatseq = concat(seq1,seq2)
|
||||||
|
echo "Concat: ", $$(concatseq)
|
||||||
|
|
||||||
|
var seq3 = @[1,2,3,4,5,5,5,7]
|
||||||
|
var discntseq = distnct(seq3)
|
||||||
|
echo "Distnct: ", $$(discntseq)
|
||||||
|
|
||||||
|
|
@ -107,8 +107,10 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
|
||||||
stdout.write("\n")
|
stdout.write("\n")
|
||||||
|
|
||||||
proc processFile(filename: string) =
|
proc processFile(filename: string) =
|
||||||
var buffer = system.readFile(filename)
|
var buffer: string
|
||||||
if isNil(buffer):
|
try:
|
||||||
|
buffer = system.readFile(filename)
|
||||||
|
except EIO:
|
||||||
echo "cannot open file: ", filename
|
echo "cannot open file: ", filename
|
||||||
return
|
return
|
||||||
stdout.writeln(filename)
|
stdout.writeln(filename)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ Bugfixes
|
||||||
- Boehm GC now works with ``--threads:on``.
|
- Boehm GC now works with ``--threads:on``.
|
||||||
- Fixed a serious memory corruption concerning message passing.
|
- Fixed a serious memory corruption concerning message passing.
|
||||||
- Fixed a serious bug concerning different instantiations of a generic proc.
|
- Fixed a serious bug concerning different instantiations of a generic proc.
|
||||||
|
- Fixed a newly introduced bug where a wrong ``EIO`` exception was raised for
|
||||||
|
the end of file for text files that do not end with a newline.
|
||||||
|
|
||||||
|
|
||||||
Changes affecting backwards compatibility
|
Changes affecting backwards compatibility
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ srcdoc: "pure/json;pure/base64;pure/scgi;pure/redis;impure/graphics"
|
||||||
srcdoc: "impure/rdstdin;wrappers/zmq;wrappers/sphinx"
|
srcdoc: "impure/rdstdin;wrappers/zmq;wrappers/sphinx"
|
||||||
srcdoc: "pure/collections/tables;pure/collections/sets;pure/collections/lists"
|
srcdoc: "pure/collections/tables;pure/collections/sets;pure/collections/lists"
|
||||||
srcdoc: "pure/collections/intsets;pure/collections/queues;pure/encodings"
|
srcdoc: "pure/collections/intsets;pure/collections/queues;pure/encodings"
|
||||||
|
srcdoc: "pure/lib/events;pure/collections/sequtils"
|
||||||
|
|
||||||
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql;wrappers/iup"
|
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql;wrappers/iup"
|
||||||
webdoc: "wrappers/sqlite3;wrappers/postgres;wrappers/tinyc"
|
webdoc: "wrappers/sqlite3;wrappers/postgres;wrappers/tinyc"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue