new syntactic construct: a{i}

This commit is contained in:
Araq 2011-10-08 02:14:42 +02:00
commit c138cc36b4
10 changed files with 43 additions and 15 deletions

View file

@ -80,6 +80,7 @@ type
nkPostfix, # something like a! (also used for visibility)
nkPar, # syntactic (); may be a tuple constructor
nkCurly, # syntactic {}
nkCurlyExpr, # an expression like a{i}
nkBracket, # syntactic []
nkBracketExpr, # an expression like a[i..j, k]
nkPragmaExpr, # an expression like a{.pragmas.}

View file

@ -189,6 +189,10 @@ proc parseSymbol(p: var TParser): PNode =
add(result, newIdentNodeP(getIdent"()", p))
getTok(p)
eat(p, tkParRi)
of tkCurlyLe:
add(result, newIdentNodeP(getIdent"{}", p))
getTok(p)
eat(p, tkCurlyRi)
of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDotDot:
add(result, newIdentNodeP(p.tok.ident, p))
getTok(p)
@ -457,7 +461,13 @@ proc primary(p: var TParser): PNode =
result = parseGStrLit(p, result)
of tkBracketLe:
result = indexExprList(p, result)
else: break
of tkCurlyLe:
var a = result
result = newNodeP(nkCurlyExpr, p)
var b = setOrTableConstr(p)
result.add(a)
for i in 0 .. <b.len: result.add(b.sons[i])
else: break
proc lowestExprAux(p: var TParser, limit: int): PNode =
result = primary(p)

View file

@ -330,7 +330,8 @@ proc lsub(n: PNode): int =
else: result = len(atom(n))
of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit:
result = len(atom(n))
of nkCall, nkBracketExpr, nkConv: result = lsub(n.sons[0]) + lcomma(n, 1) + 2
of nkCall, nkBracketExpr, nkCurlyExpr, nkConv:
result = lsub(n.sons[0]) + lcomma(n, 1) + 2
of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(n[1])
of nkCast: result = lsub(n.sons[0]) + lsub(n.sons[1]) + len("cast[]()")
of nkAddr: result = lsub(n.sons[0]) + len("addr()")
@ -702,6 +703,11 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkBracketLe, "[")
gcomma(g, n, 1)
put(g, tkBracketRi, "]")
of nkCurlyExpr:
gsub(g, n.sons[0])
put(g, tkCurlyLe, "{")
gcomma(g, n, 1)
put(g, tkCurlyRi, "}")
of nkPragmaExpr:
gsub(g, n.sons[0])
gcomma(g, n, 1)

View file

@ -712,10 +712,9 @@ proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
else:
GlobalError(n.Info, errUndeclaredFieldX, i.s)
proc buildOverloadedSubscripts(n: PNode, inAsgn: bool): PNode =
proc buildOverloadedSubscripts(n: PNode, ident: PIdent): PNode =
result = newNodeI(nkCall, n.info)
result.add(newIdentNode(
if inAsgn: getIdent"[]=" else: getIdent"[]", n.info))
result.add(newIdentNode(ident, n.info))
for i in 0 .. n.len-1: result.add(n[i])
proc semDeref(c: PContext, n: PNode): PNode =
@ -772,7 +771,7 @@ proc semArrayAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
result = semSubscript(c, n, flags)
if result == nil:
# overloaded [] operator:
result = semExpr(c, buildOverloadedSubscripts(n, inAsgn=false))
result = semExpr(c, buildOverloadedSubscripts(n, getIdent"[]"))
proc propertyWriteAccess(c: PContext, n, a: PNode): PNode =
var id = considerAcc(a[1])
@ -828,10 +827,15 @@ proc semAsgn(c: PContext, n: PNode): PNode =
# --> `[]=`(a, i, x)
a = semSubscript(c, a, {efLValue})
if a == nil:
result = buildOverloadedSubscripts(n.sons[0], inAsgn=true)
result = buildOverloadedSubscripts(n.sons[0], getIdent"[]=")
add(result, n[1])
return semExprNoType(c, result)
else:
of nkCurlyExpr:
# a{i} = x --> `{}=`(a, i, x)
result = buildOverloadedSubscripts(n.sons[0], getIdent"{}=")
add(result, n[1])
return semExprNoType(c, result)
else:
a = semExprWithType(c, a, {efLValue})
n.sons[0] = a
# a = b # both are vars, means: a[] = b[]
@ -1243,6 +1247,8 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
result = explicitGenericInstantiation(c, n, s)
else:
result = semArrayAccess(c, n, flags)
of nkCurlyExpr:
result = semExpr(c, buildOverloadedSubscripts(n, getIdent"{}"), flags)
of nkPragmaExpr:
# which pragmas are allowed for expressions? `likely`, `unlikely`
internalError(n.info, "semExpr() to implement") # XXX: to implement