diff --git a/compiler/parser.nim b/compiler/parser.nim index 9563849c66..9521014550 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -149,20 +149,27 @@ proc parseTypeDesc(p: var TParser): PNode proc parseDoBlocks(p: var TParser, call: PNode) proc parseParamList(p: var TParser, retColon = true): PNode +proc relevantOprChar(ident: PIdent): char {.inline.} = + result = ident.s[0] + var L = ident.s.len + if result == '\\' and L > 1: + result = ident.s[1] + +proc IsSigilLike(tok: TToken): bool {.inline.} = + result = tok.tokType == tkOpr and relevantOprChar(tok.ident) == '@' + proc IsLeftAssociative(tok: TToken): bool {.inline.} = - result = tok.tokType != tkOpr or tok.ident.s[0] != '^' + result = tok.tokType != tkOpr or relevantOprChar(tok.ident) != '^' proc getPrecedence(tok: TToken): int = case tok.tokType of tkOpr: - var relevantChar = tok.ident.s[0] - var L = tok.ident.s.len - if relevantChar == '\\' and L > 1: - relevantChar = tok.ident.s[1] + let L = tok.ident.s.len + let relevantChar = relevantOprChar(tok.ident) template considerAsgn(value: expr) = result = if tok.ident.s[L-1] == '=': 1 else: value - + case relevantChar of '$', '^': considerAsgn(10) of '*', '%', '/', '\\': considerAsgn(9) @@ -451,16 +458,41 @@ proc identOrLiteral(p: var TParser): PNode = getTok(p) # we must consume a token here to prevend endless loops! result = ast.emptyNode -proc primary(p: var TParser): PNode = +proc primarySuffix(p: var TParser, r: PNode): PNode = + result = r + while true: + case p.tok.tokType + of tkParLe: + var a = result + result = newNodeP(nkCall, p) + addSon(result, a) + exprColonEqExprListAux(p, nkExprEqExpr, tkParRi, tkEquals, result) + parseDoBlocks(p, result) + of tkDot: + result = dotExpr(p, result) + result = parseGStrLit(p, result) + of tkBracketLe: + result = indexExprList(p, result, nkBracketExpr, tkBracketRi) + of tkCurlyLe: + result = indexExprList(p, result, nkCurlyExpr, tkCurlyRi) + else: break + +proc primary(p: var TParser, skipSuffix = false): PNode = # prefix operator? if isOperator(p.tok): + let isSigil = IsSigilLike(p.tok) result = newNodeP(nkPrefix, p) var a = newIdentNodeP(p.tok.ident, p) addSon(result, a) getTok(p) optInd(p, a) - addSon(result, primary(p)) - return + if isSigil: + #XXX prefix operators + addSon(result, primary(p, true)) + result = primarySuffix(p, result) + else: + addSon(result, primary(p)) + return elif p.tok.tokType == tkAddr: result = newNodeP(nkAddr, p) getTok(p) @@ -478,22 +510,8 @@ proc primary(p: var TParser): PNode = addSon(result, primary(p)) return result = identOrLiteral(p) - while true: - case p.tok.tokType - of tkParLe: - var a = result - result = newNodeP(nkCall, p) - addSon(result, a) - exprColonEqExprListAux(p, nkExprEqExpr, tkParRi, tkEquals, result) - parseDoBlocks(p, result) - of tkDot: - result = dotExpr(p, result) - result = parseGStrLit(p, result) - of tkBracketLe: - result = indexExprList(p, result, nkBracketExpr, tkBracketRi) - of tkCurlyLe: - result = indexExprList(p, result, nkCurlyExpr, tkCurlyRi) - else: break + if not skipSuffix: + result = primarySuffix(p, result) proc lowestExprAux(p: var TParser, limit: int): PNode = result = primary(p) diff --git a/compiler/ropes.nim b/compiler/ropes.nim index c37745a637..b5a273e864 100755 --- a/compiler/ropes.nim +++ b/compiler/ropes.nim @@ -53,19 +53,12 @@ # Leaves have relatively high memory overhead (~30 bytes on a 32 # bit machines) and we produce many of them. This is why we cache and # share leaves accross different rope trees. -# To cache them they are inserted in another tree, a splay tree for best -# performance. But for the caching tree we use the leaves' left and right -# pointers. -# +# To cache them they are inserted in a `cache` array. import msgs, strutils, platform, hashes, crc, options -const - CacheLeafs* = true - countCacheMisses* = false # see what our little optimization gives - -type +type TFormatStr* = string # later we may change it to CString for better # performance of the code generator (assignments # copy the format strings @@ -94,7 +87,6 @@ proc writeRopeIfNotEqual*(r: PRope, filename: string): bool proc ropeToStr*(p: PRope): string proc ropef*(frmt: TFormatStr, args: openarray[PRope]): PRope proc appf*(c: var PRope, frmt: TFormatStr, args: openarray[PRope]) -proc getCacheStats*(): string proc RopeEqualsFile*(r: PRope, f: string): bool # returns true if the rope r is the same as the contents of file f proc RopeInvariant*(r: PRope): bool @@ -119,81 +111,6 @@ proc newMutableRope*(capacity = 30): PRope = var cache: array[0..2048 -1, PRope] - misses, hits: int - N: PRope # dummy rope needed for splay algorithm - -proc getCacheStats(): string = - if hits + misses != 0: - result = "Misses: " & $(misses) & " total: " & $(hits + misses) & " quot: " & - $(toFloat(misses) / toFloat(hits + misses)) - else: - result = "" - -proc splay(s: string, tree: PRope, cmpres: var int): PRope = - var c: int - var t = tree - N.left = nil - N.right = nil # reset to nil - var le = N - var r = N - while true: - c = cmp(s, t.data) - if c < 0: - if (t.left != nil) and (s < t.left.data): - var y = t.left - t.left = y.right - y.right = t - t = y - if t.left == nil: break - r.left = t - r = t - t = t.left - elif c > 0: - if (t.right != nil) and (s > t.right.data): - var y = t.right - t.right = y.left - y.left = t - t = y - if t.right == nil: break - le.right = t - le = t - t = t.right - else: - break - cmpres = c - le.right = t.left - r.left = t.right - t.left = N.right - t.right = N.left - result = t - -proc insertInCache(s: string, tree: PRope): PRope = - # Insert i into the tree t, unless it's already there. - # Return a pointer to the resulting tree. - var t = tree - if t == nil: - result = newRope(s) - if countCacheMisses: inc(misses) - return - var cmp: int - t = splay(s, t, cmp) - if cmp == 0: - # We get here if it's already in the Tree - # Don't add it again - result = t - if countCacheMisses: inc(hits) - else: - if countCacheMisses: inc(misses) - result = newRope(s) - if cmp < 0: - result.left = t.left - result.right = t - t.left = nil - else: - # i > t.item: - result.right = t.right - result.left = t - t.right = nil proc RopeInvariant(r: PRope): bool = if r == nil: @@ -215,13 +132,11 @@ proc insertInCache(s: string): PRope = result = newRope(s) cache[h] = result -proc toRope(s: string): PRope = - if s.len == 0: +proc toRope(s: string): PRope = + if s.len == 0: result = nil - elif cacheLeafs: - result = insertInCache(s) else: - result = newRope(s) + result = insertInCache(s) assert(RopeInvariant(result)) proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) = @@ -234,17 +149,6 @@ proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) = rs[i] = rs[i - 1] # this is correct, I used pen and paper to validate it rs[at] = r -proc recRopeToStr(result: var string, resultLen: var int, p: PRope) = - if p == nil: - return # do not add to result - if (p.data == nil): - recRopeToStr(result, resultLen, p.left) - recRopeToStr(result, resultLen, p.right) - else: - CopyMem(addr(result[resultLen + 0]), addr(p.data[0]), p.length) - Inc(resultLen, p.length) - assert(resultLen <= len(result)) - proc newRecRopeToStr(result: var string, resultLen: var int, r: PRope) = var stack = @[r] while len(stack) > 0: @@ -281,7 +185,7 @@ proc con(a: openarray[PRope]): PRope = for i in countup(0, high(a)): result = con(result, a[i]) proc toRope(i: BiggestInt): PRope = result = toRope($i) -#proc toRopeF*(r: BiggestFloat): PRope = result = toRope($r) + proc app(a: var PRope, b: PRope) = a = con(a, b) proc app(a: var PRope, b: string) = a = con(a, b) proc prepend(a: var PRope, b: PRope) = a = con(b, a) @@ -411,5 +315,3 @@ proc writeRopeIfNotEqual(r: PRope, filename: string): bool = result = true else: result = false - -new(N) # init dummy node for splay algorithm diff --git a/doc/manual.txt b/doc/manual.txt index 6b4ba546ae..98a8d71222 100755 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -388,19 +388,38 @@ indentation tokens is already described in the `Lexical Analysis`_ section. Nimrod allows user-definable operators. Binary operators have 10 different levels of precedence. -All binary operators are left-associative, except binary operators starting -with (or only consisting of) ``^``. + +Relevant character +------------------ + +An operator symbol's *relevant character* is its first +character unless the first character is ``\`` and its length is greater than 1 +then it is the second character. + +This rule allows to escape operator symbols with ``\`` and keeps the operator's +precedence and associativity; this is useful for meta programming. + + +Associativity +------------- + +All binary operators are left-associative, except binary operators whose +relevant char is ``^``. + +Precedence +---------- For operators that are not keywords the precedence is determined by the following rules: -An operator symbol's *relevant character* is its first -character unless the first character is ``\`` and its length is greater than 1 -then it is the second character. - If the operator ends with ``=`` and its relevant character is none of ``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which -has the lowest precedence. +has the lowest precedence. + +If the operator's relevant character is ``@`` it is a `sigil-like`:idx: +operator which binds stronger than a ``primarySuffix``: ``@x.abc`` is parsed +as ``(@x).abc`` whereas ``$x.abc`` is parsed as ``$(x.abc)``. + Otherwise precedence is determined by the relevant character. diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index b5c72491c6..6702d8bebc 100755 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -62,8 +62,8 @@ type pkBackRef, ## $i --> Internal DSL: backref(i) pkBackRefIgnoreCase, pkBackRefIgnoreStyle, - pkSearch, ## @a --> Internal DSL: @a - pkCapturedSearch, ## {@} a --> Internal DSL: @@a + pkSearch, ## @a --> Internal DSL: !*a + pkCapturedSearch, ## {@} a --> Internal DSL: !*\a pkRule, ## a <- b pkList, ## a, b pkStartAnchor ## ^ --> Internal DSL: startAnchor() @@ -200,13 +200,13 @@ proc `*`*(a: TPeg): TPeg {.nosideEffect, rtl, extern: "npegsGreedyRep".} = result.kind = pkGreedyRep result.sons = @[a] -proc `@`*(a: TPeg): TPeg {.nosideEffect, rtl, extern: "npegsSearch".} = +proc `!*`*(a: TPeg): TPeg {.nosideEffect, rtl, extern: "npegsSearch".} = ## constructs a "search" for the PEG `a` result.kind = pkSearch result.sons = @[a] -proc `@@`*(a: TPeg): TPeg {.noSideEffect, rtl, - extern: "npgegsCapturedSearch".} = +proc `!*\`*(a: TPeg): TPeg {.noSideEffect, rtl, + extern: "npgegsCapturedSearch".} = ## constructs a "captured search" for the PEG `a` result.kind = pkCapturedSearch result.sons = @[a] @@ -1486,10 +1486,10 @@ proc primary(p: var TPegParser): TPeg = return !primary(p) of tkAt: getTok(p) - return @(primary(p)) + return !*primary(p) of tkCurlyAt: getTok(p) - return @@(primary(p).token(p)) + return !*\primary(p).token(p) else: nil case p.tok.kind of tkIdentifier: diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index eef8163feb..c61cf2beab 100755 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -237,8 +237,6 @@ proc nimFloatToStr(x: float): string {.compilerproc.} = return $buf proc nimInt64ToStr(x: int64): string {.compilerRtl.} = - # we don't rely on C's runtime here as some C compiler's - # int64 support is weak result = newString(sizeof(x)*4) var i = 0 var y = x diff --git a/tests/run/toprprec.nim b/tests/run/toprprec.nim index 17ec91fdac..ce33934b57 100755 --- a/tests/run/toprprec.nim +++ b/tests/run/toprprec.nim @@ -4,6 +4,22 @@ discard """ """ # Test operator precedence: +template `@` (x: expr): expr {.immediate.} = self.x +template `@!` (x: expr): expr {.immediate.} = x +template `===` (x: expr): expr {.immediate.} = x + +type + TO = object + x: int + TA = tuple[a, b: int, obj: TO] + +proc init(self: var TA): string = + @a = 3 + === @b = 4 + @obj.x = 4 + @! === result = "abc" + result = @b.`$` + assert 3+5*5-2 == 28- -26-28 proc `^-` (x, y: int): int = @@ -12,6 +28,11 @@ proc `^-` (x, y: int): int = assert 34 ^- 6 ^- 2 == 30 assert 34 - 6 - 2 == 26 + + +var s: TA +assert init(s) == "4" + echo "done" diff --git a/tests/run/tpegs.nim b/tests/run/tpegs.nim index 8fe3020735..4473f90bb8 100755 --- a/tests/run/tpegs.nim +++ b/tests/run/tpegs.nim @@ -57,8 +57,8 @@ type pkBackRef, ## $i --> Internal DSL: backref(i) pkBackRefIgnoreCase, pkBackRefIgnoreStyle, - pkSearch, ## @a --> Internal DSL: @a - pkCapturedSearch, ## {@} a --> Internal DSL: @@a + pkSearch, ## @a --> Internal DSL: !*a + pkCapturedSearch, ## {@} a --> Internal DSL: !*\a pkRule, ## a <- b pkList, ## a, b pkStartAnchor ## ^ --> Internal DSL: startAnchor() @@ -212,13 +212,13 @@ proc `*`*(a: TPeg): TPeg {.rtl, extern: "npegsGreedyRep".} = result.kind = pkGreedyRep result.sons = @[a] -proc `@`*(a: TPeg): TPeg {.rtl, extern: "npegsSearch".} = +proc `!*`*(a: TPeg): TPeg {.rtl, extern: "npegsSearch".} = ## constructs a "search" for the PEG `a` result.kind = pkSearch result.sons = @[a] -proc `@@`*(a: TPeg): TPeg {.rtl, - extern: "npgegsCapturedSearch".} = +proc `!*\`*(a: TPeg): TPeg {.rtl, + extern: "npgegsCapturedSearch".} = ## constructs a "captured search" for the PEG `a` result.kind = pkCapturedSearch result.sons = @[a] @@ -1484,10 +1484,10 @@ proc primary(p: var TPegParser): TPeg = return !primary(p) of tkAt: getTok(p) - return @primary(p) + return !*primary(p) of tkCurlyAt: getTok(p) - return @@primary(p).token(p) + return !*\primary(p).token(p) else: nil case p.tok.kind of tkIdentifier: diff --git a/todo.txt b/todo.txt index 6037676a52..3d9bf06bf8 100755 --- a/todo.txt +++ b/todo.txt @@ -1,8 +1,8 @@ version 0.9.0 ============= -- sigil-like operator: @x.abc parsed as (@x).abc -- document AVR/embedded systems better +- document AVR/embedded systems better; document all the ``useX`` conditional + symbols for the stdlib - implement ``--script:sh|bat`` command line option - make GC realtime capable: GC_step(ms: int) - make templates hygienic by default @@ -20,8 +20,7 @@ version 0.9.0 - implement the high level optimizer - change overloading resolution - implement proper coroutines -- implement ``partial`` pragma for partial evaluation; ``hoist`` pragma for - loop hoisting +- ``hoist`` pragma for loop hoisting - we need to support iteration of 2 different data structures in parallel - make exceptions compatible with C++ exceptions - change how comments are part of the AST @@ -193,3 +192,4 @@ Version 2 and beyond case x with `=~` +- implement ``partial`` pragma for partial evaluation diff --git a/web/news.txt b/web/news.txt index b0a669def5..642bd2c725 100755 --- a/web/news.txt +++ b/web/news.txt @@ -50,6 +50,8 @@ Changes affecting backwards compatibility to get a template of old behaviour. - There is now a proper distinction in the type system between ``expr`` and ``PNimrodNode`` which unfortunately breaks the old macro system. +- ``pegs.@`` has been renamed to ``pegs.!*`` and ``pegs.@@`` has been renamed + to ``pegs.!*\`` as ``@`` operators now have different precedence. Compiler Additions @@ -73,7 +75,9 @@ Language Additions - ``addr`` is now treated like a prefix operator syntactically. - Added ``global`` pragma that can be used to introduce new global variables from within procs. -- when expressions are now allowed just like if expressions +- ``when`` expressions are now allowed just like ``if`` expressions. +- The precedence for operators starting with ``@`` is different now + allowing for *sigil-like* operators. 2012-02-09 Version 0.8.14 released