@ is a sigil-like operator

This commit is contained in:
Araq 2012-04-15 10:02:15 +02:00
commit 959e370ae9
9 changed files with 119 additions and 157 deletions

View file

@ -149,16 +149,23 @@ proc parseTypeDesc(p: var TParser): PNode
proc parseDoBlocks(p: var TParser, call: PNode) proc parseDoBlocks(p: var TParser, call: PNode)
proc parseParamList(p: var TParser, retColon = true): 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.} = 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 = proc getPrecedence(tok: TToken): int =
case tok.tokType case tok.tokType
of tkOpr: of tkOpr:
var relevantChar = tok.ident.s[0] let L = tok.ident.s.len
var L = tok.ident.s.len let relevantChar = relevantOprChar(tok.ident)
if relevantChar == '\\' and L > 1:
relevantChar = tok.ident.s[1]
template considerAsgn(value: expr) = template considerAsgn(value: expr) =
result = if tok.ident.s[L-1] == '=': 1 else: value result = if tok.ident.s[L-1] == '=': 1 else: value
@ -451,15 +458,40 @@ proc identOrLiteral(p: var TParser): PNode =
getTok(p) # we must consume a token here to prevend endless loops! getTok(p) # we must consume a token here to prevend endless loops!
result = ast.emptyNode 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? # prefix operator?
if isOperator(p.tok): if isOperator(p.tok):
let isSigil = IsSigilLike(p.tok)
result = newNodeP(nkPrefix, p) result = newNodeP(nkPrefix, p)
var a = newIdentNodeP(p.tok.ident, p) var a = newIdentNodeP(p.tok.ident, p)
addSon(result, a) addSon(result, a)
getTok(p) getTok(p)
optInd(p, a) optInd(p, a)
addSon(result, primary(p)) if isSigil:
#XXX prefix operators
addSon(result, primary(p, true))
result = primarySuffix(p, result)
else:
addSon(result, primary(p))
return return
elif p.tok.tokType == tkAddr: elif p.tok.tokType == tkAddr:
result = newNodeP(nkAddr, p) result = newNodeP(nkAddr, p)
@ -478,22 +510,8 @@ proc primary(p: var TParser): PNode =
addSon(result, primary(p)) addSon(result, primary(p))
return return
result = identOrLiteral(p) result = identOrLiteral(p)
while true: if not skipSuffix:
case p.tok.tokType result = primarySuffix(p, result)
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 lowestExprAux(p: var TParser, limit: int): PNode = proc lowestExprAux(p: var TParser, limit: int): PNode =
result = primary(p) result = primary(p)

View file

@ -53,18 +53,11 @@
# Leaves have relatively high memory overhead (~30 bytes on a 32 # 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 # bit machines) and we produce many of them. This is why we cache and
# share leaves accross different rope trees. # share leaves accross different rope trees.
# To cache them they are inserted in another tree, a splay tree for best # To cache them they are inserted in a `cache` array.
# performance. But for the caching tree we use the leaves' left and right
# pointers.
#
import import
msgs, strutils, platform, hashes, crc, options 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 TFormatStr* = string # later we may change it to CString for better
# performance of the code generator (assignments # performance of the code generator (assignments
@ -94,7 +87,6 @@ proc writeRopeIfNotEqual*(r: PRope, filename: string): bool
proc ropeToStr*(p: PRope): string proc ropeToStr*(p: PRope): string
proc ropef*(frmt: TFormatStr, args: openarray[PRope]): PRope proc ropef*(frmt: TFormatStr, args: openarray[PRope]): PRope
proc appf*(c: var PRope, frmt: TFormatStr, args: openarray[PRope]) proc appf*(c: var PRope, frmt: TFormatStr, args: openarray[PRope])
proc getCacheStats*(): string
proc RopeEqualsFile*(r: PRope, f: string): bool proc RopeEqualsFile*(r: PRope, f: string): bool
# returns true if the rope r is the same as the contents of file f # returns true if the rope r is the same as the contents of file f
proc RopeInvariant*(r: PRope): bool proc RopeInvariant*(r: PRope): bool
@ -119,81 +111,6 @@ proc newMutableRope*(capacity = 30): PRope =
var var
cache: array[0..2048 -1, PRope] 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 = proc RopeInvariant(r: PRope): bool =
if r == nil: if r == nil:
@ -218,10 +135,8 @@ proc insertInCache(s: string): PRope =
proc toRope(s: string): PRope = proc toRope(s: string): PRope =
if s.len == 0: if s.len == 0:
result = nil result = nil
elif cacheLeafs:
result = insertInCache(s)
else: else:
result = newRope(s) result = insertInCache(s)
assert(RopeInvariant(result)) assert(RopeInvariant(result))
proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) = 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[i] = rs[i - 1] # this is correct, I used pen and paper to validate it
rs[at] = r 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) = proc newRecRopeToStr(result: var string, resultLen: var int, r: PRope) =
var stack = @[r] var stack = @[r]
while len(stack) > 0: 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]) for i in countup(0, high(a)): result = con(result, a[i])
proc toRope(i: BiggestInt): PRope = result = toRope($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: PRope) = a = con(a, b)
proc app(a: var PRope, b: string) = 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) proc prepend(a: var PRope, b: PRope) = a = con(b, a)
@ -411,5 +315,3 @@ proc writeRopeIfNotEqual(r: PRope, filename: string): bool =
result = true result = true
else: else:
result = false result = false
new(N) # init dummy node for splay algorithm

