new syntactic construct: a{i}
This commit is contained in:
parent
e956abbadd
commit
c138cc36b4
10 changed files with 43 additions and 15 deletions
|
|
@ -80,6 +80,7 @@ type
|
||||||
nkPostfix, # something like a! (also used for visibility)
|
nkPostfix, # something like a! (also used for visibility)
|
||||||
nkPar, # syntactic (); may be a tuple constructor
|
nkPar, # syntactic (); may be a tuple constructor
|
||||||
nkCurly, # syntactic {}
|
nkCurly, # syntactic {}
|
||||||
|
nkCurlyExpr, # an expression like a{i}
|
||||||
nkBracket, # syntactic []
|
nkBracket, # syntactic []
|
||||||
nkBracketExpr, # an expression like a[i..j, k]
|
nkBracketExpr, # an expression like a[i..j, k]
|
||||||
nkPragmaExpr, # an expression like a{.pragmas.}
|
nkPragmaExpr, # an expression like a{.pragmas.}
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,10 @@ proc parseSymbol(p: var TParser): PNode =
|
||||||
add(result, newIdentNodeP(getIdent"()", p))
|
add(result, newIdentNodeP(getIdent"()", p))
|
||||||
getTok(p)
|
getTok(p)
|
||||||
eat(p, tkParRi)
|
eat(p, tkParRi)
|
||||||
|
of tkCurlyLe:
|
||||||
|
add(result, newIdentNodeP(getIdent"{}", p))
|
||||||
|
getTok(p)
|
||||||
|
eat(p, tkCurlyRi)
|
||||||
of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDotDot:
|
of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDotDot:
|
||||||
add(result, newIdentNodeP(p.tok.ident, p))
|
add(result, newIdentNodeP(p.tok.ident, p))
|
||||||
getTok(p)
|
getTok(p)
|
||||||
|
|
@ -457,7 +461,13 @@ proc primary(p: var TParser): PNode =
|
||||||
result = parseGStrLit(p, result)
|
result = parseGStrLit(p, result)
|
||||||
of tkBracketLe:
|
of tkBracketLe:
|
||||||
result = indexExprList(p, result)
|
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 =
|
proc lowestExprAux(p: var TParser, limit: int): PNode =
|
||||||
result = primary(p)
|
result = primary(p)
|
||||||
|
|
|
||||||
|
|
@ -330,7 +330,8 @@ proc lsub(n: PNode): int =
|
||||||
else: result = len(atom(n))
|
else: result = len(atom(n))
|
||||||
of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit:
|
of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit:
|
||||||
result = len(atom(n))
|
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 nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(n[1])
|
||||||
of nkCast: result = lsub(n.sons[0]) + lsub(n.sons[1]) + len("cast[]()")
|
of nkCast: result = lsub(n.sons[0]) + lsub(n.sons[1]) + len("cast[]()")
|
||||||
of nkAddr: result = lsub(n.sons[0]) + len("addr()")
|
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, "[")
|
put(g, tkBracketLe, "[")
|
||||||
gcomma(g, n, 1)
|
gcomma(g, n, 1)
|
||||||
put(g, tkBracketRi, "]")
|
put(g, tkBracketRi, "]")
|
||||||
|
of nkCurlyExpr:
|
||||||
|
gsub(g, n.sons[0])
|
||||||
|
put(g, tkCurlyLe, "{")
|
||||||
|
gcomma(g, n, 1)
|
||||||
|
put(g, tkCurlyRi, "}")
|
||||||
of nkPragmaExpr:
|
of nkPragmaExpr:
|
||||||
gsub(g, n.sons[0])
|
gsub(g, n.sons[0])
|
||||||
gcomma(g, n, 1)
|
gcomma(g, n, 1)
|
||||||
|
|
|
||||||
|
|
@ -712,10 +712,9 @@ proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
else:
|
else:
|
||||||
GlobalError(n.Info, errUndeclaredFieldX, i.s)
|
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 = newNodeI(nkCall, n.info)
|
||||||
result.add(newIdentNode(
|
result.add(newIdentNode(ident, n.info))
|
||||||
if inAsgn: getIdent"[]=" else: getIdent"[]", n.info))
|
|
||||||
for i in 0 .. n.len-1: result.add(n[i])
|
for i in 0 .. n.len-1: result.add(n[i])
|
||||||
|
|
||||||
proc semDeref(c: PContext, n: PNode): PNode =
|
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)
|
result = semSubscript(c, n, flags)
|
||||||
if result == nil:
|
if result == nil:
|
||||||
# overloaded [] operator:
|
# overloaded [] operator:
|
||||||
result = semExpr(c, buildOverloadedSubscripts(n, inAsgn=false))
|
result = semExpr(c, buildOverloadedSubscripts(n, getIdent"[]"))
|
||||||
|
|
||||||
proc propertyWriteAccess(c: PContext, n, a: PNode): PNode =
|
proc propertyWriteAccess(c: PContext, n, a: PNode): PNode =
|
||||||
var id = considerAcc(a[1])
|
var id = considerAcc(a[1])
|
||||||
|
|
@ -828,10 +827,15 @@ proc semAsgn(c: PContext, n: PNode): PNode =
|
||||||
# --> `[]=`(a, i, x)
|
# --> `[]=`(a, i, x)
|
||||||
a = semSubscript(c, a, {efLValue})
|
a = semSubscript(c, a, {efLValue})
|
||||||
if a == nil:
|
if a == nil:
|
||||||
result = buildOverloadedSubscripts(n.sons[0], inAsgn=true)
|
result = buildOverloadedSubscripts(n.sons[0], getIdent"[]=")
|
||||||
add(result, n[1])
|
add(result, n[1])
|
||||||
return semExprNoType(c, result)
|
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})
|
a = semExprWithType(c, a, {efLValue})
|
||||||
n.sons[0] = a
|
n.sons[0] = a
|
||||||
# a = b # both are vars, means: a[] = b[]
|
# 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)
|
result = explicitGenericInstantiation(c, n, s)
|
||||||
else:
|
else:
|
||||||
result = semArrayAccess(c, n, flags)
|
result = semArrayAccess(c, n, flags)
|
||||||
|
of nkCurlyExpr:
|
||||||
|
result = semExpr(c, buildOverloadedSubscripts(n, getIdent"{}"), flags)
|
||||||
of nkPragmaExpr:
|
of nkPragmaExpr:
|
||||||
# which pragmas are allowed for expressions? `likely`, `unlikely`
|
# which pragmas are allowed for expressions? `likely`, `unlikely`
|
||||||
internalError(n.info, "semExpr() to implement") # XXX: to implement
|
internalError(n.info, "semExpr() to implement") # XXX: to implement
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ indexExpr ::= expr
|
||||||
|
|
||||||
castExpr ::= 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')'
|
castExpr ::= 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')'
|
||||||
addrExpr ::= 'addr' '(' optInd expr optPar ')'
|
addrExpr ::= 'addr' '(' optInd expr optPar ')'
|
||||||
symbol ::= '`' (KEYWORD | IDENT | operator | '(' ')' | '[' ']'
|
symbol ::= '`' (KEYWORD | IDENT | operator | '(' ')' | '[' ']' | '{' '}'
|
||||||
| '=' | literal)+ '`'
|
| '=' | literal)+ '`'
|
||||||
| IDENT
|
| IDENT
|
||||||
|
|
||||||
|
|
@ -33,6 +33,7 @@ primaryPrefix ::= (prefixOperator | 'bind') optInd
|
||||||
primarySuffix ::= '.' optInd symbol [generalizedLit]
|
primarySuffix ::= '.' optInd symbol [generalizedLit]
|
||||||
| '(' optInd namedExprList optPar ')'
|
| '(' optInd namedExprList optPar ')'
|
||||||
| '[' optInd [indexExpr (comma indexExpr)* [comma]] optPar ']'
|
| '[' optInd [indexExpr (comma indexExpr)* [comma]] optPar ']'
|
||||||
|
| '{' optInd ':' | colonExprList optPar '}'
|
||||||
| pragma
|
| pragma
|
||||||
|
|
||||||
primary ::= primaryPrefix* (symbol [generalizedLit] |
|
primary ::= primaryPrefix* (symbol [generalizedLit] |
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ type
|
||||||
nnkTripleStrLit, nnkNilLit, nnkMetaNode, nnkDotCall,
|
nnkTripleStrLit, nnkNilLit, nnkMetaNode, nnkDotCall,
|
||||||
nnkCommand, nnkCall, nnkCallStrLit, nnkExprEqExpr,
|
nnkCommand, nnkCall, nnkCallStrLit, nnkExprEqExpr,
|
||||||
nnkExprColonExpr, nnkIdentDefs, nnkVarTuple, nnkInfix,
|
nnkExprColonExpr, nnkIdentDefs, nnkVarTuple, nnkInfix,
|
||||||
nnkPrefix, nnkPostfix, nnkPar, nnkCurly,
|
nnkPrefix, nnkPostfix, nnkPar, nnkCurly, nnkCurlyExpr,
|
||||||
nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange,
|
nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange,
|
||||||
nnkDotExpr, nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr,
|
nnkDotExpr, nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr,
|
||||||
nnkElifExpr, nnkElseExpr, nnkLambda, nnkAccQuoted,
|
nnkElifExpr, nnkElseExpr, nnkLambda, nnkAccQuoted,
|
||||||
|
|
|
||||||
|
|
@ -312,7 +312,7 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
|
||||||
# These should be private, but there is no yet
|
# These should be private, but there is no yet
|
||||||
# facility for binding local symbols within macros
|
# facility for binding local symbols within macros
|
||||||
proc styledEchoProcessArg*(s: string) = write stdout, s
|
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*(style: set[TStyle]) = setStyle style
|
||||||
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
|
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
|
||||||
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color
|
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,6 @@ discard """
|
||||||
|
|
||||||
import unidecode
|
import unidecode
|
||||||
|
|
||||||
unidecode("Äußerst")
|
assert unidecode("\\x53\\x17\\x4E\\xB0") == "Bei Jing"
|
||||||
|
echo unidecode("Äußerst")
|
||||||
|
|
||||||
|
|
|
||||||
4
todo.txt
4
todo.txt
|
|
@ -8,8 +8,8 @@ Version 0.8.14
|
||||||
- test the sort implementation again
|
- test the sort implementation again
|
||||||
- optional indentation for 'case' statement
|
- optional indentation for 'case' statement
|
||||||
- ``bind`` as a declaration
|
- ``bind`` as a declaration
|
||||||
- document splicing
|
- document & test splicing; don't forget to test negative indexes
|
||||||
- fix unidecode.nim
|
- thread local vs. global raiseHook()
|
||||||
|
|
||||||
|
|
||||||
version 0.9.0
|
version 0.9.0
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ Changes affecting backwards compatibility
|
||||||
- The ``is`` operator is now used to check type equivalence in generic code.
|
- The ``is`` operator is now used to check type equivalence in generic code.
|
||||||
- The ``pure`` pragma for procs has been renamed to ``noStackFrame``.
|
- The ``pure`` pragma for procs has been renamed to ``noStackFrame``.
|
||||||
- The threading API has been completely redesigned.
|
- 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
|
Language Additions
|
||||||
|
|
@ -52,6 +52,9 @@ Language Additions
|
||||||
- There is a new ``discardable`` pragma that can be used to mark a routine
|
- There is a new ``discardable`` pragma that can be used to mark a routine
|
||||||
so that its result can be discarded implicitely.
|
so that its result can be discarded implicitely.
|
||||||
- Constants can now have the type ``seq``.
|
- 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
|
Compiler Additions
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue