new syntactic construct: a{i}

This commit is contained in:
Araq
2011-10-08 02:14:42 +02:00
parent e956abbadd
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

View File

@@ -25,7 +25,7 @@ indexExpr ::= expr
castExpr ::= 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')'
addrExpr ::= 'addr' '(' optInd expr optPar ')'
symbol ::= '`' (KEYWORD | IDENT | operator | '(' ')' | '[' ']'
symbol ::= '`' (KEYWORD | IDENT | operator | '(' ')' | '[' ']' | '{' '}'
| '=' | literal)+ '`'
| IDENT
@@ -33,6 +33,7 @@ primaryPrefix ::= (prefixOperator | 'bind') optInd
primarySuffix ::= '.' optInd symbol [generalizedLit]
| '(' optInd namedExprList optPar ')'
| '[' optInd [indexExpr (comma indexExpr)* [comma]] optPar ']'
| '{' optInd ':' | colonExprList optPar '}'
| pragma
primary ::= primaryPrefix* (symbol [generalizedLit] |

View File

@@ -22,7 +22,7 @@ type
nnkTripleStrLit, nnkNilLit, nnkMetaNode, nnkDotCall,
nnkCommand, nnkCall, nnkCallStrLit, nnkExprEqExpr,
nnkExprColonExpr, nnkIdentDefs, nnkVarTuple, nnkInfix,
nnkPrefix, nnkPostfix, nnkPar, nnkCurly,
nnkPrefix, nnkPostfix, nnkPar, nnkCurly, nnkCurlyExpr,
nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange,
nnkDotExpr, nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr,
nnkElifExpr, nnkElseExpr, nnkLambda, nnkAccQuoted,

View File

@@ -312,7 +312,7 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
# These should be private, but there is no yet
# facility for binding local symbols within macros
proc styledEchoProcessArg*(s: string) = write stdout, s
proc styledEchoProcessArg*(style: TStyle) = setStyle {style}
proc styledEchoProcessArg*(style: TStyle) = setStyle({style})
proc styledEchoProcessArg*(style: set[TStyle]) = setStyle style
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color

View File

@@ -5,5 +5,6 @@ discard """
import unidecode
unidecode("Äußerst")
assert unidecode("\\x53\\x17\\x4E\\xB0") == "Bei Jing"
echo unidecode("Äußerst")

View File

@@ -8,8 +8,8 @@ Version 0.8.14
- test the sort implementation again
- optional indentation for 'case' statement
- ``bind`` as a declaration
- document splicing
- fix unidecode.nim
- document & test splicing; don't forget to test negative indexes
- thread local vs. global raiseHook()
version 0.9.0

View File

@@ -37,7 +37,7 @@ Changes affecting backwards compatibility
- The ``is`` operator is now used to check type equivalence in generic code.
- The ``pure`` pragma for procs has been renamed to ``noStackFrame``.
- The threading API has been completely redesigned.
- The interface of the ``unidecode`` module changed and it's now thread-safe.
- The ``unidecode`` module is now thread-safe and its interface has changed.
Language Additions
@@ -52,6 +52,9 @@ Language Additions
- There is a new ``discardable`` pragma that can be used to mark a routine
so that its result can be discarded implicitely.
- Constants can now have the type ``seq``.
- There is a new user-definable syntactic construct ``a{i, ...}``
that has no semantics yet for built-in types and so can be overloaded to your
heart's content.
Compiler Additions