diff options
-rwxr-xr-x | compiler/ast.nim | 1 | ||||
-rwxr-xr-x | compiler/parser.nim | 12 | ||||
-rwxr-xr-x | compiler/renderer.nim | 8 | ||||
-rwxr-xr-x | compiler/semexprs.nim | 18 | ||||
-rwxr-xr-x | doc/grammar.txt | 3 | ||||
-rwxr-xr-x | lib/core/macros.nim | 2 | ||||
-rwxr-xr-x | lib/pure/terminal.nim | 2 | ||||
-rw-r--r-- | tests/accept/run/tunidecode.nim | 3 | ||||
-rwxr-xr-x | todo.txt | 4 | ||||
-rwxr-xr-x | web/news.txt | 5 |
10 files changed, 43 insertions, 15 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim index 0d920b835..89aba40c6 100755 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -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.} diff --git a/compiler/parser.nim b/compiler/parser.nim index 8fbc44ebd..544cbae87 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -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) diff --git a/compiler/renderer.nim b/compiler/renderer.nim index f630655f3..783f0bd40 100755 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -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) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index b30ffc64a..9eed486e9 100755 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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 diff --git a/doc/grammar.txt b/doc/grammar.txt index 9df8f57f4..3c2741d3a 100755 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -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] | diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 825979e27..9395366e7 100755 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -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, diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index ab9c31c8f..0fd631eed 100755 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -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 diff --git a/tests/accept/run/tunidecode.nim b/tests/accept/run/tunidecode.nim index 7fee69a34..92308e1fc 100644 --- a/tests/accept/run/tunidecode.nim +++ b/tests/accept/run/tunidecode.nim @@ -5,5 +5,6 @@ discard """ import unidecode -unidecode("Äußerst") +assert unidecode("\\x53\\x17\\x4E\\xB0") == "Bei Jing" +echo unidecode("Äußerst") diff --git a/todo.txt b/todo.txt index 2ef926c5f..83564f1dd 100755 --- a/todo.txt +++ b/todo.txt @@ -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 diff --git a/web/news.txt b/web/news.txt index bd11cf184..2d6a0a507 100755 --- a/web/news.txt +++ b/web/news.txt @@ -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 |