mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 09:54:49 +00:00
Merge branch 'devel' of github.com:nim-lang/Nim into devel
This commit is contained in:
@@ -1238,10 +1238,7 @@ proc parseExprStmt(p: var TParser): PNode =
|
||||
addSon(result, e)
|
||||
if p.tok.tokType != tkComma: break
|
||||
elif p.tok.indent < 0 and isExprStart(p):
|
||||
if a.kind == nkCommand:
|
||||
result = a
|
||||
else:
|
||||
result = newNode(nkCommand, a.info, @[a])
|
||||
result = newNode(nkCommand, a.info, @[a])
|
||||
while true:
|
||||
var e = parseExpr(p)
|
||||
addSon(result, e)
|
||||
|
||||
@@ -1266,7 +1266,7 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||
result.typ = makeTypeDesc(c, semTypeNode(c, n, nil))
|
||||
#result = symNodeFromType(c, semTypeNode(c, n, nil), n.info)
|
||||
of tyTuple:
|
||||
checkSonsLen(n, 2)
|
||||
if n.len != 2: return nil
|
||||
n.sons[0] = makeDeref(n.sons[0])
|
||||
c.p.bracketExpr = n.sons[0]
|
||||
# [] operator for tuples requires constant expression:
|
||||
@@ -1276,9 +1276,9 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||
var idx = getOrdValue(n.sons[1])
|
||||
if idx >= 0 and idx < sonsLen(arr): n.typ = arr.sons[int(idx)]
|
||||
else: localError(n.info, errInvalidIndexValueForTuple)
|
||||
result = n
|
||||
else:
|
||||
localError(n.info, errIndexTypesDoNotMatch)
|
||||
result = n
|
||||
result = nil
|
||||
else:
|
||||
let s = if n.sons[0].kind == nkSym: n.sons[0].sym
|
||||
elif n[0].kind in nkSymChoices: n.sons[0][0].sym
|
||||
|
||||
@@ -44,21 +44,23 @@
|
||||
const
|
||||
cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
|
||||
template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediate.} =
|
||||
template encodeInternal(s: typed, lineLen: int, newLine: string): untyped =
|
||||
## encodes `s` into base64 representation. After `lineLen` characters, a
|
||||
## `newline` is added.
|
||||
var total = ((len(s) + 2) div 3) * 4
|
||||
var numLines = (total + lineLen - 1) div lineLen
|
||||
let numLines = (total + lineLen - 1) div lineLen
|
||||
if numLines > 0: inc(total, (numLines - 1) * newLine.len)
|
||||
|
||||
result = newString(total)
|
||||
var i = 0
|
||||
var r = 0
|
||||
var currLine = 0
|
||||
var
|
||||
i = 0
|
||||
r = 0
|
||||
currLine = 0
|
||||
while i < s.len - 2:
|
||||
var a = ord(s[i])
|
||||
var b = ord(s[i+1])
|
||||
var c = ord(s[i+2])
|
||||
let
|
||||
a = ord(s[i])
|
||||
b = ord(s[i+1])
|
||||
c = ord(s[i+2])
|
||||
result[r] = cb64[a shr 2]
|
||||
result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
||||
result[r+2] = cb64[((b and 0x0F) shl 2) or ((c and 0xC0) shr 6)]
|
||||
@@ -74,8 +76,9 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat
|
||||
currLine = 0
|
||||
|
||||
if i < s.len-1:
|
||||
var a = ord(s[i])
|
||||
var b = ord(s[i+1])
|
||||
let
|
||||
a = ord(s[i])
|
||||
b = ord(s[i+1])
|
||||
result[r] = cb64[a shr 2]
|
||||
result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
||||
result[r+2] = cb64[((b and 0x0F) shl 2)]
|
||||
@@ -83,7 +86,7 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat
|
||||
if r+4 != result.len:
|
||||
setLen(result, r+4)
|
||||
elif i < s.len:
|
||||
var a = ord(s[i])
|
||||
let a = ord(s[i])
|
||||
result[r] = cb64[a shr 2]
|
||||
result[r+1] = cb64[(a and 3) shl 4]
|
||||
result[r+2] = '='
|
||||
@@ -127,15 +130,17 @@ proc decode*(s: string): string =
|
||||
# total is an upper bound, as we will skip arbitrary whitespace:
|
||||
result = newString(total)
|
||||
|
||||
var i = 0
|
||||
var r = 0
|
||||
var
|
||||
i = 0
|
||||
r = 0
|
||||
while true:
|
||||
while s[i] in Whitespace: inc(i)
|
||||
if i < s.len-3:
|
||||
var a = s[i].decodeByte
|
||||
var b = s[i+1].decodeByte
|
||||
var c = s[i+2].decodeByte
|
||||
var d = s[i+3].decodeByte
|
||||
let
|
||||
a = s[i].decodeByte
|
||||
b = s[i+1].decodeByte
|
||||
c = s[i+2].decodeByte
|
||||
d = s[i+3].decodeByte
|
||||
|
||||
result[r] = chr((a shl 2) and 0xff or ((b shr 4) and 0x03))
|
||||
result[r+1] = chr((b shl 4) and 0xff or ((c shr 2) and 0x0F))
|
||||
@@ -169,4 +174,4 @@ when isMainModule:
|
||||
for t in items(tests):
|
||||
assert decode(encode(t)) == t
|
||||
assert decode(encode(t, lineLen=40)) == t
|
||||
assert decode(encode(t, lineLen=76)) == t
|
||||
assert decode(encode(t, lineLen=76)) == t
|
||||
|
||||
6
tests/parser/twrongcmdsyntax.nim
Normal file
6
tests/parser/twrongcmdsyntax.nim
Normal file
@@ -0,0 +1,6 @@
|
||||
discard """
|
||||
errormsg: '''identifier expected, but found 'echo 4'''
|
||||
line: 6
|
||||
"""
|
||||
|
||||
echo 4 +2
|
||||
40
tests/tuples/tuple_subscript.nim
Normal file
40
tests/tuples/tuple_subscript.nim
Normal file
@@ -0,0 +1,40 @@
|
||||
discard """
|
||||
output: '''5
|
||||
5
|
||||
str2
|
||||
str2
|
||||
4'''
|
||||
"""
|
||||
|
||||
proc`[]` (t: tuple, key: string): string =
|
||||
for name, field in fieldPairs(t):
|
||||
if name == key:
|
||||
return $field
|
||||
return ""
|
||||
|
||||
|
||||
proc`[]` [A,B](t: tuple, key: string, op: (proc(x: A): B)): B =
|
||||
for name, field in fieldPairs(t):
|
||||
when field is A:
|
||||
if name == key:
|
||||
return op(field)
|
||||
|
||||
proc`[]=`[T](t: var tuple, key: string, val: T) =
|
||||
for name, field in fieldPairs(t):
|
||||
when field is T:
|
||||
if name == key:
|
||||
field = val
|
||||
|
||||
var tt = (a: 1, b: "str1")
|
||||
|
||||
# test built in operator
|
||||
tt[0] = 5
|
||||
echo tt[0]
|
||||
echo `[]`(tt, 0)
|
||||
|
||||
|
||||
# test overloaded operator
|
||||
tt["b"] = "str2"
|
||||
echo tt["b"]
|
||||
echo `[]`(tt, "b")
|
||||
echo tt["b", proc(s: string) : int = s.len]
|
||||
Reference in New Issue
Block a user