From c138cc36b4b4ad34f982492939db5ae16f409a88 Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 8 Oct 2011 02:14:42 +0200 Subject: [PATCH] new syntactic construct: a{i} --- compiler/ast.nim | 1 + compiler/parser.nim | 12 +++++++++++- compiler/renderer.nim | 8 +++++++- compiler/semexprs.nim | 18 ++++++++++++------ doc/grammar.txt | 3 ++- lib/core/macros.nim | 2 +- lib/pure/terminal.nim | 2 +- tests/accept/run/tunidecode.nim | 3 ++- todo.txt | 4 ++-- web/news.txt | 5 ++++- 10 files changed, 43 insertions(+), 15 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 0d920b8359..89aba40c61 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 8fbc44ebdc..544cbae876 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 .. `[]=`(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 9df8f57f4b..3c2741d3af 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 825979e274..9395366e7a 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 ab9c31c8f2..0fd631eeda 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 7fee69a34a..92308e1fcc 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 2ef926c5f2..83564f1dd9 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 bd11cf1847..2d6a0a5079 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