cleaned up command expressions

This commit is contained in:
Araq 2014-02-02 14:34:37 +01:00
commit c30f6cfcf1
4 changed files with 29 additions and 18 deletions

View file

@ -642,8 +642,7 @@ proc primarySuffix(p: var TParser, r: PNode): PNode =
#| | '.' optInd ('type' | 'addr' | symbol) generalizedLit? #| | '.' optInd ('type' | 'addr' | symbol) generalizedLit?
#| | '[' optInd indexExprList optPar ']' #| | '[' optInd indexExprList optPar ']'
#| | '{' optInd indexExprList optPar '}' #| | '{' optInd indexExprList optPar '}'
#| | &( '`'|IDENT|literal|'cast') expr ^+ ',' # command syntax #| | &( '`'|IDENT|literal|'cast') expr # command syntax
#| (doBlock | macroColon)?
result = r result = r
while p.tok.indent < 0: while p.tok.indent < 0:
case p.tok.tokType case p.tok.tokType
@ -680,10 +679,10 @@ proc primarySuffix(p: var TParser, r: PNode): PNode =
if p.tok.tokType != tkComma: break if p.tok.tokType != tkComma: break
getTok(p) getTok(p)
optInd(p, a) optInd(p, a)
if p.tok.tokType == tkDo: if p.tok.tokType == tkDo:
parseDoBlocks(p, result) parseDoBlocks(p, result)
else: else:
result = parseMacroColon(p, result) result = parseMacroColon(p, result)
break break
else: else:
break break

View file

@ -59,8 +59,7 @@ primarySuffix = '(' (exprColonEqExpr comma?)* ')' doBlocks?
| '.' optInd ('type' | 'addr' | symbol) generalizedLit? | '.' optInd ('type' | 'addr' | symbol) generalizedLit?
| '[' optInd indexExprList optPar ']' | '[' optInd indexExprList optPar ']'
| '{' optInd indexExprList optPar '}' | '{' optInd indexExprList optPar '}'
| &( '`'|IDENT|literal|'cast') expr ^+ ',' # command syntax | &( '`'|IDENT|literal|'cast') expr # command syntax
(doBlock | macroColon)?
condExpr = expr colcom expr optInd condExpr = expr colcom expr optInd
('elif' expr colcom expr optInd)* ('elif' expr colcom expr optInd)*
'else' colcom expr 'else' colcom expr

View file

@ -1315,12 +1315,9 @@ Examples:
.. code-block:: nimrod .. code-block:: nimrod
type proc printItem(x: int) = ...
TCallback = proc (x: int) {.cdecl.}
proc printItem(x: Int) = ... proc forEach(c: proc (x: int) {.cdecl.}) =
proc forEach(c: TCallback) =
... ...
forEach(printItem) # this will NOT work because calling conventions differ forEach(printItem) # this will NOT work because calling conventions differ
@ -2459,6 +2456,17 @@ notation. (Thus an operator can have more than two parameters):
assert `*+`(3, 4, 6) == `*`(a, `+`(b, c)) assert `*+`(3, 4, 6) == `*`(a, `+`(b, c))
Command invocation syntax
-------------------------
Routines can be invoked without the ``()`` if the call is syntactially
a statement. This `command invocation syntax`:idx: also works for
expressions, but then only a single argument may follow. This restriction
means ``echo f 1, f 2`` is parsed as ``echo(f(1), f(2))`` and not as
``echo(f(1, f(2)))``.
Closures Closures
-------- --------

View file

@ -1,12 +1,17 @@
discard """ discard """
output: "12" output: '''140
5-120'''
""" """
proc optarg(x:int):int = x
proc singlearg(x:int):int = 20*x
echo optarg 1, singlearg 2
proc foo(x: int): int = x-1 proc foo(x: int): int = x-1
proc foo(x, y: int): int = x-y proc foo(x, y: int): int = x-y
let x = foo 7.foo, # comment here let x = optarg foo 7.foo
foo(1, foo 8) let y = singlearg foo(1, foo 8)
# 12 = 6 - -6
echo x
echo x, y