cleaned up command expressions

This commit is contained in:
Araq
2014-02-02 14:34:37 +01:00
parent 7196c7637e
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 indexExprList optPar ']'
#| | '{' optInd indexExprList optPar '}'
#| | &( '`'|IDENT|literal|'cast') expr ^+ ',' # command syntax
#| (doBlock | macroColon)?
#| | &( '`'|IDENT|literal|'cast') expr # command syntax
result = r
while p.tok.indent < 0:
case p.tok.tokType
@@ -680,10 +679,10 @@ proc primarySuffix(p: var TParser, r: PNode): PNode =
if p.tok.tokType != tkComma: break
getTok(p)
optInd(p, a)
if p.tok.tokType == tkDo:
parseDoBlocks(p, result)
else:
result = parseMacroColon(p, result)
if p.tok.tokType == tkDo:
parseDoBlocks(p, result)
else:
result = parseMacroColon(p, result)
break
else:
break

View File

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

View File

@@ -1315,12 +1315,9 @@ Examples:
.. code-block:: nimrod
type
TCallback = proc (x: int) {.cdecl.}
proc printItem(x: int) = ...
proc printItem(x: Int) = ...
proc forEach(c: TCallback) =
proc forEach(c: proc (x: int) {.cdecl.}) =
...
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))
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
--------

View File

@@ -1,12 +1,17 @@
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, y: int): int = x-y
let x = foo 7.foo, # comment here
foo(1, foo 8)
# 12 = 6 - -6
echo x
let x = optarg foo 7.foo
let y = singlearg foo(1, foo 8)
echo x, y