Merge pull request #9511 from krux02/krux02-devel

Krux02 devel
This commit is contained in:
Arne Döring 2018-10-31 17:12:36 +01:00 • committed by GitHub
commit 0059f16dfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 42 deletions

View file

@ -55,6 +55,8 @@ proc enumToString*(enums: openArray[enum]): string =
slightly. The `dumpLisp` macro in this module now outputs an slightly. The `dumpLisp` macro in this module now outputs an
indented proper Lisp, devoid of commas. indented proper Lisp, devoid of commas.
- In `strutils` empty strings now no longer matched as substrings anymore.
### Language additions ### Language additions
- Vm suport for float32<->int32 and float64<->int64 casts was added. - Vm suport for float32<->int32 and float64<->int64 casts was added.

View file

@ -509,7 +509,7 @@ proc genWhileStmt(p: BProc, t: PNode) =
# for closure support weird loop bodies are generated: # for closure support weird loop bodies are generated:
if loopBody.len == 2 and loopBody.sons[0].kind == nkEmpty: if loopBody.len == 2 and loopBody.sons[0].kind == nkEmpty:
loopBody = loopBody.sons[1] loopBody = loopBody.sons[1]
genComputedGoto(p, loopBody) # TODO foobar genComputedGoto(p, loopBody)
else: else:
p.breakIdx = startBlock(p, "while (1) {$n") p.breakIdx = startBlock(p, "while (1) {$n")
p.blocks[p.breakIdx].isLoop = true p.blocks[p.breakIdx].isLoop = true
@ -874,14 +874,15 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
discard pop(p.nestedTryStmts) discard pop(p.nestedTryStmts)
if not catchAllPresent and t[^1].kind == nkFinally:
# finally requires catch all presence
startBlock(p, "catch (...) {$n")
genSimpleBlock(p, t[^1][0])
line(p, cpsStmts, ~"throw;$n")
endBlock(p)
if t[^1].kind == nkFinally: if t[^1].kind == nkFinally:
# c++ does not have finally, therefore code needs to be generated twice
if not catchAllPresent:
# finally requires catch all presence
startBlock(p, "catch (...) {$n")
genStmts(p, t[^1][0])
line(p, cpsStmts, ~"throw;$n")
endBlock(p)
genSimpleBlock(p, t[^1][0]) genSimpleBlock(p, t[^1][0])
proc genTry(p: BProc, t: PNode, d: var TLoc) = proc genTry(p: BProc, t: PNode, d: var TLoc) =

View file

@ -1,3 +1,12 @@
#
#
# The Nim Compiler
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## code owner: Arne Döring
## e-mail: arne.doering@gmx.net
proc align(address, alignment: BiggestInt): BiggestInt = proc align(address, alignment: BiggestInt): BiggestInt =
result = (address + (alignment - 1)) and not (alignment - 1) result = (address + (alignment - 1)) and not (alignment - 1)

View file

@ -182,7 +182,10 @@ const
HighRegisterPressure = 40 HighRegisterPressure = 40
proc bestEffort(c: PCtx): TLineInfo = proc bestEffort(c: PCtx): TLineInfo =
(if c.prc == nil: c.module.info else: c.prc.sym.info) if c.prc != nil and c.prc.sym != nil:
c.prc.sym.info
else:
c.module.info
proc getTemp(cc: PCtx; tt: PType): TRegister = proc getTemp(cc: PCtx; tt: PType): TRegister =
let typ = tt.skipTypesOrNil({tyStatic}) let typ = tt.skipTypesOrNil({tyStatic})

View file