View file

@ -388,20 +388,39 @@ indentation tokens is already described in the `Lexical Analysis`_ section.
Nimrod allows user-definable operators. Nimrod allows user-definable operators.
Binary operators have 10 different levels of precedence. Binary operators have 10 different levels of precedence.
All binary operators are left-associative, except binary operators starting
with (or only consisting of) ``^``.
For operators that are not keywords the precedence is determined by the Relevant character
following rules: ------------------
An operator symbol's *relevant character* is its first An operator symbol's *relevant character* is its first
character unless the first character is ``\`` and its length is greater than 1 character unless the first character is ``\`` and its length is greater than 1
then it is the second character. 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:
If the operator ends with ``=`` and its relevant character is none of If the operator ends with ``=`` and its relevant character is none of
``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which ``<``, ``>``, ``!``, ``=``, ``~``, ``?``, 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. Otherwise precedence is determined by the relevant character.
================ =============================================== ================== =============== ================ =============================================== ================== ===============

View file

@ -62,8 +62,8 @@ type
pkBackRef, ## $i --> Internal DSL: backref(i) pkBackRef, ## $i --> Internal DSL: backref(i)
pkBackRefIgnoreCase, pkBackRefIgnoreCase,
pkBackRefIgnoreStyle, pkBackRefIgnoreStyle,
pkSearch, ## @a --> Internal DSL: @a pkSearch, ## @a --> Internal DSL: !*a
pkCapturedSearch, ## {@} a --> Internal DSL: @@a pkCapturedSearch, ## {@} a --> Internal DSL: !*\a
pkRule, ## a <- b pkRule, ## a <- b
pkList, ## a, b pkList, ## a, b
pkStartAnchor ## ^ --> Internal DSL: startAnchor() pkStartAnchor ## ^ --> Internal DSL: startAnchor()
@ -200,13 +200,13 @@ proc `*`*(a: TPeg): TPeg {.nosideEffect, rtl, extern: "npegsGreedyRep".} =
result.kind = pkGreedyRep result.kind = pkGreedyRep
result.sons = @[a] 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` ## constructs a "search" for the PEG `a`
result.kind = pkSearch result.kind = pkSearch
result.sons = @[a] result.sons = @[a]
proc `@@`*(a: TPeg): TPeg {.noSideEffect, rtl, proc `!*\`*(a: TPeg): TPeg {.noSideEffect, rtl,
extern: "npgegsCapturedSearch".} = extern: "npgegsCapturedSearch".} =
## constructs a "captured search" for the PEG `a` ## constructs a "captured search" for the PEG `a`
result.kind = pkCapturedSearch result.kind = pkCapturedSearch
result.sons = @[a] result.sons = @[a]
@ -1486,10 +1486,10 @@ proc primary(p: var TPegParser): TPeg =
return !primary(p) return !primary(p)
of tkAt: of tkAt:
getTok(p) getTok(p)
return @(primary(p)) return !*primary(p)
of tkCurlyAt: of tkCurlyAt:
getTok(p) getTok(p)
return @@(primary(p).token(p)) return !*\primary(p).token(p)
else: nil else: nil
case p.tok.kind case p.tok.kind
of tkIdentifier: of tkIdentifier:

View file

@ -237,8 +237,6 @@ proc nimFloatToStr(x: float): string {.compilerproc.} =
return $buf return $buf
proc nimInt64ToStr(x: int64): string {.compilerRtl.} = 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) result = newString(sizeof(x)*4)
var i = 0 var i = 0
var y = x var y = x

View file

@ -4,6 +4,22 @@ discard """
""" """
# Test operator precedence: # 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 assert 3+5*5-2 == 28- -26-28
proc `^-` (x, y: int): int = proc `^-` (x, y: int): int =
@ -12,6 +28,11 @@ proc `^-` (x, y: int): int =
assert 34 ^- 6 ^- 2 == 30 assert 34 ^- 6 ^- 2 == 30
assert 34 - 6 - 2 == 26 assert 34 - 6 - 2 == 26
var s: TA
assert init(s) == "4"
echo "done" echo "done"

View file

@ -57,8 +57,8 @@ type
pkBackRef, ## $i --> Internal DSL: backref(i) pkBackRef, ## $i --> Internal DSL: backref(i)
pkBackRefIgnoreCase, pkBackRefIgnoreCase,
pkBackRefIgnoreStyle, pkBackRefIgnoreStyle,
pkSearch, ## @a --> Internal DSL: @a pkSearch, ## @a --> Internal DSL: !*a
pkCapturedSearch, ## {@} a --> Internal DSL: @@a pkCapturedSearch, ## {@} a --> Internal DSL: !*\a
pkRule, ## a <- b pkRule, ## a <- b
pkList, ## a, b pkList, ## a, b
pkStartAnchor ## ^ --> Internal DSL: startAnchor() pkStartAnchor ## ^ --> Internal DSL: startAnchor()
@ -212,13 +212,13 @@ proc `*`*(a: TPeg): TPeg {.rtl, extern: "npegsGreedyRep".} =
result.kind = pkGreedyRep result.kind = pkGreedyRep
result.sons = @[a] result.sons = @[a]
proc `@`*(a: TPeg): TPeg {.rtl, extern: "npegsSearch".} = proc `!*`*(a: TPeg): TPeg {.rtl, extern: "npegsSearch".} =
## constructs a "search" for the PEG `a` ## constructs a "search" for the PEG `a`
result.kind = pkSearch result.kind = pkSearch
result.sons = @[a] result.sons = @[a]
proc `@@`*(a: TPeg): TPeg {.rtl, proc `!*\`*(a: TPeg): TPeg {.rtl,
extern: "npgegsCapturedSearch".} = extern: "npgegsCapturedSearch".} =
## constructs a "captured search" for the PEG `a` ## constructs a "captured search" for the PEG `a`
result.kind = pkCapturedSearch result.kind = pkCapturedSearch
result.sons = @[a] result.sons = @[a]
@ -1484,10 +1484,10 @@ proc primary(p: var TPegParser): TPeg =
return !primary(p) return !primary(p)
of tkAt: of tkAt:
getTok(p) getTok(p)
return @primary(p) return !*primary(p)
of tkCurlyAt: of tkCurlyAt:
getTok(p) getTok(p)
return @@primary(p).token(p) return !*\primary(p).token(p)
else: nil else: nil
case p.tok.kind case p.tok.kind
of tkIdentifier: of tkIdentifier:

View file

@ -1,8 +1,8 @@
version 0.9.0 version 0.9.0
============= =============
- sigil-like operator: @x.abc parsed as (@x).abc - document AVR/embedded systems better; document all the ``useX`` conditional
- document AVR/embedded systems better symbols for the stdlib
- implement ``--script:sh|bat`` command line option - implement ``--script:sh|bat`` command line option
- make GC realtime capable: GC_step(ms: int) - make GC realtime capable: GC_step(ms: int)
- make templates hygienic by default - make templates hygienic by default
@ -20,8 +20,7 @@ version 0.9.0
- implement the high level optimizer - implement the high level optimizer
- change overloading resolution - change overloading resolution
- implement proper coroutines - implement proper coroutines
- implement ``partial`` pragma for partial evaluation; ``hoist`` pragma for - ``hoist`` pragma for loop hoisting
loop hoisting
- we need to support iteration of 2 different data structures in parallel - we need to support iteration of 2 different data structures in parallel
- make exceptions compatible with C++ exceptions - make exceptions compatible with C++ exceptions
- change how comments are part of the AST - change how comments are part of the AST
@ -193,3 +192,4 @@ Version 2 and beyond
case x with `=~` case x with `=~`
- implement ``partial`` pragma for partial evaluation

View file

@ -50,6 +50,8 @@ Changes affecting backwards compatibility
to get a template of old behaviour. to get a template of old behaviour.
- There is now a proper distinction in the type system between ``expr`` and - There is now a proper distinction in the type system between ``expr`` and
``PNimrodNode`` which unfortunately breaks the old macro system. ``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 Compiler Additions
@ -73,7 +75,9 @@ Language Additions
- ``addr`` is now treated like a prefix operator syntactically. - ``addr`` is now treated like a prefix operator syntactically.
- Added ``global`` pragma that can be used to introduce new global variables - Added ``global`` pragma that can be used to introduce new global variables
from within procs. 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 2012-02-09 Version 0.8.14 released