mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
parser support anon iterators
This commit is contained in:
@@ -990,7 +990,7 @@ proc primary(p: var TParser, mode: TPrimaryMode): PNode =
|
||||
of tkTuple: result = parseTuple(p, mode == pmTypeDef)
|
||||
of tkProc: result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef})
|
||||
of tkIterator:
|
||||
when true:
|
||||
when false:
|
||||
if mode in {pmTypeDesc, pmTypeDef}:
|
||||
result = parseProcExpr(p, false)
|
||||
result.kind = nkIteratorTy
|
||||
@@ -1001,7 +1001,8 @@ proc primary(p: var TParser, mode: TPrimaryMode): PNode =
|
||||
result = ast.emptyNode
|
||||
else:
|
||||
result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef})
|
||||
result.kind = nkIteratorTy
|
||||
if result.kind == nkLambda: result.kind = nkIteratorDef
|
||||
else: result.kind = nkIteratorTy
|
||||
of tkEnum:
|
||||
if mode == pmTypeDef:
|
||||
result = parseEnum(p)
|
||||
|
||||
@@ -922,7 +922,7 @@ proc activate(c: PContext, n: PNode) =
|
||||
of nkCallKinds:
|
||||
for i in 1 .. <n.len: activate(c, n[i])
|
||||
else:
|
||||
nil
|
||||
discard
|
||||
|
||||
proc maybeAddResult(c: PContext, s: PSym, n: PNode) =
|
||||
if s.typ.sons[0] != nil and
|
||||
@@ -951,7 +951,11 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
|
||||
var typeIsDetermined = false
|
||||
if n[namePos].kind != nkSym:
|
||||
assert phase == stepRegisterSymbol
|
||||
s = semIdentDef(c, n.sons[0], kind)
|
||||
|
||||
if n[namePos].kind == nkEmpty:
|
||||
s = newSym(kind, idAnon, getCurrOwner(), n.info)
|
||||
else:
|
||||
s = semIdentDef(c, n.sons[0], kind)
|
||||
n.sons[namePos] = newSymNode(s)
|
||||
s.ast = n
|
||||
s.scope = c.currentScope
|
||||
|
||||
@@ -300,9 +300,12 @@ when not defined(booting):
|
||||
## that should be inserted verbatim in the program
|
||||
## Example:
|
||||
##
|
||||
## .. code-block:: nimrod
|
||||
## emit("echo " & '"' & "hello world".toUpper & '"')
|
||||
##
|
||||
eval: result = e.parseStmt
|
||||
macro payload: stmt {.gensym.} =
|
||||
result = e.parseStmt
|
||||
payload()
|
||||
|
||||
proc expectKind*(n: PNimrodNode, k: TNimrodNodeKind) {.compileTime.} =
|
||||
## checks that `n` is of kind `k`. If this is not the case,
|
||||
@@ -645,10 +648,13 @@ iterator children*(n: PNimrodNode): PNimrodNode {.inline.}=
|
||||
for i in 0 .. high(n):
|
||||
yield n[i]
|
||||
|
||||
template findChild*(n: PNimrodNode; cond: expr): PNimrodNode {.immediate, dirty.} =
|
||||
## Find the first child node matching condition (or nil)
|
||||
## var res = findChild(n, it.kind == nnkPostfix and it.basename.ident == !"foo")
|
||||
|
||||
template findChild*(n: PNimrodNode; cond: expr): PNimrodNode {.
|
||||
immediate, dirty.} =
|
||||
## Find the first child node matching condition (or nil).
|
||||
##
|
||||
## .. code-block:: nimrod
|
||||
## var res = findChild(n, it.kind == nnkPostfix and
|
||||
## it.basename.ident == !"foo")
|
||||
block:
|
||||
var result: PNimrodNode
|
||||
for it in n.children:
|
||||
@@ -736,6 +742,6 @@ proc addIdentIfAbsent*(dest: PNimrodNode, ident: string) {.compiletime.} =
|
||||
if ident.eqIdent($node): return
|
||||
of nnkExprColonExpr:
|
||||
if ident.eqIdent($node[0]): return
|
||||
else: nil
|
||||
else: discard
|
||||
dest.add(ident(ident))
|
||||
|
||||
|
||||
7
tests/macros/tmemit.nim
Normal file
7
tests/macros/tmemit.nim
Normal file
@@ -0,0 +1,7 @@
|
||||
discard """
|
||||
out: '''HELLO WORLD'''
|
||||
"""
|
||||
|
||||
import macros, strutils
|
||||
|
||||
emit("echo " & '"' & "hello world".toUpper & '"')
|
||||
17
tests/parser/tdomulttest.nim
Normal file
17
tests/parser/tdomulttest.nim
Normal file
@@ -0,0 +1,17 @@
|
||||
discard """
|
||||
file: "tdomulttest.nim"
|
||||
output: "555\ntest\nmulti lines\n99999999\nend"
|
||||
disabled: true
|
||||
"""
|
||||
proc foo(bar, baz: proc (x: int): int) =
|
||||
echo bar(555)
|
||||
echo baz(99999999)
|
||||
|
||||
foo do (x: int) -> int:
|
||||
return x
|
||||
do (x: int) -> int:
|
||||
echo("test")
|
||||
echo("multi lines")
|
||||
return x
|
||||
|
||||
echo("end")
|
||||
15
tests/parser/tinvwhen.nim
Normal file
15
tests/parser/tinvwhen.nim
Normal file
@@ -0,0 +1,15 @@
|
||||
discard """
|
||||
file: "tinvwhen.nim"
|
||||
line: 11
|
||||
errormsg: "invalid indentation"
|
||||
"""
|
||||
# This was parsed even though it should not!
|
||||
|
||||
proc chdir(path: cstring): cint {.importc: "chdir", header: "dirHeader".}
|
||||
|
||||
proc getcwd(buf: cstring, buflen: cint): cstring
|
||||
when defined(unix): {.importc: "getcwd", header: "<unistd.h>".} #ERROR_MSG invalid indentation
|
||||
elif defined(windows): {.importc: "getcwd", header: "<direct.h>"}
|
||||
else: {.error: "os library not ported to your OS. Please help!".}
|
||||
|
||||
|
||||
4
todo.txt
4
todo.txt
@@ -6,6 +6,7 @@ version 0.9.4
|
||||
- ensure (ref T)(a, b) works as a type conversion and type constructor
|
||||
- document new templating symbol binding rules
|
||||
- make '--implicitStatic:on' the default
|
||||
- change comment handling in the AST
|
||||
|
||||
- special rule for ``[]=``
|
||||
- ``=`` should be overloadable; requires specialization for ``=``; general
|
||||
@@ -27,8 +28,6 @@ Bugs
|
||||
- docgen: sometimes effects are listed twice
|
||||
- 'result' is not properly cleaned for NRVO --> use uninit checking instead
|
||||
- sneaking with qualifiedLookup() is really broken!
|
||||
- aporia.nim(968, 5) Error: ambiguous identifier: 'DELETE' --
|
||||
use a qualifier
|
||||
- blocks can "export" an identifier but the CCG generates {} for them ...
|
||||
- osproc execProcesses can deadlock if all processes fail (as experienced
|
||||
in c++ mode)
|
||||
@@ -54,7 +53,6 @@ version 0.9.X
|
||||
- implement the missing features wrt inheritance
|
||||
- better support for macros that rewrite procs
|
||||
- macros need access to types and symbols (partially implemented)
|
||||
- perhaps: change comment handling in the AST
|
||||
- enforce 'simpleExpr' more often --> doesn't work; tkProc is
|
||||
part of primary!
|
||||
- the typeDesc/expr unification is weird and only necessary because of
|
||||
|
||||
Reference in New Issue
Block a user