@ -149,8 +149,8 @@ proc isAlphaAscii*(s: string): bool {.noSideEffect, procvar,
## in `s`. ## in `s`.
runnableExamples: runnableExamples:
doAssert isAlphaAscii("fooBar") == true doAssert isAlphaAscii("fooBar") == true
doAssert isAlphaAscii("fooBar1") == false doAssert isAlphaAscii("fooBar1") == false
doAssert isAlphaAscii("foo Bar") == false doAssert isAlphaAscii("foo Bar") == false
isImpl isAlphaAscii isImpl isAlphaAscii
proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar, proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar,
@ -164,7 +164,7 @@ proc isAlphaNumeric*(s: string): bool {.noSideEffect, procvar,
## in `s`. ## in `s`.
runnableExamples: runnableExamples:
doAssert isAlphaNumeric("fooBar") == true doAssert isAlphaNumeric("fooBar") == true
doAssert isAlphaNumeric("fooBar") == true doAssert isAlphaNumeric("fooBar") == true
doAssert isAlphaNumeric("foo Bar") == false doAssert isAlphaNumeric("foo Bar") == false
isImpl isAlphaNumeric isImpl isAlphaNumeric
@ -179,7 +179,7 @@ proc isDigit*(s: string): bool {.noSideEffect, procvar,
## in `s`. ## in `s`.
runnableExamples: runnableExamples:
doAssert isDigit("1908") == true doAssert isDigit("1908") == true
doAssert isDigit("fooBar1") == false doAssert isDigit("fooBar1") == false
isImpl isDigit isImpl isDigit
proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar, proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar,
@ -191,7 +191,7 @@ proc isSpaceAscii*(s: string): bool {.noSideEffect, procvar,
## characters and there is at least one character in `s`. ## characters and there is at least one character in `s`.
runnableExamples: runnableExamples:
doAssert isSpaceAscii(" ") == true doAssert isSpaceAscii(" ") == true
doAssert isSpaceAscii("") == false doAssert isSpaceAscii("") == false
isImpl isSpaceAscii isImpl isSpaceAscii
template isCaseImpl(s, charProc, skipNonAlpha) = template isCaseImpl(s, charProc, skipNonAlpha) =
@ -420,7 +420,7 @@ proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =
## The resulting string may not have a leading zero. Its length is always ## The resulting string may not have a leading zero. Its length is always
## exactly 3. ## exactly 3.
runnableExamples: runnableExamples:
doAssert toOctal('!') == "041" doAssert toOctal('!') == "041"
result = newString(3) result = newString(3)
var val = ord(c) var val = ord(c)
for i in countdown(2, 0): for i in countdown(2, 0):
@ -933,7 +933,7 @@ proc intToStr*(x: int, minchars: Positive = 1): string {.noSideEffect,
## achieved by adding leading zeros. ## achieved by adding leading zeros.
runnableExamples: runnableExamples:
doAssert intToStr(1984) == "1984" doAssert intToStr(1984) == "1984"
doAssert intToStr(1984, 6) == "001984" doAssert intToStr(1984, 6) == "001984"
result = $abs(x) result = $abs(x)
for i in 1 .. minchars - len(result): for i in 1 .. minchars - len(result):
result = '0' & result result = '0' & result
@ -1525,6 +1525,8 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
## backwards to 0. ## backwards to 0.
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
if sub.len == 0:
return -1
let realStart = if start == -1: s.len else: start let realStart = if start == -1: s.len else: start
for i in countdown(realStart-sub.len, 0): for i in countdown(realStart-sub.len, 0):
for j in 0..sub.len-1: for j in 0..sub.len-1:
@ -1632,11 +1634,7 @@ proc replace*(s, sub: string, by = ""): string {.noSideEffect,
result = "" result = ""
let subLen = sub.len let subLen = sub.len
if subLen == 0: if subLen == 0:
for c in s: result = s
add result, by
add result, c
add result, by
return
elif subLen == 1: elif subLen == 1:
# when the pattern is a single char, we use a faster # when the pattern is a single char, we use a faster
# char-based search that doesn't need a skip table: # char-based search that doesn't need a skip table:
@ -1691,21 +1689,22 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
initSkipTable(a, sub) initSkipTable(a, sub)
var i = 0 var i = 0
let last = s.high let last = s.high
let sublen = max(sub.len, 1) let sublen = sub.len
while true: if sublen > 0:
var j = find(a, s, sub, i, last) while true:
if j < 0: break var j = find(a, s, sub, i, last)
# word boundary? if j < 0: break
if (j == 0 or s[j-1] notin wordChars) and # word boundary?
(j+sub.len >= s.len or s[j+sub.len] notin wordChars): if (j == 0 or s[j-1] notin wordChars) and
add result, substr(s, i, j - 1) (j+sub.len >= s.len or s[j+sub.len] notin wordChars):
add result, by add result, substr(s, i, j - 1)
i = j + sublen add result, by
else: i = j + sublen
add result, substr(s, i, j) else:
i = j + 1 add result, substr(s, i, j)
# copy the rest: i = j + 1
add result, substr(s, i) # copy the rest:
add result, substr(s, i)
proc multiReplace*(s: string, replacements: varargs[(string, string)]): string {.noSideEffect.} = proc multiReplace*(s: string, replacements: varargs[(string, string)]): string {.noSideEffect.} =
## Same as replace, but specialized for doing multiple replacements in a single ## Same as replace, but specialized for doing multiple replacements in a single
@ -1722,15 +1721,18 @@ proc multiReplace*(s: string, replacements: varargs[(string, string)]): string {
result = newStringOfCap(s.len) result = newStringOfCap(s.len)
var i = 0 var i = 0
var fastChk: set[char] = {} var fastChk: set[char] = {}
for tup in replacements: fastChk.incl(tup[0][0]) # Include first character of all replacements for sub, by in replacements.items:
if sub.len > 0:
# Include first character of all replacements
fastChk.incl sub[0]
while i < s.len: while i < s.len:
block sIteration: block sIteration:
# Assume most chars in s are not candidates for any replacement operation # Assume most chars in s are not candidates for any replacement operation
if s[i] in fastChk: if s[i] in fastChk:
for tup in replacements: for sub, by in replacements.items:
if s.continuesWith(tup[0], i): if sub.len > 0 and s.continuesWith(sub, i):
add result, tup[1] add result, by
inc(i, tup[0].len) inc(i, sub.len)
break sIteration break sIteration
# No matching replacement found # No matching replacement found
# copy current character from s # copy current character from s
@ -2612,7 +2614,7 @@ when isMainModule:
doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc" doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
doAssert "-lda-ldz -ld abc".replaceWord("") == "-lda-ldz -ld abc" doAssert "-lda-ldz -ld abc".replaceWord("") == "-lda-ldz -ld abc"
doAssert "oo".replace("", "abc") == "abcoabcoabc" doAssert "oo".replace("", "abc") == "oo"
type MyEnum = enum enA, enB, enC, enuD, enE type MyEnum = enum enA, enB, enC, enuD, enE
doAssert parseEnum[MyEnum]("enu_D") == enuD doAssert parseEnum[MyEnum]("enu_D") == enuD