Merge branch 'devel' of github.com:nim-lang/Nim into devel
This commit is contained in:
commit
2da66fef77
5 changed files with 73 additions and 25 deletions
|
|
@ -1238,10 +1238,7 @@ proc parseExprStmt(p: var TParser): PNode =
|
||||||
addSon(result, e)
|
addSon(result, e)
|
||||||
if p.tok.tokType != tkComma: break
|
if p.tok.tokType != tkComma: break
|
||||||
elif p.tok.indent < 0 and isExprStart(p):
|
elif p.tok.indent < 0 and isExprStart(p):
|
||||||
if a.kind == nkCommand:
|
result = newNode(nkCommand, a.info, @[a])
|
||||||
result = a
|
|
||||||
else:
|
|
||||||
result = newNode(nkCommand, a.info, @[a])
|
|
||||||
while true:
|
while true:
|
||||||
var e = parseExpr(p)
|
var e = parseExpr(p)
|
||||||
addSon(result, e)
|
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.typ = makeTypeDesc(c, semTypeNode(c, n, nil))
|
||||||
#result = symNodeFromType(c, semTypeNode(c, n, nil), n.info)
|
#result = symNodeFromType(c, semTypeNode(c, n, nil), n.info)
|
||||||
of tyTuple:
|
of tyTuple:
|
||||||
checkSonsLen(n, 2)
|
if n.len != 2: return nil
|
||||||
n.sons[0] = makeDeref(n.sons[0])
|
n.sons[0] = makeDeref(n.sons[0])
|
||||||
c.p.bracketExpr = n.sons[0]
|
c.p.bracketExpr = n.sons[0]
|
||||||
# [] operator for tuples requires constant expression:
|
# [] 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])
|
var idx = getOrdValue(n.sons[1])
|
||||||
if idx >= 0 and idx < sonsLen(arr): n.typ = arr.sons[int(idx)]
|
if idx >= 0 and idx < sonsLen(arr): n.typ = arr.sons[int(idx)]
|
||||||
else: localError(n.info, errInvalidIndexValueForTuple)
|
else: localError(n.info, errInvalidIndexValueForTuple)
|
||||||
|
result = n
|
||||||
else:
|
else:
|
||||||
localError(n.info, errIndexTypesDoNotMatch)
|
result = nil
|
||||||
result = n
|
|
||||||
else:
|
else:
|
||||||
let s = if n.sons[0].kind == nkSym: n.sons[0].sym
|
let s = if n.sons[0].kind == nkSym: n.sons[0].sym
|
||||||
elif n[0].kind in nkSymChoices: n.sons[0][0].sym
|
elif n[0].kind in nkSymChoices: n.sons[0][0].sym
|
||||||
|
|
|
||||||
|
|
@ -44,21 +44,23 @@
|
||||||
const
|
const
|
||||||
cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
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
|
## encodes `s` into base64 representation. After `lineLen` characters, a
|
||||||
## `newline` is added.
|
## `newline` is added.
|
||||||
var total = ((len(s) + 2) div 3) * 4
|
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)
|
if numLines > 0: inc(total, (numLines - 1) * newLine.len)
|
||||||
|
|
||||||
result = newString(total)
|
result = newString(total)
|
||||||
var i = 0
|
var
|
||||||
var r = 0
|
i = 0
|
||||||
var currLine = 0
|
r = 0
|
||||||
|
currLine = 0
|
||||||
while i < s.len - 2:
|
while i < s.len - 2:
|
||||||
var a = ord(s[i])
|
let
|
||||||
var b = ord(s[i+1])
|
a = ord(s[i])
|
||||||
var c = ord(s[i+2])
|
b = ord(s[i+1])
|
||||||
|
c = ord(s[i+2])
|
||||||
result[r] = cb64[a shr 2]
|
result[r] = cb64[a shr 2]
|
||||||
result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
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)]
|
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
|
currLine = 0
|
||||||
|
|
||||||
if i < s.len-1:
|
if i < s.len-1:
|
||||||
var a = ord(s[i])
|
let
|
||||||
var b = ord(s[i+1])
|
a = ord(s[i])
|
||||||
|
b = ord(s[i+1])
|
||||||
result[r] = cb64[a shr 2]
|
result[r] = cb64[a shr 2]
|
||||||
result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
||||||
result[r+2] = cb64[((b and 0x0F) shl 2)]
|
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:
|
if r+4 != result.len:
|
||||||
setLen(result, r+4)
|
setLen(result, r+4)
|
||||||
elif i < s.len:
|
elif i < s.len:
|
||||||
var a = ord(s[i])
|
let a = ord(s[i])
|
||||||
result[r] = cb64[a shr 2]
|
result[r] = cb64[a shr 2]
|
||||||
result[r+1] = cb64[(a and 3) shl 4]
|
result[r+1] = cb64[(a and 3) shl 4]
|
||||||
result[r+2] = '='
|
result[r+2] = '='
|
||||||
|
|
@ -127,15 +130,17 @@ proc decode*(s: string): string =
|
||||||
# total is an upper bound, as we will skip arbitrary whitespace:
|
# total is an upper bound, as we will skip arbitrary whitespace:
|
||||||
result = newString(total)
|
result = newString(total)
|
||||||
|
|
||||||
var i = 0
|
var
|
||||||
var r = 0
|
i = 0
|
||||||
|
r = 0
|
||||||
while true:
|
while true:
|
||||||
while s[i] in Whitespace: inc(i)
|
while s[i] in Whitespace: inc(i)
|
||||||
if i < s.len-3:
|
if i < s.len-3:
|
||||||
var a = s[i].decodeByte
|
let
|
||||||
var b = s[i+1].decodeByte
|
a = s[i].decodeByte
|
||||||
var c = s[i+2].decodeByte
|
b = s[i+1].decodeByte
|
||||||
var d = s[i+3].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] = 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))
|
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):
|
for t in items(tests):
|
||||||
assert decode(encode(t)) == t
|
assert decode(encode(t)) == t
|
||||||
assert decode(encode(t, lineLen=40)) == 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]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue